PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.3
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.3
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 2.6.3, at classes/Controllers/RestAPI.php

150 lines 3.9 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 // 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 );
30 add_filter( 'register_taxonomy_args', [ $this, 'modify_taxonomy_show_in_rest' ], 10 );
31 }
32
33 /**
34 * Register Rest API routes.
35 *
36 * @return void
37 */
38 public function register_routes() {
39 ( new \ContentControl\RestAPI\BlockTypes() )->register_routes();
40 ( new \ContentControl\RestAPI\License() )->register_routes();
41 ( new \ContentControl\RestAPI\ObjectSearch() )->register_routes();
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;
148 }
149 }
150