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+ * @phpstan-type AccessExpiration array{
10+ * type: 'session' | 'never' | 'custom',
11+ * duration: array{
12+ * value: number,
13+ * unit: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds',
14+ * }
15+ * }
16+ *
17+ * @phpstan-type AccessBehavior 'show_message' | 'redirect'
18+ *
19+ * @phpstan-type GPSADocumentSettings array{
20+ * gpsa_access: AccessExpiration,
21+ * gpsa_enabled: bool,
22+ * gpsa_content_loading_message: string,
23+ * gpsa_form_redirect_path: string,
24+ * gpsa_require_unique_form_submission: bool,
25+ * gpsa_required_form_ids: array<int>,
26+ * gpsa_requires_access_message: string,
27+ * gpsa_access_behavior: AccessBehavior
28+ * }
1029 */
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 ;
30+ class GPSA_Enable_For_All_Posts_Of_Type {
31+ /**
32+ * @var boolean
33+ */
34+ public static $ post_type ;
35+
36+ /**
37+ * @var GPSADocumentSettings
38+ */
39+ public static $ settings ;
40+
41+ /**
42+ * @param string $post_type The post type to enable GPSA for.
43+ * @param GPSADocumentSettings $settings The settings for GPSA.
44+ */
45+ public function __construct ( $ post_type , $ settings ) {
46+ self ::$ post_type = $ post_type ;
47+ self ::$ settings = $ settings ;
48+
49+ add_filter ( 'gpsa_supported_post_types ' , array ( self ::class, 'ensure_supported_post_types ' ), 10 , 1 );
50+ add_filter ('gpsa_document_settings ' , array ( self ::class, 'override_document_level_settings ' ), 10 , 2 );
51+ }
52+
53+ public static function ensure_supported_post_types ( $ post_types ) {
54+ if ( ! in_array ( self ::$ post_type , $ post_types ) ) {
55+ $ post_types [] = self ::$ post_type ;
1556 }
1657
1758 return $ post_types ;
18- });
59+ }
1960
20- add_filter ( ' gpsa_document_settings ' , function ( $ base_settings , $ post_id ) use ( $ post_type , $ settings ) {
61+ public static function override_document_level_settings ( $ settings , $ post_id ) {
2162 $ post = get_post ( $ post_id );
22- if ( $ post ->post_type === $ post_type ) {
63+ if ( $ post ->post_type === self :: $ post_type ) {
2364 $ settings = array_merge (
24- $ base_settings ,
25- $ settings
65+ $ settings ,
66+ self :: $ settings
2667 );
2768 }
2869
2970 return $ settings ;
30- }, 10 , 2 );
71+ }
3172}
3273
3374// Configuration:
@@ -39,82 +80,12 @@ function gpsa_enable_for_all_posts_of_type( $post_type, $settings = array() ) {
3980 * 1. Update this argument to match the `$post_type` of the posts you'd like to target.
4081 * 2. Update gpsa_required_form_ids with the form ID you want to require.
4182*/
42- gpsa_enable_for_all_posts_of_type (
83+ new GPSA_Enable_For_All_Posts_Of_Type (
4384 'drum-machine ' ,
4485 array (
45- /**
46- * @var boolean
47- */
4886 'gpsa_enabled ' => true ,
49- /**
50- * @var array<int>
51- */
5287 'gpsa_required_form_ids ' => array ( 1 ), // UPDATE `1` with the form id you want to require.
88+ // optionally add other settings to the array. See the GPSADocumentSettings in the above
89+ // doc blocks for available options.
5390 )
5491);
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