repository = $repository; $this->builder = $builder; $this->link_builder = $link_builder; $this->site = $site; } /** * Registers the hooks. * * @return void */ public function register_hooks() { \add_action( 'created_term', [ $this, 'build_indexable' ], \PHP_INT_MAX ); \add_action( 'edited_term', [ $this, 'build_indexable' ], \PHP_INT_MAX ); \add_action( 'delete_term', [ $this, 'delete_indexable' ], \PHP_INT_MAX ); } /** * Deletes a term from the index. * * @param int $term_id The Term ID to delete. * * @return void */ public function delete_indexable( $term_id ) { $indexable = $this->repository->find_by_id_and_type( $term_id, 'term', false ); if ( ! $indexable ) { return; } $indexable->delete(); } /** * Update the taxonomy meta data on save. * * @param int $term_id ID of the term to save data for. * * @return void */ public function build_indexable( $term_id ) { // Bail if this is a multisite installation and the site has been switched. if ( $this->site->is_multisite_and_switched() ) { return; } $term = \get_term( $term_id ); if ( $term === null || \is_wp_error( $term ) ) { return; } if ( ! \is_taxonomy_viewable( $term->taxonomy ) ) { return; } $indexable = $this->repository->find_by_id_and_type( $term_id, 'term', false ); // If we haven't found an existing indexable, create it. Otherwise update it. $indexable = $this->builder->build_for_id_and_type( $term_id, 'term', $indexable ); // Update links. $this->link_builder->build( $indexable, $term->description ); $indexable->save(); } }