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

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