Skip to content

Commit 7d87c51

Browse files
committed
gpsa-enable-for-all-posts-of-type.php: Updated to class based snippet.
1 parent 0ebfbb0 commit 7d87c51

File tree

1 file changed

+78
-91
lines changed

1 file changed

+78
-91
lines changed

gp-submit-to-access/gpsa-enable-for-all-posts-of-type.php

Lines changed: 78 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,102 @@
66
*
77
* Enables GPSA for all posts of a specific type.
88
*
9-
* @param string $post_type The post type to enable GPSA for.
9+
* Instructions:
10+
*
11+
* 1. Install the snippet.
12+
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
13+
* 2. Customize instantiation of the GPSA_Enable_For_All_Posts_Of_Type
14+
* class by passing the `post_type` and `settings` you would like
15+
* to use. (See "Configuration" section at the bottom of the snippet
16+
* for an example)
17+
*
18+
*
19+
* @phpstan-type AccessExpiration array{
20+
* type: 'session' | 'never' | 'custom',
21+
* duration: array{
22+
* value: int,
23+
* unit: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds',
24+
* }
25+
* }
26+
*
27+
* @phpstan-type AccessBehavior 'show_message' | 'redirect'
28+
*
29+
* @phpstan-type GPSADocumentSettings array{
30+
* gpsa_access: AccessExpiration,
31+
* gpsa_enabled: bool,
32+
* gpsa_content_loading_message: string,
33+
* gpsa_form_redirect_path: string,
34+
* gpsa_require_unique_form_submission: bool,
35+
* gpsa_required_form_ids: array<int>,
36+
* gpsa_requires_access_message: string,
37+
* gpsa_access_behavior: AccessBehavior
38+
* }
1039
*/
11-
function gpsa_enable_for_all_posts_of_type( $post_type, $settings = array() ) {
12-
add_filter( 'gpsa_supported_post_types', function( $post_types ) use ( $post_type ) {
13-
if ( ! in_array( $post_type, $post_types ) ) {
14-
$post_types[] = $post_type;
40+
class GPSA_Enable_For_All_Posts_Of_Type {
41+
/**
42+
* @var boolean
43+
*/
44+
public static $post_type;
45+
46+
/**
47+
* @var GPSADocumentSettings
48+
*/
49+
public static $settings;
50+
51+
/**
52+
* @param array{
53+
* post_type: string
54+
* settings: GPSADocumentSettings
55+
* } $args
56+
*/
57+
public function __construct( $args = array() ) {
58+
$args = wp_parse_args( $args, array(
59+
'post_type' => 'post',
60+
'settings' => array(),
61+
) );
62+
63+
self::$post_type = $args['post_type'];
64+
self::$settings = wp_parse_args( $args['settings'], array(
65+
'gpsa_enabled' => true,
66+
) );
67+
68+
add_filter( 'gpsa_supported_post_types', array( self::class, 'ensure_supported_post_types' ), 10, 1 );
69+
add_filter( 'gpsa_document_settings', array( self::class, 'override_document_level_settings' ), 10, 2 );
70+
}
71+
72+
public static function ensure_supported_post_types( $post_types ) {
73+
if ( ! in_array( self::$post_type, $post_types ) ) {
74+
$post_types[] = self::$post_type;
1575
}
1676

1777
return $post_types;
18-
});
78+
}
1979

20-
add_filter('gpsa_document_settings', function( $base_settings, $post_id ) use ( $post_type, $settings ) {
80+
public static function override_document_level_settings( $settings, $post_id ) {
2181
$post = get_post( $post_id );
22-
if ( $post->post_type === $post_type ) {
82+
if ( $post->post_type === self::$post_type ) {
2383
$settings = array_merge(
24-
$base_settings,
25-
$settings
84+
$settings,
85+
self::$settings
2686
);
2787
}
2888

2989
return $settings;
30-
}, 10, 2);
90+
}
3191
}
3292

3393
// Configuration:
3494

3595
/**
3696
* Minimal configuration to enable for all posts of type `drum-machine`.
37-
*
38-
* Usage:
39-
* 1. Update this argument to match the `$post_type` of the posts you'd like to target.
40-
* 2. Update gpsa_required_form_ids with the form ID you want to require.
4197
*/
42-
gpsa_enable_for_all_posts_of_type(
43-
'drum-machine',
98+
new GPSA_Enable_For_All_Posts_Of_Type(
4499
array(
45-
/**
46-
* @var boolean
47-
*/
48-
'gpsa_enabled' => true,
49-
/**
50-
* @var array<int>
51-
*/
52-
'gpsa_required_form_ids' => array( 1 ), // UPDATE `1` with the form id you want to require.
100+
'post_type' => 'drum-machine', // Update `drum-machine` to match the post type you want to target.
101+
'settings' => array(
102+
'gpsa_required_form_ids' => array( 3 ), // UPDATE `1` with the form id you want to require.
103+
// optionally add other settings to the array. See the GPSADocumentSettings in the above
104+
// doc blocks for available options.
105+
),
53106
)
54107
);
55-
56-
57-
/**
58-
* Advanced configuration to enable for all posts of type `drum-machine`.
59-
60-
* Usage:
61-
* 1. Update this argument to match the `$post_type` of the posts you'd like to target.
62-
* 2. Update gpsa_required_form_ids with the form ID you want to require.
63-
* 3. Optionally uncomment and provide values for the additional settings.
64-
*/
65-
// gpsa_enable_for_all_posts_of_type(
66-
// 'drum-machine',
67-
// array(
68-
// /**
69-
// * @var boolean
70-
// */
71-
// 'gpsa_enabled' => true,
72-
//
73-
// /**
74-
// * @var array<int>
75-
// */
76-
// 'gpsa_required_form_ids' => array( 1 ), // UPDATE `1` with the form id you want to require.
77-
//
78-
// /**
79-
// * optionally override the default message to display while the content is loading
80-
// * @var string
81-
// */
82-
// // 'gpsa_content_loading_message' => '',
83-
//
84-
// /**
85-
// * optionally redirect to a specific URL where the access form is located
86-
// * @var string
87-
// */
88-
// // 'gpsa_form_redirect_path' => '',
89-
//
90-
// /**
91-
// * optionally require a unique form submission for every post
92-
// * @var boolean
93-
// */
94-
// // 'gpsa_require_unique_form_submission' => false,
95-
//
96-
// /**
97-
// * optionally override the default access duration.
98-
// * @var array{
99-
// * type: 'session' | 'never' | 'custom',
100-
// * duration: array{
101-
// * value: number,
102-
// * unit: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds',
103-
// * }
104-
// * }
105-
// */
106-
// // 'gpsa_access' => '',
107-
//
108-
// /**
109-
// * optionally override the default requires access message
110-
// * @var string
111-
// */
112-
// // 'gpsa_requires_access_message' = '',
113-
//
114-
// /**
115-
// * optionally override the default access behavior
116-
// * @var string 'show_message' | 'redirect'
117-
// */
118-
// // 'gpsa_access_behavior' = '',
119-
// ),
120-
// );

0 commit comments

Comments
 (0)