Advertisement
Guest User

gf-rc

a guest
May 22nd, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. <?php
  2. class GW_Submit_Access_GravityForms_Restrict_Content {
  3.  
  4. public function __construct( $args = array() ) {
  5. $this->_args = wp_parse_args( $args,
  6. array(
  7. 'requires_submission_message' => __( 'Oops! You do not have access to this page.' ),
  8. 'requires_submission_link' => __( 'click to submit form' ),
  9. 'loading_message' => ''
  10. )
  11. );
  12. add_action( 'init', array( $this, 'init' ) );
  13. }
  14.  
  15. public function init() {
  16.  
  17. if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) )
  18. return;
  19.  
  20. if( empty( $this->_args['loading_message'] ) )
  21. $this->_args['loading_message'] = '<span class="GW_Submit_Access-loading"><img src="' . GFCommon::get_base_url() . '/images/spinner.gif" /></span>';
  22.  
  23. add_filter('the_content', array( $this, 'maybe_hide_the_content' ) );
  24. add_filter('gform_entry_post_save', array( $this, 'add_submitted_form_lead' ), 10, 2);
  25. add_action('wp_ajax_GW_Submit_Access_get_content', array( $this, 'ajax_get_content' ) );
  26. add_action('wp_ajax_nopriv_GW_Submit_Access_get_content', array( $this, 'ajax_get_content' ) );
  27.  
  28. }
  29.  
  30. public function maybe_hide_the_content( $content ) {
  31.  
  32. global $post;
  33. if( ! get_post_meta( $post->ID , 'form_id', true ) )
  34. return $content;
  35.  
  36. $ajax = ( get_post_meta( $post->ID, 'form_ajax', true ) == 1 ) ? true : false;
  37.  
  38. if( $ajax )
  39. $content = $this->cache_bypass_content( $content );
  40.  
  41. else if( ! $this->has_access( $post->ID ) )
  42. $content = $this->get_requires_submission_message( $post->ID );
  43.  
  44. return $content;
  45.  
  46. }
  47.  
  48. function cache_bypass_content( $content ) {
  49. global $post;
  50. ob_start();
  51. ?>
  52. <div id="GW_Submit_Access-content">
  53. <?php echo $this->_args['loading_message']; ?>
  54. </div>
  55. <script type="text/javascript">
  56. var ajaxUrl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
  57. ( function( $ ) {
  58. $.post( ajaxUrl, {
  59. action: 'GW_Submit_Access_get_content',
  60. post: <?php echo $post->ID; ?>
  61. }, function( response ) {
  62. $( '#GW_Submit_Access-content' ).html( response );
  63. } );
  64.  
  65. } )( jQuery );
  66. </script>
  67. <?php
  68. return ob_get_clean();
  69. }
  70.  
  71. function ajax_get_content() {
  72.  
  73. $post_id = rgpost( 'post' );
  74.  
  75. if( $this->has_access( $post_id ) ) {
  76.  
  77. $post = get_post( $post_id );
  78. $GLOBALS['post'] = get_post( $post_id );
  79. setup_postdata( $post );
  80. remove_filter( 'the_content', array( $this, 'maybe_hide_the_content' ) );
  81. ob_start();
  82. the_content();
  83. $content = ob_get_clean();
  84.  
  85. }
  86. else
  87. $content = $this->get_requires_submission_message( $post_id );
  88.  
  89. die( $content );
  90. }
  91.  
  92. function get_requires_submission_message( $post_id ) {
  93.  
  94. $requires_submission_message = do_shortcode( get_post_meta( $post_id, 'form_message', true ) );
  95. $requires_submission_message = $requires_submission_message ? $requires_submission_message : $this->_args['requires_submission_message'];
  96. $requires_submission_url = get_post_meta( $post_id, 'form_url', true ) ? get_post_meta( $post_id, 'form_url', true ) : '';
  97. $title = get_post_meta( $post_id, 'form_title', true ) ? get_post_meta( $post_id, 'form_title', true ) : $this->_args['requires_submission_link'];
  98. $form_url = ( $requires_submission_url && $requires_submission_url != '' ) ? '<br/><a href="'.$requires_submission_url.'">'.$title.'</a>' : '';
  99.  
  100. return $requires_submission_message.$form_url;
  101.  
  102. }
  103.  
  104. function add_submitted_form_lead($lead, $form) {
  105.  
  106. $submitted_forms_leads = isset( $_COOKIE['GW_Submit_Access_submitted_forms_leads'] ) ? (array) json_decode( $_COOKIE['GW_Submit_Access_submitted_forms_leads'] ) : array();
  107.  
  108. $form_lead = $form['id'].'VSH'.$lead['id'];
  109.  
  110. if( ! in_array( $form_lead, $submitted_forms_leads ) ) {
  111. $submitted_forms_leads[] = $form_lead;
  112. setcookie( 'GW_Submit_Access_submitted_forms_leads', json_encode( $submitted_forms_leads ), null, '/' );
  113. }
  114.  
  115. }
  116.  
  117.  
  118. function has_access( $post_id ) {
  119.  
  120. if( ! get_post_meta( $post_id , 'form_id', true ) )
  121. return true;
  122.  
  123. $form_ids = array_filter( array_map( 'trim', explode( ',', get_post_meta( $post_id, 'form_id', true ) ) ) );
  124.  
  125. $submitted_forms_leads = (array) json_decode( stripslashes( rgar( $_COOKIE, 'GW_Submit_Access_submitted_forms_leads' ) ) );
  126.  
  127. $submitted_forms = array();
  128. $submitted_leads = array();
  129.  
  130. foreach ( (array) $submitted_forms_leads as $form_lead ) {
  131. list($form_id, $lead_id) = explode("VSH", $form_lead );
  132. $submitted_forms[] = $form_id;
  133. $submitted_leads[] = $lead_id;
  134. }
  135.  
  136.  
  137. if( empty( $form_ids ) && ! empty( $submitted_forms ) ) {
  138. return true;
  139. }
  140.  
  141. $matching_form_ids = array_intersect( $form_ids, $submitted_forms );
  142.  
  143. if( empty( $matching_form_ids ) or empty( $submitted_leads ) )
  144. return false;
  145.  
  146.  
  147. foreach ( (array) $submitted_leads as $lead_id ) {
  148.  
  149. $lead = array();
  150.  
  151. $lead = RGFormsModel::get_lead($lead_id);
  152.  
  153. $form_id = $lead['form_id'];
  154.  
  155. $success = false;
  156.  
  157. if ( in_array ( $form_id , $matching_form_ids) ) {
  158.  
  159. if ( isset($lead['payment_method']) || gform_get_meta($lead['id'], 'payment_gateway') ) {
  160.  
  161. if ( isset($lead['payment_status']) and ( strtolower($lead['payment_status']) == 'paid' || strtolower($lead['payment_status']) == 'active' ) ) {
  162. $success = true;
  163. break;
  164. }
  165. }
  166. else {
  167. $success = true;
  168. break;
  169. }
  170. }
  171. }
  172.  
  173. if( $success == true )
  174. return true;
  175.  
  176.  
  177. return false;
  178.  
  179. }
  180.  
  181. }
  182. new GW_Submit_Access_GravityForms_Restrict_Content();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement