primary-term-watcher.php
4.32 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Yoast\WP\SEO\Integrations\Watchers;
use WP_Term;
use WPSEO_Meta;
use WPSEO_Primary_Term;
use Yoast\WP\SEO\Builders\Primary_Term_Builder;
use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
use Yoast\WP\SEO\Helpers\Site_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
/**
* Primary Term watcher.
*
* Watches Posts to save the primary term when set.
*/
class Primary_Term_Watcher implements Integration_Interface {
/**
* The primary term repository.
*
* @var Primary_Term_Repository
*/
protected $repository;
/**
* Represents the site helper.
*
* @var Site_Helper
*/
protected $site;
/**
* The primary term helper.
*
* @var Primary_Term_Helper
*/
protected $primary_term;
/**
* The primary term builder.
*
* @var Primary_Term_Builder
*/
protected $primary_term_builder;
/**
* Returns the conditionals based on which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Migrations_Conditional::class ];
}
/**
* Primary_Term_Watcher constructor.
*
* @codeCoverageIgnore It sets dependencies.
*
* @param Primary_Term_Repository $repository The primary term repository.
* @param Site_Helper $site The site helper.
* @param Primary_Term_Helper $primary_term The primary term helper.
* @param Primary_Term_Builder $primary_term_builder The primary term builder.
*/
public function __construct(
Primary_Term_Repository $repository,
Site_Helper $site,
Primary_Term_Helper $primary_term,
Primary_Term_Builder $primary_term_builder
) {
$this->repository = $repository;
$this->site = $site;
$this->primary_term = $primary_term;
$this->primary_term_builder = $primary_term_builder;
}
/**
* Initializes the integration.
*
* This is the place to register hooks and filters.
*/
public function register_hooks() {
\add_action( 'save_post', [ $this, 'save_primary_terms' ], \PHP_INT_MAX );
\add_action( 'delete_post', [ $this, 'delete_primary_terms' ] );
}
/**
* Saves all selected primary terms.
*
* @param int $post_id Post ID to save primary terms for.
*/
public function save_primary_terms( $post_id ) {
// Bail if this is a multisite installation and the site has been switched.
if ( $this->site->is_multisite_and_switched() ) {
return;
}
$taxonomies = $this->primary_term->get_primary_term_taxonomies( $post_id );
foreach ( $taxonomies as $taxonomy ) {
$this->save_primary_term( $post_id, $taxonomy );
}
$this->primary_term_builder->build( $post_id );
}
/**
* Saves the primary term for a specific taxonomy.
*
* @param int $post_id Post ID to save primary term for.
* @param WP_Term $taxonomy Taxonomy to save primary term for.
*/
protected function save_primary_term( $post_id, $taxonomy ) {
if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting to an integer.
$primary_term_id = (int) \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] );
if ( $primary_term_id <= 0 ) {
$primary_term = '';
}
else {
$primary_term = (string) $primary_term_id;
}
// We accept an empty string here because we need to save that if no terms are selected.
if ( \check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) !== null ) {
$primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id );
$primary_term_object->set_primary_term( $primary_term );
}
}
}
/**
* Deletes primary terms for a post.
*
* @param int $post_id The post to delete the terms of.
*
* @return void
*/
public function delete_primary_terms( $post_id ) {
foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) {
$primary_term = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy->name, false );
if ( ! $primary_term ) {
continue;
}
$primary_term->delete();
}
}
}