PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.2
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 / PostTypes.php

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

233 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post type setup.
4 *
5 * @copyright (c) 2023, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\Controllers;
10
11 use ContentControl\Base\Controller;
12
13 /**
14 * Post type controller.
15 */
16 class PostTypes extends Controller {
17
18 /**
19 * Init controller.
20 *
21 * @return void
22 */
23 public function init() {
24 add_action( 'init', [ $this, 'register_post_type' ] );
25 add_action( 'init', [ $this, 'register_rest_fields' ] );
26 add_action( 'save_post_cc_restriction', [ $this, 'save_post' ], 10, 3 );
27 add_filter( 'rest_pre_dispatch', [ $this, 'rest_pre_dispatch' ], 10, 3 );
28 }
29
30 /**
31 * Register `restriction` post type.
32 *
33 * @return void
34 */
35 public function register_post_type() {
36 /**
37 * Post Type: Restrictions.
38 */
39 $labels = [
40 'name' => __( 'Restrictions', 'content-control' ),
41 'singular_name' => __( 'Restriction', 'content-control' ),
42 ];
43
44 $args = [
45 'label' => __( 'Restrictions', 'content-control' ),
46 'labels' => $labels,
47 'description' => '',
48 'public' => false,
49 'publicly_queryable' => false,
50 'show_ui' => false,
51 'show_in_rest' => true,
52 'rest_base' => 'restrictions',
53 'rest_namespace' => 'content-control/v2',
54 'has_archive' => false,
55 'show_in_menu' => false,
56 'show_in_nav_menus' => false,
57 'delete_with_user' => false,
58 'exclude_from_search' => true,
59 'map_meta_cap' => true,
60 'hierarchical' => false,
61 'can_export' => true,
62 'rewrite' => false,
63 'query_var' => false,
64 'supports' => [ 'title' ],
65 'show_in_graphql' => false,
66 'capabilities' => [
67 'create_posts' => $this->container->get_permission( 'edit_restrictions' ),
68 'edit_posts' => $this->container->get_permission( 'edit_restrictions' ),
69 'delete_posts' => $this->container->get_permission( 'edit_restrictions' ),
70 ],
71
72 ];
73
74 register_post_type( 'cc_restriction', $args );
75 }
76
77 /**
78 * Registers custom REST API fields for cc_restrictions post type.
79 *
80 * @return void
81 */
82 public function register_rest_fields() {
83 register_rest_field( 'cc_restriction', 'title', [
84 'get_callback' => function ( $obj ) {
85 // remove get_the_title character encoding escape.
86 remove_filter( 'the_title', 'wptexturize' );
87 $title = get_the_title( $obj['id'] );
88 add_filter( 'the_title', 'wptexturize' );
89 return $title;
90 },
91 'update_callback' => function ( $value, $obj ) {
92 wp_update_post( [
93 'ID' => $obj->ID,
94 'post_title' => $value,
95 ] );
96 },
97 'schema' => [
98 'type' => 'string',
99 'arg_options' => [
100 'sanitize_callback' => function ( $value ) {
101 // Make the value safe for storage.
102 return sanitize_text_field( $value );
103 },
104 'validate_callback' => function ( $value ) {
105 // Validate title based on post_title.
106 return is_string( $value );
107 },
108 ],
109 ],
110 ] );
111
112 register_rest_field( 'cc_restriction', 'description', [
113 'get_callback' => function ( $obj ) {
114 return get_the_excerpt( $obj['id'] );
115 },
116 'update_callback' => function ( $value, $obj ) {
117 wp_update_post( [
118 'ID' => $obj->ID,
119 'post_excerpt' => sanitize_text_field( $value ),
120 ] );
121 },
122 'schema' => [
123 'type' => 'string',
124 'arg_options' => [
125 'sanitize_callback' => function ( $value ) {
126 // Make the value safe for storage.
127 return sanitize_text_field( $value );
128 },
129 'validate_callback' => function ( $value ) {
130 // Validate title based on post_title.
131 return is_string( $value );
132 },
133 ],
134 ],
135 ] );
136
137 register_rest_field( 'cc_restriction', 'settings', [
138 'get_callback' => function ( $obj ) {
139 return get_post_meta( $obj['id'], 'restriction_settings', true );
140 },
141 'update_callback' => function ( $value, $obj ) {
142 // Update the field/meta value.
143 update_post_meta( $obj->ID, 'restriction_settings', $value );
144 },
145 'permission_callback' => function () {
146 return current_user_can( $this->container->get_permission( 'edit_restrictions' ) );
147 },
148 ] );
149
150 register_rest_field( 'cc_restriction', 'priority', [
151 'get_callback' => function ( $obj ) {
152 $priority = get_post_field( 'menu_order', $obj['id'] );
153
154 return $priority >= 0 ? $priority : 0;
155 },
156 'update_callback' => function ( $value, $obj ) {
157 // Update the posts menu order.
158 wp_update_post( [
159 'ID' => $obj->ID,
160 'menu_order' => $value,
161 ] );
162 },
163 'permission_callback' => function () {
164 return current_user_can( $this->container->get_permission( 'edit_restrictions' ) );
165 },
166 ] );
167
168 register_rest_field( 'cc_restriction', 'data_version', [
169 'get_callback' => function ( $obj ) {
170 return get_post_meta( $obj['id'], 'data_version', true );
171 },
172 'update_callback' => function ( $value, $obj ) {
173 // Update the field/meta value.
174 update_post_meta( $obj->ID, 'data_version', $value );
175 },
176 'permission_callback' => function () {
177 return current_user_can( $this->container->get_permission( 'edit_restrictions' ) );
178 },
179 ] );
180 }
181
182 /**
183 * Add data version meta to new restrictions.
184 *
185 * @param int $post_id Post ID.
186 * @param WP_Post $post Post object.
187 * @param bool $update Whether this is an existing post being updated or not.
188 *
189 * @return void
190 */
191 public function save_post( $post_id, $post, $update ) {
192 if ( $update ) {
193 return;
194 }
195
196 add_post_meta( $post_id, 'data_version', 1 );
197 }
198
199 /**
200 * Prevent access to restrictions endpoint.
201 *
202 * @param mixed $result Response to replace the requested version with.
203 * @param WP_REST_Server $server Server instance.
204 * @param WP_REST_Request $request Request used to generate the response.
205 * @return mixed
206 */
207 public function rest_pre_dispatch( $result, $server, $request ) {
208 // Get the route being requested.
209 $route = $request->get_route();
210
211 // Only proceed if we're creating a user.
212 if ( false === strpos( $route, '/content-control/v2/restrictions' ) ) {
213 return $result;
214 }
215
216 $current_user_can = current_user_can( $this->container->get_permission( 'edit_restrictions' ) );
217
218 // Prevent discovery of the endpoints data from unauthorized users.
219 if ( ! $current_user_can ) {
220 return new \WP_Error(
221 'rest_forbidden',
222 __( 'Access to this endpoint requires authorization.', 'content-control' ),
223 [
224 'status' => rest_authorization_required_code(),
225 ]
226 );
227 }
228
229 // Return data to the client to parse.
230 return $result;
231 }
232 }
233