PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/taxonomy/class-taxonomy.php

https://gitlab.com/hop23typhu/faci-parkhill
PHP | 304 lines | 150 code | 46 blank | 108 comment | 9 complexity | 83feed3904d3836c3c33d62d4e550b23 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * Class that handles the edit boxes on taxonomy edit pages.
  7. */
  8. class WPSEO_Taxonomy {
  9. /**
  10. * The current active taxonomy
  11. *
  12. * @var string
  13. */
  14. private $taxonomy = '';
  15. /**
  16. * Class constructor
  17. */
  18. public function __construct() {
  19. $this->taxonomy = $this->get_taxonomy();
  20. add_action( 'edit_term', array( $this, 'update_term' ), 99, 3 );
  21. add_action( 'init', array( $this, 'custom_category_descriptions_allow_html' ) );
  22. add_action( 'admin_init', array( $this, 'admin_init' ) );
  23. add_filter( 'category_description', array( $this, 'custom_category_descriptions_add_shortcode_support' ) );
  24. if ( self::is_term_overview( $GLOBALS['pagenow'] ) ) {
  25. new WPSEO_Taxonomy_Columns();
  26. }
  27. }
  28. /**
  29. * Add hooks late enough for taxonomy object to be available for checks.
  30. */
  31. public function admin_init() {
  32. $taxonomy = get_taxonomy( $this->taxonomy );
  33. if ( empty( $taxonomy ) || empty( $taxonomy->public ) || ! $this->show_metabox() ) {
  34. return;
  35. }
  36. add_action( sanitize_text_field( $this->taxonomy ) . '_edit_form', array( $this, 'term_metabox' ), 90, 1 );
  37. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  38. }
  39. /**
  40. * Show the SEO inputs for term.
  41. *
  42. * @param stdClass|WP_Term $term Term to show the edit boxes for.
  43. */
  44. public function term_metabox( $term ) {
  45. $metabox = new WPSEO_Taxonomy_Metabox( $this->taxonomy, $term );
  46. $metabox->display();
  47. }
  48. /**
  49. * Queue assets for taxonomy screens.
  50. *
  51. * @since 1.5.0
  52. */
  53. public function admin_enqueue_scripts() {
  54. $pagenow = $GLOBALS['pagenow'];
  55. if ( ! ( self::is_term_edit( $pagenow ) || self::is_term_overview( $pagenow ) ) ) {
  56. return;
  57. }
  58. $asset_manager = new WPSEO_Admin_Asset_Manager();
  59. $asset_manager->enqueue_style( 'scoring' );
  60. $tag_id = filter_input( INPUT_GET, 'tag_ID' );
  61. if (
  62. self::is_term_edit( $pagenow ) &&
  63. ! empty( $tag_id ) // After we drop support for <4.5 this can be removed.
  64. ) {
  65. wp_enqueue_media(); // Enqueue files needed for upload functionality.
  66. $asset_manager->enqueue_style( 'metabox-css' );
  67. $asset_manager->enqueue_style( 'snippet' );
  68. $asset_manager->enqueue_style( 'scoring' );
  69. wp_editor( '', 'description' );
  70. $asset_manager->enqueue_script( 'metabox' );
  71. $asset_manager->enqueue_script( 'term-scraper' );
  72. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'term-scraper', 'wpseoTermScraperL10n', $this->localize_term_scraper_script() );
  73. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'replacevar-plugin', 'wpseoReplaceVarsL10n', $this->localize_replace_vars_script() );
  74. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox', 'wpseoSelect2Locale', substr( get_locale(), 0, 2 ) );
  75. $asset_manager->enqueue_script( 'admin-media' );
  76. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', array(
  77. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  78. ) );
  79. }
  80. }
  81. /**
  82. * Update the taxonomy meta data on save.
  83. *
  84. * @param int $term_id ID of the term to save data for.
  85. * @param int $tt_id The taxonomy_term_id for the term.
  86. * @param string $taxonomy The taxonomy the term belongs to.
  87. */
  88. public function update_term( $term_id, $tt_id, $taxonomy ) {
  89. /* Create post array with only our values */
  90. $new_meta_data = array();
  91. foreach ( WPSEO_Taxonomy_Meta::$defaults_per_term as $key => $default ) {
  92. if ( $posted_value = filter_input( INPUT_POST, $key ) ) {
  93. $new_meta_data[ $key ] = $posted_value;
  94. }
  95. }
  96. unset( $key, $default );
  97. // Saving the values.
  98. WPSEO_Taxonomy_Meta::set_values( $term_id, $taxonomy, $new_meta_data );
  99. }
  100. /**
  101. * Allows HTML in descriptions
  102. */
  103. public function custom_category_descriptions_allow_html() {
  104. $filters = array(
  105. 'pre_term_description',
  106. 'pre_link_description',
  107. 'pre_link_notes',
  108. 'pre_user_description',
  109. );
  110. foreach ( $filters as $filter ) {
  111. remove_filter( $filter, 'wp_filter_kses' );
  112. }
  113. remove_filter( 'term_description', 'wp_kses_data' );
  114. }
  115. /**
  116. * Adds shortcode support to category descriptions.
  117. *
  118. * @param string $desc String to add shortcodes in.
  119. *
  120. * @return string
  121. */
  122. public function custom_category_descriptions_add_shortcode_support( $desc ) {
  123. // Wrap in output buffering to prevent shortcodes that echo stuff instead of return from breaking things.
  124. ob_start();
  125. $desc = do_shortcode( $desc );
  126. ob_end_clean();
  127. return $desc;
  128. }
  129. /**
  130. * Pass variables to js for use with the term-scraper
  131. *
  132. * @return array
  133. */
  134. public function localize_term_scraper_script() {
  135. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  136. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  137. $taxonomy = get_taxonomy( $term->taxonomy );
  138. $term_formatter = new WPSEO_Metabox_Formatter(
  139. new WPSEO_Term_Metabox_Formatter( $taxonomy, $term, WPSEO_Options::get_option( 'wpseo_titles' ) )
  140. );
  141. return $term_formatter->get_values();
  142. }
  143. /**
  144. * Pass some variables to js for replacing variables.
  145. */
  146. public function localize_replace_vars_script() {
  147. return array(
  148. 'no_parent_text' => __( '(no parent)', 'wordpress-seo' ),
  149. 'replace_vars' => $this->get_replace_vars(),
  150. );
  151. }
  152. /**
  153. * @param string $page The string to check for the term overview page.
  154. *
  155. * @return bool
  156. */
  157. public static function is_term_overview( $page ) {
  158. return 'edit-tags.php' === $page;
  159. }
  160. /**
  161. * @param string $page The string to check for the term edit page.
  162. *
  163. * @return bool
  164. */
  165. public static function is_term_edit( $page ) {
  166. return 'term.php' === $page
  167. || 'edit-tags.php' === $page; // After we drop support for <4.5 this can be removed.
  168. }
  169. /**
  170. * Retrieves a template.
  171. * Check if metabox for current taxonomy should be displayed.
  172. *
  173. * @return bool
  174. */
  175. private function show_metabox() {
  176. $options = WPSEO_Options::get_option( 'wpseo_titles' );
  177. $option_key = 'hideeditbox-tax-' . $this->taxonomy;
  178. return ( empty( $options[ $option_key ] ) );
  179. }
  180. /**
  181. * Getting the taxonomy from the URL
  182. *
  183. * @return string
  184. */
  185. private function get_taxonomy() {
  186. return filter_input( INPUT_GET, 'taxonomy', FILTER_DEFAULT, array( 'options' => array( 'default' => '' ) ) );
  187. }
  188. /**
  189. * Prepares the replace vars for localization.
  190. *
  191. * @return array replace vars.
  192. */
  193. private function get_replace_vars() {
  194. $term_id = filter_input( INPUT_GET, 'tag_ID' );
  195. $term = get_term_by( 'id', $term_id, $this->get_taxonomy() );
  196. $cached_replacement_vars = array();
  197. $vars_to_cache = array(
  198. 'date',
  199. 'id',
  200. 'sitename',
  201. 'sitedesc',
  202. 'sep',
  203. 'page',
  204. 'currenttime',
  205. 'currentdate',
  206. 'currentday',
  207. 'currentmonth',
  208. 'currentyear',
  209. 'term_title',
  210. 'term_description',
  211. 'category_description',
  212. 'tag_description',
  213. 'searchphrase',
  214. );
  215. foreach ( $vars_to_cache as $var ) {
  216. $cached_replacement_vars[ $var ] = wpseo_replace_vars( '%%' . $var . '%%', $term );
  217. }
  218. return $cached_replacement_vars;
  219. }
  220. /********************** DEPRECATED METHODS **********************/
  221. /**
  222. * @deprecated 3.2
  223. *
  224. * Retrieves the title template.
  225. *
  226. * @param object $term taxonomy term.
  227. *
  228. * @return string
  229. */
  230. public static function get_title_template( $term ) {
  231. _deprecated_function( 'WPSEO_Taxonomy::get_title_template', 'WPSEO 3.2', 'WPSEO_Term_Scraper::get_title_template' );
  232. return '';
  233. }
  234. /**
  235. * @deprecated 3.2
  236. *
  237. * Retrieves the metadesc template.
  238. *
  239. * @param object $term taxonomy term.
  240. *
  241. * @return string
  242. */
  243. public static function get_metadesc_template( $term ) {
  244. _deprecated_function( 'WPSEO_Taxonomy::get_metadesc_template', 'WPSEO 3.2', 'WPSEO_Term_Scraper::get_metadesc_template' );
  245. return '';
  246. }
  247. /**
  248. * @deprecated 3.2
  249. *
  250. * Translate options text strings for use in the select fields
  251. *
  252. * @internal IMPORTANT: if you want to add a new string (option) somewhere, make sure you add
  253. * that array key to the main options definition array in the class WPSEO_Taxonomy_Meta() as well!!!!
  254. */
  255. public function translate_meta_options() {
  256. _deprecated_function( 'WPSEO_Taxonomy::translate_meta_options', 'WPSEO 3.2', 'WPSEO_Taxonomy_Settings_Fields::translate_meta_options' );
  257. }
  258. }