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

183 lines 6.5 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', '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 $data = [];
96 $loop = new WP_Query($args);
97
98 if($loop->have_posts()){
99 while($loop->have_posts()) {
100 $loop->the_post();
101 $var = array();
102 $post_id = get_the_ID();
103 $user_id = get_the_author_meta('ID');
104 $var['title'] = get_the_title();
105 $var['permalink'] = get_permalink();
106 $var['excerpt'] = strip_tags(get_the_content());
107 $var['excerpt_full']= strip_tags(get_the_excerpt());
108 $var['time'] = get_the_date();
109 $var['post_time'] = human_time_diff(get_the_time('U'),current_time('U'));
110 $var['view'] = get_post_meta(get_the_ID(),'__post_views_count', true);
111 $var['comments'] = get_comments_number();
112 $var['author_link'] = get_author_posts_url($user_id);
113 $var['avatar_url'] = get_avatar_url($user_id);
114 $var['display_name']= get_the_author_meta('display_name');
115 $var['reading_time']= ceil(strlen(get_the_content())/1200).__(' min read', 'ultimate-post');
116
117 // image
118 if( has_post_thumbnail() ){
119 $thumb_id = get_post_thumbnail_id($post_id);
120 $image_sizes = ultimate_post()->get_image_size();
121 $image_src = array();
122 foreach ($image_sizes as $key => $value) {
123 $image_src[$key] = wp_get_attachment_image_src($thumb_id, $key, false)[0];
124 }
125 $var['image'] = $image_src;
126 }
127
128 // tag
129 $tag = get_the_terms($post_id, (isset($prams['tag'])?esc_attr($prams['tag']):'post_tag'));
130 if(!empty($tag)){
131 $v = array();
132 foreach ($tag as $val) {
133 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
134 }
135 $var['tag'] = $v;
136 }
137
138 // cat
139 $cat = get_the_terms($post_id, (isset($prams['cat'])?esc_attr($prams['cat']):'category'));
140 if(!empty($cat)){
141 $v = array();
142 foreach ($cat as $val) {
143 $v[] = array('slug' => $val->slug, 'name' => $val->name, 'url' => get_term_link($val->term_id));
144 }
145 $var['category'] = $v;
146 }
147 $data[] = $var;
148 }
149 wp_reset_postdata();
150 }
151
152 return rest_ensure_response( $data );
153 }
154
155
156
157 function ultp_route_taxonomy_data($prams) {
158 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
159 return rest_ensure_response([]);
160 }
161 return rest_ensure_response(ultimate_post()->taxonomy($prams['taxonomy']));
162 }
163
164 function ultp_route_taxonomy_info_data($prams) {
165 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce')){
166 return rest_ensure_response([]);
167 }
168
169 $data = array();
170 $terms = get_terms( $prams, array(
171 'hide_empty' => true,
172 ));
173 if( !empty($terms) ){
174 foreach ($terms as $val) {
175 $data['name'] = $val->name;
176 $data['count'] = $val->count;
177 $data['url'] = get_term_link($val->term_id);
178 $data['color'] = get_term_meta($val->term_id, '_background', true);
179 }
180 }
181
182 return rest_ensure_response($data);
183 }