PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.6 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 All 130 releases
wordpress-seo / admin / taxonomy / class-taxonomy.php

class-taxonomy.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at admin/taxonomy/class-taxonomy.php

422 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
9
10 /**
11 * Class that handles the edit boxes on taxonomy edit pages.
12 */
13 class WPSEO_Taxonomy {
14
15 /**
16 * The current active taxonomy.
17 *
18 * @var string
19 */
20 private $taxonomy = '';
21
22 /**
23 * Holds the metabox SEO analysis instance.
24 *
25 * @var WPSEO_Metabox_Analysis_SEO
26 */
27 private $analysis_seo;
28
29 /**
30 * Holds the metabox readability analysis instance.
31 *
32 * @var WPSEO_Metabox_Analysis_Readability
33 */
34 private $analysis_readability;
35
36 /**
37 * Class constructor.
38 */
39 public function __construct() {
40 $this->taxonomy = $this->get_taxonomy();
41
42 add_action( 'edit_term', [ $this, 'update_term' ], 99, 3 );
43 add_action( 'init', [ $this, 'custom_category_descriptions_allow_html' ] );
44 add_action( 'admin_init', [ $this, 'admin_init' ] );
45
46 if ( self::is_term_overview( $GLOBALS['pagenow'] ) ) {
47 new WPSEO_Taxonomy_Columns();
48 }
49 $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO();
50 $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
51 }
52
53 /**
54 * Add hooks late enough for taxonomy object to be available for checks.
55 */
56 public function admin_init() {
57
58 $taxonomy = get_taxonomy( $this->taxonomy );
59
60 if ( empty( $taxonomy ) || empty( $taxonomy->public ) || ! $this->show_metabox() ) {
61 return;
62 }
63
64 $this->insert_description_field_editor();
65
66 add_action( sanitize_text_field( $this->taxonomy ) . '_edit_form', [ $this, 'term_metabox' ], 90, 1 );
67 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
68 }
69
70 /**
71 * Show the SEO inputs for term.
72 *
73 * @param stdClass|WP_Term $term Term to show the edit boxes for.
74 */
75 public function term_metabox( $term ) {
76 if ( WPSEO_Metabox::is_internet_explorer() ) {
77 $this->show_internet_explorer_notice();
78 return;
79 }
80
81 $metabox = new WPSEO_Taxonomy_Metabox( $this->taxonomy, $term );
82 $metabox->display();
83 }
84
85 /**
86 * Renders the content for the internet explorer metabox.
87 *
88 * @return void
89 */
90 private function show_internet_explorer_notice() {
91 $product_title = YoastSEO()->helpers->product->get_product_name();
92
93 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $product_title is hardcoded.
94 printf( '<div id="wpseo_meta" class="postbox yoast wpseo-taxonomy-metabox-postbox"><h2><span>%1$s</span></h2>', $product_title );
95 echo '<div class="inside">';
96
97 $content = sprintf(
98 /* translators: 1: Link start tag to the Firefox website, 2: Link start tag to the Chrome website, 3: Link start tag to the Edge website, 4: Link closing tag. */
99 esc_html__( 'The browser you are currently using is unfortunately rather dated. Since we strive to give you the best experience possible, we no longer support this browser. Instead, please use %1$sFirefox%4$s, %2$sChrome%4$s or %3$sMicrosoft Edge%4$s.', 'wordpress-seo' ),
100 '<a href="https://www.mozilla.org/firefox/new/">',
101 '<a href="https://www.google.com/chrome/">',
102 '<a href="https://www.microsoft.com/windows/microsoft-edge">',
103 '</a>'
104 );
105 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped above.
106 echo new Alert_Presenter( $content );
107
108 echo '</div></div>';
109 }
110
111 /**
112 * Queue assets for taxonomy screens.
113 *
114 * @since 1.5.0
115 */
116 public function admin_enqueue_scripts() {
117
118 $pagenow = $GLOBALS['pagenow'];
119
120 if ( ! ( self::is_term_edit( $pagenow ) || self::is_term_overview( $pagenow ) ) ) {
121 return;
122 }
123
124 $asset_manager = new WPSEO_Admin_Asset_Manager();
125 $asset_manager->enqueue_style( 'scoring' );
126 $asset_manager->enqueue_style( 'monorepo' );
127
128 $tag_id = filter_input( INPUT_GET, 'tag_ID' );
129 if (
130 self::is_term_edit( $pagenow )
131 && ! empty( $tag_id ) // After we drop support for <4.5 this can be removed.
132 ) {
133 wp_enqueue_media(); // Enqueue files needed for upload functionality.
134
135 $asset_manager->enqueue_style( 'metabox-css' );
136 $asset_manager->enqueue_style( 'scoring' );
137 $asset_manager->enqueue_script( 'term-edit' );
138
139 $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
140 $yoast_components_l10n->localize_script( 'term-edit' );
141
142 /**
143 * Remove the emoji script as it is incompatible with both React and any
144 * contenteditable fields.
145 */
146 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
147
148 $asset_manager->localize_script( 'term-edit', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
149
150 $script_data = [
151 'analysis' => [
152 'plugins' => [
153 'replaceVars' => [
154 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
155 'replace_vars' => $this->get_replace_vars(),
156 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
157 'scope' => $this->determine_scope(),
158 ],
159 ],
160 'worker' => [
161 'url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ),
162 'dependencies' => YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ),
163 'keywords_assessment_url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ),
164 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
165 ],
166 ],
167 'media' => [
168 // @todo replace this translation with JavaScript translations.
169 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
170 ],
171 'metabox' => $this->localize_term_scraper_script(),
172 'userLanguageCode' => WPSEO_Language_Utils::get_language( \get_user_locale() ),
173 'isTerm' => true,
174 ];
175 $asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data );
176 $asset_manager->enqueue_user_language_script();
177 }
178
179 if ( self::is_term_overview( $pagenow ) ) {
180 $asset_manager->enqueue_script( 'edit-page' );
181 }
182 }
183
184 /**
185 * Update the taxonomy meta data on save.
186 *
187 * @param int $term_id ID of the term to save data for.
188 * @param int $tt_id The taxonomy_term_id for the term.
189 * @param string $taxonomy The taxonomy the term belongs to.
190 */
191 public function update_term( $term_id, $tt_id, $taxonomy ) {
192 // Bail if this is a multisite installation and the site has been switched.
193 if ( is_multisite() && ms_is_switched() ) {
194 return;
195 }
196
197 /* Create post array with only our values. */
198 $new_meta_data = [];
199 foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) {
200 $posted_value = filter_input( INPUT_POST, $key );
201 if ( isset( $posted_value ) && $posted_value !== false ) {
202 $new_meta_data[ $key ] = $posted_value;
203 }
204
205 // If analysis is disabled remove that analysis score value from the DB.
206 if ( $this->is_meta_value_disabled( $key ) ) {
207 $new_meta_data[ $key ] = '';
208 }
209 }
210 unset( $key, $default );
211
212 // Saving the values.
213 WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data );
214 }
215
216 /**
217 * Determines if the given meta value key is disabled.
218 *
219 * @param string $key The key of the meta value.
220 * @return bool Whether the given meta value key is disabled.
221 */
222 public function is_meta_value_disabled( $key ) {
223 if ( $key === 'wpseo_linkdex' && ! $this->analysis_seo->is_enabled() ) {
224 return true;
225 }
226
227 if ( $key === 'wpseo_content_score' && ! $this->analysis_readability->is_enabled() ) {
228 return true;
229 }
230
231 return false;
232 }
233
234 /**
235 * Allows post-kses-filtered HTML in term descriptions.
236 */
237 public function custom_category_descriptions_allow_html() {
238 remove_filter( 'term_description', 'wp_kses_data' );
239 remove_filter( 'pre_term_description', 'wp_filter_kses' );
240 add_filter( 'term_description', 'wp_kses_post' );
241 add_filter( 'pre_term_description', 'wp_filter_post_kses' );
242 }
243
244 /**
245 * Output the WordPress editor.
246 */
247 public function custom_category_description_editor() {
248 wp_editor( '', 'description' );
249 }
250
251 /**
252 * Pass variables to js for use with the term-scraper.
253 *
254 * @return array
255 */
256 public function localize_term_scraper_script() {
257 $term_id = filter_input( INPUT_GET, 'tag_ID' );
258 $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
259 $taxonomy = get_taxonomy( $term->taxonomy );
260
261 $term_formatter = new WPSEO_Metabox_Formatter(
262 new WPSEO_Term_Metabox_Formatter( $taxonomy, $term )
263 );
264
265 return $term_formatter->get_values();
266 }
267
268 /**
269 * Pass some variables to js for replacing variables.
270 *
271 * @return array
272 */
273 public function localize_replace_vars_script() {
274 return [
275 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
276 'replace_vars' => $this->get_replace_vars(),
277 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
278 'scope' => $this->determine_scope(),
279 ];
280 }
281
282 /**
283 * Determines the scope based on the current taxonomy.
284 * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
285 *
286 * @return string String decribing the current scope.
287 */
288 private function determine_scope() {
289 $taxonomy = $this->get_taxonomy();
290
291 if ( $taxonomy === 'category' ) {
292 return 'category';
293 }
294
295 if ( $taxonomy === 'post_tag' ) {
296 return 'tag';
297 }
298
299 return 'term';
300 }
301
302 /**
303 * Determines if a given page is the term overview page.
304 *
305 * @param string $page The string to check for the term overview page.
306 *
307 * @return bool
308 */
309 public static function is_term_overview( $page ) {
310 return $page === 'edit-tags.php';
311 }
312
313 /**
314 * Determines if a given page is the term edit page.
315 *
316 * @param string $page The string to check for the term edit page.
317 *
318 * @return bool
319 */
320 public static function is_term_edit( $page ) {
321 return $page === 'term.php';
322 }
323
324 /**
325 * Function to get the labels for the current taxonomy.
326 *
327 * @return object Labels for the current taxonomy.
328 */
329 public static function get_labels() {
330 $term = filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, [ 'options' => [ 'default' => '' ] ] );
331 $taxonomy = get_taxonomy( $term );
332
333 return $taxonomy->labels;
334 }
335
336 /**
337 * Retrieves a template.
338 * Check if metabox for current taxonomy should be displayed.
339 *
340 * @return bool
341 */
342 private function show_metabox() {
343 $option_key = 'display-metabox-tax-' . $this->taxonomy;
344
345 return WPSEO_Options::get( $option_key );
346 }
347
348 /**
349 * Getting the taxonomy from the URL.
350 *
351 * @return string
352 */
353 private function get_taxonomy() {
354 return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, [ 'options' => [ 'default' => '' ] ] );
355 }
356
357 /**
358 * Prepares the replace vars for localization.
359 *
360 * @return array The replacement variables.
361 */
362 private function get_replace_vars() {
363 $term_id = filter_input( INPUT_GET, 'tag_ID' );
364 $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
365
366 $cached_replacement_vars = [];
367
368 $vars_to_cache = [
369 'date',
370 'id',
371 'sitename',
372 'sitedesc',
373 'sep',
374 'page',
375 'term_title',
376 'term_description',
377 'term_hierarchy',
378 'category_description',
379 'tag_description',
380 'searchphrase',
381 'currentyear',
382 ];
383
384 foreach ( $vars_to_cache as $var ) {
385 $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term );
386 }
387
388 return $cached_replacement_vars;
389 }
390
391 /**
392 * Prepares the recommended replace vars for localization.
393 *
394 * @return array The recommended replacement variables.
395 */
396 private function get_recommended_replace_vars() {
397 $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
398 $taxonomy = filter_input( INPUT_GET, 'taxonomy' );
399
400 // What is recommended depends on the current context.
401 $page_type = $recommended_replace_vars->determine_for_term( $taxonomy );
402
403 return $recommended_replace_vars->get_recommended_replacevars_for( $page_type );
404 }
405
406 /**
407 * Adds custom category description editor.
408 * Needs a hook that runs before the description field. Prior to WP version 4.5 we need to use edit_form as
409 * term_edit_form_top was introduced in WP 4.5. This can be removed after <4.5 is no longer supported.
410 *
411 * @return void
412 */
413 private function insert_description_field_editor() {
414 if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ) {
415 add_action( "{$this->taxonomy}_edit_form", [ $this, 'custom_category_description_editor' ] );
416 return;
417 }
418
419 add_action( "{$this->taxonomy}_term_edit_form_top", [ $this, 'custom_category_description_editor' ] );
420 }
421 }
422