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

276 lines 10.2 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 $data = [];
163 $loop = new \WP_Query($args);
164
165 if($loop->have_posts()){
166 while($loop->have_posts()) {
167 $loop->the_post();
168 $var = array();
169 $post_id = get_the_ID();
170 $user_id = get_the_author_meta('ID');
171 $content_data = get_the_content();
172 $var['ID'] = $post_id;
173 $var['title'] = get_the_title();
174 $var['permalink'] = get_permalink();
175 $var['seo_meta'] = ultimate_post()->get_excerpt($post_id, 1);
176 $var['excerpt'] = strip_tags($content_data);
177 $var['excerpt_full']= strip_tags(get_the_excerpt());
178 $var['time'] = get_the_date();
179 $var['timeModified']= get_the_modified_date();
180 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
181 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
182 $var['comments'] = get_comments_number();
183 $var['author_link'] = get_author_posts_url($user_id);
184 $var['avatar_url'] = get_avatar_url($user_id);
185 $var['display_name']= get_the_author_meta('display_name');
186 $var['reading_time']= ceil(strlen($content_data)/1200).__(' min read', 'ultimate-post');
187
188 // image
189 if( has_post_thumbnail() ){
190 $thumb_id = get_post_thumbnail_id($post_id);
191 $image_sizes = ultimate_post()->get_image_size();
192 $image_src = array();
193 foreach ($image_sizes as $key => $value) {
194 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
195 }
196 $var['image'] = $image_src;
197 }
198
199 // tag
200 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
201 if(!empty($tag)){
202 $v = array();
203 foreach ($tag as $val) {
204 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
205 }
206 $var['tag'] = $v;
207 }
208
209 // cat
210 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
211 if(!empty($cat)){
212 $v = array();
213 foreach ($cat as $val) {
214 $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));
215 }
216 $var['category'] = $v;
217 }
218 $data[] = $var;
219 }
220 wp_reset_postdata();
221 }
222
223 return rest_ensure_response( $data );
224 }
225
226
227 /**
228 * Taxonomy Data Response of REST API
229 *
230 * @since v.1.0.0
231 * @param ARRAY | Parameter (ARRAY)
232 * @return ARRAY | Response Taxonomy List as Array
233 */
234 public function ultp_route_taxonomy_data($prams) {
235 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
236 return rest_ensure_response([]);
237 }
238
239 $all_post_type = ultimate_post()->get_post_type();
240 $data = array();
241 foreach ($all_post_type as $post_type_slug => $post_type ) {
242 $data_term = array();
243 $taxonomies = get_object_taxonomies($post_type_slug);
244 foreach ($taxonomies as $key => $taxonomy_slug) {
245 $taxonomy_value = get_terms(array(
246 'taxonomy' => $taxonomy_slug,
247 'hide_empty' => false
248 ));
249 if (!is_wp_error($taxonomy_value)) {
250 $data_tax = array();
251 foreach ($taxonomy_value as $k => $taxonomy) {
252 $data_tax[urldecode_deep($taxonomy->slug)] = $taxonomy->name;
253 }
254 $data_term[$taxonomy_slug] = $data_tax;
255 }
256 }
257 $data[$post_type_slug] = $data_term;
258 }
259 return rest_ensure_response($data);
260 }
261
262 /**
263 * Specific Taxonomy Data Response of REST API
264 *
265 * @since v.1.0.0
266 * @param ARRAY | Parameter (ARRAY)
267 * @return ARRAY | Response Taxonomy List as Array
268 */
269 public function ultp_route_taxonomy_info_data($prams) {
270 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')) {
271 return rest_ensure_response([]);
272 }
273
274 return rest_ensure_response( ultimate_post()->get_category_data(json_decode($prams['taxValue']), $prams['queryNumber'], $prams['taxType'], $prams['taxSlug']) );
275 }
276 }