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