PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.0.3
Post Grid Gutenberg Blocks – PostX v4.0.3
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 4.0.3, at addons/builder/RequestAPI.php

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