diff --git a/src/RatingsController.php b/src/RatingsController.php index f403f72..3f81fe4 100644 --- a/src/RatingsController.php +++ b/src/RatingsController.php @@ -44,6 +44,8 @@ public function __construct( Plugin $plugin ) { // Filters. \add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 ); \add_filter( 'the_content', array( $this, 'object_content_ratings' ) ); + \add_filter( 'render_block_data', array( $this, 'render_query_block_data' ), 10, 2 ); + \add_filter( 'pre_get_posts', array( $this, 'pre_get_posts_pronamic_review_object' ) ); } /** @@ -360,4 +362,89 @@ public function object_content_ratings( $content ) { return $content . \PHP_EOL . $ratings_content; } + + /** + * Render Query block data. + * + * @param array $parsed_block Parsed block. + * @return array + */ + public function render_query_block_data( $parsed_block ) { + // Check Query block. + if ( ! \array_key_exists( 'blockName', $parsed_block ) ) { + return $parsed_block; + } + + if ( 'core/query' !== $parsed_block['blockName'] ) { + return $parsed_block; + } + + // Check query post type. + if ( ! isset( $parsed_block['attrs']['query']['postType'] ) ) { + return $parsed_block; + } + + if ( 'pronamic_review' !== $parsed_block['attrs']['query']['postType'] ) { + return $parsed_block; + } + + // Determine post type ratings support. + $object_post_id = \get_the_ID(); + + $post_type = \get_post_type( $object_post_id ); + + if ( ! \post_type_supports( $post_type, 'pronamic_ratings' ) ) { + return $parsed_block; + } + + // Set search query. + if ( ! \array_key_exists( 'search', $parsed_block['attrs']['query'] ) ) { + $parsed_block['attrs']['query']['search'] = ''; + } + + $parsed_block['attrs']['query']['search'] .= sprintf( '[pronamic_review_object_post_id=%d]', $object_post_id ); + + return $parsed_block; + } + + /** + * Set meta query from object post ID in search query. + * + * @param \WP_Query $query Query. + * @return void + */ + public function pre_get_posts_pronamic_review_object( \WP_Query $query ) { + // Check search. + if ( ! $query->is_search() ) { + return; + } + + // Check search query. + $search = $query->get( 's' ); + + $pattern = '/\[pronamic_review_object_post_id=(?\d+)\]/'; + + \preg_match( $pattern, $search, $matches ); + + if ( ! \array_key_exists( 'object_post_id', $matches ) ) { + return; + } + + // Cleanup search query. + $query->set( 's', \preg_replace( $pattern, '', $search ) ); + + // Add meta query for review object ID. + $meta_query = $query->get( 'meta_query' ); + + if ( ! \is_array( $meta_query ) ) { + $meta_query = array(); + } + + $meta_query[] = array( + 'key' => '_pronamic_review_object_post_id', + 'value' => $matches['object_post_id'], + ); + + $query->set( 'meta_query', $meta_query ); + } } diff --git a/src/ReviewPostType.php b/src/ReviewPostType.php index d45539c..bd5d11d 100644 --- a/src/ReviewPostType.php +++ b/src/ReviewPostType.php @@ -37,9 +37,9 @@ public function register_post_type() { \register_post_type( 'pronamic_review', array( - 'label' => __( 'Review', 'pronamic_reviews_ratings' ), - 'description' => __( 'Reviews', 'pronamic_reviews_ratings' ), - 'labels' => array( + 'label' => __( 'Review', 'pronamic_reviews_ratings' ), + 'description' => __( 'Reviews', 'pronamic_reviews_ratings' ), + 'labels' => array( 'name' => _x( 'Reviews', 'Post Type General Name', 'pronamic_reviews_ratings' ), 'singular_name' => _x( 'Review', 'Post Type Singular Name', 'pronamic_reviews_ratings' ), 'menu_name' => __( 'Reviews', 'pronamic_reviews_ratings' ), @@ -68,22 +68,22 @@ public function register_post_type() { 'items_list_navigation' => __( 'Reviews list navigation', 'pronamic_reviews_ratings' ), 'filter_items_list' => __( 'Filter reviews list', 'pronamic_reviews_ratings' ), ), - 'supports' => array( 'title', 'editor', 'thumbnail' ), - 'taxonomies' => array( 'category' ), - 'hierarchical' => false, - 'public' => true, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_position' => 5, - 'menu_icon' => 'dashicons-admin-page', - 'show_in_admin_bar' => true, - 'show_in_nav_menus' => true, - 'can_export' => true, - 'has_archive' => true, - 'exclude_from_search' => false, - 'publicly_queryable' => true, - 'capability_type' => 'page', - 'show_in_rest' => true, + 'supports' => array( 'title', 'editor', 'thumbnail' ), + 'taxonomies' => array(), + 'hierarchical' => false, + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_position' => 5, + 'menu_icon' => 'dashicons-admin-page', + 'show_in_admin_bar' => true, + 'show_in_nav_menus' => true, + 'can_export' => true, + 'has_archive' => true, + 'exclude_from_search' => false, + 'publicly_queryable' => true, + 'capability_type' => 'page', + 'show_in_rest' => true, ) ); } @@ -128,24 +128,23 @@ public function meta_box_review_details( $post ) { * @return void */ public function save_review_post( $post_id ) { - $prev_object_post_id = \get_post_meta( $post_id, '_pronamic_review_object_post_id', true ); - $updated_object_post_id = $prev_object_post_id; - // Save meta box details. if ( \filter_has_var( \INPUT_POST, 'pronamic_reviews_ratings_nonce' ) && \check_admin_referer( 'pronamic_reviews_ratings_save_review', 'pronamic_reviews_ratings_nonce' ) ) { + $object_post_id = \get_post_meta( $post_id, '_pronamic_review_object_post_id', true ); + // Object post ID. if ( \filter_has_var( \INPUT_POST, 'pronamic_review_object_post_id' ) ) { - $updated_object_post_id = \filter_input( \INPUT_POST, 'pronamic_review_object_post_id', \FILTER_VALIDATE_INT ); + $object_post_id = \filter_input( \INPUT_POST, 'pronamic_review_object_post_id', \FILTER_VALIDATE_INT ); - \update_post_meta( $post_id, '_pronamic_review_object_post_id', $updated_object_post_id ); + \update_post_meta( $post_id, '_pronamic_review_object_post_id', $object_post_id ); } // Ratings. - $post_type = \get_post_type( $prev_object_post_id ); + $post_type = \get_post_type( $object_post_id ); $rating_types = \pronamic_get_rating_types( $post_type ); @@ -161,39 +160,6 @@ public function save_review_post( $post_id ) { \update_post_meta( $post_id, '_pronamic_rating_value_' . $name, $ratings[ $name ] ); } } - - // Add category with object post ID for filtering in Query Loop block. - if ( ! empty( $updated_object_post_id ) ) { - \wp_remove_object_terms( $post_id, array( $prev_object_post_id ), 'category' ); - - $term = \get_terms( - array( - 'taxonomy' => 'category', - 'hide_empty' => false, - 'slug' => (string) $updated_object_post_id, - ) - ); - - if ( \is_array( $term ) ) { - $term = reset( $term ); - } - - if ( empty( $term ) ) { - $term = \wp_insert_term( - \get_the_title( $updated_object_post_id ), - 'category', - array( - 'slug' => $updated_object_post_id, - ) - ); - - $term = \get_term_by( 'id', $term['term_id'] ); - } - - if ( $term instanceof \WP_Term ) { - \wp_set_post_categories( $post_id, array( $term->term_id ), true ); - } - } } /**