PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.5.6
Post Grid Gutenberg Blocks – PostX v2.5.6
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.6, at classes/REST_API.php

308 lines 11.6 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 's' => $post['term'],
89 'posts_per_page' => 10,
90 );
91 $post_results = new \WP_Query($args);
92 $data = [];
93 if (!empty($post_results)) {
94 while ( $post_results->have_posts() ) {
95 $post_results->the_post();
96 $id = get_the_ID();
97 $title = get_the_title();
98 $data[] = array('value'=>$id, 'title'=>($title?$title:('##'.$id)));
99 }
100 wp_reset_postdata();
101 }
102 return ['success' => true, 'data' => $data];
103 break;
104
105 case 'author':
106 $term = '%'. $wpdb->esc_like( $post['term'] ) .'%';
107 $post_results = $wpdb->get_results(
108 $wpdb->prepare(
109 "SELECT ID, display_name
110 FROM $wpdb->users
111 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
112 )
113 );
114 $data = [];
115 if (!empty($post_results)) {
116 foreach ($post_results as $key => $val) {
117 $data[] = array('value'=>$val->ID, 'title'=>$val->display_name);
118 }
119 }
120 return ['success' => true, 'data' => $data];
121 break;
122
123 case 'taxvalue':
124 $split = explode('###', $post['condition']);
125 $condition = $split[1] != 'multiTaxonomy' ? array($split[1]) : get_object_taxonomies($split[0]);
126 $args = array(
127 'taxonomy' => $condition,
128 'fields' => 'all',
129 'orderby' => 'id',
130 'order' => 'ASC',
131 'name__like'=> $post['term']
132 );
133 $post_results = get_terms( $args );
134 $data = [];
135 if (!empty($post_results)) {
136 foreach ($post_results as $key => $val) {
137 if ($split[1] == 'multiTaxonomy') {
138 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> $val->taxonomy.': '.$val->name);
139 } else {
140 $data[] = array('value'=>$val->slug, 'title'=>$val->name);
141 }
142 }
143 }
144 return ['success' => true, 'data' => $data];
145 break;
146
147 case 'taxExclude':
148 $condition = get_object_taxonomies($post['condition']);
149 $args = array(
150 'taxonomy' => $condition,
151 'fields' => 'all',
152 'orderby' => 'id',
153 'order' => 'ASC',
154 'name__like'=> $post['term']
155 );
156 $post_results = get_terms( $args );
157 $data = [];
158 if (!empty($post_results)) {
159 foreach ($post_results as $key => $val) {
160 $data[] = array('value'=>$val->taxonomy.'###'.$val->slug, 'title'=> $val->taxonomy.': '.$val->name);
161 }
162 }
163 return ['success' => true, 'data' => $data];
164 break;
165
166
167 default:
168 return ['success' => true, 'data' => [['value'=>'', 'title'=>'- Select -']]];
169 break;
170 }
171 }
172
173 /**
174 * Post Data Response of REST API
175 *
176 * @since v.1.0.0
177 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
178 * @return ARRAY | Response Image Size as Array
179 */
180 public function ultp_route_post_data($prams,$local=false) {
181 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
182 return ;
183 }
184
185 $prams = $prams->get_params();
186
187 $data = [];
188 $loop = new \WP_Query( ultimate_post()->get_query($prams) );
189
190 if($loop->have_posts()){
191 while($loop->have_posts()) {
192 $loop->the_post();
193 $var = array();
194 $post_id = get_the_ID();
195 $user_id = get_the_author_meta('ID');
196 $content_data = get_the_content();
197 $var['ID'] = $post_id;
198 $var['title'] = get_the_title();
199 $var['permalink'] = get_permalink();
200 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
201 $var['excerpt'] = strip_tags($content_data);
202 $var['excerpt_full']= strip_tags(get_the_excerpt());
203 $var['time'] = (int)get_the_date('U')*1000;
204 $var['timeModified']= (int)get_the_modified_date('U')*1000;
205 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
206 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
207 $var['comments'] = get_comments_number();
208 $var['author_link'] = get_author_posts_url($user_id);
209 $var['avatar_url'] = get_avatar_url($user_id);
210 $var['display_name']= get_the_author_meta('display_name');
211 $var['reading_time']= ceil(strlen($content_data)/1200);
212
213 // image
214 if( has_post_thumbnail() ){
215 $thumb_id = get_post_thumbnail_id($post_id);
216 $image_sizes = ultimate_post()->get_image_size();
217 $image_src = array();
218 foreach ($image_sizes as $key => $value) {
219 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
220 }
221 $var['image'] = $image_src;
222 }
223
224 // tag
225 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
226 if(!empty($tag)){
227 $v = array();
228 foreach ($tag as $val) {
229 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
230 }
231 $var['tag'] = $v;
232 }
233
234 // cat
235 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
236 if(!empty($cat)){
237 $v = array();
238 foreach ($cat as $val) {
239 $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));
240 }
241 $var['category'] = $v;
242 }
243 $data[] = $var;
244 }
245 wp_reset_postdata();
246 }
247
248 return rest_ensure_response( $data );
249 }
250
251
252 /**
253 * Taxonomy Data Response of REST API
254 *
255 * @since v.1.0.0
256 * @param ARRAY | Parameter (ARRAY)
257 * @return ARRAY | Response Taxonomy List as Array
258 */
259 public function ultp_route_common_data($prams) {
260 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
261 return rest_ensure_response([]);
262 }
263
264 $all_post_type = ultimate_post()->get_post_type();
265 $data = array();
266 foreach ($all_post_type as $post_type_slug => $post_type ) {
267 $data_term = array();
268 $taxonomies = get_object_taxonomies($post_type_slug);
269 foreach ($taxonomies as $key => $taxonomy_slug) {
270 $taxonomy_value = get_terms(array(
271 'taxonomy' => $taxonomy_slug,
272 'hide_empty' => false
273 ));
274 if (!is_wp_error($taxonomy_value)) {
275 $data_tax = array();
276 foreach ($taxonomy_value as $k => $taxonomy) {
277 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
278 }
279 if (count($data_tax) > 0) {
280 $data_term[$taxonomy_slug] = $data_tax;
281 }
282 }
283 }
284 $data[$post_type_slug] = $data_term;
285 }
286 // Global Customizer
287 $global = get_option('ultp_global', []);
288 // Image Size
289 $image_sizes = ultimate_post()->get_image_size();
290
291 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($all_post_type)]);
292 }
293
294 /**
295 * Specific Taxonomy Data Response of REST API
296 *
297 * @since v.1.0.0
298 * @param ARRAY | Parameter (ARRAY)
299 * @return ARRAY | Response Taxonomy List as Array
300 */
301 public function ultp_route_taxonomy_info_data($prams) {
302 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')) {
303 return rest_ensure_response([]);
304 }
305
306 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
307 }
308 }