diff --git a/gp-submit-to-access/gpsa-enable-for-all-posts-of-type.php b/gp-submit-to-access/gpsa-enable-for-all-posts-of-type.php new file mode 100644 index 000000000..d49e00175 --- /dev/null +++ b/gp-submit-to-access/gpsa-enable-for-all-posts-of-type.php @@ -0,0 +1,107 @@ +, + * gpsa_requires_access_message: string, + * gpsa_access_behavior: AccessBehavior + * } + */ +class GPSA_Enable_For_All_Posts_Of_Type { + /** + * @var boolean + */ + public static $post_type; + + /** + * @var GPSADocumentSettings + */ + public static $settings; + + /** + * @param array{ + * post_type: string + * settings: GPSADocumentSettings + * } $args + */ + public function __construct( $args = array() ) { + $args = wp_parse_args( $args, array( + 'post_type' => 'post', + 'settings' => array(), + ) ); + + self::$post_type = $args['post_type']; + self::$settings = wp_parse_args( $args['settings'], array( + 'gpsa_enabled' => true, + ) ); + + add_filter( 'gpsa_supported_post_types', array( self::class, 'ensure_supported_post_types' ), 10, 1 ); + add_filter( 'gpsa_document_settings', array( self::class, 'override_document_level_settings' ), 10, 2 ); + } + + public static function ensure_supported_post_types( $post_types ) { + if ( ! in_array( self::$post_type, $post_types ) ) { + $post_types[] = self::$post_type; + } + + return $post_types; + } + + public static function override_document_level_settings( $settings, $post_id ) { + $post = get_post( $post_id ); + if ( $post->post_type === self::$post_type ) { + $settings = array_merge( + $settings, + self::$settings + ); + } + + return $settings; + } +} + +// Configuration: + +/** + * Minimal configuration to enable for all posts of type `drum-machine`. +*/ +new GPSA_Enable_For_All_Posts_Of_Type( + array( + 'post_type' => 'drum-machine', // Update `drum-machine` to match the post type you want to target. + 'settings' => array( + 'gpsa_required_form_ids' => array( 3 ), // UPDATE `1` with the form id you want to require. + // optionally add other settings to the array. See the GPSADocumentSettings in the above + // doc blocks for available options. + ), + ) +);