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

247 lines 9.4 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', '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 }
56
57
58 /**
59 * Post Data Response of REST API
60 *
61 * @since v.1.0.0
62 * @param MIXED | Pram (ARRAY), Local (BOOLEAN)
63 * @return ARRAY | Response Image Size as Array
64 */
65 public function ultp_route_post_data($prams,$local=false) {
66 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
67 return ;
68 }
69
70 // Query Builder
71 $args = array(
72 'post_type' => 'post',
73 'orderby' => 'date',
74 'post_status' => 'publish',
75 'order' => 'DESC',
76 );
77
78 if(isset($prams['count'])){ $args['posts_per_page'] = esc_attr($prams['count']); }
79 if(isset($prams['post_type'])){ $args['post_type'] = esc_attr( $prams['post_type'] ); }
80
81 if(isset($prams['taxonomy'])){ // taxonomy slug__val__slug__val
82 $relation = isset($prams['queryRelation']) ? $prams['queryRelation'] : 'OR';
83 $tax_arr = array('relation' => $relation);
84 $tax = explode('__', esc_attr($prams['taxonomy']));
85 if(!empty($tax)){
86 for($i=0; $i < count($tax) ; $i++){
87 if($i%2 == 0){
88 $tax_arr[] = array('taxonomy'=>$tax[$i], 'field' => 'slug', 'terms' => array($tax[$i+1]));
89 }
90 }
91 }
92 if(!empty($tax_arr)){
93 $args['tax_query'] = $tax_arr;
94 }
95 }
96
97 if(isset($prams['exclude'])){ $args['post__not_in'] = explode('__', esc_attr($prams['exclude'])); }
98 if(isset($prams['orderby'])){ $args['orderby'] = esc_attr($prams['orderby']); }
99 if(isset($prams['order'])){ $args['order'] = esc_attr($prams['order']); }
100 if(isset($prams['offset'])){ $args['offset'] = esc_attr($prams['offset']); }
101 if(isset($prams['paged'])){ $args['paged'] = esc_attr($prams['paged']); }
102
103 if(isset($prams['orderby']) && isset($prams['meta_key'])){
104 if($prams['orderby'] == 'meta_value_num') {
105 $args['meta_key'] = $prams['meta_key'];
106 $args['meta_type'] = 'NUMERIC';
107 }
108 }
109
110 if(isset($prams['queryQuick'])){
111 if($prams['queryQuick'] != ''){
112 $args = ultimate_post()->get_quick_query($prams, $args);
113 }
114 }
115
116 if(isset($prams['include'])){
117 $args['post__in'] = explode('__', esc_attr($prams['include']));
118 $args['ignore_sticky_posts'] = 1;
119 $args['orderby'] = 'post__in';
120 }
121
122 if(isset($prams['queryAuthor'])){
123 $args['author'] = esc_attr($prams['queryAuthor']);
124 }
125
126 $data = [];
127 $loop = new \WP_Query($args);
128
129 if($loop->have_posts()){
130 while($loop->have_posts()) {
131 $loop->the_post();
132 $var = array();
133 $post_id = get_the_ID();
134 $user_id = get_the_author_meta('ID');
135 $content_data = get_the_content();
136 $var['ID'] = $post_id;
137 $var['title'] = get_the_title();
138 $var['permalink'] = get_permalink();
139 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
140 $var['excerpt'] = strip_tags($content_data);
141 $var['excerpt_full']= strip_tags(get_the_excerpt());
142 $var['time'] = (int)get_the_date('U')*1000;
143 $var['timeModified']= (int)get_the_modified_date('U')*1000;
144 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
145 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
146 $var['comments'] = get_comments_number();
147 $var['author_link'] = get_author_posts_url($user_id);
148 $var['avatar_url'] = get_avatar_url($user_id);
149 $var['display_name']= get_the_author_meta('display_name');
150 $var['reading_time']= ceil(strlen($content_data)/1200);
151
152 // image
153 if( has_post_thumbnail() ){
154 $thumb_id = get_post_thumbnail_id($post_id);
155 $image_sizes = ultimate_post()->get_image_size();
156 $image_src = array();
157 foreach ($image_sizes as $key => $value) {
158 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
159 }
160 $var['image'] = $image_src;
161 }
162
163 // tag
164 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
165 if(!empty($tag)){
166 $v = array();
167 foreach ($tag as $val) {
168 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
169 }
170 $var['tag'] = $v;
171 }
172
173 // cat
174 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
175 if(!empty($cat)){
176 $v = array();
177 foreach ($cat as $val) {
178 $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));
179 }
180 $var['category'] = $v;
181 }
182 $data[] = $var;
183 }
184 wp_reset_postdata();
185 }
186
187 return rest_ensure_response( $data );
188 }
189
190
191 /**
192 * Taxonomy Data Response of REST API
193 *
194 * @since v.1.0.0
195 * @param ARRAY | Parameter (ARRAY)
196 * @return ARRAY | Response Taxonomy List as Array
197 */
198 public function ultp_route_common_data($prams) {
199 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
200 return rest_ensure_response([]);
201 }
202
203 $all_post_type = ultimate_post()->get_post_type();
204 $data = array();
205 foreach ($all_post_type as $post_type_slug => $post_type ) {
206 $data_term = array();
207 $taxonomies = get_object_taxonomies($post_type_slug);
208 foreach ($taxonomies as $key => $taxonomy_slug) {
209 $taxonomy_value = get_terms(array(
210 'taxonomy' => $taxonomy_slug,
211 'hide_empty' => false
212 ));
213 if (!is_wp_error($taxonomy_value)) {
214 $data_tax = array();
215 foreach ($taxonomy_value as $k => $taxonomy) {
216 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
217 }
218 $data_term[$taxonomy_slug] = $data_tax;
219 }
220 }
221 $data[$post_type_slug] = $data_term;
222 }
223 // Global Customizer
224 $global = get_option('ultp_global', []);
225 // Image Size
226 $image_sizes = ultimate_post()->get_image_size();
227 // Post Types
228 $post_types = ultimate_post()->get_post_type();
229
230 return rest_ensure_response(['taxonomy' => $data, 'global' => $global, 'image' => json_encode($image_sizes), 'posttype' => json_encode($post_types)]);
231 }
232
233 /**
234 * Specific Taxonomy Data Response of REST API
235 *
236 * @since v.1.0.0
237 * @param ARRAY | Parameter (ARRAY)
238 * @return ARRAY | Response Taxonomy List as Array
239 */
240 public function ultp_route_taxonomy_info_data($prams) {
241 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')) {
242 return rest_ensure_response([]);
243 }
244
245 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
246 }
247 }