Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GW_Submit_Access_GravityForms_Restrict_Content {
- public function __construct( $args = array() ) {
- $this->_args = wp_parse_args( $args,
- array(
- 'requires_submission_message' => __( 'Oops! You do not have access to this page.' ),
- 'requires_submission_link' => __( 'click to submit form' ),
- 'loading_message' => ''
- )
- );
- add_action( 'init', array( $this, 'init' ) );
- }
- public function init() {
- if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) )
- return;
- if( empty( $this->_args['loading_message'] ) )
- $this->_args['loading_message'] = '<span class="GW_Submit_Access-loading"><img src="' . GFCommon::get_base_url() . '/images/spinner.gif" /></span>';
- add_filter('the_content', array( $this, 'maybe_hide_the_content' ) );
- add_filter('gform_entry_post_save', array( $this, 'add_submitted_form_lead' ), 10, 2);
- add_action('wp_ajax_GW_Submit_Access_get_content', array( $this, 'ajax_get_content' ) );
- add_action('wp_ajax_nopriv_GW_Submit_Access_get_content', array( $this, 'ajax_get_content' ) );
- }
- public function maybe_hide_the_content( $content ) {
- global $post;
- if( ! get_post_meta( $post->ID , 'form_id', true ) )
- return $content;
- $ajax = ( get_post_meta( $post->ID, 'form_ajax', true ) == 1 ) ? true : false;
- if( $ajax )
- $content = $this->cache_bypass_content( $content );
- else if( ! $this->has_access( $post->ID ) )
- $content = $this->get_requires_submission_message( $post->ID );
- return $content;
- }
- function cache_bypass_content( $content ) {
- global $post;
- ob_start();
- ?>
- <div id="GW_Submit_Access-content">
- <?php echo $this->_args['loading_message']; ?>
- </div>
- <script type="text/javascript">
- var ajaxUrl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
- ( function( $ ) {
- $.post( ajaxUrl, {
- action: 'GW_Submit_Access_get_content',
- post: <?php echo $post->ID; ?>
- }, function( response ) {
- $( '#GW_Submit_Access-content' ).html( response );
- } );
- } )( jQuery );
- </script>
- <?php
- return ob_get_clean();
- }
- function ajax_get_content() {
- $post_id = rgpost( 'post' );
- if( $this->has_access( $post_id ) ) {
- $post = get_post( $post_id );
- $GLOBALS['post'] = get_post( $post_id );
- setup_postdata( $post );
- remove_filter( 'the_content', array( $this, 'maybe_hide_the_content' ) );
- ob_start();
- the_content();
- $content = ob_get_clean();
- }
- else
- $content = $this->get_requires_submission_message( $post_id );
- die( $content );
- }
- function get_requires_submission_message( $post_id ) {
- $requires_submission_message = do_shortcode( get_post_meta( $post_id, 'form_message', true ) );
- $requires_submission_message = $requires_submission_message ? $requires_submission_message : $this->_args['requires_submission_message'];
- $requires_submission_url = get_post_meta( $post_id, 'form_url', true ) ? get_post_meta( $post_id, 'form_url', true ) : '';
- $title = get_post_meta( $post_id, 'form_title', true ) ? get_post_meta( $post_id, 'form_title', true ) : $this->_args['requires_submission_link'];
- $form_url = ( $requires_submission_url && $requires_submission_url != '' ) ? '<br/><a href="'.$requires_submission_url.'">'.$title.'</a>' : '';
- return $requires_submission_message.$form_url;
- }
- function add_submitted_form_lead($lead, $form) {
- $submitted_forms_leads = isset( $_COOKIE['GW_Submit_Access_submitted_forms_leads'] ) ? (array) json_decode( $_COOKIE['GW_Submit_Access_submitted_forms_leads'] ) : array();
- $form_lead = $form['id'].'VSH'.$lead['id'];
- if( ! in_array( $form_lead, $submitted_forms_leads ) ) {
- $submitted_forms_leads[] = $form_lead;
- setcookie( 'GW_Submit_Access_submitted_forms_leads', json_encode( $submitted_forms_leads ), null, '/' );
- }
- }
- function has_access( $post_id ) {
- if( ! get_post_meta( $post_id , 'form_id', true ) )
- return true;
- $form_ids = array_filter( array_map( 'trim', explode( ',', get_post_meta( $post_id, 'form_id', true ) ) ) );
- $submitted_forms_leads = (array) json_decode( stripslashes( rgar( $_COOKIE, 'GW_Submit_Access_submitted_forms_leads' ) ) );
- $submitted_forms = array();
- $submitted_leads = array();
- foreach ( (array) $submitted_forms_leads as $form_lead ) {
- list($form_id, $lead_id) = explode("VSH", $form_lead );
- $submitted_forms[] = $form_id;
- $submitted_leads[] = $lead_id;
- }
- if( empty( $form_ids ) && ! empty( $submitted_forms ) ) {
- return true;
- }
- $matching_form_ids = array_intersect( $form_ids, $submitted_forms );
- if( empty( $matching_form_ids ) or empty( $submitted_leads ) )
- return false;
- foreach ( (array) $submitted_leads as $lead_id ) {
- $lead = array();
- $lead = RGFormsModel::get_lead($lead_id);
- $form_id = $lead['form_id'];
- $success = false;
- if ( in_array ( $form_id , $matching_form_ids) ) {
- if ( isset($lead['payment_method']) || gform_get_meta($lead['id'], 'payment_gateway') ) {
- if ( isset($lead['payment_status']) and ( strtolower($lead['payment_status']) == 'paid' || strtolower($lead['payment_status']) == 'active' ) ) {
- $success = true;
- break;
- }
- }
- else {
- $success = true;
- break;
- }
- }
- }
- if( $success == true )
- return true;
- return false;
- }
- }
- new GW_Submit_Access_GravityForms_Restrict_Content();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement