/wp-content/plugins/wordpress-seo/admin/class-primary-term-admin.php

https://bitbucket.org/carloskikea/helpet · PHP · 234 lines · 104 code · 38 blank · 92 comment · 12 complexity · d1026c8b8394763b31a22f0baa0386af MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Adds the UI to change the primary term for a post
  9. */
  10. class WPSEO_Primary_Term_Admin {
  11. /**
  12. * Constructor.
  13. */
  14. public function __construct() {
  15. add_action( 'admin_footer', array( $this, 'wp_footer' ), 10 );
  16. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  17. add_action( 'save_post', array( $this, 'save_primary_terms' ) );
  18. $primary_term = new WPSEO_Frontend_Primary_Category();
  19. $primary_term->register_hooks();
  20. }
  21. /**
  22. * Get the current post ID.
  23. *
  24. * @return integer The post ID.
  25. */
  26. protected function get_current_id() {
  27. $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
  28. if ( empty( $post_id ) && isset( $GLOBALS['post_ID'] ) ) {
  29. $post_id = filter_var( $GLOBALS['post_ID'], FILTER_SANITIZE_NUMBER_INT );
  30. }
  31. return $post_id;
  32. }
  33. /**
  34. * Add primary term templates
  35. */
  36. public function wp_footer() {
  37. $taxonomies = $this->get_primary_term_taxonomies();
  38. if ( ! empty( $taxonomies ) ) {
  39. $this->include_js_templates();
  40. }
  41. }
  42. /**
  43. * Enqueues all the assets needed for the primary term interface
  44. *
  45. * @return void
  46. */
  47. public function enqueue_assets() {
  48. global $pagenow;
  49. if ( ! WPSEO_Metabox::is_post_edit( $pagenow ) ) {
  50. return;
  51. }
  52. $taxonomies = $this->get_primary_term_taxonomies();
  53. // Only enqueue if there are taxonomies that need a primary term.
  54. if ( empty( $taxonomies ) ) {
  55. return;
  56. }
  57. $asset_manager = new WPSEO_Admin_Asset_Manager();
  58. $asset_manager->enqueue_style( 'primary-category' );
  59. $asset_manager->enqueue_script( 'primary-category' );
  60. $taxonomies = array_map( array( $this, 'map_taxonomies_for_js' ), $taxonomies );
  61. $data = array(
  62. 'taxonomies' => $taxonomies,
  63. );
  64. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'primary-category', 'wpseoPrimaryCategoryL10n', $data );
  65. }
  66. /**
  67. * Saves all selected primary terms
  68. *
  69. * @param int $post_id Post ID to save primary terms for.
  70. */
  71. public function save_primary_terms( $post_id ) {
  72. // Bail if this is a multisite installation and the site has been switched.
  73. if ( is_multisite() && ms_is_switched() ) {
  74. return;
  75. }
  76. $taxonomies = $this->get_primary_term_taxonomies( $post_id );
  77. foreach ( $taxonomies as $taxonomy ) {
  78. $this->save_primary_term( $post_id, $taxonomy );
  79. }
  80. }
  81. /**
  82. * /**
  83. * Get the id of the primary term
  84. *
  85. * @param string $taxonomy_name Taxonomy name for the term.
  86. *
  87. * @return int primary term id
  88. */
  89. protected function get_primary_term( $taxonomy_name ) {
  90. $primary_term = new WPSEO_Primary_Term( $taxonomy_name, $this->get_current_id() );
  91. return $primary_term->get_primary_term();
  92. }
  93. /**
  94. * Returns all the taxonomies for which the primary term selection is enabled
  95. *
  96. * @param int $post_id Default current post ID.
  97. * @return array
  98. */
  99. protected function get_primary_term_taxonomies( $post_id = null ) {
  100. if ( null === $post_id ) {
  101. $post_id = $this->get_current_id();
  102. }
  103. $taxonomies = wp_cache_get( 'primary_term_taxonomies_' . $post_id, 'wpseo' );
  104. if ( false !== $taxonomies ) {
  105. return $taxonomies;
  106. }
  107. $taxonomies = $this->generate_primary_term_taxonomies( $post_id );
  108. wp_cache_set( 'primary_term_taxonomies_' . $post_id, $taxonomies, 'wpseo' );
  109. return $taxonomies;
  110. }
  111. /**
  112. * Include templates file
  113. */
  114. protected function include_js_templates() {
  115. include_once WPSEO_PATH . 'admin/views/js-templates-primary-term.php';
  116. }
  117. /**
  118. * Save the primary term for a specific taxonomy
  119. *
  120. * @param int $post_id Post ID to save primary term for.
  121. * @param WP_Term $taxonomy Taxonomy to save primary term for.
  122. */
  123. protected function save_primary_term( $post_id, $taxonomy ) {
  124. $primary_term = filter_input( INPUT_POST, WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_term', FILTER_SANITIZE_NUMBER_INT );
  125. // We accept an empty string here because we need to save that if no terms are selected.
  126. if ( null !== $primary_term && check_admin_referer( 'save-primary-term', WPSEO_Meta::$form_prefix . 'primary_' . $taxonomy->name . '_nonce' ) ) {
  127. $primary_term_object = new WPSEO_Primary_Term( $taxonomy->name, $post_id );
  128. $primary_term_object->set_primary_term( $primary_term );
  129. }
  130. }
  131. /**
  132. * Generate the primary term taxonomies.
  133. *
  134. * @param int $post_id ID of the post.
  135. *
  136. * @return array
  137. */
  138. protected function generate_primary_term_taxonomies( $post_id ) {
  139. $post_type = get_post_type( $post_id );
  140. $all_taxonomies = get_object_taxonomies( $post_type, 'objects' );
  141. $all_taxonomies = array_filter( $all_taxonomies, array( $this, 'filter_hierarchical_taxonomies' ) );
  142. /**
  143. * Filters which taxonomies for which the user can choose the primary term.
  144. *
  145. * @api array $taxonomies An array of taxonomy objects that are primary_term enabled.
  146. *
  147. * @param string $post_type The post type for which to filter the taxonomies.
  148. * @param array $all_taxonomies All taxonomies for this post types, even ones that don't have primary term
  149. * enabled.
  150. */
  151. $taxonomies = (array) apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies );
  152. return $taxonomies;
  153. }
  154. /**
  155. * Returns an array suitable for use in the javascript
  156. *
  157. * @param stdClass $taxonomy The taxonomy to map.
  158. *
  159. * @return array
  160. */
  161. private function map_taxonomies_for_js( $taxonomy ) {
  162. $primary_term = $this->get_primary_term( $taxonomy->name );
  163. if ( empty( $primary_term ) ) {
  164. $primary_term = '';
  165. }
  166. return array(
  167. 'title' => $taxonomy->labels->singular_name,
  168. 'name' => $taxonomy->name,
  169. 'primary' => $primary_term,
  170. 'terms' => array_map( array( $this, 'map_terms_for_js' ), get_terms( $taxonomy->name ) ),
  171. );
  172. }
  173. /**
  174. * Returns an array suitable for use in the javascript
  175. *
  176. * @param stdClass $term The term to map.
  177. *
  178. * @return array
  179. */
  180. private function map_terms_for_js( $term ) {
  181. return array(
  182. 'id' => $term->term_id,
  183. 'name' => $term->name,
  184. );
  185. }
  186. /**
  187. * Returns whether or not a taxonomy is hierarchical
  188. *
  189. * @param stdClass $taxonomy Taxonomy object.
  190. *
  191. * @return bool
  192. */
  193. private function filter_hierarchical_taxonomies( $taxonomy ) {
  194. return (bool) $taxonomy->hierarchical;
  195. }
  196. }