indexable-term-watcher.php
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Yoast\WP\SEO\Integrations\Watchers;
use Yoast\WP\SEO\Builders\Indexable_Builder;
use Yoast\WP\SEO\Builders\Indexable_Link_Builder;
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
use Yoast\WP\SEO\Helpers\Site_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
use Yoast\WP\SEO\Repositories\Indexable_Repository;
/**
* Watches Terms/Taxonomies to fill the related Indexable.
*/
class Indexable_Term_Watcher implements Integration_Interface {
/**
* The indexable repository.
*
* @var Indexable_Repository
*/
protected $repository;
/**
* The indexable builder.
*
* @var Indexable_Builder
*/
protected $builder;
/**
* The link builder.
*
* @var Indexable_Link_Builder
*/
protected $link_builder;
/**
* Represents the site helper.
*
* @var Site_Helper
*/
protected $site;
/**
* Returns the conditionals based on which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Migrations_Conditional::class ];
}
/**
* Indexable_Term_Watcher constructor.
*
* @param Indexable_Repository $repository The repository to use.
* @param Indexable_Builder $builder The post builder to use.
* @param Indexable_Link_Builder $link_builder The lint builder to use.
* @param Site_Helper $site The site helper.
*/
public function __construct(
Indexable_Repository $repository,
Indexable_Builder $builder,
Indexable_Link_Builder $link_builder,
Site_Helper $site
) {
$this->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();
\do_action( 'wpseo_indexable_deleted', $indexable );
}
/**
* 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 );
if ( ! $indexable ) {
return;
}
// Update links.
$this->link_builder->build( $indexable, $term->description );
$indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) );
$indexable->save();
}
}