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

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