'rating', 'hp_listing' => 'listing_id', '_hp_vendor' => 'vendor_id', ); /** * Registers WordPress hooks for this interceptor. * * @return void */ public function register_hooks(): void { add_filter( 'update_post_metadata', array( $this, 'filter_update_meta' ), 10, 5 ); add_action( 'wp_insert_post', array( $this, 'action_insert_post' ), 10, 3 ); add_filter( 'get_post_metadata', array( $this, 'filter_get_meta' ), 10, 4 ); add_action( 'before_delete_post', array( $this, 'action_delete_post' ), 10, 2 ); } /** * Deletes review record when a post is deleted. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. * @return void */ public function action_delete_post( int $post_id, \WP_Post $post ): void { if ( 'hp_review' !== $post->post_type || ! $this->is_active() ) { return; } try { global $wpdb; $wpdb->delete( TMDO_DB::table( 'hpct_reviews' ), array( 'post_id' => $post_id ), array( '%d' ) ); } catch ( \Throwable $e ) { TMDO_Logger::error( $this->module, 'before_delete_post', $e->getMessage() ); } } /** * Filters update_post_metadata for hp_review posts. * * @param mixed $check Whether to short-circuit. * @param int $post_id Post ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. * @param mixed $prev_value Previous meta value. * @return mixed Filtered check value. */ public function filter_update_meta( $check, int $post_id, string $meta_key, $meta_value, $prev_value ) { if ( ! $this->is_hp_review( $post_id ) || ! isset( self::FIELD_MAP[ $meta_key ] ) ) { return $check; } if ( ! $this->is_active() ) { return $check; } try { global $wpdb; $col = self::FIELD_MAP[ $meta_key ]; $wpdb->query( $wpdb->prepare( 'UPDATE ' . TMDO_DB::table( 'hpct_reviews' ) . " SET `{$col}` = %s, updated_at = %s WHERE post_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared -- Table name from TMDO_DB::table(); column validated by FIELD_MAP constant. $meta_value, TMDO_DB::now(), $post_id ) ); } catch ( \Throwable $e ) { TMDO_Logger::error( $this->module, 'update_post_metadata', $e->getMessage() ); } return $check; } /** * Inserts a new review record when a post is inserted. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. * @param bool $update Whether this is an update. * @return void */ public function action_insert_post( int $post_id, \WP_Post $post, bool $update ): void { if ( $update || 'hp_review' !== $post->post_type || ! $this->is_active() ) { return; } try { global $wpdb; $now = TMDO_DB::now(); $wpdb->insert( TMDO_DB::table( 'hpct_reviews' ), 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, ), array( '%d', '%d', '%s', '%s', '%s', '%s', '%s' ) ); } catch ( \Throwable $e ) { TMDO_Logger::error( $this->module, 'wp_insert_post', $e->getMessage() ); } } /** * Filters get_post_metadata for hp_rating on hp_review posts. * * @param mixed $value Current value or null. * @param int $post_id Post ID. * @param string $meta_key Meta key. * @param bool $single Whether to return single value. * @return mixed Filtered value. */ public function filter_get_meta( $value, int $post_id, string $meta_key, bool $single ) { if ( ! $this->is_enabled() || 'hp_rating' !== $meta_key || ! $this->is_hp_review( $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', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table name from TMDO_DB::table(). $post_id ) ); return null !== $rating ? ( $single ? $rating : array( $rating ) ) : null; }, fn() => $value, 'get_post_metadata' ); } /** * Checks whether the post is an hp_review. * * @param int $post_id Post ID. * @return bool True if hp_review post type. */ private function is_hp_review( int $post_id ): bool { return 'hp_review' === get_post_type( $post_id ); } }