PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.5.9
Post Grid Gutenberg Blocks – PostX v2.5.9
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 / classes / REST_API.php

REST_API.php in Post Grid Gutenberg Blocks – PostX 2.5.9, at classes/REST_API.php

322 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Action.
4 *
5 * @package ULTP\REST_API
6 * @since v.1.0.0
7 */
8 namespace ULTP;
9
10 defined('ABSPATH') || exit;
11
12 /**
13 * Styles class.
14 */
15 class REST_API{
16
17 /**
18 * Setup class.
19 *
20 * @since v.1.0.0
21 */
22 public function __construct() {
23 add_action( 'rest_api_init', array($this, 'ultp_register_route') );
24 }
25
26
27 /**
28 * REST API Action
29 *
30 * @since v.1.0.0
31 * @return NULL
32 */
33 public function ultp_register_route() {
34 register_rest_route( 'ultp', 'posts', array(
35 'methods' => \WP_REST_Server::READABLE,
36 'args' => array(),
37 'callback' => array($this,'ultp_route_post_data'),
38 'permission_callback' => '__return_true'
39 )
40 );
41 register_rest_route( 'ultp', 'common', array(
42 'methods' => \WP_REST_Server::READABLE,
43 'args' => array('wpnonce' => []),
44 'callback' => array($this,'ultp_route_common_data'),
45 'permission_callback' => '__return_true'
46 )
47 );
48 register_rest_route( 'ultp', 'specific_taxonomy', array(
49 'methods' => \WP_REST_Server::READABLE,
50 'args' => array('taxType' => [], 'taxSlug' => [], 'taxValue' => [], 'queryNumber' => [], 'wpnonce' => []),
51 'callback' => array($this,'ultp_route_taxonomy_info_data'),
52 'permission_callback' => '__return_true'
53 )
54 );
55 register_rest_route(
56 'ultp/v1',
57 '/search/',
58 array(
59 array(
60 'methods' => 'POST',
61 'callback' => array($this, 'search_settings_action'),
62 'permission_callback' => function () {
63 return current_user_can('edit_posts');
64 },
65 'args' => array()
66 )
67 )
68 );
69 }
70
71 public function search_settings_action($server) {
72 global $wpdb;
73 $post = $server->get_params();
74
75 switch ($post['type']) {
76 case 'posts':
77 case 'allpost':
78 case 'postExclude':
79 $post_type = array('post');
80 if ($post['type'] == 'allpost') {
81 $post_type = array_keys(ultimate_post()->get_post_type());
82 } else if ($post['type'] == 'postExclude') {
83 $post_type = array($post['condition']);
84 }
85 $args = array(
86 'post_type' => $post_type,
87 'post_status' => 'publish',
88 'posts_per_page' => 10,
89 );
90 if (is_numeric($post['term'])) {
91 $args['p'] = $post['term'];
92 } else {
93 $args['s'] = $post['term'];
94 }
95
96 $post_results = new \WP_Query($args);
97 $data = [];
98 if (!empty($post_results)) {
99 while ( $post_results->have_posts() ) {
100 $post_results->the_post();
101 $id = get_the_ID();
102 $title = html_entity_decode(get_the_title());
103 $data[] = array('value'=>$id, 'title'=>($title?'[ID: '.$id.'] '.$title:('[ID: '.$id.']')));
104 }
105 wp_reset_postdata();
106 }
107 return ['success' => true, 'data' => $data];
108 break;
109
110 case 'author':
111 $term = '%'. $wpdb->esc_like( $post['term'] ) .'%';
112 $post_results = $wpdb->get_results(
113 $wpdb->prepare(
114 "SELECT ID, display_name
115 FROM $wpdb->users
116 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
117 )
118 );
119 $data = [];
120 if (!empty($post_results)) {
121 foreach ($post_results as $key => $val) {
122 $data[] = array('value'=>$val->ID, 'title'=>'[ID: '.$val->ID.'] '.$val->display_name);
123 }
124 }
125 return ['success' => true, 'data' => $data];
126 break;
127
128 case 'taxvalue':
129 $split = explode('###', $post['condition']);
130 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
131 $args = array(
132 'taxonomy' => $condition,
133 'fields' => 'all',
134 'orderby' => 'id',
135 'order' => 'ASC',
136 'name__like'=> $post['term']
137 );
138 if (is_numeric($post['term'])) {
139 unset($args['name__like']);
140 $args['include'] = array($post['term']);
141 }
142
143 $post_results = get_terms( $args );
144 $data = [];
145 if (!empty($post_results)) {
146 foreach ($post_results as $key => $val) {
147 if ($split[1] == 'multiTaxonomy') {
148 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
149 } else {
150 $data[] = array('value'=>$val->slug, 'title'=>'[ID: '.$val->term_id.'] '.$val->name);
151 }
152 }
153 }
154 return ['success' => true, 'data' => $data];
155 break;
156
157 case 'taxExclude':
158 $condition = get_object_taxonomies($post['condition']);
159 $args = array(
160 'taxonomy' => $condition,
161 'fields' => 'all',
162 'orderby' => 'id',
163 'order' => 'ASC',
164 'name__like'=> $post['term']
165 );
166 if (is_numeric($post['term'])) {
167 unset($args['name__like']);
168 $args['include'] = array($post['term']);
169 }
170 $post_results = get_terms( $args );
171 $data = [];
172 if (!empty($post_results)) {
173 foreach ($post_results as $key => $val) {
174 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> '[ID: '.$val->term_id.'] '.$val->taxonomy.': '.$val->name);
175 }
176 }
177 return ['success' => true, 'data' => $data];
178 break;
179
180
181 default:
182 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
183 break;
184 }
185 }
186
187 /**
188 * Post Data Response of REST API
189 *
190 * @since v.1.0.0
191 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
192 * @return ARRAY | Response Image Size as Array
193 */
194 public function ultp_route_post_data($prams,$local=false) {
195 if (!wp_verify_nonce(wp_unslash($_REQUEST['wpnonce']), 'ultp-nonce') && $local){
196 return ;
197 }
198
199 $prams = $prams->get_params();
200
201 $data = [];
202 $loop = new \WP_Query( ultimate_post()->get_query($prams) );
203
204 if($loop->have_posts()){
205 while($loop->have_posts()) {
206 $loop->the_post();
207 $var = array();
208 $post_id = get_the_ID();
209 $user_id = get_the_author_meta('ID');
210 $content_data = get_the_content();
211 $var['ID'] = $post_id;
212 $var['title'] = get_the_title();
213 $var['permalink'] = get_permalink();
214 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
215 $var['excerpt'] = strip_tags($content_data);
216 $var['excerpt_full']= strip_tags(get_the_excerpt());
217 $var['time'] = (int)get_the_date('U')*1000;
218 $var['timeModified']= (int)get_the_modified_date('U')*1000;
219 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
220 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
221 $var['comments'] = get_comments_number();
222 $var['author_link'] = get_author_posts_url($user_id);
223 $var['avatar_url'] = get_avatar_url($user_id);
224 $var['display_name']= get_the_author_meta('display_name');
225 $var['reading_time']= ceil(strlen($content_data)/1200);
226
227 // image
228 if( has_post_thumbnail() ){
229 $thumb_id = get_post_thumbnail_id($post_id);
230 $image_sizes = ultimate_post()->get_image_size();
231 $image_src = array();
232 foreach ($image_sizes as $key => $value) {
233 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
234 }
235 $var['image'] = $image_src;
236 }
237
238 // tag
239 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
240 if(!empty($tag)){
241 $v = array();
242 foreach ($tag as $val) {
243 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
244 }
245 $var['tag'] = $v;
246 }
247
248 // cat
249 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
250 if(!empty($cat)){
251 $v = array();
252 foreach ($cat as $val) {
253 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id), 'color' => get_term_meta($val->term_id, 'ultp_category_color', true));
254 }
255 $var['category'] = $v;
256 }
257 $data[] = $var;
258 }
259 wp_reset_postdata();
260 }
261
262 return rest_ensure_response( $data );
263 }
264
265
266 /**
267 * Taxonomy Data Response of REST API
268 *
269 * @since v.1.0.0
270 * @param ARRAY | Parameter (ARRAY)
271 * @return ARRAY | Response Taxonomy List as Array
272 */
273 public function ultp_route_common_data($prams) {
274 if (!wp_verify_nonce(wp_unslash($_REQUEST['wpnonce']), 'ultp-nonce')){
275 return rest_ensure_response([]);
276 }
277
278 $all_post_type = ultimate_post()->get_post_type();
279 $data = array();
280 foreach ($all_post_type as $post_type_slug => $post_type ) {
281 $data_term = array();
282 $taxonomies = get_object_taxonomies($post_type_slug);
283 foreach ($taxonomies as $key => $taxonomy_slug) {
284 $taxonomy_value = get_terms(array(
285 'taxonomy' => $taxonomy_slug,
286 'hide_empty' => false
287 ));
288 if (!is_wp_error($taxonomy_value)) {
289 $data_tax = array();
290 foreach ($taxonomy_value as $k => $taxonomy) {
291 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
292 }
293 if (count($data_tax) > 0) {
294 $data_term[$taxonomy_slug] = $data_tax;
295 }
296 }
297 }
298 $data[$post_type_slug] = $data_term;
299 }
300 // Global Customizer
301 $global = get_option('ultp_global', []);
302 // Image Size
303 $image_sizes = ultimate_post()->get_image_size();
304
305 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
306 }
307
308 /**
309 * Specific Taxonomy Data Response of REST API
310 *
311 * @since v.1.0.0
312 * @param ARRAY | Parameter (ARRAY)
313 * @return ARRAY | Response Taxonomy List as Array
314 */
315 public function ultp_route_taxonomy_info_data($prams) {
316 if (!wp_verify_nonce(wp_unslash($_REQUEST['wpnonce']), 'ultp-nonce')) {
317 return rest_ensure_response([]);
318 }
319
320 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
321 }
322 }