PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.5.4
Post Grid Gutenberg Blocks – PostX v2.5.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
ultimate-post / classes / REST_API.php

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

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