PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.0
Post Grid Gutenberg Blocks – PostX v5.1.0
5.1.1 5.1.0 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 All 240 releases
ultimate-post / addons / builder / RequestAPI.php

RequestAPI.php in Post Grid Gutenberg Blocks – PostX 5.1.0, at addons/builder/RequestAPI.php

537 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Cache.
5 *
6 * @package ULTP\RequestAPI
7 * @since v.2.7.0
8 */
9
10 namespace ULTP;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * RequestAPI class.
16 */
17 class RequestAPI {
18 private $api_endpoint = 'https://postxkit.wpxpo.com/wp-json/restapi/v2/';
19
20 public function __construct() {
21 add_action( 'rest_api_init', array( $this, 'get_template_data' ) );
22 }
23
24 /**
25 * Create Builder Post Type
26 *
27 * @since v.2.7.0
28 * @return NULL
29 */
30 public function get_template_data() {
31 register_rest_route(
32 'ultp/v2',
33 '/get_single_premade/',
34 array(
35 array(
36 'methods' => 'POST',
37 'callback' => array( $this, 'get_single_premade_callback' ),
38 'permission_callback' => function () {
39 return current_user_can( 'manage_options' );
40 },
41 'args' => array(),
42 ),
43 )
44 );
45 register_rest_route(
46 'ultp/v2',
47 '/condition/',
48 array(
49 array(
50 'methods' => 'POST',
51 'callback' => array( $this, 'condition_settings_action' ),
52 'permission_callback' => function () {
53 return current_user_can( 'manage_options' );
54 },
55 'args' => array(),
56 ),
57 )
58 );
59 register_rest_route(
60 'ultp/v2',
61 '/condition_save/',
62 array(
63 array(
64 'methods' => 'POST',
65 'callback' => array( $this, 'condition_save_action' ),
66 'permission_callback' => function () {
67 return current_user_can( 'manage_options' );
68 },
69 'args' => array(),
70 ),
71 )
72 );
73 register_rest_route(
74 'ultp/v2',
75 '/data_builder/',
76 array(
77 array(
78 'methods' => 'POST',
79 'callback' => array( $this, 'data_builder_action' ),
80 'permission_callback' => function () {
81 return current_user_can( 'manage_options' );
82 },
83 'args' => array(),
84 ),
85 )
86 );
87 }
88
89 /**
90 * Builder Post Type Data
91 *
92 * @since v.2.7.0
93 * @param ARRAY
94 * @return ARRAY | Information of Builder Post
95 */
96 public function data_builder_action( $server ) {
97 $post = $server->get_params();
98 $s_pid = isset( $post['pid'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['pid'] ) : '';
99
100 $args = array(
101 'post_type' => 'ultp_builder',
102 'post_status' => array( 'publish', 'draft' ),
103 'orderby' => 'title',
104 'order' => 'ASC',
105 'posts_per_page' => -1,
106 );
107 $post_results = new \WP_Query( $args );
108 $post_list = array();
109 if ( ! empty( $post_results ) ) {
110 while ( $post_results->have_posts() ) {
111 $post_results->the_post();
112 $id = get_the_ID();
113 $meta_type = get_post_meta( $id, '__ultp_builder_type', true );
114 $post_list[] = array(
115 'id' => $id,
116 'title' => get_the_title(),
117 'author' => get_the_author_meta( 'display_name' ),
118 'date' => get_the_date( get_option( 'date_format' ) ),
119 'edit' => get_edit_post_link( $id ),
120 'type' => $meta_type ? $meta_type : 'archive',
121 'status' => get_post_status(),
122 );
123 }
124 wp_reset_postdata();
125 }
126
127 $arg = array(
128 'success' => true,
129 'postlist' => $post_list,
130 'settings' => get_option( 'ultp_builder_conditions', array() ),
131 'defaults' => $this->builder_data(),
132 );
133 if ( $s_pid ) {
134 $post_meta = get_post_meta( $s_pid, '__ultp_builder_type', true );
135 $arg['type'] = $post_meta ? $post_meta : 'archive';
136 }
137 return $arg;
138 }
139
140
141 /**
142 * Get Add Default Condition Data
143 *
144 * @since v.2.7.0
145 * @return ARRAY | Default Data
146 */
147 public function builder_data() {
148 $archive_data = array(
149 array(
150 'label' => 'All Archive',
151 'value' => '',
152 ),
153 array(
154 'label' => 'Author Archive',
155 'value' => 'author',
156 'search' => 'author###',
157 ),
158 array(
159 'label' => 'Date Archive',
160 'value' => 'date',
161 ),
162 array(
163 'label' => 'Search Results',
164 'value' => 'search',
165 ),
166 );
167 $single_data = array();
168 $header_data = array(
169 array(
170 'label' => 'Entire Site',
171 'value' => 'entire_site',
172 ),
173 array(
174 'label' => 'Archive',
175 'value' => 'archive',
176 ),
177 array(
178 'label' => 'Singular',
179 'value' => 'singular',
180 ),
181 );
182
183 $post_type = get_post_types( array( 'public' => true ), 'objects' );
184 foreach ( $post_type as $key => $type ) {
185 // Post Type
186 $single_temp = array(
187 'label' => $type->label,
188 'value' => $type->name,
189 'search' => 'type###' . $type->name,
190 );
191 $archive_temp = array();
192
193 // Taxonomy
194 $taxonomy = get_object_taxonomies( $type->name, 'objects' );
195 if ( ! empty( $taxonomy ) ) {
196 $single_tax = $archive_tax = array();
197 $single_tax[] = $single_temp;
198
199 $archive_temp = array(
200 'label' => $type->label . ' Archive',
201 'value' => $type->name . '_archive',
202 );
203 // $archive_tax[] = $archive_temp;
204 foreach ( $taxonomy as $key => $val ) {
205 if ( $val->public ) {
206 $single_tax[] = array(
207 'label' => 'In ' . $val->label,
208 'value' => 'in_' . $val->name,
209 'search' => 'term###' . $val->name,
210 );
211 $archive_tax[] = array(
212 'label' => $val->label,
213 'value' => $val->name,
214 'search' => 'term###' . $val->name,
215 );
216
217 if ( $val->hierarchical ) {
218 // Hierarchical
219 $single_tax[] = array(
220 'label' => 'In Child ' . $val->label,
221 'value' => 'in_' . $val->name . '_children',
222 'search' => 'term###' . $val->name,
223 );
224 $archive_tax[] = array(
225 'label' => 'Direct Child ' . $val->label . ' Of',
226 'value' => 'child_of_' . $val->name,
227 'search' => 'term###' . $val->name,
228 );
229 $archive_tax[] = array(
230 'label' => 'Any Child ' . $val->label . ' Of',
231 'value' => 'any_child_of_' . $val->name,
232 'search' => 'term###' . $val->name,
233 );
234 }
235 }
236 }
237 // Author
238 $single_tax[] = array(
239 'label' => 'Posts By Author',
240 'value' => 'post_by_author',
241 'search' => 'author###',
242 );
243 $single_temp['attr'] = $single_tax;
244 $archive_temp['attr'] = $archive_tax;
245 }
246 $single_data[] = $single_temp;
247 if ( ! empty( $archive_temp ) ) {
248 $archive_data[] = $archive_temp;
249 }
250 }
251
252 return array(
253 'singular' => $single_data,
254 'archive' => $archive_data,
255 'header' => $header_data,
256 'footer' => $header_data,
257 );
258 }
259
260
261 /**
262 * Save Conditions Data
263 *
264 * @since v.2.7.0
265 * @param ARRAY
266 * @return ARRAY | Message of the Condition Success
267 */
268 public function condition_save_action( $server ) {
269 $post = $server->get_params();
270 $s_settings = isset( $post['settings'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['settings'] ) : '';
271
272 if ( $s_settings ) {
273 update_option( 'ultp_builder_conditions', $s_settings );
274 return array(
275 'success' => true,
276 'data' => 'Settings Saved!!!',
277 's_settings' => $s_settings,
278 );
279 }
280 }
281
282
283 /**
284 * Condition Settings Actions
285 *
286 * @since v.2.7.0
287 * @param ARRAY
288 * @return ARRAY | Data of the Condition
289 */
290 public function condition_settings_action( $server ) {
291 global $wpdb;
292 $post = $server->get_params();
293 $s_type = isset( $post['type'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['type'] ) : '';
294 $s_term = isset( $post['term'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['term'] ) : '';
295 $s_title_return = isset( $post['title_return'] ) ? rest_sanitize_boolean( $post['title_return'] ) : '';
296 $search_type = explode( '###', $s_type );
297
298 switch ( $search_type[0] ) {
299 case 'type':
300 $args = array(
301 'post_type' => $search_type[1],
302 'post_status' => 'publish',
303 'orderby' => 'title',
304 'order' => 'ASC',
305 's' => $s_term,
306 'posts_per_page' => 10,
307 );
308 if ( $s_title_return ) {
309 unset( $args['s'] );
310 $args['p'] = $s_term;
311 }
312 $post_results = new \WP_Query( $args );
313 $title = '';
314 $data = array();
315 if ( ! empty( $post_results ) ) {
316 while ( $post_results->have_posts() ) {
317 $post_results->the_post();
318 $id = get_the_ID();
319 $title = html_entity_decode( get_the_title() );
320 $data[] = array(
321 'value' => $id,
322 'title' => ( $title ? $title : ( '##' . $id ) ),
323 );
324 }
325 wp_reset_postdata();
326 }
327 return array(
328 'success' => true,
329 'data' => ( $s_title_return ? $title : $data ),
330 );
331 break;
332
333 case 'term':
334 $args = array(
335 'taxonomy' => $search_type[1],
336 'fields' => 'all',
337 'orderby' => 'id',
338 'order' => 'ASC',
339 'name__like' => $s_term,
340 );
341 if ( $s_title_return ) {
342 $args['term_taxonomy_id'] = array( $s_term );
343 unset( $args['name__like'] );
344 }
345 $post_results = get_terms( $args );
346 $title = '';
347 $data = array();
348 if ( ! empty( $post_results ) ) {
349 foreach ( $post_results as $key => $val ) {
350 $title = $val->name;
351 $data[] = array(
352 'value' => $val->term_id,
353 'title' => $title,
354 );
355 }
356 }
357 return array(
358 'success' => true,
359 'data' => ( $s_title_return ? $title : $data ),
360 );
361 break;
362
363 case 'author':
364 $term = $s_title_return ? $wpdb->esc_like( $s_term ) : '%' . $wpdb->esc_like( $s_term ) . '%';
365 $post_results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
366 $wpdb->prepare(
367 "SELECT ID, display_name
368 FROM $wpdb->users
369 WHERE user_login LIKE %s OR ID LIKE %s OR user_nicename LIKE %s OR user_email LIKE %s OR display_name LIKE %s LIMIT 10",
370 $term,
371 $term,
372 $term,
373 $term,
374 $term
375 )
376 );
377 $title = '';
378 $data = array();
379 if ( ! empty( $post_results ) ) {
380 foreach ( $post_results as $key => $val ) {
381 $title = $val->display_name;
382 $data[] = array(
383 'value' => $val->ID,
384 'title' => $val->display_name,
385 );
386 }
387 }
388 return array(
389 'success' => true,
390 'data' => ( $s_title_return ? $title : $data ),
391 );
392 break;
393 default:
394 return array( 'success' => false );
395 break;
396 }
397 return array(
398 'success' => true,
399 'data' => 'This is Testing !!!',
400 );
401 }
402
403
404 /**
405 * Single Premade Data and Create Builder Posts
406 *
407 * @since v.2.7.0
408 * @param ARRAY
409 * @return ARRAY | Data of the Premade
410 */
411 public function get_single_premade_callback( $server ) {
412
413 $post = $server->get_params();
414 $id = isset( $post['ID'] ) ? sanitize_text_field( $post['ID'] ) : '';
415 $api_endpoint = isset( $post['apiEndPoint'] ) ? sanitize_text_field( $post['apiEndPoint'] ) : '';
416 $s_type = isset( $post['type'] ) ? ultimate_post()->ultp_rest_sanitize_params( $post['type'] ) : '';
417
418 if ( $id ) {
419 // SSRF protection: only the template service, over HTTPS.
420 if ( 0 !== strpos( $api_endpoint, 'https://' ) || ! ultimate_post()->is_allowed_template_endpoint( $api_endpoint ) ) {
421 return rest_ensure_response(
422 array(
423 'success' => false,
424 'message' => __( 'Invalid API endpoint. Only trusted domains are allowed.', 'ultimate-post' ),
425 )
426 );
427 }
428
429 $import_single = ultimate_post()->get_template_request_body(
430 array(
431 'id' => $id,
432 'type' => 'single',
433 ),
434 'builder_premade'
435 );
436 $response = wp_safe_remote_post(
437 $api_endpoint . '/wp-json/importer/single',
438 array(
439 'method' => 'POST',
440 'timeout' => 120,
441 'body' => $import_single,
442 )
443 );
444
445 if ( is_wp_error( $response ) ) {
446 return rest_ensure_response(
447 array(
448 'success' => false,
449 'data' => 'Something went wrong:' . $response->get_error_message(),
450 )
451 );
452 }
453
454 $response_data = json_decode( wp_remote_retrieve_body( $response ) );
455
456 if ( empty( $response_data->success ) || ! isset( $response_data->content[0]->content ) ) {
457 return rest_ensure_response(
458 array(
459 'success' => false,
460 )
461 );
462 }
463
464 $content = $response_data->content[0]->content;
465 $post_id = $this->set_post_content( $s_type, wp_slash( $content ) );
466
467 return rest_ensure_response(
468 array(
469 'success' => true,
470 'link' => get_edit_post_link( $post_id ),
471 )
472 );
473 } else {
474 $post_id = $this->set_post_content( $s_type, '' );
475 return array(
476 'success' => true,
477 'link' => get_edit_post_link( $post_id ),
478 );
479 }
480 }
481
482 /**
483 * Single Premade Data and Insert Builder Posts
484 *
485 * @since v.2.7.0
486 * @param ARRAY
487 * @return INT | Post ID
488 */
489 public function set_post_content( $type, $body = '' ) {
490 $post_id = wp_insert_post(
491 array(
492 'post_title' => ucfirst( $type ) . ' Template',
493 'post_content' => $body,
494 'post_type' => 'ultp_builder',
495 'post_status' => 'draft',
496 )
497 );
498 $settings = get_option( 'ultp_builder_conditions', array() );
499 switch ( $type ) {
500 case 'singular':
501 update_post_meta( $post_id, '__ultp_builder_type', 'singular' );
502 $settings['singular'][ $post_id ] = array( 'include/singular/post' );
503 break;
504 case 'front_page':
505 update_post_meta( $post_id, '__ultp_builder_type', 'front_page' );
506 $settings['singular'][ $post_id ] = array( 'include/front_page' );
507 break;
508 case 'author':
509 case 'post_tag':
510 case 'date':
511 case 'search':
512 case 'archive':
513 case 'category':
514 update_post_meta( $post_id, '__ultp_builder_type', 'archive' );
515 $extra = $type != 'archive' ? '/' . $type : '';
516 $settings['archive'][ $post_id ] = array( 'include/archive' . $extra );
517 break;
518 case 'header':
519 update_post_meta( $post_id, '__ultp_builder_type', 'header' );
520 $settings['header'][ $post_id ] = array( 'include/header/entire_site' );
521 break;
522 case 'footer':
523 update_post_meta( $post_id, '__ultp_builder_type', 'footer' );
524 $settings['footer'][ $post_id ] = array( 'include/footer/entire_site' );
525 break;
526 case '404':
527 update_post_meta( $post_id, '__ultp_builder_type', '404' );
528 $settings['404'][ $post_id ] = array( 'include/404' );
529 break;
530 default:
531 break;
532 }
533 update_option( 'ultp_builder_conditions', $settings );
534 return $post_id;
535 }
536 }
537