'rating', 'hp_listing' => 'listing_id', '_hp_vendor' => 'vendor_id', ); /** * Returns the WordPress post_type slug handled by this interceptor. * * @return string */ protected function get_post_type(): string { return 'hp_review'; } /** * Returns the table key passed to TMDO_DB::table(). * * @return string */ protected function get_table_key(): string { return 'hpct_reviews'; } /** * Registers WordPress hooks, including the extra get_post_metadata hook. * * @return void */ public function register_hooks(): void { parent::register_hooks(); add_filter( 'get_post_metadata', array( $this, 'filter_get_meta' ), 10, 4 ); } /** * Builds the INSERT row for a newly created review post. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. * @param string $now Formatted datetime string. * @return array{values: array, formats: array} */ protected function build_insert_data( int $post_id, \WP_Post $post, string $now ): array { return array( 'values' => array( 'post_id' => $post_id, 'user_id' => (int) $post->post_author, 'status' => $post->post_status, 'title' => sanitize_text_field( $post->post_title ), 'content' => $post->post_content, 'created_at' => $now, 'updated_at' => $now, ), 'formats' => array( '%d', '%d', '%s', '%s', '%s', '%s', '%s' ), ); } /** * Reads hp_rating directly from the reviews flat table. * * @param mixed $value Short-circuit value. * @param int $post_id Post ID. * @param string $meta_key Meta key. * @param bool $single Whether to return a single value. * @return mixed */ public function filter_get_meta( $value, int $post_id, string $meta_key, bool $single ) { if ( ! $this->is_enabled() || 'hp_rating' !== $meta_key || 'hp_review' !== get_post_type( $post_id ) ) { return $value; } return $this->intercept( function () use ( $post_id, $single ) { global $wpdb; $rating = $wpdb->get_var( $wpdb->prepare( 'SELECT rating FROM ' . TMDO_DB::table( 'hpct_reviews' ) . ' WHERE post_id = %d LIMIT 1', $post_id ) ); return null !== $rating ? ( $single ? $rating : array( $rating ) ) : null; }, fn() => $value, 'get_post_metadata' ); } }