PluginProbe
Post Grid Gutenberg Blocks – PostX / 1.1.8
Post Grid Gutenberg Blocks – PostX v1.1.8
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 1.1.8, at classes/REST_API.php

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