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

294 lines 10.8 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('post_type' => [], 'taxonomy' => [], 'include' => [], 'exclude' => [], 'order' => [], 'orderby' => [], 'count' => [], 'size' => [], 'tag' => [], 'cat' => [], 'meta_key' => [], 'queryQuick' => [], 'wpnonce' => []),
37 'callback' => array($this,'ultp_route_post_data'),
38 'permission_callback' => '__return_true'
39 )
40 );
41 register_rest_route( 'ultp', 'taxonomy', array(
42 'methods' => \WP_REST_Server::READABLE,
43 'args' => array('wpnonce' => []),
44 'callback' => array($this,'ultp_route_taxonomy_data'),
45 'permission_callback' => '__return_true'
46 )
47 );
48 register_rest_route( 'ultp', 'imagesize', array(
49 'methods' => \WP_REST_Server::READABLE,
50 'args' => array('taxonomy' => [], 'wpnonce' => []),
51 'callback' => array($this,'ultp_route_imagesize_data'),
52 'permission_callback' => '__return_true'
53 )
54 );
55 register_rest_route( 'ultp', 'posttype', array(
56 'methods' => \WP_REST_Server::READABLE,
57 'args' => array('wpnonce' => []),
58 'callback' => array($this,'ultp_route_posttype_data'),
59 'permission_callback' => '__return_true'
60 )
61 );
62 register_rest_route( 'ultp', 'specific_taxonomy', array(
63 'methods' => \WP_REST_Server::READABLE,
64 'args' => array('taxType' => [], 'taxSlug' => [], 'taxValue' => [], 'queryNumber' => [], 'wpnonce' => []),
65 'callback' => array($this,'ultp_route_taxonomy_info_data'),
66 'permission_callback' => '__return_true'
67 )
68 );
69 }
70
71 /**
72 * REST API Action
73 *
74 * @since v.1.0.0
75 * @param ARRAY | Parameters of the REST API
76 * @return ARRAY | Response as Array
77 */
78 public function ultp_route_posttype_data() {
79 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
80 return ;
81 }
82 return ultimate_post()->get_post_type();
83 }
84
85 /**
86 * Image Size Data
87 *
88 * @since v.1.0.0
89 * @param NULL
90 * @return ARRAY | Response Image Size as Array
91 */
92 public function ultp_route_imagesize_data() {
93 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
94 return ;
95 }
96 return ultimate_post()->get_image_size();
97 }
98
99 /**
100 * Post Data Response of REST API
101 *
102 * @since v.1.0.0
103 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
104 * @return ARRAY | Response Image Size as Array
105 */
106 public function ultp_route_post_data($prams,$local=false) {
107 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
108 return ;
109 }
110
111 // Query Builder
112 $args = array(
113 'post_type' => 'post',
114 'orderby' => 'date',
115 'post_status' => 'publish',
116 'order' => 'DESC',
117 );
118
119 if(isset($prams['count'])){ $args['posts_per_page'] = esc_attr($prams['count']); }
120 if(isset($prams['post_type'])){ $args['post_type'] = esc_attr( $prams['post_type'] ); }
121
122 if(isset($prams['taxonomy'])){ // taxonomy slug__val__slug__val
123 $relation = isset($prams['queryRelation']) ? $prams['queryRelation'] : 'OR';
124 $tax_arr = array('relation' => $relation);
125 $tax = explode('__', esc_attr($prams['taxonomy']));
126 if(!empty($tax)){
127 for($i=0; $i < count($tax) ; $i++){
128 if($i%2 == 0){
129 $tax_arr[] = array('taxonomy'=>$tax[$i], 'field' => 'slug', 'terms' => array($tax[$i+1]));
130 }
131 }
132 }
133 if(!empty($tax_arr)){
134 $args['tax_query'] = $tax_arr;
135 }
136 }
137
138 if(isset($prams['exclude'])){ $args['post__not_in'] = explode('__', esc_attr($prams['exclude'])); }
139 if(isset($prams['orderby'])){ $args['orderby'] = esc_attr($prams['orderby']); }
140 if(isset($prams['order'])){ $args['order'] = esc_attr($prams['order']); }
141 if(isset($prams['offset'])){ $args['offset'] = esc_attr($prams['offset']); }
142 if(isset($prams['paged'])){ $args['paged'] = esc_attr($prams['paged']); }
143
144 if(isset($prams['orderby']) && isset($prams['meta_key'])){
145 if($prams['orderby'] == 'meta_value_num') {
146 $args['meta_key'] = $prams['meta_key'];
147 $args['meta_type'] = 'NUMERIC';
148 }
149 }
150
151 if(isset($prams['queryQuick'])){
152 if($prams['queryQuick'] != ''){
153 $args = ultimate_post()->get_quick_query($prams, $args);
154 }
155 }
156
157 if(isset($prams['include'])){
158 $args['post__in'] = explode('__', esc_attr($prams['include']));
159 $args['ignore_sticky_posts'] = 1;
160 $args['orderby'] = 'post__in';
161 }
162
163 if(isset($prams['queryAuthor'])){
164 $args['author'] = esc_attr($prams['queryAuthor']);
165 }
166
167 $data = [];
168 $loop = new \WP_Query($args);
169
170 if($loop->have_posts()){
171 while($loop->have_posts()) {
172 $loop->the_post();
173 $var = array();
174 $post_id = get_the_ID();
175 $user_id = get_the_author_meta('ID');
176 $content_data = get_the_content();
177 $var['ID'] = $post_id;
178 $var['title'] = get_the_title();
179 $var['permalink'] = get_permalink();
180 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
181 $var['excerpt'] = strip_tags($content_data);
182 $var['excerpt_full']= strip_tags(get_the_excerpt());
183 $var['time'] = (int)get_the_date('U')*1000;
184 $var['timeModified']= (int)get_the_modified_date('U')*1000;
185 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
186 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
187 $var['comments'] = get_comments_number();
188 $var['author_link'] = get_author_posts_url($user_id);
189 $var['avatar_url'] = get_avatar_url($user_id);
190 $var['display_name']= get_the_author_meta('display_name');
191 $var['reading_time']= ceil(strlen($content_data)/1200);
192
193 // image
194 if( has_post_thumbnail() ){
195 $thumb_id = get_post_thumbnail_id($post_id);
196 $image_sizes = ultimate_post()->get_image_size();
197 $image_src = array();
198 foreach ($image_sizes as $key => $value) {
199 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
200 }
201 $var['image'] = $image_src;
202 }
203
204 // tag
205 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
206 if(!empty($tag)){
207 $v = array();
208 foreach ($tag as $val) {
209 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
210 }
211 $var['tag'] = $v;
212 }
213
214 // cat
215 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
216 if(!empty($cat)){
217 $v = array();
218 foreach ($cat as $val) {
219 $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));
220 }
221 $var['category'] = $v;
222 }
223 $data[] = $var;
224 }
225 wp_reset_postdata();
226 }
227
228 return rest_ensure_response( $data );
229 }
230
231
232 /**
233 * Taxonomy Data Response of REST API
234 *
235 * @since v.1.0.0
236 * @param ARRAY | Parameter (ARRAY)
237 * @return ARRAY | Response Taxonomy List as Array
238 */
239 public function get_taxonomy_hierarchy( $taxonomy, $parent = 0, $depth = 0 ) {
240 $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
241 $terms = get_terms( array(
242 'taxonomy' => $taxonomy,
243 'hide_empty'=>false,
244 'parent' => $parent
245 )
246 );
247
248 $children = array();
249 foreach ( $terms as $term ){
250 if ($parent) {
251 $depth++;
252 }
253 $svg = $this->get_taxonomy_hierarchy( $taxonomy, $term->term_id, $depth );
254 $term->children = $svg;
255 $children[ urldecode_deep($term->slug) ] = str_repeat(' - ',$depth).$term->name;
256 if (!empty($svg)) {
257 $children = array_merge($children, $svg);
258 }
259 }
260 return $children;
261 }
262 public function ultp_route_taxonomy_data($prams) {
263 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
264 return rest_ensure_response([]);
265 }
266
267 $all_post_type = ultimate_post()->get_post_type();
268 $data = array();
269 foreach ($all_post_type as $post_type_slug => $post_type ) {
270 $data_term = array();
271 $taxonomies = get_object_taxonomies($post_type_slug);
272 foreach ($taxonomies as $key => $taxonomy_slug) {
273 $data_term[$taxonomy_slug] = $this->get_taxonomy_hierarchy($taxonomy_slug);
274 }
275 $data[$post_type_slug] = $data_term;
276 }
277 return rest_ensure_response($data);
278 }
279
280 /**
281 * Specific Taxonomy Data Response of REST API
282 *
283 * @since v.1.0.0
284 * @param ARRAY | Parameter (ARRAY)
285 * @return ARRAY | Response Taxonomy List as Array
286 */
287 public function ultp_route_taxonomy_info_data($prams) {
288 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')) {
289 return rest_ensure_response([]);
290 }
291
292 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
293 }
294 }