WPSEO_Taxonomy

Class that handles the edit boxes on taxonomy edit pages.

Defined (1)

The class is defined in the following location(s).

/admin/taxonomy/class-taxonomy.php  
  1. class WPSEO_Taxonomy { 
  2.  
  3. /** 
  4. * The current active taxonomy 
  5. * @var string 
  6. */ 
  7. private $taxonomy = ''; 
  8.  
  9. /** 
  10. * @var WPSEO_Metabox_Analysis_SEO 
  11. */ 
  12. private $analysis_seo; 
  13.  
  14. /** 
  15. * @var WPSEO_Metabox_Analysis_Readability 
  16. */ 
  17. private $analysis_readability; 
  18.  
  19. /** 
  20. * Class constructor 
  21. */ 
  22. public function __construct() { 
  23. $this->taxonomy = $this->get_taxonomy(); 
  24.  
  25. add_action( 'edit_term', array( $this, 'update_term' ), 99, 3 ); 
  26. add_action( 'init', array( $this, 'custom_category_descriptions_allow_html' ) ); 
  27. add_action( 'admin_init', array( $this, 'admin_init' ) ); 
  28.  
  29. $this->insert_description_field_editor(); 
  30.  
  31. add_filter( 'category_description', array( $this, 'custom_category_descriptions_add_shortcode_support' ) ); 
  32.  
  33. if ( self::is_term_overview( $GLOBALS['pagenow'] ) ) { 
  34. new WPSEO_Taxonomy_Columns(); 
  35. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO(); 
  36. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability(); 
  37.  
  38. /** 
  39. * Add hooks late enough for taxonomy object to be available for checks. 
  40. */ 
  41. public function admin_init() { 
  42.  
  43. $taxonomy = get_taxonomy( $this->taxonomy ); 
  44.  
  45. if ( empty( $taxonomy ) || empty( $taxonomy->public ) || ! $this->show_metabox() ) { 
  46. return; 
  47.  
  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. * @param stdClass|WP_Term $term Term to show the edit boxes for. 
  54. */ 
  55. public function term_metabox( $term ) { 
  56. $metabox = new WPSEO_Taxonomy_Metabox( $this->taxonomy, $term ); 
  57. $metabox->display(); 
  58.  
  59. /** 
  60. * Queue assets for taxonomy screens. 
  61. * @since 1.5.0 
  62. */ 
  63. public function admin_enqueue_scripts() { 
  64. $pagenow = $GLOBALS['pagenow']; 
  65.  
  66. if ( ! ( self::is_term_edit( $pagenow ) || self::is_term_overview( $pagenow ) ) ) { 
  67. return; 
  68.  
  69. $asset_manager = new WPSEO_Admin_Asset_Manager(); 
  70. $asset_manager->enqueue_style( 'scoring' ); 
  71.  
  72.  
  73. $tag_id = filter_input( INPUT_GET, 'tag_ID' ); 
  74. if ( 
  75. self::is_term_edit( $pagenow ) && 
  76. ! empty( $tag_id ) // After we drop support for <4.5 this can be removed. 
  77. ) { 
  78. wp_enqueue_media(); // Enqueue files needed for upload functionality. 
  79.  
  80. $asset_manager->enqueue_style( 'metabox-css' ); 
  81. $asset_manager->enqueue_style( 'snippet' ); 
  82. $asset_manager->enqueue_style( 'scoring' ); 
  83. $asset_manager->enqueue_script( 'metabox' ); 
  84. $asset_manager->enqueue_script( 'term-scraper' ); 
  85. $asset_manager->enqueue_style( 'kb-search' ); 
  86.  
  87. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper', 'wpseoTermScraperL10n', $this->localize_term_scraper_script() ); 
  88. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() ); 
  89. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', WPSEO_Utils::get_language( WPSEO_Utils::get_user_locale() ) ); 
  90. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoAdminL10n', WPSEO_Help_Center::get_translated_texts() ); 
  91.  
  92. $asset_manager->enqueue_script( 'admin-media' ); 
  93.  
  94. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', array( 
  95. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),  
  96. ) ); 
  97.  
  98. /** 
  99. * Update the taxonomy meta data on save. 
  100. * @param int $term_id ID of the term to save data for. 
  101. * @param int $tt_id The taxonomy_term_id for the term. 
  102. * @param string $taxonomy The taxonomy the term belongs to. 
  103. */ 
  104. public function update_term( $term_id, $tt_id, $taxonomy ) { 
  105. /** Create post array with only our values */ 
  106. $new_meta_data = array(); 
  107. foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) { 
  108. if ( $posted_value = filter_input( INPUT_POST, $key ) ) { 
  109. $new_meta_data[ $key ] = $posted_value; 
  110.  
  111. // If analysis is disabled remove that analysis score value from the DB. 
  112. if ( $this->is_meta_value_disabled( $key ) ) { 
  113. $new_meta_data[ $key ] = ''; 
  114. unset( $key, $default ); 
  115.  
  116. // Saving the values. 
  117. WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data ); 
  118.  
  119. /** 
  120. * Determines if the given meta value key is disabled 
  121. * @param string $key The key of the meta value. 
  122. * @return bool Whether the given meta value key is disabled. 
  123. */ 
  124. public function is_meta_value_disabled( $key ) { 
  125. if ( 'wpseo_linkdex' === $key && ! $this->analysis_seo->is_enabled() ) { 
  126. return true; 
  127.  
  128. if ( 'wpseo_content_score' === $key && ! $this->analysis_readability->is_enabled() ) { 
  129. return true; 
  130.  
  131. return false; 
  132.  
  133. /** 
  134. * Allows HTML in descriptions 
  135. */ 
  136. public function custom_category_descriptions_allow_html() { 
  137. $filters = array( 
  138. 'pre_term_description',  
  139. 'pre_link_description',  
  140. 'pre_link_notes',  
  141. 'pre_user_description',  
  142. ); 
  143.  
  144. foreach ( $filters as $filter ) { 
  145. remove_filter( $filter, 'wp_filter_kses' ); 
  146. remove_filter( 'term_description', 'wp_kses_data' ); 
  147.  
  148. /** 
  149. * Output the WordPress editor. 
  150. */ 
  151. public function custom_category_description_editor() { 
  152.  
  153. if ( ! $this->show_metabox() ) { 
  154. return; 
  155.  
  156. wp_editor( '', 'description' ); 
  157.  
  158. /** 
  159. * Adds shortcode support to category descriptions. 
  160. * @param string $desc String to add shortcodes in. 
  161. * @return string 
  162. */ 
  163. public function custom_category_descriptions_add_shortcode_support( $desc ) { 
  164. // Wrap in output buffering to prevent shortcodes that echo stuff instead of return from breaking things. 
  165. ob_start(); 
  166. $desc = do_shortcode( $desc ); 
  167. ob_end_clean(); 
  168.  
  169. return $desc; 
  170.  
  171. /** 
  172. * Pass variables to js for use with the term-scraper 
  173. * @return array 
  174. */ 
  175. public function localize_term_scraper_script() { 
  176. $term_id = filter_input( INPUT_GET, 'tag_ID' ); 
  177. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() ); 
  178. $taxonomy = get_taxonomy( $term->taxonomy ); 
  179.  
  180. $term_formatter = new WPSEO_Metabox_Formatter( 
  181. new WPSEO_Term_Metabox_Formatter( $taxonomy, $term, WPSEO_Options::get_option( 'wpseo_titles' ) ) 
  182. ); 
  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. 'scope' => $this->determine_scope(),  
  194. ); 
  195.  
  196. /** 
  197. * Determines the scope based on the current taxonomy. 
  198. * This can be used by the replacevar plugin to determine if a replacement needs to be executed. 
  199. * @return string String decribing the current scope. 
  200. */ 
  201. private function determine_scope() { 
  202. $taxonomy = $this->get_taxonomy(); 
  203.  
  204. if ( $taxonomy === 'category' ) { 
  205. return 'category'; 
  206.  
  207. if ( $taxonomy === 'post_tag' ) { 
  208. return 'tag'; 
  209.  
  210. return 'term'; 
  211.  
  212. /** 
  213. * @param string $page The string to check for the term overview page. 
  214. * @return bool 
  215. */ 
  216. public static function is_term_overview( $page ) { 
  217. return 'edit-tags.php' === $page; 
  218.  
  219. /** 
  220. * @param string $page The string to check for the term edit page. 
  221. * @return bool 
  222. */ 
  223. public static function is_term_edit( $page ) { 
  224. return 'term.php' === $page 
  225. || 'edit-tags.php' === $page; // After we drop support for <4.5 this can be removed. 
  226.  
  227. /** 
  228. * Retrieves a template. 
  229. * Check if metabox for current taxonomy should be displayed. 
  230. * @return bool 
  231. */ 
  232. private function show_metabox() { 
  233. $options = WPSEO_Options::get_option( 'wpseo_titles' ); 
  234. $option_key = 'hideeditbox-tax-' . $this->taxonomy; 
  235.  
  236. return ( empty( $options[ $option_key ] ) ); 
  237.  
  238. /** 
  239. * Getting the taxonomy from the URL 
  240. * @return string 
  241. */ 
  242. private function get_taxonomy() { 
  243. return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, array( 'options' => array( 'default' => '' ) ) ); 
  244.  
  245. /** 
  246. * Prepares the replace vars for localization. 
  247. * @return array replace vars. 
  248. */ 
  249. private function get_replace_vars() { 
  250. $term_id = filter_input( INPUT_GET, 'tag_ID' ); 
  251. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() ); 
  252. $cached_replacement_vars = array(); 
  253.  
  254. $vars_to_cache = array( 
  255. 'date',  
  256. 'id',  
  257. 'sitename',  
  258. 'sitedesc',  
  259. 'sep',  
  260. 'page',  
  261. 'currenttime',  
  262. 'currentdate',  
  263. 'currentday',  
  264. 'currentmonth',  
  265. 'currentyear',  
  266. 'term_title',  
  267. 'term_description',  
  268. 'category_description',  
  269. 'tag_description',  
  270. 'searchphrase',  
  271. ); 
  272.  
  273. foreach ( $vars_to_cache as $var ) { 
  274. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term ); 
  275.  
  276. return $cached_replacement_vars; 
  277.  
  278. /** 
  279. * Adds custom category description editor. 
  280. * Needs a hook that runs before the description field. Prior to WP version 4.5 we need to use edit_form as 
  281. * term_edit_form_top was introduced in WP 4.5. This can be removed after <4.5 is no longer supported. 
  282. * @return {void} 
  283. */ 
  284. private function insert_description_field_editor() { 
  285. if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ) { 
  286. add_action( "{$this->taxonomy}_edit_form", array( $this, 'custom_category_description_editor' ) ); 
  287. return; 
  288.  
  289. add_action( "{$this->taxonomy}_term_edit_form_top", array( $this, 'custom_category_description_editor' ) ); 
  290.  
  291. /********************** DEPRECATED METHODS **********************/ 
  292.  
  293. // @codeCoverageIgnoreStart 
  294. /** 
  295. * @deprecated 3.2 
  296. * Retrieves the title template. 
  297. * @param object $term taxonomy term. 
  298. * @return string 
  299. */ 
  300. public static function get_title_template( $term ) { 
  301. _deprecated_function( __METHOD__, 'WPSEO 3.2', 'WPSEO_Term_Scraper::get_title_template' ); 
  302.  
  303. return ''; 
  304.  
  305. /** 
  306. * @deprecated 3.2 
  307. * Retrieves the metadesc template. 
  308. * @param object $term taxonomy term. 
  309. * @return string 
  310. */ 
  311. public static function get_metadesc_template( $term ) { 
  312. _deprecated_function( __METHOD__, 'WPSEO 3.2', 'WPSEO_Term_Scraper::get_metadesc_template' ); 
  313.  
  314. return ''; 
  315.  
  316. /** 
  317. * @deprecated 3.2 
  318. * Translate options text strings for use in the select fields 
  319. * @internal IMPORTANT: if you want to add a new string (option) somewhere, make sure you add 
  320. * that array key to the main options definition array in the class WPSEO_Taxonomy_Meta() as well!!!! 
  321. */ 
  322. public function translate_meta_options() { 
  323. _deprecated_function( __METHOD__, 'WPSEO 3.2', 'WPSEO_Taxonomy_Settings_Fields::translate_meta_options' ); 
  324. // @codeCoverageIgnoreEnd