| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace GutSlider\Blocks; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Lets the Blog Post block preview in the editor match the published page. |
| 12 |
* |
| 13 |
* The page queries posts with WP_Query (render.php); the editor previews them |
| 14 |
* through the REST API, which can't order randomly or by comment count, filter |
| 15 |
* to posts with a featured image, or report a post's comment count. This adds |
| 16 |
* exactly those: |
| 17 |
* |
| 18 |
* - `orderby=rand` and `orderby=comment_count`; |
| 19 |
* - `gutslider_has_image=1`: only posts with a featured image; |
| 20 |
* - a read-only `gutslider_comments` field: the approved comment count. |
| 21 |
* |
| 22 |
* Applied to every public post type in the REST API (the block can show any of them). |
| 23 |
* |
| 24 |
* @package GutSlider\Blocks |
| 25 |
* @since 3.1.1 |
| 26 |
*/ |
| 27 |
final class PostSliderRest { |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
// Before core registers its routes (priority 99), so the extra params are accepted. |
| 34 |
add_action( 'rest_api_init', array( $this, 'extend_post_types' ), 5 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Public post types with a REST endpoint. |
| 39 |
* |
| 40 |
* @return string[] |
| 41 |
*/ |
| 42 |
private function post_types() { |
| 43 |
$types = array(); |
| 44 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $type ) { |
| 45 |
if ( 'attachment' !== $type->name && is_post_type_viewable( $type ) ) { |
| 46 |
$types[] = $type->name; |
| 47 |
} |
| 48 |
} |
| 49 |
return $types; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Hook every public post type. |
| 54 |
*/ |
| 55 |
public function extend_post_types() { |
| 56 |
foreach ( $this->post_types() as $type ) { |
| 57 |
add_filter( "rest_{$type}_collection_params", array( $this, 'collection_params' ) ); |
| 58 |
add_filter( "rest_{$type}_query", array( $this, 'query_args' ), 10, 2 ); |
| 59 |
} |
| 60 |
$this->register_fields(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Accept the extra order and filter parameters. |
| 65 |
* |
| 66 |
* @param array $params Collection parameters. |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function collection_params( $params ) { |
| 70 |
if ( isset( $params['orderby']['enum'] ) && is_array( $params['orderby']['enum'] ) ) { |
| 71 |
$params['orderby']['enum'] = array_values( array_unique( array_merge( $params['orderby']['enum'], array( 'rand', 'comment_count' ) ) ) ); |
| 72 |
} |
| 73 |
|
| 74 |
$params['gutslider_has_image'] = array( |
| 75 |
'description' => __( 'Limit results to posts with a featured image.', 'slider-blocks' ), |
| 76 |
'type' => 'boolean', |
| 77 |
); |
| 78 |
|
| 79 |
return $params; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Map the extra parameters onto WP_Query. |
| 84 |
* |
| 85 |
* @param array $args WP_Query arguments. |
| 86 |
* @param \WP_REST_Request $request Request. |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function query_args( $args, $request ) { |
| 90 |
$orderby = $request->get_param( 'orderby' ); |
| 91 |
if ( in_array( $orderby, array( 'rand', 'comment_count' ), true ) ) { |
| 92 |
$args['orderby'] = $orderby; |
| 93 |
} |
| 94 |
|
| 95 |
if ( $request->get_param( 'gutslider_has_image' ) ) { |
| 96 |
$meta_query = isset( $args['meta_query'] ) && is_array( $args['meta_query'] ) ? $args['meta_query'] : array(); |
| 97 |
$meta_query[] = array( |
| 98 |
'key' => '_thumbnail_id', |
| 99 |
'compare' => 'EXISTS', |
| 100 |
); |
| 101 |
$args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 102 |
} |
| 103 |
|
| 104 |
return $args; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Comment count on post records (the editor shows it in the meta line). |
| 109 |
*/ |
| 110 |
public function register_fields() { |
| 111 |
register_rest_field( |
| 112 |
$this->post_types(), |
| 113 |
'gutslider_comments', |
| 114 |
array( |
| 115 |
'get_callback' => static function ( $post ) { |
| 116 |
return (int) get_comments_number( $post['id'] ); |
| 117 |
}, |
| 118 |
'schema' => array( |
| 119 |
'description' => __( 'Approved comment count.', 'slider-blocks' ), |
| 120 |
'type' => 'integer', |
| 121 |
'context' => array( 'view', 'edit' ), |
| 122 |
'readonly' => true, |
| 123 |
), |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|