| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registers Registration Forms (wppb-rf-cpt) and Edit Profile Forms (wppb-epf-cpt). |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 7 |
|
| 8 |
// Priority 11: must run after bundled multiple-forms CPT registration (RF at 9, |
| 9 |
// EPF at default 10). At 10, classic EPF would win FIFO and wipe show_in_rest. |
| 10 |
add_action( 'init', 'wppb_fb_register_form_cpts', 11 ); |
| 11 |
function wppb_fb_register_form_cpts() { |
| 12 |
$rf_labels = array( |
| 13 |
'name' => __( 'Registration Forms', 'profile-builder' ), |
| 14 |
'singular_name' => __( 'Registration Form', 'profile-builder' ), |
| 15 |
'add_new' => __( 'Add New Registration Form', 'profile-builder' ), |
| 16 |
'add_new_item' => __( 'Add New Registration Form', 'profile-builder' ), |
| 17 |
'edit_item' => __( 'Edit Registration Form', 'profile-builder' ), |
| 18 |
'new_item' => __( 'New Registration Form', 'profile-builder' ), |
| 19 |
'view_item' => __( 'View Registration Form', 'profile-builder' ), |
| 20 |
'search_items' => __( 'Search Registration Forms', 'profile-builder' ), |
| 21 |
'not_found' => __( 'No Registration Forms found', 'profile-builder' ), |
| 22 |
'not_found_in_trash' => __( 'No Registration Forms found in Trash', 'profile-builder' ), |
| 23 |
); |
| 24 |
|
| 25 |
$epf_labels = array( |
| 26 |
'name' => __( 'Edit Profile Forms', 'profile-builder' ), |
| 27 |
'singular_name' => __( 'Edit Profile Form', 'profile-builder' ), |
| 28 |
'add_new' => __( 'Add New Edit Profile Form', 'profile-builder' ), |
| 29 |
'add_new_item' => __( 'Add New Edit Profile Form', 'profile-builder' ), |
| 30 |
'edit_item' => __( 'Edit Edit Profile Form', 'profile-builder' ), |
| 31 |
'new_item' => __( 'New Edit Profile Form', 'profile-builder' ), |
| 32 |
'view_item' => __( 'View Edit Profile Form', 'profile-builder' ), |
| 33 |
'search_items' => __( 'Search Edit Profile Forms', 'profile-builder' ), |
| 34 |
'not_found' => __( 'No Edit Profile Forms found', 'profile-builder' ), |
| 35 |
'not_found_in_trash' => __( 'No Edit Profile Forms found in Trash', 'profile-builder' ), |
| 36 |
); |
| 37 |
|
| 38 |
$common = array( |
| 39 |
'public' => false, |
| 40 |
'show_ui' => true, |
| 41 |
'show_in_menu' => 'profile-builder', |
| 42 |
'query_var' => false, |
| 43 |
'rewrite' => false, |
| 44 |
'has_archive' => false, |
| 45 |
'hierarchical' => false, |
| 46 |
'capability_type' => 'post', |
| 47 |
// Primitives → manage_options; keep map_meta_cap so meta caps still |
| 48 |
// resolve (delete protection + current_user_can( 'edit_post', … )). |
| 49 |
// Free installs also get create_posts => do_not_allow via licensing. |
| 50 |
'map_meta_cap' => true, |
| 51 |
'capabilities' => array_merge( array( |
| 52 |
'edit_post' => 'edit_post', |
| 53 |
'read_post' => 'read_post', |
| 54 |
'delete_post' => 'delete_post', |
| 55 |
'edit_posts' => 'manage_options', |
| 56 |
'edit_others_posts' => 'manage_options', |
| 57 |
'publish_posts' => 'manage_options', |
| 58 |
'read_private_posts' => 'manage_options', |
| 59 |
'create_posts' => 'manage_options', |
| 60 |
'delete_posts' => 'manage_options', |
| 61 |
'delete_private_posts' => 'manage_options', |
| 62 |
'delete_published_posts' => 'manage_options', |
| 63 |
'delete_others_posts' => 'manage_options', |
| 64 |
'edit_private_posts' => 'manage_options', |
| 65 |
'edit_published_posts' => 'manage_options', |
| 66 |
), wppb_fb_form_cpt_capability_overrides() ), |
| 67 |
'supports' => array( 'title', 'editor', 'custom-fields' ), |
| 68 |
'show_in_rest' => true, |
| 69 |
// Custom controller: default allows anonymous collection reads. |
| 70 |
'rest_controller_class' => 'WPPB_FB_Forms_REST_Controller', |
| 71 |
); |
| 72 |
|
| 73 |
// No block `template` on RF: template blocks get id=0 and first save would |
| 74 |
// duplicate Username/E-mail/Password rows. Seed via defaults.php instead. |
| 75 |
// Classic-mode CPTs: skip registration so bundled WCK registration wins. |
| 76 |
if ( wppb_fb_is_active_for( 'wppb-rf-cpt' ) ) { |
| 77 |
register_post_type( 'wppb-rf-cpt', array_merge( $common, array( |
| 78 |
'labels' => $rf_labels, |
| 79 |
'rest_base' => 'wppb-rf-cpt', |
| 80 |
) ) ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( wppb_fb_is_active_for( 'wppb-epf-cpt' ) ) { |
| 84 |
register_post_type( 'wppb-epf-cpt', array_merge( $common, array( |
| 85 |
'labels' => $epf_labels, |
| 86 |
'rest_base' => 'wppb-epf-cpt', |
| 87 |
) ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Require manage_options for form CPT REST reads (anonymous GET would leak forms). |
| 93 |
* WP_REST_Posts_Controller is loaded from wp-settings.php before plugins. |
| 94 |
*/ |
| 95 |
if ( class_exists( 'WP_REST_Posts_Controller' ) && ! class_exists( 'WPPB_FB_Forms_REST_Controller' ) ) { |
| 96 |
|
| 97 |
class WPPB_FB_Forms_REST_Controller extends WP_REST_Posts_Controller { |
| 98 |
|
| 99 |
public function get_items_permissions_check( $request ) { |
| 100 |
$check = parent::get_items_permissions_check( $request ); |
| 101 |
if ( is_wp_error( $check ) || false === $check ) { |
| 102 |
return $check; |
| 103 |
} |
| 104 |
return $this->wppb_fb_require_manage_options(); |
| 105 |
} |
| 106 |
|
| 107 |
public function get_item_permissions_check( $request ) { |
| 108 |
$check = parent::get_item_permissions_check( $request ); |
| 109 |
if ( is_wp_error( $check ) || false === $check ) { |
| 110 |
return $check; |
| 111 |
} |
| 112 |
return $this->wppb_fb_require_manage_options(); |
| 113 |
} |
| 114 |
|
| 115 |
protected function wppb_fb_require_manage_options() { |
| 116 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 117 |
return new WP_Error( |
| 118 |
'rest_forbidden', |
| 119 |
__( 'Sorry, you are not allowed to view Profile Builder forms.', 'profile-builder' ), |
| 120 |
array( 'status' => rest_authorization_required_code() ) |
| 121 |
); |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|