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

204 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || exit;
3
4
5 // ------------- Custom Endpoint Start ----------
6
7 add_action( 'rest_api_init', 'ultp_register_route' );
8 function ultp_register_route() {
9 register_rest_route( 'ultp', 'posts', array(
10 'methods' => WP_REST_Server::READABLE,
11 'args' => array('post_type', 'taxonomy', 'include', 'exclude', 'order', 'orderby', 'count', 'size', 'tag', 'cat', 'meta_key', 'queryQuick', 'wpnonce'),
12 'callback' => 'ultp_route_post_data',
13 'permission_callback' => '__return_true'
14 )
15 );
16 register_rest_route( 'ultp', 'taxonomy', array(
17 'methods' => WP_REST_Server::READABLE,
18 'args' => array('taxonomy', 'wpnonce'),
19 'callback' => 'ultp_route_taxonomy_data',
20 'permission_callback' => '__return_true'
21 )
22 );
23 register_rest_route( 'ultp', 'imagesize', array(
24 'methods' => WP_REST_Server::READABLE,
25 'args' => array('taxonomy', 'wpnonce'),
26 'callback' => 'ultp_route_imagesize_data',
27 'permission_callback' => '__return_true'
28 )
29 );
30 register_rest_route( 'ultp', 'posttype', array(
31 'methods' => WP_REST_Server::READABLE,
32 'args' => array('wpnonce'),
33 'callback' => 'ultp_route_posttype_data',
34 'permission_callback' => '__return_true'
35 )
36 );
37 register_rest_route( 'ultp', 'tax_info', array(
38 'methods' => WP_REST_Server::READABLE,
39 'args' => array('taxonomy', 'wpnonce'),
40 'callback' => 'ultp_route_taxonomy_info_data',
41 'permission_callback' => '__return_true'
42 )
43 );
44 }
45
46 function ultp_route_posttype_data() {
47 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
48 return ;
49 }
50
51 return ultimate_post()->get_post_type();
52 }
53
54 function ultp_route_imagesize_data() {
55 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
56 return ;
57 }
58
59 return ultimate_post()->get_image_size();
60 }
61
62 function ultp_route_post_data($prams,$local=false) {
63 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
64 return ;
65 }
66
67 // Query Builder
68 $args = array(
69 'post_type' => 'post',
70 'orderby' => 'date',
71 'post_status' => 'publish',
72 'order' => 'DESC',
73 );
74
75 if(isset($prams['count'])){ $args['posts_per_page'] = esc_attr($prams['count']); }
76 if(isset($prams['post_type'])){ $args['post_type'] = esc_attr( $prams['post_type'] ); }
77
78 if(isset($prams['taxonomy'])){ // taxonomy slug__val__slug__val
79 $tax_arr = array('relation' => 'OR');
80 $tax = explode('__', esc_attr($prams['taxonomy']));
81 if(!empty($tax)){
82 for($i=0; $i < count($tax) ; $i++){
83 if($i%2 == 0){
84 $tax_arr[] = array('taxonomy'=>$tax[$i], 'field' => 'slug', 'terms' => array($tax[$i+1]));
85 }
86 }
87 }
88 if(!empty($tax_arr)){
89 $args['tax_query'] = $tax_arr;
90 }
91 }
92
93 if(isset($prams['include'])){ $args['post__in'] = explode('__', esc_attr($prams['include'])); }
94 if(isset($prams['exclude'])){ $args['post__not_in'] = explode('__', esc_attr($prams['exclude'])); }
95 if(isset($prams['orderby'])){ $args['orderby'] = esc_attr($prams['orderby']); }
96 if(isset($prams['order'])){ $args['order'] = esc_attr($prams['order']); }
97 if(isset($prams['offset'])){ $args['offset'] = esc_attr($prams['offset']); }
98 if(isset($prams['paged'])){ $args['paged'] = esc_attr($prams['paged']); }
99
100 if(isset($prams['orderby']) && isset($prams['meta_key'])){
101 if($prams['orderby'] == 'meta_value_num') {
102 $args['meta_key'] = $prams['meta_key'];
103 $args['meta_type'] = 'NUMERIC';
104 }
105 }
106
107 if(isset($prams['queryQuick'])){
108 if($prams['queryQuick'] != ''){
109 if(function_exists('ultimate_post_pro')){
110 $args = ultimate_post_pro()->get_quick_query($prams, $args);
111 }
112 }
113 }
114
115 $data = [];
116 $loop = new WP_Query($args);
117
118 if($loop->have_posts()){
119 while($loop->have_posts()) {
120 $loop->the_post();
121 $var = array();
122 $post_id = get_the_ID();
123 $user_id = get_the_author_meta('ID');
124 $var['ID'] = $post_id;
125 $var['title'] = get_the_title();
126 $var['permalink'] = get_permalink();
127 $var['excerpt'] = strip_tags(get_the_content());
128 $var['excerpt_full']= strip_tags(get_the_excerpt());
129 $var['time'] = get_the_date();
130 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
131 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
132 $var['comments'] = get_comments_number();
133 $var['author_link'] = get_author_posts_url($user_id);
134 $var['avatar_url'] = get_avatar_url($user_id);
135 $var['display_name']= get_the_author_meta('display_name');
136 $var['reading_time']= ceil(strlen(get_the_content())/1200).__(' min read', 'ultimate-post');
137
138 // image
139 if( has_post_thumbnail() ){
140 $thumb_id = get_post_thumbnail_id($post_id);
141 $image_sizes = ultimate_post()->get_image_size();
142 $image_src = array();
143 foreach ($image_sizes as $key => $value) {
144 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
145 }
146 $var['image'] = $image_src;
147 }
148
149 // tag
150 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
151 if(!empty($tag)){
152 $v = array();
153 foreach ($tag as $val) {
154 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
155 }
156 $var['tag'] = $v;
157 }
158
159 // cat
160 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
161 if(!empty($cat)){
162 $v = array();
163 foreach ($cat as $val) {
164 $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));
165 }
166 $var['category'] = $v;
167 }
168 $data[] = $var;
169 }
170 wp_reset_postdata();
171 }
172
173 return rest_ensure_response( $data );
174 }
175
176
177
178 function ultp_route_taxonomy_data($prams) {
179 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
180 return rest_ensure_response([]);
181 }
182 return rest_ensure_response(ultimate_post()->taxonomy($prams['taxonomy']));
183 }
184
185 function ultp_route_taxonomy_info_data($prams) {
186 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
187 return rest_ensure_response([]);
188 }
189
190 $data = array();
191 $terms = get_terms( $prams, array(
192 'hide_empty' => true,
193 ));
194 if( !empty($terms) ){
195 foreach ($terms as $val) {
196 $data['name'] = $val->name;
197 $data['count'] = $val->count;
198 $data['url'] = get_term_link($val->term_id);
199 $data['color'] = get_term_meta($val->term_id, '_background', true);
200 }
201 }
202
203 return rest_ensure_response($data);
204 }