PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 2.1.2
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v2.1.2
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / src / inc / block-posts-config / class-post-rest-api.php

class-post-rest-api.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 2.1.2, at src/inc/block-posts-config/class-post-rest-api.php

420 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Setup the Posts Block Rest Calls
4 *
5 * @since 2.0.0
6 * @package AFT Blocks
7 */
8
9
10 /**
11 * Class to setup new rest calls.
12 */
13 class Aft_Post_Rest_Controller extends WP_REST_Controller {
14 /**
15 * Type property name.
16 */
17 const PROP_TYPE = 'type';
18
19 /**
20 * Type property name.
21 */
22 const PROP_TYPE_ARRAY = 'type_array';
23
24 /**
25 * Query property name.
26 */
27 const PROP_QUERY = 'query';
28 /**
29 * Query property name.
30 */
31 const PROP_MULTIPLE = 'multiple';
32
33 /**
34 * Query property name.
35 */
36 const PROP_ORDER_BY = 'order_by';
37
38 /**
39 * Query property name.
40 */
41 const PROP_ORDER = 'order';
42
43 /**
44 * Query property name.
45 */
46 const PROP_ALLOW_STICKY = 'allow_sticky';
47
48 /**
49 * Query property name.
50 */
51 const PROP_OFFSET = 'offset';
52
53 /**
54 * Query property name.
55 */
56 const PROP_TAX = 'tax';
57
58 /**
59 * Query property name.
60 */
61 const PROP_EXCLUDE = 'exclude';
62
63 /**
64 * Query property name.
65 */
66 const PROP_CUSTOM_TAX = 'custom_tax';
67 /**
68 * Query property name.
69 */
70 const PROP_TAGS = 'tags';
71 /**
72 * Query property name.
73 */
74 const PROP_CATEGORY = 'category';
75
76 /**
77 * Query property name.
78 */
79 const PROP_TAX_TYPE = 'tax_type';
80
81 /**
82 * Search property name.
83 */
84 const PROP_SEARCH = 'search';
85
86 /**
87 * Include property name.
88 */
89 const PROP_INCLUDE = 'include';
90
91 /**
92 * Per page property name.
93 */
94 const PROP_PER_PAGE = 'per_page';
95
96 /**
97 * Per page property name.
98 */
99 const PROP_POST_ID = 'post_id';
100
101 /**
102 * Page property name.
103 */
104 const PROP_PAGE = 'page';
105
106 /**
107 * Constructor.
108 */
109 public function __construct() {
110 $this->namespace = 'aft/v1';
111 $this->query_base = 'post-query';
112 }
113
114 /**
115 * Registers the routes for the objects of the controller.
116 *
117 * @see register_rest_route()
118 */
119 public function register_routes() {
120
121 register_rest_route(
122 $this->namespace,
123 '/' . $this->query_base,
124 array(
125 array(
126 'methods' => WP_REST_Server::READABLE,
127 'callback' => array( $this, 'get_query_items' ),
128 'permission_callback' => array( $this, 'get_items_permission_check' ),
129 'args' => $this->get_query_params(),
130 ),
131 )
132 );
133 }
134 public function get_items_permission_check( $request ) {
135 return current_user_can( 'edit_posts' );
136 }
137 public function get_query_items( $request ) {
138
139 $prop_type = $request->get_param( self::PROP_TYPE );
140 $query_type = $request->get_param( self::PROP_QUERY );
141 $tax_type = $request->get_param( self::PROP_TAX_TYPE );
142
143 $categories = ( $request->get_param( self::PROP_CATEGORY ) ? wp_parse_list( $request->get_param( self::PROP_CATEGORY ) ) : array() );
144 $tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() );
145 if ( empty( $query_type ) ) {
146 return array();
147 }
148
149 $query_args = array(
150 'post_type' => $prop_type,
151 );
152 if ( 'individual' === $query_type ) {
153 $query_args['post__in'] = $request->get_param( self::PROP_INCLUDE );
154 $query_args['orderby'] = 'post__in';
155 $query_args['posts_per_page'] = -1;
156 $query_args['ignore_sticky_posts'] = 1;
157 } else {
158 $query_args['posts_per_page'] = $request->get_param( self::PROP_PER_PAGE );
159 $query_args['tax_query'] = array();
160 $query_args['orderby'] = $request->get_param( self::PROP_ORDER_BY );
161 $query_args['order'] = $request->get_param( self::PROP_ORDER );
162 $query_args['offset'] = $request->get_param( self::PROP_OFFSET );
163
164 $query_args['post_status'] = 'publish';
165 $query_args['ignore_sticky_posts'] = $request->get_param( self::PROP_ALLOW_STICKY );
166 $current_post_id = $request->get_param( self::PROP_POST_ID );
167 if ( ! empty( $current_post_id ) ) {
168 $query_args['post__not_in'] = array( $current_post_id );
169 }
170 if ( $tax_type != 'category' && $tax_type != 'post_tag') {
171 $query_args['tax_query'][] = array(
172 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
173 'field' => 'id',
174 'terms' => $categories,
175 'operator' => 'IN' ,
176 );
177 }else {
178
179 $query_args['category__in'] = $categories;
180 $query_args['tag__in'] = $tags;
181
182
183 }
184 }
185 $query = new WP_Query( $query_args );
186 $posts = array();
187
188 foreach ( $query->posts as $post ) {
189 $posts[] = $this->prepare_query_item_for_response( $post, $request,$tax_type );
190 }
191
192 return rest_ensure_response( $posts );
193 }
194
195 public function prepare_query_item_for_response( $post, $request,$tax_type ) {
196 $data = array(
197 'id' => $post->ID,
198 'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
199 'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ),
200 'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
201 'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
202 'title' => array(
203 'raw' => $post->post_title,
204 'rendered' => get_the_title( $post->ID ),
205 ),
206 'excerpt' => array(
207 'raw' => $post->post_excerpt,
208 'rendered' => get_the_excerpt( $post->ID ),
209 ),
210 'type' => $post->post_type,
211 'slug' => $post->post_name,
212 'status' => $post->post_status,
213 'link' => get_permalink( $post->ID ),
214 'author' => absint( $post->post_author ),
215 'display_name'=> get_the_author_meta('display_name', absint( $post->post_author )),
216 'author_link'=>get_author_posts_url(absint( $post->post_author )),
217 'featured_media' => get_post_thumbnail_id( $post->ID ),
218 );
219
220 $attachment_id = get_post_thumbnail_id( $post->ID );
221
222 $image= wp_get_attachment_image_src(
223 get_post_thumbnail_id( $post ),
224 'full',
225 false
226 );
227 $sizes = get_intermediate_image_sizes();
228
229 $imageSizes = array(
230 'full' => is_array($image) ? $image : '',
231 );
232
233 foreach ($sizes as $size) {
234 $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : '';
235 }
236
237 $data['featured_image_src_large'] = $imageSizes;
238
239 $author_data = array();
240 if ( post_type_supports( $post->post_type, 'author' ) ) {
241 // Get the author name.
242 $author_data['display_name'] = get_the_author_meta( 'display_name', absint( $post->post_author ) );
243 // Get the author link.
244 $author_data['author_link'] = get_author_posts_url( absint( $post->post_author ) );
245 }
246 $data['author_info'] = $author_data;
247 if ( post_type_supports( $post->post_type, 'comments' ) ) {
248 $comments_count = wp_count_comments( $post->ID );
249 $data['comment_count'] = $comments_count->total_comments;
250 }
251 if ( 'post' === $post->post_type ) {
252 $data['category_info'] = get_the_category( $post->ID );
253 $data['tag_info'] = get_the_tags( $post->ID );
254 }
255 $taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
256 $taxs = array();
257 $term_items = array();
258 foreach ( $taxonomies as $term_slug => $term ) {
259 if ( ! $term->public || ! $term->show_ui ) {
260 continue;
261 }
262 $terms = get_the_terms( $post->ID, $term_slug );
263
264 if ( ! empty( $terms ) ) {
265 foreach ( $terms as $term_key => $term_item ) {
266 $term_items[] = array(
267 'value' => $term_item->term_id,
268 'label' => $term_item->name,
269 );
270 }
271
272 }
273 }
274
275 $data['taxonomy_info'] = json_encode($term_items);
276
277 return $data;
278 }
279
280 protected function prepare_date_response( $date_gmt, $date = null ) {
281 // Use the date if passed.
282 if ( isset( $date ) ) {
283 return mysql2date( 'Y-m-d\TH:i:s', $date, false );
284 }
285
286 // Return null if $date_gmt is empty/zeros.
287 if ( '0000-00-00 00:00:00' === $date_gmt ) {
288 return null;
289 }
290
291 // Return the formatted datetime.
292 return mysql2date( 'Y-m-d\TH:i:s', $date_gmt, false );
293 }
294
295
296 public function get_query_params() {
297 $query_params = parent::get_collection_params();
298
299 $query_params[ self::PROP_TYPE ] = array(
300 'description' => __( 'Limit results to items of a specific post type.', 'blockspare' ),
301 'type' => 'string',
302 'sanitize_callback' => array( $this, 'sanitize_post_type_string' ),
303 'validate_callback' => array( $this, 'validate_post_type_string' ),
304 );
305
306 $query_params[ self::PROP_INCLUDE ] = array(
307 'description' => __( 'Include posts by ID.', 'blockspare' ),
308 'type' => 'array',
309 'validate_callback' => array( $this, 'validate_post_ids' ),
310 'sanitize_callback' => array( $this, 'sanitize_post_ids' ),
311 );
312 $query_params[ self::PROP_PER_PAGE ] = array(
313 'description' => __( 'Number of results to return.', 'blockspare' ),
314 'type' => 'number',
315 'sanitize_callback' => array( $this, 'sanitize_post_perpage' ),
316 'default' => 25,
317 );
318 $query_params[ self::PROP_QUERY ] = array(
319 'description' => __( 'Define Type of Query.', 'blockspare' ),
320 'type' => 'string',
321 );
322 $query_params[ self::PROP_ORDER ] = array(
323 'description' => __( 'Define Query Order.', 'blockspare' ),
324 'type' => 'string',
325 );
326 $query_params[ self::PROP_ORDER_BY ] = array(
327 'description' => __( 'Define Query Order By.', 'blockspare' ),
328 'type' => 'string',
329 );
330 $query_params[ self::PROP_ALLOW_STICKY ] = array(
331 'description' => __( 'Allow Sticky in Query.', 'blockspare' ),
332 'type' => 'boolean',
333 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ),
334 );
335 $query_params[ self::PROP_EXCLUDE ] = array(
336 'description' => __( 'Exclude Category.', 'blockspare' ),
337 'type' => 'string',
338 );
339 $query_params[ self::PROP_OFFSET ] = array(
340 'description' => __( 'Number of items to offset in query.', 'blockspare' ),
341 'type' => 'number',
342 'sanitize_callback' => array( $this, 'sanitize_results_page_number' ),
343 'default' => 0,
344 );
345 $query_params[ self::PROP_POST_ID ] = array(
346 'description' => __( 'The Current Post ID.', 'blockspare' ),
347 'type' => 'number',
348 );
349 $query_params[ self::PROP_CUSTOM_TAX ] = array(
350 'description' => __( 'Check if using a custom Taxonomy', 'blockspare' ),
351 'type' => 'boolean',
352 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ),
353 );
354 $query_params[ self::PROP_TAX_TYPE ] = array(
355 'description' => __( 'Define Query Order By.', 'blockspare' ),
356 'type' => 'string',
357 );
358 $query_params[ self::PROP_CATEGORY ] = array(
359 'description' => __( 'Include posts category.', 'blockspare' ),
360 'type' => 'string',
361 'sanitize_callback' => 'wp_parse_id_list',
362 );
363 $query_params[ self::PROP_TAGS ] = array(
364 'description' => __( 'Include posts tags.', 'blockspare' ),
365 'type' => 'string',
366 'sanitize_callback' => 'wp_parse_id_list',
367 );
368 return $query_params;
369 }
370
371 public function sanitize_post_ids( $ids ) {
372 return array_map( 'absint', $ids );
373 }
374 public function validate_post_ids( $ids ) {
375 return count( $ids ) > 0;
376 }
377 public function sanitize_post_perpage( $val ) {
378 return min( absint( $val ), 100 );
379 }
380 public function sanitize_allow_sticky( $val ) {
381 return $val ? 0 : 1;
382 }
383 public function sanitize_results_page_number( $val ) {
384 return absint( $val );
385 }
386 public function validate_post_type_string( $value ) {
387 $allowed_types = $this->get_allowed_post_types();
388 return in_array( $value, $allowed_types );
389 }
390
391 public function sanitize_post_type_string( $post_type, $request ) {
392 return sanitize_text_field( $post_type );
393 }
394
395 public function get_allowed_post_types() {
396 $allowed_types = array_values(
397 get_post_types(
398 array(
399 'show_in_rest' => true,
400 'public' => true,
401 )
402 )
403 );
404
405 $key = array_search( 'attachment', $allowed_types, true );
406
407 if ( false !== $key ) {
408 unset( $allowed_types[ $key ] );
409 }
410
411 /**
412 * Filter the allowed post types.
413 *
414 * Note that if you allow this for posts that are not otherwise public,
415 * this data will be accessible using this endpoint for any logged in user with the edit_post capability.
416 */
417 return apply_filters( 'aft_blocks_allowed_post_types', $allowed_types );
418 }
419
420 }