PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Controllers / RestAPI.php

RestAPI.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/Controllers/RestAPI.php

178 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * RestAPI blocks setup.
4 *
5 * @copyright (c) 2021, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\Controllers;
10
11 defined( 'ABSPATH' ) || exit;
12
13 use ContentControl\Base\Controller;
14
15 use function ContentControl\check_referrer_is_admin;
16 use function ContentControl\is_rest;
17
18 /**
19 * RestAPI function initialization.
20 */
21 class RestAPI extends Controller {
22 /**
23 * Initiate rest api integrations.
24 */
25 public function init() {
26 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
27
28 if ( $this->container->is_legacy_pro_active() ) {
29 add_filter( 'rest_endpoints', [ $this, 'disable_legacy_pro_installer_route' ], PHP_INT_MAX );
30 }
31
32 // Handle CPT & Taxonomy that are not registered with the `show_in_rest` arg when searching from our settings pages.
33 add_filter( 'register_post_type_args', [ $this, 'modify_post_type_show_in_rest' ], 10 );
34 add_filter( 'register_taxonomy_args', [ $this, 'modify_taxonomy_show_in_rest' ], 10 );
35 }
36
37 /**
38 * Register Rest API routes.
39 *
40 * @return void
41 */
42 public function register_routes() {
43 ( new \ContentControl\RestAPI\BlockTypes() )->register_routes();
44
45 if ( $this->container->is_legacy_pro_active() ) {
46 ( new \ContentControl\RestAPI\License() )->register_routes();
47 }
48
49 ( new \ContentControl\RestAPI\ObjectSearch() )->register_routes();
50 ( new \ContentControl\RestAPI\Settings() )->register_routes();
51 }
52
53 /**
54 * Remove the installer route registered by active Pro versions below 1.3.0.
55 *
56 * Those versions resolve an upgrader service that is unavailable in Core
57 * 2.7.0. Removing the endpoint prevents a fatal request while Pro is updated
58 * manually. Core does not register or implement the endpoint.
59 *
60 * @since 2.7.0
61 * @deprecated 2.7.0 Remove after Pro 1.3.0 is the minimum supported version.
62 *
63 * @param array<string,array<int,array<string,mixed>>> $endpoints Registered REST endpoints.
64 *
65 * @return array<string,array<int,array<string,mixed>>>
66 */
67 public function disable_legacy_pro_installer_route( $endpoints ) {
68 unset( $endpoints['/content-control/v2/addons/install'] );
69
70 return $endpoints;
71 }
72
73 /**
74 * Modify show_in_rest for post types.
75 *
76 * @param array<string,mixed> $args Post type args.
77 *
78 * @return array<string,mixed>
79 */
80 public function modify_post_type_show_in_rest( $args ) {
81 $include_private = $this->container->get_option( 'includePrivatePostTypes', true );
82 return $this->modify_type_force_show_in_rest( $args, $include_private );
83 }
84
85 /**
86 * Modify show_in_rest for taxonomies.
87 *
88 * @param array<string,mixed> $args Taxonomy args.
89 *
90 * @return array<string,mixed>
91 */
92 public function modify_taxonomy_show_in_rest( $args ) {
93 $include_private = $this->container->get_option( 'includePrivateTaxonomies', true );
94 return $this->modify_type_force_show_in_rest( $args, $include_private );
95 }
96
97 /**
98 * Modify show_in_rest for post types.
99 *
100 * @param array<string,mixed> $args Post type args.
101 * @param boolean $include_private Whether to include private post types.
102 *
103 * @return array<string,mixed>
104 */
105 public function modify_type_force_show_in_rest( $args, $include_private = false ) {
106 if ( ! $this->force_show_in_rest() ) {
107 return $args;
108 }
109
110 // If show_in_rest is already false, return early.
111 if ( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) {
112 return $args;
113 }
114
115 $is_public = isset( $args['public'] ) && $args['public'];
116
117 // Check if this is a private taxonomy.
118 if ( ! $is_public ) {
119 if ( $include_private ) {
120 $args['show_in_rest'] = true; // Enable REST API.
121 }
122 } else {
123 $args['show_in_rest'] = true; // Enable REST API.
124 }
125
126 return $args;
127 }
128
129 /**
130 * Force show_in_rest to true.
131 *
132 * @return boolean
133 */
134 public function force_show_in_rest() {
135 static $force_show_in_rest;
136
137 if ( isset( $force_show_in_rest ) ) {
138 return $force_show_in_rest;
139 }
140
141 $force_show_in_rest = false;
142
143 if ( ! is_rest() ) {
144 return false;
145 }
146
147 // Return false if referrer is not our settings page. /wp-admin/options-general.php?page=content-control-settings.
148 if ( ! isset( $_SERVER['HTTP_REFERER'] ) || ! is_string( $_SERVER['HTTP_REFERER'] ) ) {
149 return false;
150 }
151
152 if ( ! check_referrer_is_admin() ) {
153 return false;
154 }
155
156 $referrer = sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
157
158 // Check if referrer is our settings page and contains the page=content-control-settings query param.
159 if ( strpos( $referrer, 'options-general.php' ) === false ) {
160 return false;
161 }
162
163 // Check if referrer is our settings page and contains the page=content-control-settings query param.
164 if ( strpos( $referrer, 'page=content-control-settings' ) === false ) {
165 return false;
166 }
167
168 // Check if current user has permission to make plugin settings changes.
169 if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) {
170 return false;
171 }
172
173 $force_show_in_rest = true;
174
175 return true;
176 }
177 }
178