PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.34
Post Grid Gutenberg Blocks – PostX v5.0.34
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 2.1.2 All 237 releases
ultimate-post / addons / builder / RequestAPI.php

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

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