| @@ -11,8 +11,11 @@ | ||
| 11 | 11 | defined( 'ABSPATH' ) || exit; |
| 12 | 12 | |
| 13 | 13 | use ContentControl\Base\Controller; |
| 14 | 14 | |
| 15 | +use function ContentControl\check_referrer_is_admin; | |
| 16 | +use function ContentControl\is_rest; | |
| 17 | + | |
| 15 | 18 | /** |
| 16 | 19 | * RestAPI function initialization. |
| 17 | 20 | */ |
| 18 | 21 | class RestAPI extends Controller { |
| @@ -20,12 +23,18 @@ | ||
| 20 | 23 | * Initiate rest api integrations. |
| 21 | 24 | */ |
| 22 | 25 | public function init() { |
| 23 | 26 | add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 27 | + | |
| 28 | + // Handle CPT & Taxonomy that are not registered with the `show_in_rest` arg when searching from our settings pages. | |
| 29 | + add_filter( 'register_post_type_args', [ $this, 'modify_post_type_show_in_rest' ], 10, 2 ); | |
| 30 | + add_filter( 'register_taxonomy_args', [ $this, 'modify_taxonomy_show_in_rest' ], 10, 2 ); | |
| 24 | 31 | } |
| 25 | 32 | |
| 26 | 33 | /** |
| 27 | 34 | * Register Rest API routes. |
| 35 | + * | |
| 36 | + * @return void | |
| 28 | 37 | */ |
| 29 | 38 | public function register_routes() { |
| 30 | 39 | ( new \ContentControl\RestAPI\BlockTypes() )->register_routes(); |
| 31 | 40 | ( new \ContentControl\RestAPI\License() )->register_routes(); |
| @@ -30,6 +39,111 @@ | ||
| 30 | 39 | ( new \ContentControl\RestAPI\BlockTypes() )->register_routes(); |
| 31 | 40 | ( new \ContentControl\RestAPI\License() )->register_routes(); |
| 32 | 41 | ( new \ContentControl\RestAPI\ObjectSearch() )->register_routes(); |
| 33 | 42 | ( new \ContentControl\RestAPI\Settings() )->register_routes(); |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Modify show_in_rest for post types. | |
| 47 | + * | |
| 48 | + * @param array<string,mixed> $args Post type args. | |
| 49 | + * | |
| 50 | + * @return array<string,mixed> | |
| 51 | + */ | |
| 52 | + public function modify_post_type_show_in_rest( $args ) { | |
| 53 | + $include_private = $this->container->get_option( 'includePrivatePostTypes', true ); | |
| 54 | + return $this->modify_type_force_show_in_rest( $args, $include_private ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Modify show_in_rest for taxonomies. | |
| 59 | + * | |
| 60 | + * @param array<string,mixed> $args Taxonomy args. | |
| 61 | + * | |
| 62 | + * @return array<string,mixed> | |
| 63 | + */ | |
| 64 | + public function modify_taxonomy_show_in_rest( $args ) { | |
| 65 | + $include_private = $this->container->get_option( 'includePrivateTaxonomies', true ); | |
| 66 | + return $this->modify_type_force_show_in_rest( $args, $include_private ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Modify show_in_rest for post types. | |
| 71 | + * | |
| 72 | + * @param array<string,mixed> $args Post type args. | |
| 73 | + * @param boolean $include_private Whether to include private post types. | |
| 74 | + * | |
| 75 | + * @return array<string,mixed> | |
| 76 | + */ | |
| 77 | + public function modify_type_force_show_in_rest( $args, $include_private = false ) { | |
| 78 | + if ( ! $this->force_show_in_rest() ) { | |
| 79 | + return $args; | |
| 80 | + } | |
| 81 | + | |
| 82 | + // If show_in_rest is already false, return early. | |
| 83 | + if ( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) { | |
| 84 | + return $args; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $is_public = isset( $args['public'] ) && $args['public']; | |
| 88 | + | |
| 89 | + // Check if this is a private taxonomy. | |
| 90 | + if ( ! $is_public ) { | |
| 91 | + if ( $include_private ) { | |
| 92 | + $args['show_in_rest'] = true; // Enable REST API. | |
| 93 | + } | |
| 94 | + } else { | |
| 95 | + $args['show_in_rest'] = true; // Enable REST API. | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $args; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Force show_in_rest to true. | |
| 103 | + * | |
| 104 | + * @return boolean | |
| 105 | + */ | |
| 106 | + public function force_show_in_rest() { | |
| 107 | + static $force_show_in_rest; | |
| 108 | + | |
| 109 | + if ( isset( $force_show_in_rest ) ) { | |
| 110 | + return $force_show_in_rest; | |
| 111 | + } | |
| 112 | + | |
| 113 | + $force_show_in_rest = false; | |
| 114 | + | |
| 115 | + if ( ! is_rest() ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Return false if referrer is not our settings page. /wp-admin/options-general.php?page=content-control-settings. | |
| 120 | + if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) { | |
| 121 | + return false; | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( ! check_referrer_is_admin() ) { | |
| 125 | + return false; | |
| 126 | + } | |
| 127 | + | |
| 128 | + $referrer = sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) ); | |
| 129 | + | |
| 130 | + // Check if referrer is our settings page and contains the page=content-control-settings query param. | |
| 131 | + if ( strpos( $referrer, 'options-general.php' ) === false ) { | |
| 132 | + return false; | |
| 133 | + } | |
| 134 | + | |
| 135 | + // Check if referrer is our settings page and contains the page=content-control-settings query param. | |
| 136 | + if ( strpos( $referrer, 'page=content-control-settings' ) === false ) { | |
| 137 | + return false; | |
| 138 | + } | |
| 139 | + | |
| 140 | + // Check if current user has permission to make plugin settings changes. | |
| 141 | + if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) { | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + | |
| 145 | + $force_show_in_rest = true; | |
| 146 | + | |
| 147 | + return true; | |
| 34 | 148 | } |
| 35 | 149 | } |