PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.7.8
Post Grid Gutenberg Blocks – PostX v2.7.8
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 2.7.8, at addons/builder/RequestAPI.php

378 lines 12.0 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
17 private $api_endpoint = 'https://ultp.wpxpo.com/wp-json/restapi/v2/';
18
19 public function __construct() {
20 add_action('rest_api_init', array($this, 'get_template_data'));
21 }
22
23 /**
24 * Create Builder Post Type
25 *
26 * @since v.2.7.0
27 * @return NULL
28 */
29 public function get_template_data() {
30 register_rest_route(
31 'ultp/v2',
32 '/get_single_premade/',
33 array(
34 array(
35 'methods' => 'POST',
36 'callback' => array( $this, 'get_single_premade_callback'),
37 'permission_callback' => function () {
38 return current_user_can( 'edit_posts' );
39 },
40 'args' => array()
41 )
42 )
43 );
44 register_rest_route(
45 'ultp/v2',
46 '/condition/',
47 array(
48 array(
49 'methods' => 'POST',
50 'callback' => array($this, 'condition_settings_action'),
51 'permission_callback' => function () {
52 return current_user_can('edit_posts');
53 },
54 'args' => array()
55 )
56 )
57 );
58 register_rest_route(
59 'ultp/v2',
60 '/condition_save/',
61 array(
62 array(
63 'methods' => 'POST',
64 'callback' => array($this, 'condition_save_action'),
65 'permission_callback' => function () {
66 return current_user_can('edit_posts');
67 },
68 'args' => array()
69 )
70 )
71 );
72 register_rest_route(
73 'ultp/v2',
74 '/data_builder/',
75 array(
76 array(
77 'methods' => 'POST',
78 'callback' => array($this, 'data_builder_action'),
79 'permission_callback' => function () {
80 return current_user_can('edit_posts');
81 },
82 'args' => array()
83 )
84 )
85 );
86 register_rest_route(
87 'ultp/v2',
88 '/template_action/',
89 array(
90 array(
91 'methods' => 'POST',
92 'callback' => array($this, 'template_page_action'),
93 'permission_callback' => function () {
94 return current_user_can('edit_posts');
95 },
96 'args' => array()
97 )
98 )
99 );
100
101 }
102
103 /**
104 * Delete Template Page
105 *
106 * @since v.2.6.6
107 * @param STRING
108 * @return ARRAY | Success Message
109 */
110 public function template_page_action($server) {
111 $post = $server->get_params();
112 if (isset($post['type']) && isset($post['id']) && $post['id']) {
113 if ($post['type'] == 'delete') {
114 wp_delete_post( $post['id'], true);
115 } else if ($post['type'] == 'status') {
116 if ($post['status']) {
117 wp_update_post(array(
118 'ID' => $post['id'],
119 'post_status' => $post['status']
120 ));
121 }
122 }
123 }
124 return array( 'success' => true);
125 }
126
127
128 /**
129 * Builder Post Type Data
130 *
131 * @since v.2.7.0
132 * @param ARRAY
133 * @return ARRAY | Information of Builder Post
134 */
135 public function data_builder_action($server) {
136 $post = $server->get_params();
137 $args = array(
138 'post_type' => 'ultp_builder',
139 'post_status' => array('publish', 'draft'),
140 'orderby' => 'title',
141 'order' => 'ASC',
142 'posts_per_page' => -1,
143 );
144 $post_results = new \WP_Query($args);
145 $post_list = [];
146 if (!empty($post_results)) {
147 while ( $post_results->have_posts() ) {
148 $post_results->the_post();
149 $id = get_the_ID();
150 $meta_type = get_post_meta( $id, '__ultp_builder_type', true );
151 $post_list[] = array(
152 'id' => $id,
153 'title' => get_the_title(),
154 'author' => get_the_author_meta('display_name'),
155 'date' => get_the_date( get_option('date_format')),
156 'edit' => get_edit_post_link($id),
157 'type' => $meta_type ? $meta_type : 'archive',
158 'status' => get_post_status(),
159 );
160 }
161 wp_reset_postdata();
162 }
163
164 $arg = [
165 'success' => true,
166 'postlist' => $post_list,
167 'settings' => get_option('ultp_builder_conditions', array()),
168 'defaults' => ultimate_post()->builder_data(),
169 // 'new_url' => add_query_arg(array('post_type'=>'ultp_builder'),admin_url('post-new.php'))
170 ];
171 if (isset($post['pid']) && $post['pid']) {
172 $post_meta = get_post_meta( $post['pid'], '__ultp_builder_type', true );
173 $arg['type'] = $post_meta ? $post_meta : 'archive';
174 }
175 return $arg;
176 }
177
178
179 /**
180 * Save Conditions Data
181 *
182 * @since v.2.7.0
183 * @param ARRAY
184 * @return ARRAY | Message of the Condition Success
185 */
186 public function condition_save_action($server) {
187 $post = $server->get_params();
188 if (isset($post['settings'])) {
189 update_option('ultp_builder_conditions', $post['settings']);
190 return [
191 'success' => true,
192 'data' => 'Settings Saved!!!'
193 ];
194 }
195 }
196
197
198 /**
199 * Condition Settings Actions
200 *
201 * @since v.2.7.0
202 * @param ARRAY
203 * @return ARRAY | Data of the Condition
204 */
205 public function condition_settings_action($server) {
206 global $wpdb;
207 $post = $server->get_params();
208 $search_type = explode('###', $post['type']);
209
210 switch ($search_type[0]) {
211 case 'type':
212 $args = array(
213 'post_type' => $search_type[1],
214 'post_status' => 'publish',
215 'orderby' => 'title',
216 'order' => 'ASC',
217 's' => $post['term'],
218 'posts_per_page' => 10,
219 );
220 if ($post['title_return']){
221 unset($args['s']);
222 $args['p'] = $post['term'];
223 }
224 $post_results = new \WP_Query($args);
225 $title = '';
226 $data = [];
227 if (!empty($post_results)) {
228 while ( $post_results->have_posts() ) {
229 $post_results->the_post();
230 $id = get_the_ID();
231 $title = get_the_title();
232 $data[] = array('value'=>$id, 'title'=>($title?$title:('##'.$id)));
233 }
234 wp_reset_postdata();
235 }
236 return ['success' => true, 'data' => ($post['title_return'] ? $title : $data )];
237 break;
238
239 case 'term':
240 $args = array(
241 'taxonomy' => $search_type[1],
242 'fields' => 'all',
243 'orderby' => 'id',
244 'order' => 'ASC',
245 'name__like'=> $post['term']
246 );
247 if ($post['title_return']) {
248 $args['term_taxonomy_id'] = array($post['term']);
249 unset($args['name__like']);
250 }
251 $post_results = get_terms( $args );
252 $title = '';
253 $data = [];
254 if (!empty($post_results)) {
255 foreach ($post_results as $key => $val) {
256 $title = $val->name;
257 $data[] = array('value'=>$val->term_id, 'title'=>$title);
258 }
259 }
260 return ['success' => true, 'data' => ($post['title_return'] ? $title : $data )];
261 break;
262
263 case 'author':
264 $term = $post['title_return'] ? $wpdb->esc_like( $post['term'] ) : '%'. $wpdb->esc_like( $post['term'] ) .'%';
265 $post_results = $wpdb->get_results(
266 $wpdb->prepare(
267 "SELECT ID, display_name
268 FROM $wpdb->users
269 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
270 )
271 );
272 $title = '';
273 $data = [];
274 if (!empty($post_results)) {
275 foreach ($post_results as $key => $val) {
276 $title = $val->display_name;
277 $data[] = array('value'=>$val->ID, 'title'=>$val->display_name);
278 }
279 }
280 return ['success' => true, 'data' => ($post['title_return'] ? $title : $data )];
281 break;
282 default:
283 return ['success' => false];
284 break;
285 }
286 return ['success' => true, 'data' => 'This is Testing !!!'];
287 }
288
289
290 /**
291 * Single Premade Data and Create Builder Posts
292 *
293 * @since v.2.7.0
294 * @param ARRAY
295 * @return ARRAY | Data of the Premade
296 */
297 public function get_single_premade_callback($server) {
298
299 $is_active = ultimate_post()->is_lc_active();
300 $obj_count = wp_count_posts('ultp_builder');
301 if (!$is_active) {
302 $p_count = isset($obj_count->publish) ? $obj_count->publish : 0;
303 $d_count = isset($obj_count->draft) ? $obj_count->draft : 0;
304 if (($p_count + $d_count) > 0) {
305 return array( 'success' => false );
306 }
307 }
308
309 $post = $server->get_params();
310 $id = isset($post['ID']) ? sanitize_text_field($post['ID']) : '';
311
312 if ($id) {
313 $response = wp_remote_post( $this->api_endpoint.'single-premade', array(
314 'method' => 'POST',
315 'timeout' => 120,
316 'body' => array( 'section_id' => $id, 'license' => get_option('edd_ultp_license_key') )
317 ));
318 if (is_wp_error( $response ) ) {
319 return array( 'success' => false, 'data' => "Something went wrong:" . $response->get_error_message() );
320 } else {
321 if (isset($response['body']) && isset($post['type'])) {
322 $body = json_decode($response['body']);
323 if (isset($body->rawData) && isset($body->success) && $body->success) {
324 $post_id = $this->set_post_content($post['type'], wp_slash($body->rawData));
325 return array( 'success' => true, 'link' => get_edit_post_link($post_id) );
326 } else {
327 return array( 'success' => false );
328 }
329 }
330 }
331 } else {
332 $post_id = $this->set_post_content($post['type'], '');
333 return array( 'success' => true, 'link' => get_edit_post_link($post_id) );
334 }
335
336 }
337
338 /**
339 * Single Premade Data and Insert Builder Posts
340 *
341 * @since v.2.7.0
342 * @param ARRAY
343 * @return INT | Post ID
344 */
345 public function set_post_content($type, $body = '') {
346 $post_id = wp_insert_post(
347 array(
348 'post_title' => ucfirst($type) . ' Template',
349 'post_content' => $body,
350 'post_type' => 'ultp_builder',
351 'post_status' => 'draft'
352 )
353 );
354 $settings = get_option('ultp_builder_conditions', array());
355 switch ($type) {
356 case 'singular':
357 update_post_meta($post_id, '__ultp_builder_type', 'singular');
358 $settings['singular'][$post_id] = ['include/singular/post'];
359 update_option('ultp_builder_conditions', $settings);
360 break;
361 case 'author':
362 case 'post_tag':
363 case 'date':
364 case 'search':
365 case 'archive':
366 case 'category':
367 update_post_meta($post_id, '__ultp_builder_type', 'archive');
368 $extra = $type != 'archive' ? '/'.$type : '';
369 $settings['archive'][$post_id] = ['include/archive'.$extra];
370 update_option('ultp_builder_conditions', $settings);
371 break;
372 default:
373 break;
374 }
375 return $post_id;
376 }
377
378 }