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

280 lines 10.3 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 $tax_arr = array('relation' => 'OR');
124 $tax = explode('__', esc_attr($prams['taxonomy']));
125 if(!empty($tax)){
126 for($i=0; $i < count($tax) ; $i++){
127 if($i%2 == 0){
128 $tax_arr[] = array('taxonomy'=>$tax[$i], 'field' => 'slug', 'terms' => array($tax[$i+1]));
129 }
130 }
131 }
132 if(!empty($tax_arr)){
133 $args['tax_query'] = $tax_arr;
134 }
135 }
136
137 if(isset($prams['exclude'])){ $args['post__not_in'] = explode('__', esc_attr($prams['exclude'])); }
138 if(isset($prams['orderby'])){ $args['orderby'] = esc_attr($prams['orderby']); }
139 if(isset($prams['order'])){ $args['order'] = esc_attr($prams['order']); }
140 if(isset($prams['offset'])){ $args['offset'] = esc_attr($prams['offset']); }
141 if(isset($prams['paged'])){ $args['paged'] = esc_attr($prams['paged']); }
142
143 if(isset($prams['orderby']) && isset($prams['meta_key'])){
144 if($prams['orderby'] == 'meta_value_num') {
145 $args['meta_key'] = $prams['meta_key'];
146 $args['meta_type'] = 'NUMERIC';
147 }
148 }
149
150 if(isset($prams['queryQuick'])){
151 if($prams['queryQuick'] != ''){
152 $args = ultimate_post()->get_quick_query($prams, $args);
153 }
154 }
155
156 if(isset($prams['include'])){
157 $args['post__in'] = explode('__', esc_attr($prams['include']));
158 $args['ignore_sticky_posts'] = 1;
159 $args['orderby'] = 'post__in';
160 }
161
162 if(isset($prams['queryAuthor'])){
163 $args['author'] = esc_attr($prams['queryAuthor']);
164 }
165
166 $data = [];
167 $loop = new \WP_Query($args);
168
169 if($loop->have_posts()){
170 while($loop->have_posts()) {
171 $loop->the_post();
172 $var = array();
173 $post_id = get_the_ID();
174 $user_id = get_the_author_meta('ID');
175 $content_data = get_the_content();
176 $var['ID'] = $post_id;
177 $var['title'] = get_the_title();
178 $var['permalink'] = get_permalink();
179 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
180 $var['excerpt'] = strip_tags($content_data);
181 $var['excerpt_full']= strip_tags(get_the_excerpt());
182 $var['time'] = get_the_date();
183 $var['timeModified']= get_the_modified_date();
184 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
185 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
186 $var['comments'] = get_comments_number();
187 $var['author_link'] = get_author_posts_url($user_id);
188 $var['avatar_url'] = get_avatar_url($user_id);
189 $var['display_name']= get_the_author_meta('display_name');
190 $var['reading_time']= ceil(strlen($content_data)/1200).__(' min read', 'ultimate-post');
191
192 // image
193 if( has_post_thumbnail() ){
194 $thumb_id = get_post_thumbnail_id($post_id);
195 $image_sizes = ultimate_post()->get_image_size();
196 $image_src = array();
197 foreach ($image_sizes as $key => $value) {
198 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
199 }
200 $var['image'] = $image_src;
201 }
202
203 // tag
204 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
205 if(!empty($tag)){
206 $v = array();
207 foreach ($tag as $val) {
208 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
209 }
210 $var['tag'] = $v;
211 }
212
213 // cat
214 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
215 if(!empty($cat)){
216 $v = array();
217 foreach ($cat as $val) {
218 $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));
219 }
220 $var['category'] = $v;
221 }
222 $data[] = $var;
223 }
224 wp_reset_postdata();
225 }
226
227 return rest_ensure_response( $data );
228 }
229
230
231 /**
232 * Taxonomy Data Response of REST API
233 *
234 * @since v.1.0.0
235 * @param ARRAY | Parameter (ARRAY)
236 * @return ARRAY | Response Taxonomy List as Array
237 */
238 public function ultp_route_taxonomy_data($prams) {
239 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
240 return rest_ensure_response([]);
241 }
242
243 $all_post_type = ultimate_post()->get_post_type();
244 $data = array();
245 foreach ($all_post_type as $post_type_slug => $post_type ) {
246 $data_term = array();
247 $taxonomies = get_object_taxonomies($post_type_slug);
248 foreach ($taxonomies as $key => $taxonomy_slug) {
249 $taxonomy_value = get_terms(array(
250 'taxonomy' => $taxonomy_slug,
251 'hide_empty' => false
252 ));
253 if (!is_wp_error($taxonomy_value)) {
254 $data_tax = array();
255 foreach ($taxonomy_value as $k => $taxonomy) {
256 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
257 }
258 $data_term[$taxonomy_slug] = $data_tax;
259 }
260 }
261 $data[$post_type_slug] = $data_term;
262 }
263 return rest_ensure_response($data);
264 }
265
266 /**
267 * Specific 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_taxonomy_info_data($prams) {
274 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')) {
275 return rest_ensure_response([]);
276 }
277
278 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
279 }
280 }