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

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