class-taxonomy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. * @var WPSEO_Metabox_Analysis_SEO
  19. */
  20. private $analysis_seo;
  21. /**
  22. * @var WPSEO_Metabox_Analysis_Readability
  23. */
  24. private $analysis_readability;
  25. /**
  26. * Class constructor.
  27. */
  28. public function __construct() {
  29. $this->taxonomy = $this->get_taxonomy();
  30. add_action( 'edit_term', array( $this, 'update_term' ), 99, 3 );
  31. add_action( 'init', array( $this, 'custom_category_descriptions_allow_html' ) );
  32. add_action( 'admin_init', array( $this, 'admin_init' ) );
  33. if ( self::is_term_overview( $GLOBALS['pagenow'] ) ) {
  34. new WPSEO_Taxonomy_Columns();
  35. }
  36. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  37. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  38. }
  39. /**
  40. * Add hooks late enough for taxonomy object to be available for checks.
  41. */
  42. public function admin_init() {
  43. $taxonomy = get_taxonomy( $this->taxonomy );
  44. if ( empty( $taxonomy ) || empty( $taxonomy->public ) || ! $this->show_metabox() ) {
  45. return;
  46. }
  47. $this->insert_description_field_editor();
  48. add_action( sanitize_text_field( $this->taxonomy ) . '_edit_form', array( $this, 'term_metabox' ), 90, 1 );
  49. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  50. }
  51. /**
  52. * Show the SEO inputs for term.
  53. *
  54. * @param stdClass|WP_Term $term Term to show the edit boxes for.
  55. */
  56. public function term_metabox( $term ) {
  57. $tab = new WPSEO_Help_Center_Template_Variables_Tab();
  58. $tab->register_hooks();
  59. $metabox = new WPSEO_Taxonomy_Metabox( $this->taxonomy, $term );
  60. $metabox->display();
  61. }
  62. /**
  63. * Queue assets for taxonomy screens.
  64. *
  65. * @since 1.5.0
  66. */
  67. public function admin_enqueue_scripts() {
  68. $pagenow = $GLOBALS['pagenow'];
  69. if ( ! ( self::is_term_edit( $pagenow ) || self::is_term_overview( $pagenow ) ) ) {
  70. return;
  71. }
  72. $asset_manager = new WPSEO_Admin_Asset_Manager();
  73. $asset_manager->enqueue_style( 'scoring' );
  74. $tab = new WPSEO_Help_Center_Template_Variables_Tab();
  75. $tab->enqueue_assets();
  76. $tag_id = filter_input( INPUT_GET, 'tag_ID' );
  77. if (
  78. self::is_term_edit( $pagenow ) &&
  79. ! empty( $tag_id ) // After we drop support for <4.5 this can be removed.
  80. ) {
  81. wp_enqueue_media(); // Enqueue files needed for upload functionality.
  82. $asset_manager->enqueue_style( 'metabox-css' );
  83. $asset_manager->enqueue_style( 'scoring' );
  84. $asset_manager->enqueue_script( 'metabox' );
  85. $asset_manager->enqueue_script( 'term-scraper' );
  86. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper', 'wpseoTermScraperL10n', $this->localize_term_scraper_script() );
  87. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  88. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper' );
  89. $analysis_worker_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ) );
  90. $used_keywords_assessment_location = new WPSEO_Admin_Asset_Analysis_Worker_Location( $asset_manager->flatten_version( WPSEO_VERSION ), 'used-keywords-assessment' );
  91. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper', 'wpseoAnalysisWorkerL10n', array(
  92. 'url' => $analysis_worker_location->get_url( $analysis_worker_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  93. 'keywords_assessment_url' => $used_keywords_assessment_location->get_url( $used_keywords_assessment_location->get_asset(), WPSEO_Admin_Asset::TYPE_JS ),
  94. ) );
  95. /**
  96. * Remove the emoji script as it is incompatible with both React and any
  97. * contenteditable fields.
  98. */
  99. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  100. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  101. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', WPSEO_Utils::get_language( WPSEO_Utils::get_user_locale() ) );
  102. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
  103. $asset_manager->enqueue_script( 'admin-media' );
  104. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', array(
  105. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  106. ) );
  107. }
  108. if ( self::is_term_overview( $pagenow ) ) {
  109. $asset_manager->enqueue_script( 'edit-page-script' );
  110. }
  111. }
  112. /**
  113. * Update the taxonomy meta data on save.
  114. *
  115. * @param int $term_id ID of the term to save data for.
  116. * @param int $tt_id The taxonomy_term_id for the term.
  117. * @param string $taxonomy The taxonomy the term belongs to.
  118. */
  119. public function update_term( $term_id, $tt_id, $taxonomy ) {
  120. /* Create post array with only our values. */
  121. $new_meta_data = array();
  122. foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) {
  123. $posted_value = filter_input( INPUT_POST, $key );
  124. if ( isset( $posted_value ) && $posted_value !== false ) {
  125. $new_meta_data[ $key ] = $posted_value;
  126. }
  127. // If analysis is disabled remove that analysis score value from the DB.
  128. if ( $this->is_meta_value_disabled( $key ) ) {
  129. $new_meta_data[ $key ] = '';
  130. }
  131. }
  132. unset( $key, $default );
  133. // Saving the values.
  134. WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data );
  135. }
  136. /**
  137. * Determines if the given meta value key is disabled.
  138. *
  139. * @param string $key The key of the meta value.
  140. * @return bool Whether the given meta value key is disabled.
  141. */
  142. public function is_meta_value_disabled( $key ) {
  143. if ( 'wpseo_linkdex' === $key && ! $this->analysis_seo->is_enabled() ) {
  144. return true;
  145. }
  146. if ( 'wpseo_content_score' === $key && ! $this->analysis_readability->is_enabled() ) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Allows HTML in descriptions.
  153. */
  154. public function custom_category_descriptions_allow_html() {
  155. $filters = array(
  156. 'pre_term_description',
  157. 'pre_link_description',
  158. 'pre_link_notes',
  159. 'pre_user_description',
  160. );
  161. foreach ( $filters as $filter ) {
  162. remove_filter( $filter, 'wp_filter_kses' );
  163. }
  164. remove_filter( 'term_description', 'wp_kses_data' );
  165. }
  166. /**
  167. * Output the WordPress editor.
  168. */
  169. public function custom_category_description_editor() {
  170. wp_editor( '', 'description' );
  171. }
  172. /**
  173. * Pass variables to js for use with the term-scraper.
  174. *
  175. * @return array
  176. */
  177. public function localize_term_scraper_script() {
  178. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  179. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  180. $taxonomy = get_taxonomy( $term->taxonomy );
  181. $term_formatter = new WPSEO_Metabox_Formatter(
  182. new WPSEO_Term_Metabox_Formatter( $taxonomy, $term )
  183. );
  184. return $term_formatter->get_values();
  185. }
  186. /**
  187. * Pass some variables to js for replacing variables.
  188. */
  189. public function localize_replace_vars_script() {
  190. return array(
  191. 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
  192. 'replace_vars' => $this->get_replace_vars(),
  193. 'recommended_replace_vars' => $this->get_recommended_replace_vars(),
  194. 'scope' => $this->determine_scope(),
  195. );
  196. }
  197. /**
  198. * Determines the scope based on the current taxonomy.
  199. * This can be used by the replacevar plugin to determine if a replacement needs to be executed.
  200. *
  201. * @return string String decribing the current scope.
  202. */
  203. private function determine_scope() {
  204. $taxonomy = $this->get_taxonomy();
  205. if ( $taxonomy === 'category' ) {
  206. return 'category';
  207. }
  208. if ( $taxonomy === 'post_tag' ) {
  209. return 'tag';
  210. }
  211. return 'term';
  212. }
  213. /**
  214. * @param string $page The string to check for the term overview page.
  215. *
  216. * @return bool
  217. */
  218. public static function is_term_overview( $page ) {
  219. return 'edit-tags.php' === $page;
  220. }
  221. /**
  222. * @param string $page The string to check for the term edit page.
  223. *
  224. * @return bool
  225. */
  226. public static function is_term_edit( $page ) {
  227. return 'term.php' === $page;
  228. }
  229. /**
  230. * Retrieves a template.
  231. * Check if metabox for current taxonomy should be displayed.
  232. *
  233. * @return bool
  234. */
  235. private function show_metabox() {
  236. $option_key = 'display-metabox-tax-' . $this->taxonomy;
  237. return WPSEO_Options::get( $option_key );
  238. }
  239. /**
  240. * Getting the taxonomy from the URL.
  241. *
  242. * @return string
  243. */
  244. private function get_taxonomy() {
  245. return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, array( 'options' => array( 'default' => '' ) ) );
  246. }
  247. /**
  248. * Prepares the replace vars for localization.
  249. *
  250. * @return array The replacement variables.
  251. */
  252. private function get_replace_vars() {
  253. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  254. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  255. $cached_replacement_vars = array();
  256. $vars_to_cache = array(
  257. 'date',
  258. 'id',
  259. 'sitename',
  260. 'sitedesc',
  261. 'sep',
  262. 'page',
  263. 'term_title',
  264. 'term_description',
  265. 'category_description',
  266. 'tag_description',
  267. 'searchphrase',
  268. 'currentyear',
  269. );
  270. foreach ( $vars_to_cache as $var ) {
  271. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term );
  272. }
  273. return $cached_replacement_vars;
  274. }
  275. /**
  276. * Prepares the recommended replace vars for localization.
  277. *
  278. * @return array The recommended replacement variables.
  279. */
  280. private function get_recommended_replace_vars() {
  281. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  282. $taxonomy = filter_input( INPUT_GET, 'taxonomy' );
  283. // What is recommended depends on the current context.
  284. $page_type = $recommended_replace_vars->determine_for_term( $taxonomy );
  285. return $recommended_replace_vars->get_recommended_replacevars_for( $page_type );
  286. }
  287. /**
  288. * Adds custom category description editor.
  289. * Needs a hook that runs before the description field. Prior to WP version 4.5 we need to use edit_form as
  290. * term_edit_form_top was introduced in WP 4.5. This can be removed after <4.5 is no longer supported.
  291. *
  292. * @return {void}
  293. */
  294. private function insert_description_field_editor() {
  295. if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ) {
  296. add_action( "{$this->taxonomy}_edit_form", array( $this, 'custom_category_description_editor' ) );
  297. return;
  298. }
  299. add_action( "{$this->taxonomy}_term_edit_form_top", array( $this, 'custom_category_description_editor' ) );
  300. }
  301. /**
  302. * Adds shortcode support to category descriptions.
  303. *
  304. * @deprecated 7.9.0
  305. *
  306. * @param string $desc String to add shortcodes in.
  307. *
  308. * @return string Content with shortcodes filtered out.
  309. */
  310. public function custom_category_descriptions_add_shortcode_support( $desc ) {
  311. _deprecated_function( __FUNCTION__, 'WPSEO 7.9.0', 'WPSEO_Frontend::custom_category_descriptions_add_shortcode_support' );
  312. $frontend = WPSEO_Frontend::get_instance();
  313. return $frontend->custom_category_descriptions_add_shortcode_support( $desc );
  314. }
  315. }