PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 3.1.4
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v3.1.4
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 / inc / block-posts-config / class-post-rest-api.php

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

543 lines 15.2 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 if(!class_exists('Aft_Post_Rest_Controller')){
14 class Aft_Post_Rest_Controller extends WP_REST_Controller {
15
16 /**
17 * Type property name.
18 */
19 const PROP_TYPE = 'type';
20
21 /**
22 * Type property name.
23 */
24 const PROP_TYPE_ARRAY = 'type_array';
25
26 /**
27 * Query property name.
28 */
29 const PROP_QUERY = 'query';
30 /**
31 * Query property name.
32 */
33 const PROP_MULTIPLE = 'multiple';
34
35 /**
36 * Query property name.
37 */
38 const PROP_ORDER_BY = 'order_by';
39
40 /**
41 * Query property name.
42 */
43 const PROP_ORDER = 'order';
44
45 /**
46 * Query property name.
47 */
48 const PROP_ALLOW_STICKY = 'allow_sticky';
49
50 /**
51 * Query property name.
52 */
53 const PROP_OFFSET = 'offset';
54
55 /**
56 * Query property name.
57 */
58 const PROP_TAX = 'tax';
59
60 /**
61 * Query property name.
62 */
63 const PROP_EXCLUDE = 'exclude';
64
65 /**
66 * Query property name.
67 */
68 const PROP_CUSTOM_TAX = 'custom_tax';
69 /**
70 * Query property name.
71 */
72 const PROP_TAGS = 'tags';
73 /**
74 * Query property name.
75 */
76 const PROP_CATEGORY = 'category';
77
78 /**
79 * Query property name.
80 */
81 const PROP_TAX_TYPE = 'tax_type';
82
83 /**
84 * Search property name.
85 */
86 const PROP_SEARCH = 'search';
87
88 /**
89 * Include property name.
90 */
91 const PROP_INCLUDE = 'include';
92
93 /**
94 * Per page property name.
95 */
96 const PROP_PER_PAGE = 'per_page';
97
98 /**
99 * Per page property name.
100 */
101 const PROP_POST_ID = 'post_id';
102
103 /**
104 * Page property name.
105 */
106 const PROP_PAGE = 'page';
107
108
109 /**
110 * Constructor.
111 */
112 public function __construct() {
113 $this->namespace = 'aft/v1';
114 $this->query_base = 'post-query';
115 }
116
117 /**
118 * Registers the routes for the objects of the controller.
119 *
120 * @see register_rest_route()
121 */
122 public function register_routes() {
123
124 register_rest_route(
125 $this->namespace,
126 '/' . $this->query_base,
127 array(
128 array(
129 'methods' => WP_REST_Server::READABLE,
130 'callback' => array( $this, 'get_query_items' ),
131 'permission_callback' => array( $this, 'get_items_permission_check' ),
132 'args' => $this->get_query_params(),
133 ),
134 )
135 );
136 }
137 public function get_items_permission_check( $request ) {
138 return current_user_can( 'edit_posts' );
139 }
140 public function get_query_items( $request ) {
141
142 $prop_type = $request->get_param( self::PROP_TYPE );
143 $query_type = $request->get_param( self::PROP_QUERY );
144 $tax_type = $request->get_param( self::PROP_TAX_TYPE );
145
146
147 $categories = ( $request->get_param( self::PROP_CATEGORY ) ? wp_parse_list( $request->get_param( self::PROP_CATEGORY ) ) : array() );
148 $tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() );
149 if ( empty( $query_type ) ) {
150 return array();
151 }
152
153 $query_args = array(
154 'post_type' => $prop_type,
155 );
156 if ( 'individual' === $query_type ) {
157 $query_args['post__in'] = $request->get_param( self::PROP_INCLUDE );
158 $query_args['orderby'] = 'post__in';
159 $query_args['posts_per_page'] = -1;
160 $query_args['ignore_sticky_posts'] = 1;
161 } else {
162 $query_args['posts_per_page'] = $request->get_param( self::PROP_PER_PAGE );
163 $query_args['tax_query'] = array();
164 $query_args['orderby'] = $request->get_param( self::PROP_ORDER_BY );
165 $query_args['order'] = $request->get_param( self::PROP_ORDER );
166 $query_args['offset'] = $request->get_param( self::PROP_OFFSET );
167
168 $query_args['post_status'] = 'publish';
169 $query_args['ignore_sticky_posts'] = $request->get_param( self::PROP_ALLOW_STICKY );
170 $current_post_id = $request->get_param( self::PROP_POST_ID );
171 if ( ! empty( $current_post_id ) ) {
172 $query_args['post__not_in'] = array( $current_post_id );
173 }
174 if ( 'post' !== $prop_type || $request->get_param( self::PROP_CUSTOM_TAX ) ) {
175 if ( $tax_type ) {
176 if(!empty($categories)){
177 $query_args['tax_query'][] = array(
178 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
179 'field' => 'id',
180 'terms' => (isset($categories))?$categories:'',
181 'operator' => ( isset( $exclude ) && 'exclude' === $exclude ? 'NOT IN' : 'IN' ),
182 );
183 }
184 }
185 } else if('post' == $prop_type){
186 if ( $tax_type ) {
187
188 if(!empty($categories) ){
189 $query_args['tax_query'][] = array(
190 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
191 'field' => 'term_id',
192 'terms' => (!empty($categories))?$categories:'',
193 'operator' => ( isset( $exclude ) && 'exclude' === $exclude ? 'NOT IN' : 'IN' )
194
195 );
196 }else{
197
198 if($tax_type == 'post_tag'){
199 if(empty($tags)){
200 $taxonomy = $tax_type; // this is the name of the taxonomy
201 $terms = get_terms($taxonomy);
202
203 $query_args['tax_query'][] = array(
204 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
205 'field' => 'term_id',
206 'terms' => wp_list_pluck($terms,'term_id')
207
208 );
209 }else{
210 $query_args['tax_query'][] = array(
211 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
212 'field' => 'id',
213 'terms' => $tags,
214
215 );
216 }
217 }else{
218
219 $taxonomy = $tax_type; // this is the name of the taxonomy
220 $terms = get_terms($taxonomy);
221 $query_args['tax_query'][] = array(
222 'taxonomy' => ( isset( $tax_type ) ) ? $tax_type : 'category',
223 'field' => 'term_id',
224 'terms' => wp_list_pluck($terms,'term_id')
225
226 );
227 }
228
229 }
230 }
231 }else {
232 $tags = ( $request->get_param( self::PROP_TAGS ) ? wp_parse_list( $request->get_param( self::PROP_TAGS ) ) : array() );
233
234 $query_args['category__in'] = $categories;
235 $query_args['tag__in'] = $tags;
236
237 }
238 }
239 $query = new WP_Query( $query_args );
240 $posts = array();
241
242 foreach ( $query->posts as $post ) {
243 $posts[] = $this->prepare_query_item_for_response( $post, $request,$tax_type );
244 }
245
246 return rest_ensure_response( $posts );
247 }
248
249 public function prepare_query_item_for_response( $post, $request,$tax_type ) {
250 $excerpt = $this->blockspare_api_excerpt($post,$length=15);
251 $new_excerpt = apply_filters( 'the_excerpt', $excerpt );
252 $data = array(
253 'id' => $post->ID,
254 'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
255 'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ),
256 'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
257 'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
258 'title' => array(
259 'raw' => $post->post_title,
260 'rendered' => get_the_title( $post->ID ),
261 ),
262 'excerpt' => array(
263 'raw' => $post->post_excerpt,
264 'rendered' => get_the_excerpt( $post->ID ),
265 ),
266 'data_content'=>wp_kses_post($excerpt),
267 'type' => $post->post_type,
268 'slug' => $post->post_name,
269 'status' => $post->post_status,
270 'link' => get_permalink( $post->ID ),
271 'author' => absint( $post->post_author ),
272 'display_name'=> get_the_author_meta('display_name', $request['author']),
273 'author_link'=>get_author_posts_url($request['author']),
274 'featured_media' => get_post_thumbnail_id( $post->ID ),
275 );
276
277 $attachment_id = get_post_thumbnail_id( $post->ID );
278
279 $image= wp_get_attachment_image_src(
280 get_post_thumbnail_id( $post ),
281 'full',
282 false
283 );
284 $sizes = get_intermediate_image_sizes();
285
286 $imageSizes = array(
287 'full' => is_array($image) ? $image : '',
288 );
289
290 foreach ($sizes as $size) {
291 $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : '';
292 }
293
294 $data['featured_image_src_large'] = $imageSizes;
295
296 //$author_data = array();
297 if ( post_type_supports( $post->post_type, 'author' ) ) {
298 $blockspare = new BlocksapreMultiAuthorForBackend();
299
300
301 $data['author_info'] = $blockspare->blockspare_by_author($post);
302
303 }
304 if ( 'post' != $post->post_type || 'comments' != $post->post_type || 'author' != $post->post_type ) {
305 $blockspare = new BlocksapreMultiAuthorForBackend();
306
307 $data['author_info'] = $blockspare->blockspare_by_author($post);
308
309 }
310 //$data['author_info'] = $author_data;
311 if ( post_type_supports( $post->post_type, 'comments' ) ) {
312 $comments_count = wp_count_comments( $post->ID );
313 $data['comment_count'] = $comments_count->total_comments;
314 }
315 if ( 'post' === $post->post_type ) {
316 if($tax_type == 'category' || $tax_type =='' ){
317 $data['category_info'] = get_the_category( $post->ID );
318 }else if($tax_type == 'post_tag'){
319 $data['taxonomy_info'] = json_encode(get_the_tags( $post->ID ));
320 }else if(!empty($tax_type) && ($tax_type != 'category' || $tax_type != 'post_tag')){
321
322 $term_items = array();
323 $terms = get_the_terms( $post->ID, $tax_type );
324
325 if ( ! empty( $terms ) ) {
326
327 foreach ( $terms as $term_key => $term_item ) {
328 $term_items[] = array(
329 'term_id' => $term_item->term_id,
330 'name' => $term_item->name,
331 );
332 }
333
334 $data['taxonomy_info'] =json_encode($term_items);
335 }
336 }else{
337 $data['category_info'] = get_the_category( $post->ID );
338 }
339 }else{
340 $taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
341 $taxs = array();
342 $term_items = array();
343 foreach ( $taxonomies as $term_slug => $term ) {
344 if ( ! $term->public || ! $term->show_ui ) {
345 continue;
346 }
347 $terms = get_the_terms( $post->ID, $term_slug );
348
349 if ( ! empty( $terms ) ) {
350
351 foreach ( $terms as $term_key => $term_item ) {
352 $term_items[] = array(
353 'term_id' => $term_item->term_id,
354 'name' => $term_item->name,
355 );
356 }
357
358 }
359 }
360 $data['category_info'] = get_the_category( $post->ID );
361 $data['taxonomy_info'] = json_encode($term_items);
362 }
363
364
365 return $data;
366 }
367
368 protected function blockspare_api_excerpt($post,$length=''){
369
370 $excerpt = get_post_field(
371 'post_excerpt',
372 $post->post_id,
373 'display'
374 );
375
376 preg_replace( '~^(\s*(?:&nbsp;)?)*~i', '', $excerpt );
377 if ( empty( $excerpt ) ) {
378
379 $excerpt = preg_replace(
380 array(
381 '/\<figcaption>.*\<\/figcaption>/',
382 '/\[caption.*\[\/caption\]/',
383 '`[[^]]*]`'
384 ),
385 '',
386 $post->post_content
387 );
388 }
389
390
391 // Trim the excerpt if necessary.
392 if ( isset( $length ) ) {
393 $excerpt = wp_trim_words(
394 $excerpt,
395 $length
396 );
397 }
398
399 return $excerpt;
400 }
401
402 protected function prepare_date_response( $date_gmt, $date = null ) {
403 // Use the date if passed.
404 if ( isset( $date ) ) {
405 return mysql2date( 'Y-m-d\TH:i:s', $date, false );
406 }
407
408 // Return null if $date_gmt is empty/zeros.
409 if ( '0000-00-00 00:00:00' === $date_gmt ) {
410 return null;
411 }
412
413 // Return the formatted datetime.
414 return mysql2date( 'Y-m-d\TH:i:s', $date_gmt, false );
415 }
416
417
418 public function get_query_params() {
419 $query_params = parent::get_collection_params();
420
421 $query_params[ self::PROP_TYPE ] = array(
422 'description' => __( 'Limit results to items of a specific post type.', 'latest-posts-block' ),
423 'type' => 'string',
424 'sanitize_callback' => array( $this, 'sanitize_post_type_string' ),
425 'validate_callback' => array( $this, 'validate_post_type_string' ),
426 );
427
428 $query_params[ self::PROP_INCLUDE ] = array(
429 'description' => __( 'Include posts by ID.', 'latest-posts-block' ),
430 'type' => 'array',
431 'validate_callback' => array( $this, 'validate_post_ids' ),
432 'sanitize_callback' => array( $this, 'sanitize_post_ids' ),
433 );
434 $query_params[ self::PROP_PER_PAGE ] = array(
435 'description' => __( 'Number of results to return.', 'latest-posts-block' ),
436 'type' => 'number',
437 'sanitize_callback' => array( $this, 'sanitize_post_perpage' ),
438 'default' => 25,
439 );
440 $query_params[ self::PROP_QUERY ] = array(
441 'description' => __( 'Define Type of Query.', 'latest-posts-block' ),
442 'type' => 'string',
443 );
444 $query_params[ self::PROP_ORDER ] = array(
445 'description' => __( 'Define Query Order.', 'latest-posts-block' ),
446 'type' => 'string',
447 );
448 $query_params[ self::PROP_ORDER_BY ] = array(
449 'description' => __( 'Define Query Order By.', 'latest-posts-block' ),
450 'type' => 'string',
451 );
452 $query_params[ self::PROP_ALLOW_STICKY ] = array(
453 'description' => __( 'Allow Sticky in Query.', 'latest-posts-block' ),
454 'type' => 'boolean',
455 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ),
456 );
457 $query_params[ self::PROP_EXCLUDE ] = array(
458 'description' => __( 'Exclude Category.', 'latest-posts-block' ),
459 'type' => 'string',
460 );
461 $query_params[ self::PROP_OFFSET ] = array(
462 'description' => __( 'Number of items to offset in query.', 'latest-posts-block' ),
463 'type' => 'number',
464 'sanitize_callback' => array( $this, 'sanitize_results_page_number' ),
465 'default' => 0,
466 );
467 $query_params[ self::PROP_POST_ID ] = array(
468 'description' => __( 'The Current Post ID.', 'latest-posts-block' ),
469 'type' => 'number',
470 );
471 $query_params[ self::PROP_CUSTOM_TAX ] = array(
472 'description' => __( 'Check if using a custom Taxonomy', 'latest-posts-block' ),
473 'type' => 'boolean',
474 'sanitize_callback' => array( $this, 'sanitize_allow_sticky' ),
475 );
476 $query_params[ self::PROP_TAX_TYPE ] = array(
477 'description' => __( 'Define Query Order By.', 'latest-posts-block' ),
478 'type' => 'string',
479 );
480 $query_params[ self::PROP_CATEGORY ] = array(
481 'description' => __( 'Include posts category.', 'latest-posts-block' ),
482 'type' => 'string',
483 'sanitize_callback' => 'wp_parse_id_list',
484 );
485 $query_params[ self::PROP_TAGS ] = array(
486 'description' => __( 'Include posts tags.', 'latest-posts-block' ),
487 'type' => 'string',
488 'sanitize_callback' => 'wp_parse_id_list',
489 );
490 return $query_params;
491 }
492
493 public function sanitize_post_ids( $ids ) {
494 return array_map( 'absint', $ids );
495 }
496 public function validate_post_ids( $ids ) {
497 return count( $ids ) > 0;
498 }
499 public function sanitize_post_perpage( $val ) {
500 return min( absint( $val ), 100 );
501 }
502 public function sanitize_allow_sticky( $val ) {
503 return $val ? 0 : 1;
504 }
505 public function sanitize_results_page_number( $val ) {
506 return absint( $val );
507 }
508 public function validate_post_type_string( $value ) {
509 $allowed_types = $this->get_allowed_post_types();
510 return in_array( $value, $allowed_types );
511 }
512
513 public function sanitize_post_type_string( $post_type, $request ) {
514 return sanitize_text_field( $post_type );
515 }
516
517 public function get_allowed_post_types() {
518 $allowed_types = array_values(
519 get_post_types(
520 array(
521 'show_in_rest' => true,
522 'public' => true,
523 )
524 )
525 );
526
527 $key = array_search( 'attachment', $allowed_types, true );
528
529 if ( false !== $key ) {
530 unset( $allowed_types[ $key ] );
531 }
532
533 /**
534 * Filter the allowed post types.
535 *
536 * Note that if you allow this for posts that are not otherwise public,
537 * this data will be accessible using this endpoint for any logged in user with the edit_post capability.
538 */
539 return apply_filters( 'aft_blocks_allowed_post_types', $allowed_types );
540 }
541
542 }
543 }