| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Builders\Indexable_Builder; |
| 6 |
use Yoast\WP\SEO\Builders\Indexable_Link_Builder; |
| 7 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 8 |
use Yoast\WP\SEO\Helpers\Site_Helper; |
| 9 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 11 |
|
| 12 |
/** |
| 13 |
* Watches Terms/Taxonomies to fill the related Indexable. |
| 14 |
*/ |
| 15 |
class Indexable_Term_Watcher implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The indexable repository. |
| 19 |
* |
| 20 |
* @var Indexable_Repository |
| 21 |
*/ |
| 22 |
protected $repository; |
| 23 |
|
| 24 |
/** |
| 25 |
* The indexable builder. |
| 26 |
* |
| 27 |
* @var Indexable_Builder |
| 28 |
*/ |
| 29 |
protected $builder; |
| 30 |
|
| 31 |
/** |
| 32 |
* The link builder. |
| 33 |
* |
| 34 |
* @var Indexable_Link_Builder |
| 35 |
*/ |
| 36 |
protected $link_builder; |
| 37 |
|
| 38 |
/** |
| 39 |
* Represents the site helper. |
| 40 |
* |
| 41 |
* @var Site_Helper |
| 42 |
*/ |
| 43 |
protected $site; |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the conditionals based on which this loadable should be active. |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public static function get_conditionals() { |
| 51 |
return [ Migrations_Conditional::class ]; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Indexable_Term_Watcher constructor. |
| 56 |
* |
| 57 |
* @param Indexable_Repository $repository The repository to use. |
| 58 |
* @param Indexable_Builder $builder The post builder to use. |
| 59 |
* @param Indexable_Link_Builder $link_builder The lint builder to use. |
| 60 |
* @param Site_Helper $site The site helper. |
| 61 |
*/ |
| 62 |
public function __construct( |
| 63 |
Indexable_Repository $repository, |
| 64 |
Indexable_Builder $builder, |
| 65 |
Indexable_Link_Builder $link_builder, |
| 66 |
Site_Helper $site |
| 67 |
) { |
| 68 |
$this->repository = $repository; |
| 69 |
$this->builder = $builder; |
| 70 |
$this->link_builder = $link_builder; |
| 71 |
$this->site = $site; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers the hooks. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function register_hooks() { |
| 80 |
\add_action( 'created_term', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 81 |
\add_action( 'edited_term', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 82 |
\add_action( 'delete_term', [ $this, 'delete_indexable' ], \PHP_INT_MAX ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Deletes a term from the index. |
| 87 |
* |
| 88 |
* @param int $term_id The Term ID to delete. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function delete_indexable( $term_id ) { |
| 93 |
$indexable = $this->repository->find_by_id_and_type( $term_id, 'term', false ); |
| 94 |
|
| 95 |
if ( ! $indexable ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$indexable->delete(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update the taxonomy meta data on save. |
| 104 |
* |
| 105 |
* @param int $term_id ID of the term to save data for. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function build_indexable( $term_id ) { |
| 110 |
// Bail if this is a multisite installation and the site has been switched. |
| 111 |
if ( $this->site->is_multisite_and_switched() ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$term = \get_term( $term_id ); |
| 116 |
|
| 117 |
if ( $term === null || \is_wp_error( $term ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! \is_taxonomy_viewable( $term->taxonomy ) ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$indexable = $this->repository->find_by_id_and_type( $term_id, 'term', false ); |
| 126 |
|
| 127 |
// If we haven't found an existing indexable, create it. Otherwise update it. |
| 128 |
$indexable = $this->builder->build_for_id_and_type( $term_id, 'term', $indexable ); |
| 129 |
|
| 130 |
if ( ! $indexable ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Update links. |
| 135 |
$this->link_builder->build( $indexable, $term->description ); |
| 136 |
|
| 137 |
$indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) ); |
| 138 |
$indexable->save(); |
| 139 |
} |
| 140 |
} |
| 141 |
|