PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / watchers / primary-term-watcher.php

primary-term-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/integrations/watchers/primary-term-watcher.php

162 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Watchers;
4
5 use WP_Term;
6 use WPSEO_Meta;
7 use WPSEO_Primary_Term;
8 use Yoast\WP\SEO\Builders\Primary_Term_Builder;
9 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
10 use Yoast\WP\SEO\Helpers\Primary_Term_Helper;
11 use Yoast\WP\SEO\Helpers\Site_Helper;
12 use Yoast\WP\SEO\Integrations\Integration_Interface;
13 use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
14
15 /**
16 * Primary Term watcher.
17 *
18 * Watches Posts to save the primary term when set.
19 */
20 class Primary_Term_Watcher implements Integration_Interface {
21
22 /**
23 * The primary term repository.
24 *
25 * @var Primary_Term_Repository
26 */
27 protected $repository;
28
29 /**
30 * Represents the site helper.
31 *
32 * @var Site_Helper
33 */
34 protected $site;
35
36 /**
37 * The primary term helper.
38 *
39 * @var Primary_Term_Helper
40 */
41 protected $primary_term;
42
43 /**
44 * The primary term builder.
45 *
46 * @var Primary_Term_Builder
47 */
48 protected $primary_term_builder;
49
50 /**
51 * Returns the conditionals based on which this loadable should be active.
52 *
53 * @return array
54 */
55 public static function get_conditionals() {
56 return [ Migrations_Conditional::class ];
57 }
58
59 /**
60 * Primary_Term_Watcher constructor.
61 *
62 * @codeCoverageIgnore It sets dependencies.
63 *
64 * @param Primary_Term_Repository $repository The primary term repository.
65 * @param Site_Helper $site The site helper.
66 * @param Primary_Term_Helper $primary_term The primary term helper.
67 * @param Primary_Term_Builder $primary_term_builder The primary term builder.
68 */
69 public function __construct(
70 Primary_Term_Repository $repository,
71 Site_Helper $site,
72 Primary_Term_Helper $primary_term,
73 Primary_Term_Builder $primary_term_builder
74 ) {
75 $this->repository = $repository;
76 $this->site = $site;
77 $this->primary_term = $primary_term;
78 $this->primary_term_builder = $primary_term_builder;
79 }
80
81 /**
82 * Initializes the integration.
83 *
84 * This is the place to register hooks and filters.
85 *
86 * @return void
87 */
88 public function register_hooks() {
89 \add_action( 'save_post', [ $this, 'save_primary_terms' ], \PHP_INT_MAX );
90 \add_action( 'delete_post', [ $this, 'delete_primary_terms' ] );
91 }
92
93 /**
94 * Saves all selected primary terms.
95 *
96 * @param int $post_id Post ID to save primary terms for.
97 *
98 * @return void
99 */
100 public function save_primary_terms( $post_id ) {
101 // Bail if this is a multisite installation and the site has been switched.
102 if ( $this->site->is_multisite_and_switched() ) {
103 return;
104 }
105
106 $taxonomies = $this->primary_term->get_primary_term_taxonomies( $post_id );
107
108 foreach ( $taxonomies as $taxonomy ) {
109 $this->save_primary_term( $post_id, $taxonomy );
110 }
111
112 $this->primary_term_builder->build( $post_id );
113 }
114
115 /**
116 * Saves the primary term for a specific taxonomy.
117 *
118 * @param int $post_id Post ID to save primary term for.
119 * @param WP_Term $taxonomy Taxonomy to save primary term for.
120 *
121 * @return void
122 */
123 protected function save_primary_term( $post_id, $taxonomy ) {
124 if ( isset( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) && \is_string( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] ) ) {
125 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting to an integer.
126 $primary_term_id = (int) \wp_unslash( $_POST[ WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term' ] );
127
128 if ( $primary_term_id <= 0 ) {
129 $primary_term = '';
130 }
131 else {
132 $primary_term = (string) $primary_term_id;
133 }
134
135 // We accept an empty string here because we need to save that if no terms are selected.
136 if ( \check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) !== null ) {
137 $primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id );
138 $primary_term_object->set_primary_term( $primary_term );
139 }
140 }
141 }
142
143 /**
144 * Deletes primary terms for a post.
145 *
146 * @param int $post_id The post to delete the terms of.
147 *
148 * @return void
149 */
150 public function delete_primary_terms( $post_id ) {
151 foreach ( $this->primary_term->get_primary_term_taxonomies( $post_id ) as $taxonomy ) {
152 $primary_term_indexable = $this->repository->find_by_post_id_and_taxonomy( $post_id, $taxonomy->name, false );
153
154 if ( ! $primary_term_indexable ) {
155 continue;
156 }
157
158 $primary_term_indexable->delete();
159 }
160 }
161 }
162