Submit to Access

Require that a form be submitted before a post or page can be accessed.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough

Code

Filename: gw-submit-to-access.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
 * Gravity Wiz // Gravity Forms // Submit to Access
 *
 * Require that a form be submitted before a post or page can be accessed.
 *
 * Plugin Name: Gravity Forms Submit to Access
 * Plugin URI:  https://gravitywiz.com/submit-gravity-form-access-content/
 * Description: Require that a form be submitted before a post or page can be accessed.
 * Author:      Gravity Wiz
 * Version:     1.14
 * Author URI:  https://gravitywiz.com
 */
class GW_Submit_Access {

	private static $instance = null;

	public $_args = array();

	private function __construct( $args = array() ) {

		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args( $args, array(
			'requires_submission_message' => __( 'Oops! You do not have access to this page.' ),
			'bypass_cache'                => false,
			'loading_message'             => '', // set later so we can use GFCommon to get URL to GF spinner,
			'enable_user_meta'            => false,
			'is_persistent'               => true,
			'cookie_expiration'           => null,
		) );

		// do version check in the init to make sure if GF is going to be loaded, it is already loaded
		add_action( 'init', array( $this, 'init' ) );

	}

	public static function get_instance( $args = array() ) {
		if ( self::$instance === null ) {
			self::$instance = new GW_Submit_Access( $args );
		}
		return self::$instance;
	}

	public function init() {

		// make sure we're running the required minimum version of Gravity Forms
		if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '2.4.7', '>=' ) ) {
			return;
		}

		// setting later so we can use GFCommon::get_base_url() to get GF's spinner URL
		if ( empty( $this->_args['loading_message'] ) ) {
			$this->_args['loading_message'] = '<span class="gwsa-loading">Loading content... <img src="' . GFCommon::gf_global( false, true )['spinnerUrl'] . '" /></span>';
		}

		add_action( 'wp', array( $this, 'check_global_requirements' ), 5 );
		add_action( 'admin_init', array( $this, 'check_global_requirements' ), 5 );
		add_action( 'wp', array( $this, 'check_for_access_redirect' ) );

		add_action( 'gform_pre_submission', array( $this, 'add_submitted_form' ) );
		add_filter( 'the_content', array( $this, 'maybe_hide_the_content' ) );

		add_action( 'wp_ajax_gwas_get_content', array( $this, 'ajax_get_content' ) );
		add_action( 'wp_ajax_nopriv_gwas_get_content', array( $this, 'ajax_get_content' ) );

		add_shortcode( 'gwsa', array( $this, 'do_gwsa_shortcoee' ) );

	}

	public function check_global_requirements() {

		if ( current_user_can( 'administrator' ) && is_admin() ) {
			return;
		}

		$global_posts = $this->get_global_posts();
		if ( empty( $global_posts ) ) {
			return;
		}

		// if we're already on a global post, don't do anything
		$object = get_queried_object();
		if ( is_a( $object, 'WP_Post' ) && in_array( $object->ID, wp_list_pluck( $global_posts, 'ID' ) ) ) {
			return;
		}

		foreach ( $global_posts as $global_post ) {
			if ( ! $this->has_access( $global_post->ID ) ) {
				wp_redirect( get_permalink( $global_post ) );
				exit;
			}
		}

	}

	public function get_global_posts() {

		$query = array(
			'post_type'  => 'any',
			'meta_query' => array(
				'relation' => 'or',
				array(
					'key'   => 'gwsa_require_submission',
					'value' => 'global',
				),
			),
		);

		if ( is_user_logged_in() ) {
			$query['meta_query'][] = array(
				'key'   => 'gwsa_require_submission',
				'value' => 'global_logged_in',
			);
		}

		$query        = apply_filters( 'gfsa_get_global_posts_query', $query );
		$global_posts = get_posts( $query );

		return $global_posts;
	}

	public function check_for_access_redirect() {
		global $post;

		if ( is_admin() ) {
			return;
		}

		if ( ! $post || $this->has_access( $post->ID ) ) {
			return;
		}

		$url = $this->get_requires_submission_redirect( $post->ID );
		if ( $url ) {
			wp_redirect( $url );
		}

	}

	public function maybe_hide_the_content( $content ) {
		global $post;

		if ( ! $this->requires_access( $post->ID ) ) {
			return $content;
		}

		if ( $this->_args['bypass_cache'] ) {
			$content = $this->cache_bypass_content( $content );
		} elseif ( ! $this->has_access( $post->ID ) ) {
			$content = $this->get_requires_submission_message( $post->ID );
		}

		return $content;
	}

	public function cache_bypass_content( $content ) {
		global $post;

		ob_start();

		// Output the form scripts (including jQuery), otherwise submission may not work.
		$form_ids = $this->get_form_ids( $post->ID );
		$form     = GFAPI::get_form( $form_ids[0] );
		require_once( GFCommon::get_base_path() . '/form_display.php' );
		GFFormDisplay::print_form_scripts( $form, true );
		?>

		<div id="gwsa-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: 'gwas_get_content',
					post:   <?php echo $post->ID; ?>,
				}, function( response ) {
					$( '#gwsa-content' ).html( response );
				} );

			} )( jQuery );

		</script>

		<?php

		return ob_get_clean();
	}

	public 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' ) );

			// use the_content() so we get the content exactly as WP would have originally displayed it
			ob_start();
			the_content();
			$content = ob_get_clean();

		} else {

			$content = $this->get_requires_submission_message( $post_id );

		}

		die( $content );
	}

	public function get_requires_submission_message( $post_id ) {

		$requires_submission_message = get_post_meta( $post_id, 'gwsa_requires_submission_message', true );

		if ( ! $requires_submission_message ) {
			$requires_submission_message = $this->_args['requires_submission_message'];
		}

		$contains_form_merge_tag = strpos( $requires_submission_message, '{form}' ) !== false;

		$form_ids = $this->get_form_ids( $post_id );

		if ( ! empty( $form_ids ) ) {

			$form = GFAPI::get_form( $form_ids[0] );
			require_once( GFCommon::get_base_path() . '/form_display.php' );
			GFFormDisplay::print_form_scripts( $form, true );

			ob_start();
			gravity_form( $form_ids[0], false, false, false, array(), $this->_args['bypass_cache'] );
			$form_markup = ob_get_clean();

			$requires_submission_message = $contains_form_merge_tag ? str_replace( '{form}', $form_markup, $requires_submission_message ) : $requires_submission_message . $form_markup;

			// Replace form's action URL.
			if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
				$search  = remove_query_arg( 'gf_token' );
				$replace = get_permalink( rgpost( 'post' ) );
				// get_permalink() defaults to whatever protocol the site url is configured for; we need to be sure
				// if the form is being loaded on an https page, that our action url is also https.
				if ( is_ssl() ) {
					$replace = str_replace( 'http://', 'https://', $replace );
				}
				$requires_submission_message = str_replace( $search, $replace, $requires_submission_message );
			}
		}

		return do_shortcode( $requires_submission_message );
	}

	public function get_requires_submission_redirect( $post_id ) {
		return get_post_meta( $post_id, 'gwsa_requires_submission_redirect', true );
	}

	public function has_access( $post_id ) {

		if ( ! $this->requires_access( $post_id ) ) {
			return true;
		}

		$form_ids   = $this->get_form_ids( $post_id );
		$per_page   = $this->requires_submission_per_page( $post_id );
		$has_access = $this->has_submitted_form( $form_ids, $per_page, $post_id );

		/**
		 * Filter whether the current viewer has access to the given post.
		 *
		 * @since 1.10
		 *
		 * @param bool $has_access Whether the current viewer has access.
		 * @param int  $post_id    The ID of the post for which access is being assessed.
		 */
		$has_access = apply_filters( 'gfsa_has_access', $has_access, $post_id );

		return $has_access;
	}

	public function has_submitted_form( $form_ids, $per_page, $post_id ) {

		$submitted_forms = $this->get_submitted_forms();

		// if not form-specific and at least one form is submitted, user has access
		if ( empty( $form_ids ) && ! empty( $submitted_forms ) ) {
			return true;
		}

		if ( ! $per_page ) {

			// has specifically required form been submitted?
			$matching_form_ids = array_intersect( $form_ids, array_keys( $submitted_forms ) );
			if ( ! empty( $matching_form_ids ) ) {
				return true;
			}
		} else {

			foreach ( $form_ids as $form_id ) {
				// If form has never been submitted, access is not granted
				if ( empty( $submitted_forms[ $form_id ] ) ) {
					return false;
				}

				// If current post ID is not in the submitted form's array of post IDs, do not grant access
				if ( ! in_array( $post_id, $submitted_forms[ $form_id ] ) ) {
					return false;
				}
			}

			return true;

		}

		return false;
	}

	public function requires_access( $post_id ) {

		// Never require access for edit API requests. Interferes with the Block Editor.
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			$route   = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] ) ?: '/';
			$request = new WP_REST_Request( $_SERVER['REQUEST_METHOD'], $route );
			if ( $request->get_method() !== 'GET' || ( $request['context'] === 'edit' && current_user_can( 'edit_post', $post_id ) ) ) {
				return false;
			}
		}

		return get_post_meta( $post_id, 'gwsa_require_submission', true ) == true;
	}

	public function requires_submission_per_page( $post_id ) {
		return get_post_meta( $post_id, 'gwsa_require_submission', true ) === 'per_page';
	}

	public function get_submitted_forms() {

		// always check the cookie first; will allow user meta vs cookie to be set per page in the future
		$submitted_forms = (array) json_decode( stripslashes( rgar( $_COOKIE, 'gwsa_submitted_forms' ) ) );

		// if user meta is enabled, merge forms stored there as well
		if ( $this->_args['enable_user_meta'] ) {
			$user_meta_forms = (array) wp_get_current_user()->get( 'gwsa_submitted_forms' );
			$submitted_forms = array_merge_recursive( $submitted_forms, $user_meta_forms );
		}

		return array_filter( $submitted_forms );
	}

	public function add_submitted_form( $form ) {

		$submitted_forms = $this->get_submitted_forms();
		$form_id         = $form['id'];

		if ( ! headers_sent() ) {

			if ( ! isset( $submitted_forms[ $form_id ] ) || ! is_array( $submitted_forms[ $form_id ] ) ) {
				$submitted_forms[ $form_id ] = array();
			}

			$submitted_forms[ $form_id ][] = url_to_postid( GFFormsModel::get_current_page_url() );

			if ( $this->_args['enable_user_meta'] && is_user_logged_in() ) {
				update_user_meta( get_current_user_id(), 'gwsa_submitted_forms', $submitted_forms );
			} else {
				$expiration = $this->_args['is_persistent'] ? rgar( $this->_args, 'cookie_expiration', strtotime( '+1 year' ) ) : null;
				setcookie( 'gwsa_submitted_forms', json_encode( $submitted_forms ), $expiration, '/' );
			}
		}

	}

	public function get_form_ids( $post_id ) {
		return array_filter( array_map( 'trim', explode( ',', get_post_meta( $post_id, 'gwsa_form_ids', true ) ) ) );
	}

}

function gw_submit_to_access( $args = array() ) {
	return GW_Submit_Access::get_instance( $args );
}

gw_submit_to_access();

Comments

  1. Mihail
    Mihail August 23, 2024 at 2:47 am

    Hello. We faced the following error in the logs: ‘PHP message: PHP Warning: Attempt to read property “ID” on null in gw-submit-to-access/gw-submit-to-access.php on line 141’. Could you please add a check, something like this: if (!$post->ID || !$this->requires_access($post->ID)) …

    Thanks!

    Regards, Mihail

    Reply
    1. Roxy Stoltz
      Roxy Stoltz Staff August 23, 2024 at 3:44 am

      Hey Mihail,

      It looks like the error may be related to $post being null or not properly set in certain contexts. This issue can often be caused by configuration or setup details. If you have an active Gravity Perks License, you can get in touch with us via our support form so we can dig into this.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.