| 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 |
/** |
| 140 |
* Remove the emoji script as it is incompatible with both React and any |
| 141 |
* contenteditable fields. |
| 142 |
*/ |
| 143 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 144 |
|
| 145 |
$asset_manager->localize_script( 'term-edit', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() ); |
| 146 |
|
| 147 |
$script_data = [ |
| 148 |
'analysis' => [ |
| 149 |
'plugins' => [ |
| 150 |
'replaceVars' => [ |
| 151 |
'no_parent_text' => __( '(no parent)', 'wordpress-seo' ), |
| 152 |
'replace_vars' => $this->get_replace_vars(), |
| 153 |
'recommended_replace_vars' => $this->get_recommended_replace_vars(), |
| 154 |
'scope' => $this->determine_scope(), |
| 155 |
], |
| 156 |
], |
| 157 |
'worker' => [ |
| 158 |
'url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-analysis-worker' ), |
| 159 |
'dependencies' => YoastSEO()->helpers->asset->get_dependency_urls_by_handle( 'yoast-seo-analysis-worker' ), |
| 160 |
'keywords_assessment_url' => YoastSEO()->helpers->asset->get_asset_url( 'yoast-seo-used-keywords-assessment' ), |
| 161 |
'log_level' => WPSEO_Utils::get_analysis_worker_log_level(), |
| 162 |
], |
| 163 |
], |
| 164 |
'media' => [ |
| 165 |
// @todo replace this translation with JavaScript translations. |
| 166 |
'choose_image' => __( 'Use Image', 'wordpress-seo' ), |
| 167 |
], |
| 168 |
'metabox' => $this->localize_term_scraper_script(), |
| 169 |
'userLanguageCode' => WPSEO_Language_Utils::get_language( \get_user_locale() ), |
| 170 |
'isTerm' => true, |
| 171 |
]; |
| 172 |
$asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data ); |
| 173 |
$asset_manager->enqueue_user_language_script(); |
| 174 |
} |
| 175 |
|
| 176 |
if ( self::is_term_overview( $pagenow ) ) { |
| 177 |
$asset_manager->enqueue_script( 'edit-page' ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Update the taxonomy meta data on save. |
| 183 |
* |
| 184 |
* @param int $term_id ID of the term to save data for. |
| 185 |
* @param int $tt_id The taxonomy_term_id for the term. |
| 186 |
* @param string $taxonomy The taxonomy the term belongs to. |
| 187 |
*/ |
| 188 |
public function update_term( $term_id, $tt_id, $taxonomy ) { |
| 189 |
// Bail if this is a multisite installation and the site has been switched. |
| 190 |
if ( is_multisite() && ms_is_switched() ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
/* Create post array with only our values. */ |
| 195 |
$new_meta_data = []; |
| 196 |
foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) { |
| 197 |
$posted_value = filter_input( INPUT_POST, $key ); |
| 198 |
if ( isset( $posted_value ) && $posted_value !== false ) { |
| 199 |
$new_meta_data[ $key ] = $posted_value; |
| 200 |
} |
| 201 |
|
| 202 |
// If analysis is disabled remove that analysis score value from the DB. |
| 203 |
if ( $this->is_meta_value_disabled( $key ) ) { |
| 204 |
$new_meta_data[ $key ] = ''; |
| 205 |
} |
| 206 |
} |
| 207 |
unset( $key, $default ); |
| 208 |
|
| 209 |
// Saving the values. |
| 210 |
WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Determines if the given meta value key is disabled. |
| 215 |
* |
| 216 |
* @param string $key The key of the meta value. |
| 217 |
* @return bool Whether the given meta value key is disabled. |
| 218 |
*/ |
| 219 |
public function is_meta_value_disabled( $key ) { |
| 220 |
if ( $key === 'wpseo_linkdex' && ! $this->analysis_seo->is_enabled() ) { |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
if ( $key === 'wpseo_content_score' && ! $this->analysis_readability->is_enabled() ) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Allows post-kses-filtered HTML in term descriptions. |
| 233 |
*/ |
| 234 |
public function custom_category_descriptions_allow_html() { |
| 235 |
remove_filter( 'term_description', 'wp_kses_data' ); |
| 236 |
remove_filter( 'pre_term_description', 'wp_filter_kses' ); |
| 237 |
add_filter( 'term_description', 'wp_kses_post' ); |
| 238 |
add_filter( 'pre_term_description', 'wp_filter_post_kses' ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Output the WordPress editor. |
| 243 |
*/ |
| 244 |
public function custom_category_description_editor() { |
| 245 |
wp_editor( '', 'description' ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Pass variables to js for use with the term-scraper. |
| 250 |
* |
| 251 |
* @return array |
| 252 |
*/ |
| 253 |
public function localize_term_scraper_script() { |
| 254 |
$term_id = filter_input( INPUT_GET, 'tag_ID' ); |
| 255 |
$term = get_term_by( 'id', $term_id, $this->get_taxonomy() ); |
| 256 |
$taxonomy = get_taxonomy( $term->taxonomy ); |
| 257 |
|
| 258 |
$term_formatter = new WPSEO_Metabox_Formatter( |
| 259 |
new WPSEO_Term_Metabox_Formatter( $taxonomy, $term ) |
| 260 |
); |
| 261 |
|
| 262 |
return $term_formatter->get_values(); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Pass some variables to js for replacing variables. |
| 267 |
* |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
public function localize_replace_vars_script() { |
| 271 |
return [ |
| 272 |
'no_parent_text' => __( '(no parent)', 'wordpress-seo' ), |
| 273 |
'replace_vars' => $this->get_replace_vars(), |
| 274 |
'recommended_replace_vars' => $this->get_recommended_replace_vars(), |
| 275 |
'scope' => $this->determine_scope(), |
| 276 |
]; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Determines the scope based on the current taxonomy. |
| 281 |
* This can be used by the replacevar plugin to determine if a replacement needs to be executed. |
| 282 |
* |
| 283 |
* @return string String decribing the current scope. |
| 284 |
*/ |
| 285 |
private function determine_scope() { |
| 286 |
$taxonomy = $this->get_taxonomy(); |
| 287 |
|
| 288 |
if ( $taxonomy === 'category' ) { |
| 289 |
return 'category'; |
| 290 |
} |
| 291 |
|
| 292 |
if ( $taxonomy === 'post_tag' ) { |
| 293 |
return 'tag'; |
| 294 |
} |
| 295 |
|
| 296 |
return 'term'; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Determines if a given page is the term overview page. |
| 301 |
* |
| 302 |
* @param string $page The string to check for the term overview page. |
| 303 |
* |
| 304 |
* @return bool |
| 305 |
*/ |
| 306 |
public static function is_term_overview( $page ) { |
| 307 |
return $page === 'edit-tags.php'; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Determines if a given page is the term edit page. |
| 312 |
* |
| 313 |
* @param string $page The string to check for the term edit page. |
| 314 |
* |
| 315 |
* @return bool |
| 316 |
*/ |
| 317 |
public static function is_term_edit( $page ) { |
| 318 |
return $page === 'term.php'; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Function to get the labels for the current taxonomy. |
| 323 |
* |
| 324 |
* @return object Labels for the current taxonomy. |
| 325 |
*/ |
| 326 |
public static function get_labels() { |
| 327 |
$term = filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, [ 'options' => [ 'default' => '' ] ] ); |
| 328 |
$taxonomy = get_taxonomy( $term ); |
| 329 |
|
| 330 |
return $taxonomy->labels; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Retrieves a template. |
| 335 |
* Check if metabox for current taxonomy should be displayed. |
| 336 |
* |
| 337 |
* @return bool |
| 338 |
*/ |
| 339 |
private function show_metabox() { |
| 340 |
$option_key = 'display-metabox-tax-' . $this->taxonomy; |
| 341 |
|
| 342 |
return WPSEO_Options::get( $option_key ); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Getting the taxonomy from the URL. |
| 347 |
* |
| 348 |
* @return string |
| 349 |
*/ |
| 350 |
private function get_taxonomy() { |
| 351 |
return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, [ 'options' => [ 'default' => '' ] ] ); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Prepares the replace vars for localization. |
| 356 |
* |
| 357 |
* @return array The replacement variables. |
| 358 |
*/ |
| 359 |
private function get_replace_vars() { |
| 360 |
$term_id = filter_input( INPUT_GET, 'tag_ID' ); |
| 361 |
$term = get_term_by( 'id', $term_id, $this->get_taxonomy() ); |
| 362 |
|
| 363 |
$cached_replacement_vars = []; |
| 364 |
|
| 365 |
$vars_to_cache = [ |
| 366 |
'date', |
| 367 |
'id', |
| 368 |
'sitename', |
| 369 |
'sitedesc', |
| 370 |
'sep', |
| 371 |
'page', |
| 372 |
'term_title', |
| 373 |
'term_description', |
| 374 |
'term_hierarchy', |
| 375 |
'category_description', |
| 376 |
'tag_description', |
| 377 |
'searchphrase', |
| 378 |
'currentyear', |
| 379 |
]; |
| 380 |
|
| 381 |
foreach ( $vars_to_cache as $var ) { |
| 382 |
$cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term ); |
| 383 |
} |
| 384 |
|
| 385 |
return $cached_replacement_vars; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Prepares the recommended replace vars for localization. |
| 390 |
* |
| 391 |
* @return array The recommended replacement variables. |
| 392 |
*/ |
| 393 |
private function get_recommended_replace_vars() { |
| 394 |
$recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars(); |
| 395 |
$taxonomy = filter_input( INPUT_GET, 'taxonomy' ); |
| 396 |
|
| 397 |
// What is recommended depends on the current context. |
| 398 |
$page_type = $recommended_replace_vars->determine_for_term( $taxonomy ); |
| 399 |
|
| 400 |
return $recommended_replace_vars->get_recommended_replacevars_for( $page_type ); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Adds custom category description editor. |
| 405 |
* Needs a hook that runs before the description field. Prior to WP version 4.5 we need to use edit_form as |
| 406 |
* term_edit_form_top was introduced in WP 4.5. This can be removed after <4.5 is no longer supported. |
| 407 |
* |
| 408 |
* @return void |
| 409 |
*/ |
| 410 |
private function insert_description_field_editor() { |
| 411 |
if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ) { |
| 412 |
add_action( "{$this->taxonomy}_edit_form", [ $this, 'custom_category_description_editor' ] ); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
add_action( "{$this->taxonomy}_term_edit_form_top", [ $this, 'custom_category_description_editor' ] ); |
| 417 |
} |
| 418 |
} |
| 419 |
|