PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.6.4
Post Grid Gutenberg Blocks – PostX v2.6.4
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 +288 -177 2.1.12.6.4 View file →
@@ -1,211 +1,322 @@
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 + }
45 70
46 -function ultp_route_posttype_data() {
47 - if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
48 - return ;
49 - }
50 -
51 - return ultimate_post()->get_post_type();
52 -}
71 + public function search_settings_action($server) {
72 + global $wpdb;
73 + $post = $server->get_params();
53 74
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 -}
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 + }
61 95
62 -function ultp_route_post_data($prams,$local=false) {
63 - if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
64 - return ;
65 - }
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;
66 109
67 - // Query Builder
68 - $args = array(
69 - 'post_type' => 'post',
70 - 'orderby' => 'date',
71 - 'post_status' => 'publish',
72 - 'order' => 'DESC',
73 - );
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;
74 127
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'] ); }
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 + }
77 142
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]));
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 + }
85 153 }
86 - }
87 - }
88 - if(!empty($tax_arr)){
89 - $args['tax_query'] = $tax_arr;
90 - }
91 - }
154 + return ['success' => true, 'data' => $data];
155 + break;
92 156
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']); }
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 +
98 180
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';
181 + default:
182 + return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
183 + break;
103 184 }
104 - }
185 + }
105 186
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 - }
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 ;
111 197 }
112 - }
113 198
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];
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;
152 236 }
153 - $var['image'] = $image_src;
154 - }
155 237
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));
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;
162 246 }
163 - $var['tag'] = $v;
164 - }
165 247
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));
248 + // Taxonomy
249 + $cat = get_the_terms($post_id, (isset($prams['taxonomy'])?esc_attr($prams['taxonomy']):'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;
172 256 }
173 - $var['category'] = $v;
257 + $data[] = $var;
174 258 }
175 - $data[] = $var;
259 + wp_reset_postdata();
176 260 }
177 - wp_reset_postdata();
261 +
262 + return rest_ensure_response( $data );
178 263 }
179 264
180 - return rest_ensure_response( $data );
181 -}
182 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();
183 304
184 -
185 -function ultp_route_taxonomy_data($prams) {
186 - if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
187 - return rest_ensure_response([]);
305 + return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
188 306 }
189 - return rest_ensure_response(ultimate_post()->taxonomy($prams['taxonomy']));
190 -}
191 307
192 -function ultp_route_taxonomy_info_data($prams) {
193 - if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
194 - return rest_ensure_response([]);
195 - }
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 + }
196 319
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 - }
320 + return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
208 321 }
209 -
210 - return rest_ensure_response($data);
211 322 }