options_helper = $options_helper; $this->primary_term_repository = $primary_term_repository; $this->post_type_helper = $post_type_helper; $this->indexable_repository = $indexable_repository; $this->indexable_hierarchy_builder = $indexable_hierarchy_builder; } /** * Initializes the integration. * * This is the place to register hooks and filters. * * @return void */ public function register_hooks() { \add_action( 'set_object_terms', [ $this, 'validate_primary_category' ], 10, 4 ); } /** * Returns the conditionals based on which this loadable should be active. * * @return array The conditionals. */ public static function get_conditionals() { return [ Migrations_Conditional::class, Doing_Post_Quick_Edit_Save_Conditional::class ]; } /** * Validates if the current primary category is still present. If not just remove the post meta for it. * * @param int $object_id Object ID. * @param array $terms Unused. An array of object terms. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. */ public function validate_primary_category( $object_id, $terms, $tt_ids, $taxonomy ) { $post = \get_post( $object_id ); if ( $post === null ) { return; } $main_taxonomy = $this->options_helper->get( 'post_types-' . $post->post_type . '-maintax' ); if ( ! $main_taxonomy || $main_taxonomy === '0' ) { return; } if ( $main_taxonomy !== $taxonomy ) { return; } $primary_category = $this->get_primary_term_id( $post->ID, $main_taxonomy ); if ( $primary_category === false ) { return; } // The primary category isn't removed. if ( \in_array( (string) $primary_category, $tt_ids, true ) ) { return; } $this->remove_primary_term( $post->ID, $main_taxonomy ); // Rebuild the post hierarchy for this post now the primary term has been changed. $this->build_post_hierarchy( $post ); } /** * Returns the primary term id of a post. * * @param int $post_id The post ID. * @param string $main_taxonomy The main taxonomy. * * @return int|false The ID of the primary term, or `false` if the post ID is invalid. */ private function get_primary_term_id( $post_id, $main_taxonomy ) { $primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); if ( $primary_term ) { return $primary_term->term_id; } return \get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy, true ); } /** * Removes the primary category. * * @param int $post_id The post id to set primary taxonomy for. * @param string $main_taxonomy Name of the taxonomy that is set to be the primary one. */ private function remove_primary_term( $post_id, $main_taxonomy ) { $primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); if ( $primary_term ) { $primary_term->delete(); } // Remove it from the post meta. \delete_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy ); } /** * Builds the hierarchy for a post. * * @param \WP_Post $post The post. */ public function build_post_hierarchy( $post ) { if ( $this->post_type_helper->is_excluded( $post->post_type ) ) { return; } $indexable = $this->indexable_repository->find_by_id_and_type( $post->ID, 'post' ); if ( $indexable instanceof Indexable ) { $this->indexable_hierarchy_builder->build( $indexable ); } } } // phpcs:enable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded