WPSEO_Primary_Term_Admin

Adds the UI to change the primary term for a post.

Defined (1)

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

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