PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.24
Post Grid Gutenberg Blocks – PostX v5.0.24
5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 All 238 releases
ultimate-post / addons / dynamic_content / includes / DCController.php

DCController.php in Post Grid Gutenberg Blocks – PostX 5.0.24, at addons/dynamic_content/includes/DCController.php

592 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ULTP;
3
4 use WP_REST_Response;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 /**
11 *
12 * Dynamic Content
13 *
14 * @package ULTP\Addons
15 * @since 4.1.1
16 */
17 final class DCController {
18
19 /**
20 * Setup class.
21 *
22 * @since v.4.1.1
23 */
24 public function __construct() {
25 require_once ULTP_PATH . '/addons/dynamic_content/includes/DCService.php';
26
27 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
28 }
29
30 public function register_routes() {
31 register_rest_route(
32 'ultp/v2',
33 '/get_dynamic_content/',
34 array(
35 array(
36 'methods' => 'POST',
37 'callback' => array( $this, 'handle_dynamic_data' ),
38 // 'permission_callback' => function () {
39 // return is_user_logged_in() && current_user_can( 'edit_posts' );
40 // },
41 'permission_callback' => function ( $request ) {
42 // there is an unauthenticated route for fetching user data against post, that can be used to fetch user sensitive data like user email, user password reset token hast ( user_activation_key ) , private or draft post title e.t.c as unauthenticated user.
43 $post_id = method_exists( $request, 'get_param' )
44 ? intval( $request->get_param('post_id') )
45 : ( isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0 );
46
47 $data_type = method_exists( $request, 'get_param' )
48 ? $request->get_param('data_type')
49 : ( isset( $request['data_type'] ) ? $request['data_type'] : '' );
50
51 // User must be logged in
52 if ( ! is_user_logged_in() ) {
53 return false;
54 }
55
56 // If no post_id, require edit_posts capability
57 if ( $post_id <= 0 ) {
58 return current_user_can( 'edit_posts' );
59 }
60
61 // Get the post
62 $post = get_post( $post_id );
63 if ( ! $post ) {
64 return false;
65 }
66
67 // Check if post is password protected
68 if ( post_password_required( $post ) ) {
69 return false;
70 }
71
72 // User must be able to edit this specific post
73 if ( ! current_user_can( 'edit_post', $post_id ) ) {
74 return false;
75 }
76
77 // For non-published posts, user must be author OR have edit_posts capability
78 if ( $post->post_status !== 'publish' ) {
79 if ( (int) $post->post_author !== get_current_user_id() && ! current_user_can( 'edit_posts' ) ) {
80 return false;
81 }
82 }
83
84 // Restrict author_info to users who can edit the author OR admins
85 if ( $data_type === 'author_info' ) {
86 $author_id = (int) $post->post_author;
87 if ( ! current_user_can( 'edit_user', $author_id ) && ! current_user_can( 'manage_options' ) ) {
88 return false;
89 }
90 }
91
92 return true;
93 },
94 'args' => array(),
95 ),
96 )
97 );
98
99 register_rest_route(
100 'ultp/v2',
101 '/get_custom_fields/',
102 array(
103 array(
104 'methods' => 'POST',
105 'callback' => array( $this, 'handle_custom_fields' ),
106 'permission_callback' => '__return_true',
107 'args' => array(),
108 ),
109 )
110 );
111 }
112
113 /**
114 * Dynamic data to show in the editor
115 *
116 * @since v.4.1.1
117 * @param WP_REST_Request $server
118 * @return WP_REST_Response
119 */
120 public function handle_dynamic_data( $server ) {
121 $args = $server->get_params();
122 $post_id = isset( $args['post_id'] ) ? intval( $args['post_id'] ) : '';
123 $data_type = isset( $args['data_type'] ) ? $args['data_type'] : '';
124 $key = isset( $args['key'] ) ? $args['key'] : '';
125
126 return rest_ensure_response(
127 array(
128 'data' => DCService::get_dynamic_data(
129 array(
130 'post_id' => $post_id,
131 'key' => $key,
132 'data_type' => $data_type,
133 )
134 ),
135 )
136 );
137 }
138
139 /**
140 * Gets custom fields keys and labels
141 *
142 * @since v.4.1.1
143 * @param WP_REST_Request $server
144 * @return WP_REST_Response
145 */
146 public function handle_custom_fields( $server ) {
147 $args = $server->get_params();
148
149 $post_id = isset( $args['post_id'] ) ? intval( $args['post_id'] ) : 0;
150 $post_type = isset( $args['post_type'] ) ? $args['post_type'] : '';
151 $field_type = isset( $args['acf_field_type'] ) ? $args['acf_field_type'] : '';
152
153 $res = array();
154
155 // All metas
156 $all_custom_metas = $this->get_custom_metas( $post_id, $post_type );
157
158 // ACF
159 $acf_field_keys = $this->get_acf_fields( $post_id, $field_type, $post_type );
160
161 // Meta Box
162 $mb_field_keys = $this->get_mb_fields( $post_id, $field_type, $post_type );
163
164 // Pods
165 $pods_field_keys = $this->get_pods_fields( $post_id, $field_type, $post_type );
166
167 $filtered_custom_metas = array_values( array_diff( $all_custom_metas, $acf_field_keys['_fields'] ) );
168 $filtered_custom_metas = array_filter(
169 $filtered_custom_metas,
170 function ( $item ) use ( $acf_field_keys ) {
171 foreach ( $acf_field_keys['prefixes'] as $prefix ) {
172 if ( str_starts_with( $item, $prefix ) ) {
173 return false;
174 }
175 }
176 return true;
177 }
178 );
179 $filtered_custom_metas = array_values( array_diff( $filtered_custom_metas, $mb_field_keys['_fields'] ) );
180 $filtered_custom_metas = array_values( array_diff( $filtered_custom_metas, $pods_field_keys['_fields'] ) );
181
182 // Return value
183 $res['custom_metas'] = array(
184 'fields' => array_map(
185 function ( $item ) {
186 return array(
187 'label' => $item,
188 'value' => $item,
189 );
190 },
191 $filtered_custom_metas
192 ),
193 );
194
195 $res['acf'] = array(
196 'fields' => $acf_field_keys['fields'],
197 );
198
199 $res['mb'] = array(
200 'fields' => $mb_field_keys['fields'],
201 );
202
203 $res['pods'] = array(
204 'fields' => $pods_field_keys['fields'],
205 );
206
207 return rest_ensure_response(
208 array(
209 'data' => $res,
210 )
211 );
212 }
213
214 /**
215 * Get ACF fields
216 *
217 * @param int $post_id
218 * @param string $field_type
219 * @param string $post_type
220 *
221 * @return array
222 * @since 4.1.1
223 */
224 public function get_acf_fields( $post_id, $field_type, $post_type ) {
225 $res = array(
226 'fields' => array(),
227 '_fields' => array(),
228 'prefixes' => array(),
229 );
230
231 if ( ! class_exists( 'ACF' ) ) {
232 return $res;
233 }
234
235 if ( empty( $post_id ) && empty( $post_type ) ) {
236 global $post;
237 $post_id = isset( $post->ID ) ? $post->ID : '';
238 }
239
240 $allowed_types = $this->get_allowed_acf_field_types( $field_type );
241
242 // If post_id set set, return ACF fields for that post
243 if ( ! empty( $post_id ) ) {
244 $fields = get_field_objects( $post_id );
245 if ( is_array( $fields ) ) {
246 foreach ( $fields as $field ) {
247 if ( in_array( $field['type'], $allowed_types, true ) ) {
248 $res['fields'][] = array(
249 'value' => $field['name'],
250 'label' => $field['label'],
251 );
252 }
253
254 $res['_fields'][] = $field['name'];
255
256 // For filtering out pesky repeater field's sub fields
257 if ( $field['type'] === 'repeater' ) {
258 $res['prefixes'][] = $field['name'];
259 }
260 }
261 }
262 } elseif ( ! empty( $post_type ) ) {
263 $groups = acf_get_field_groups( array( 'post_type' => $post_type ) );
264 foreach ( $groups as $group ) {
265 $fields = acf_get_fields( $group['ID'] );
266 if ( is_array( $fields ) ) {
267 foreach ( $fields as $field ) {
268 if ( in_array( $field['type'], $allowed_types, true ) ) {
269 $res['fields'][] = array(
270 'value' => $field['name'],
271 'label' => $field['label'] . ' [' . $group['title'] . ']',
272 );
273 }
274 $res['_fields'][] = $field['name'];
275
276 if ( $field['type'] === 'repeater' ) {
277 $res['prefixes'][] = $field['name'];
278 }
279 }
280 }
281 }
282 }
283
284 return $res;
285 }
286
287 /**
288 * Get Meta Box fields
289 *
290 * @param int $post_id
291 * @param string $field_type
292 * @param string $post_type
293 *
294 * @return array
295 * @since 4.1.1
296 */
297 public function get_mb_fields( $post_id, $field_type, $post_type ) {
298 $res = array(
299 'fields' => array(),
300 '_fields' => array(),
301 'prefixes' => array(),
302 );
303
304 if ( ! function_exists( 'rwmb_get_field_settings' ) ) {
305 return $res;
306 }
307
308 $value = null;
309
310 if ( empty( $post_id ) && empty( $post_type ) ) {
311 global $post;
312 $value = isset( $post->ID ) ? $post->ID : '';
313 } elseif ( ! empty( $post_id ) ) {
314 $value = $post_id;
315 } elseif ( ! empty( $post_type ) ) {
316 $value = $post_type;
317 }
318
319 if ( empty( $value ) ) {
320 return $res;
321 }
322
323 $allowed_types = self::get_allowed_mb_field_type( $field_type );
324
325 $fields = rwmb_get_object_fields( $value );
326
327 if ( is_array( $fields ) ) {
328 foreach ( $fields as $field ) {
329 if ( in_array( $field['type'], $allowed_types, true ) ) {
330 $res['fields'][] = array(
331 'value' => $field['id'],
332 'label' => $field['name'],
333 );
334 }
335
336 $res['_fields'][] = $field['id'];
337 }
338 }
339
340 return $res;
341 }
342
343 /**
344 * Get Pods fields
345 *
346 * @param int $post_id
347 * @param string $field_type
348 * @param string $post_type
349 *
350 * @return array
351 * @since 4.1.1
352 */
353 public function get_pods_fields( $post_id, $field_type, $post_type ) {
354 $res = array(
355 'fields' => array(),
356 '_fields' => array(),
357 'prefixes' => array(),
358 );
359
360 if ( ! function_exists( 'pods' ) ) {
361 return $res;
362 }
363
364 if ( empty( $post_id ) ) {
365 global $post;
366 $post_id = isset( $post->ID ) ? $post->ID : '';
367 }
368
369 if ( empty( $post_type ) ) {
370 $post_type = get_post_type( $post_id );
371 }
372
373 if ( empty( $post_type ) ) {
374 return $res;
375 }
376
377 $allowed_types = self::get_allowed_pods_field_types( $field_type );
378
379 $pods = pods( $post_type, $post_id );
380 if ( is_object( $pods ) && method_exists( $pods, 'exists' ) && $pods->exists() && method_exists( $pods, 'fields' ) ) {
381 $fields = $pods->fields();
382 if ( is_array( $fields ) ) {
383 foreach ( $fields as $field ) {
384 if ( in_array( $field['type'], $allowed_types, true ) &&
385 'field' === $field['object_type'] &&
386 'post_type' === $field['object_storage_type']
387 ) {
388 $res['fields'][] = array(
389 'value' => $field['name'],
390 'label' => $field['label'],
391 );
392 }
393
394 $res['_fields'][] = $field['name'];
395 }
396 }
397 }
398
399 return $res;
400 }
401
402 /**
403 * Get ACF allowed field types
404 *
405 * @param string $type optional, possible values 'text'|'image'|'url'.
406 *
407 * @return array
408 * @since 4.1.1
409 */
410 public static function get_allowed_acf_field_types( $type = 'text' ) {
411
412 if ( 'image' === $type ) {
413 return array(
414 'image',
415 );
416 } elseif ( 'url' === $type ) {
417 return array(
418 'text',
419 'email',
420 'image',
421 'file',
422 'page_link',
423 'url',
424 'link',
425 );
426 }
427
428 return array(
429 'text',
430 'textarea',
431 'number',
432 'range',
433 'email',
434 'url',
435 'password',
436 'wysiwyg',
437 'select',
438 'checkbox',
439 'radio',
440 'true_false',
441 'date_picker',
442 'time_picker',
443 'date_time_picker',
444 'color_picker',
445 );
446 }
447
448 /**
449 * Get Meta Box allowed field types
450 *
451 * @param string $type optional, possible values 'text'|'image'|'url'.
452 *
453 * @return array
454 * @since 4.1.1
455 */
456 public static function get_allowed_mb_field_type( $type ) {
457
458 if ( $type === 'image' ) {
459 return array(
460 'image',
461 'image_advanced',
462 'image_upload',
463 'single_image',
464 'url',
465 'file',
466 'file_advanced',
467 'file_input',
468 'file_upload',
469 );
470 } elseif ( $type === 'url' ) {
471 return array(
472 'url',
473 'file',
474 'file_advanced',
475 'file_input',
476 'file_upload',
477 );
478 }
479
480 return array(
481 'text',
482 'email',
483 'number',
484 'textaraa',
485 'select',
486 'radio',
487 'checkbox',
488 'checkbox_list',
489 );
490 }
491
492 /**
493 * Get Pods allowed field types
494 *
495 * @param string $type optional, possible values 'text'|'image'|'url'.
496 *
497 * @return array
498 * @since 4.1.1
499 */
500 public static function get_allowed_pods_field_types( $type = 'text' ) {
501
502 if ( 'image' === $type ) {
503 return array(
504 'images',
505 'file',
506 );
507 } elseif ( 'url' === $type ) {
508 return array(
509 'text',
510 'email',
511 'images',
512 'file',
513 'link',
514 'website',
515 );
516 }
517
518 return array(
519 'text',
520 'paragraph',
521 'password',
522 'phone',
523 'time',
524 'website',
525 'number',
526 'wysiwyg',
527 'email',
528 'link',
529 'boolean',
530 'code',
531 'number',
532 'currency',
533 'date',
534 'datetime',
535 );
536 }
537
538 /**
539 * Get custom post metas
540 *
541 * @param string $post_type Post Type.
542 * @since 4.1.0
543 * @return array
544 */
545 public function get_custom_metas( $post_id, $post_type ) {
546
547 $meta_keys = array();
548 $all_keys = array();
549
550 if ( $post_id !== 0 ) {
551 $all_keys = get_post_custom_keys( $post_id );
552
553 } else {
554 if ( ! isset( $post_type ) ) {
555 return $meta_keys;
556 }
557
558 $posts = get_posts(
559 array(
560 'post_type' => $post_type,
561 'posts_per_page' => -1,
562 'fields' => 'id',
563 )
564 );
565
566 if ( empty( $posts ) ) {
567 return $meta_keys;
568 }
569
570 foreach ( $posts as $post ) {
571 $post_id = isset( $post->ID ) ? $post->ID : 0;
572 $keys = get_post_custom_keys( $post_id );
573 if ( is_array( $keys ) ) {
574 foreach ( $keys as $key ) {
575 $all_keys[] = $key;
576 }
577 }
578 }
579 }
580
581 if ( is_array( $all_keys ) ) {
582 foreach ( $all_keys as $key ) {
583 if ( ! isset( $meta_keys[ $key ] ) && ! str_starts_with( $key, '_' ) ) {
584 $meta_keys[ $key ] = $key;
585 }
586 }
587 }
588
589 return array_keys( $meta_keys );
590 }
591 }
592