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