class-taxonomy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class that handles the edit boxes on taxonomy edit pages.
  9. */
  10. class WPSEO_Taxonomy {
  11. /**
  12. * The current active taxonomy.
  13. *
  14. * @var string
  15. */
  16. private $taxonomy = '';
  17. /**
  18. * Holds the metabox SEO analysis instance.
  19. *
  20. * @var WPSEO_Metabox_Analysis_SEO
  21. */
  22. private $analysis_seo;
  23. /**
  24. * Holds the metabox readability analysis instance.
  25. *
  26. * @var WPSEO_Metabox_Analysis_Readability
  27. */
  28. private $analysis_readability;
  29. /**
  30. * Class constructor.
  31. */
  32. public function __construct() {
  33. $this->taxonomy = $this->get_taxonomy();
  34. add_action( 'edit_term', [ $this, 'update_term' ], 99, 3 );
  35. add_action( 'init', [ $this, 'custom_category_descriptions_allow_html' ] );
  36. add_action( 'admin_init', [ $this, 'admin_init' ] );
  37. if ( self::is_term_overview( $GLOBALS['pagenow'] ) ) {
  38. new WPSEO_Taxonomy_Columns();
  39. }
  40. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  41. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  42. }
  43. /**
  44. * Add hooks late enough for taxonomy object to be available for checks.
  45. */
  46. public function admin_init() {
  47. $taxonomy = get_taxonomy( $this->taxonomy );
  48. if ( empty( $taxonomy ) || empty( $taxonomy->public ) || ! $this->show_metabox() ) {
  49. return;
  50. }
  51. $this->insert_description_field_editor();
  52. add_action( sanitize_text_field( $this->taxonomy ) . '_edit_form', [ $this, 'term_metabox' ], 90, 1 );
  53. add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
  54. }
  55. /**
  56. * Show the SEO inputs for term.
  57. *
  58. * @param stdClass|WP_Term $term Term to show the edit boxes for.
  59. */
  60. public function term_metabox( $term ) {
  61. if ( WPSEO_Metabox::is_internet_explorer() ) {
  62. $this->show_internet_explorer_notice();
  63. return;
  64. }
  65. $metabox = new WPSEO_Taxonomy_Metabox( $this->taxonomy, $term );
  66. $metabox->display();
  67. }
  68. /**
  69. * Renders the content for the internet explorer metabox.
  70. */
  71. private function show_internet_explorer_notice() {
  72. $product_title = 'Yoast SEO';
  73. if ( file_exists( WPSEO_PATH . 'premium/' ) ) {
  74. $product_title .= ' Premium';
  75. }
  76. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $product_title is hardcoded.
  77. printf( '<div id="wpseo_meta" class="postbox yoast wpseo-taxonomy-metabox-postbox"><h2><span>%1$s</span></h2>', $product_title );
  78. echo '<div class="inside">';
  79. echo '<div class="yoast-alert-box yoast-alert-box__warning">';
  80. echo '<span class="icon">';
  81. echo '<svg xmlns="http://www.w3.org/2000/svg" fill="#674E00" height="14px" width="14px" viewBox="0 0 576 512" role="img" aria-hidden="true" focusable="false"><path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"/></svg>';
  82. echo '</span>';
  83. echo '<div style="float: left">';
  84. printf(
  85. 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' ),
  86. '<a href="https://www.mozilla.org/firefox/new/">',
  87. '<a href="https://www.google.com/intl/nl/chrome/">',
  88. '<a href="https://www.microsoft.com/windows/microsoft-edge">',
  89. '</a>'
  90. );
  91. echo '</div></div>';
  92. echo '</div></div>';
  93. }
  94. /**
  95. * Queue assets for taxonomy screens.
  96. *
  97. * @since 1.5.0
  98. */
  99. public function admin_enqueue_scripts() {
  100. $pagenow = $GLOBALS['pagenow'];
  101. if ( ! ( self::is_term_edit( $pagenow ) || self::is_term_overview( $pagenow ) ) ) {
  102. return;
  103. }
  104. $asset_manager = new WPSEO_Admin_Asset_Manager();
  105. $asset_manager->enqueue_style( 'scoring' );
  106. $tag_id = filter_input( INPUT_GET, 'tag_ID' );
  107. if (
  108. self::is_term_edit( $pagenow ) &&
  109. ! empty( $tag_id ) // After we drop support for <4.5 this can be removed.
  110. ) {
  111. wp_enqueue_media(); // Enqueue files needed for upload functionality.
  112. $asset_manager->enqueue_style( 'metabox-css' );
  113. $asset_manager->enqueue_style( 'scoring' );
  114. $asset_manager->enqueue_script( 'metabox' );
  115. $asset_manager->enqueue_script( 'term-scraper' );
  116. $asset_manager->enqueue_script( 'admin-script' );
  117. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper', 'wpseoTermScraperL10n', $this->localize_term_scraper_script() );
  118. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  119. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper' );
  120. $analysis_worker_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ) );
  121. $used_keywords_assessment_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ), 'used-keywords-assessment' );
  122. $localization_data = [
  123. 'url' => $analysis_worker_location->get_url(
  124. $analysis_worker_location->get_asset(),
  125. WPSEO_Admin_Asset::TYPE_JS
  126. ),
  127. 'keywords_assessment_url' => $used_keywords_assessment_location->get_url(
  128. $used_keywords_assessment_location->get_asset(),
  129. WPSEO_Admin_Asset::TYPE_JS
  130. ),
  131. 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
  132. ];
  133. wp_localize_script(
  134. WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper',
  135. 'wpseoAnalysisWorkerL10n',
  136. $localization_data
  137. );
  138. /**
  139. * Remove the emoji script as it is incompatible with both React and any
  140. * contenteditable fields.
  141. */
  142. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  143. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  144. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', WPSEO_Language_Utils::get_language( WPSEO_Language_Utils::get_user_locale() ) );
  145. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
  146. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoFeaturesL10n', WPSEO_Utils::retrieve_enabled_features() );
  147. $asset_manager->enqueue_script( 'admin-media' );
  148. wp_localize_script(
  149. WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media',
  150. 'wpseoMediaL10n',
  151. [ 'choose_image' => __( 'Use Image', 'wordpress-seo' ) ]
  152. );
  153. }
  154. if ( self::is_term_overview( $pagenow ) ) {
  155. $asset_manager->enqueue_script( 'edit-page-script' );
  156. }
  157. }
  158. /**
  159. * Update the taxonomy meta data on save.
  160. *
  161. * @param int $term_id ID of the term to save data for.
  162. * @param int $tt_id The taxonomy_term_id for the term.
  163. * @param string $taxonomy The taxonomy the term belongs to.
  164. */
  165. public function update_term( $term_id, $tt_id, $taxonomy ) {
  166. if ( is_multisite() && ms_is_switched() ) {
  167. return;
  168. }
  169. /* Create post array with only our values. */
  170. $new_meta_data = [];
  171. foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) {
  172. $posted_value = filter_input( INPUT_POST, $key );
  173. if ( isset( $posted_value ) && $posted_value !== false ) {
  174. $new_meta_data[ $key ] = $posted_value;
  175. }
  176. // If analysis is disabled remove that analysis score value from the DB.
  177. if ( $this->is_meta_value_disabled( $key ) ) {
  178. $new_meta_data[ $key ] = '';
  179. }
  180. }
  181. unset( $key, $default );
  182. // Saving the values.
  183. WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data );
  184. }
  185. /**
  186. * Determines if the given meta value key is disabled.
  187. *
  188. * @param string $key The key of the meta value.
  189. * @return bool Whether the given meta value key is disabled.
  190. */
  191. public function is_meta_value_disabled( $key ) {
  192. if ( 'wpseo_linkdex' === $key && ! $this->analysis_seo->is_enabled() ) {
  193. return true;
  194. }
  195. if ( 'wpseo_content_score' === $key && ! $this->analysis_readability->is_enabled() ) {
  196. return true;
  197. }
  198. return false;
  199. }
  200. /**
  201. * Allows HTML in descriptions.
  202. */
  203. public function custom_category_descriptions_allow_html() {
  204. $filters = [
  205. 'pre_term_description',
  206. 'pre_link_description',
  207. 'pre_link_notes',
  208. 'pre_user_description',
  209. ];
  210. foreach ( $filters as $filter ) {
  211. remove_filter( $filter, 'wp_filter_kses' );
  212. if ( ! current_user_can( 'unfiltered_html' ) ) {
  213. add_filter( $filter, 'wp_filter_post_kses' );
  214. }
  215. }
  216. remove_filter( 'term_description', 'wp_kses_data' );
  217. }
  218. /**
  219. * Output the WordPress editor.
  220. */
  221. public function custom_category_description_editor() {
  222. wp_editor( '', 'description' );
  223. }
  224. /**
  225. * Pass variables to js for use with the term-scraper.
  226. *
  227. * @return array
  228. */
  229. public function localize_term_scraper_script() {
  230. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  231. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  232. $taxonomy = get_taxonomy( $term->taxonomy );
  233. $term_formatter = new WPSEO_Metabox_Formatter(
  234. new WPSEO_Term_Metabox_Formatter( $taxonomy, $term )
  235. );
  236. return $term_formatter->get_values();
  237. }
  238. /**
  239. * Pass some variables to js for replacing variables.
  240. */
  241. public function localize_replace_vars_script() {
  242. return [
  243. 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
  244. 'replace_vars' => $this->get_replace_vars(),
  245. 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
  246. 'scope' => $this->determine_scope(),
  247. ];
  248. }
  249. /**
  250. * Determines the scope based on the current taxonomy.
  251. * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
  252. *
  253. * @return string String decribing the current scope.
  254. */
  255. private function determine_scope() {
  256. $taxonomy = $this->get_taxonomy();
  257. if ( $taxonomy === 'category' ) {
  258. return 'category';
  259. }
  260. if ( $taxonomy === 'post_tag' ) {
  261. return 'tag';
  262. }
  263. return 'term';
  264. }
  265. /**
  266. * Determines if a given page is the term overview page.
  267. *
  268. * @param string $page The string to check for the term overview page.
  269. *
  270. * @return bool
  271. */
  272. public static function is_term_overview( $page ) {
  273. return 'edit-tags.php' === $page;
  274. }
  275. /**
  276. * Determines if a given page is the term edit page.
  277. *
  278. * @param string $page The string to check for the term edit page.
  279. *
  280. * @return bool
  281. */
  282. public static function is_term_edit( $page ) {
  283. return 'term.php' === $page;
  284. }
  285. /**
  286. * Retrieves a template.
  287. * Check if metabox for current taxonomy should be displayed.
  288. *
  289. * @return bool
  290. */
  291. private function show_metabox() {
  292. $option_key = 'display-metabox-tax-' . $this->taxonomy;
  293. return WPSEO_Options::get( $option_key );
  294. }
  295. /**
  296. * Getting the taxonomy from the URL.
  297. *
  298. * @return string
  299. */
  300. private function get_taxonomy() {
  301. return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, [ 'options' => [ 'default' => '' ] ] );
  302. }
  303. /**
  304. * Prepares the replace vars for localization.
  305. *
  306. * @return array The replacement variables.
  307. */
  308. private function get_replace_vars() {
  309. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  310. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  311. $cached_replacement_vars = [];
  312. $vars_to_cache = [
  313. 'date',
  314. 'id',
  315. 'sitename',
  316. 'sitedesc',
  317. 'sep',
  318. 'page',
  319. 'term_title',
  320. 'term_description',
  321. 'category_description',
  322. 'tag_description',
  323. 'searchphrase',
  324. 'currentyear',
  325. ];
  326. foreach ( $vars_to_cache as $var ) {
  327. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term );
  328. }
  329. return $cached_replacement_vars;
  330. }
  331. /**
  332. * Prepares the recommended replace vars for localization.
  333. *
  334. * @return array The recommended replacement variables.
  335. */
  336. private function get_recommended_replace_vars() {
  337. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  338. $taxonomy = filter_input( INPUT_GET, 'taxonomy' );
  339. // What is recommended depends on the current context.
  340. $page_type = $recommended_replace_vars->determine_for_term( $taxonomy );
  341. return $recommended_replace_vars->get_recommended_replacevars_for( $page_type );
  342. }
  343. /**
  344. * Adds custom category description editor.
  345. * Needs a hook that runs before the description field. Prior to WP version 4.5 we need to use edit_form as
  346. * term_edit_form_top was introduced in WP 4.5. This can be removed after <4.5 is no longer supported.
  347. *
  348. * @return {void}
  349. */
  350. private function insert_description_field_editor() {
  351. if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ) {
  352. add_action( "{$this->taxonomy}_edit_form", [ $this, 'custom_category_description_editor' ] );
  353. return;
  354. }
  355. add_action( "{$this->taxonomy}_term_edit_form_top", [ $this, 'custom_category_description_editor' ] );
  356. }
  357. /* ********************* DEPRECATED METHODS ********************* */
  358. /**
  359. * Adds shortcode support to category descriptions.
  360. *
  361. * @deprecated 7.9.0
  362. * @codeCoverageIgnore
  363. *
  364. * @param string $desc String to add shortcodes in.
  365. *
  366. * @return string Content with shortcodes filtered out.
  367. */
  368. public function custom_category_descriptions_add_shortcode_support( $desc ) {
  369. _deprecated_function( __FUNCTION__, 'WPSEO 7.9.0', 'WPSEO_Frontend::custom_category_descriptions_add_shortcode_support' );
  370. $frontend = WPSEO_Frontend::get_instance();
  371. return $frontend->custom_category_descriptions_add_shortcode_support( $desc );
  372. }
  373. }