WPSEO_Taxonomy_Columns

This class adds columns to the taxonomy table.

Defined (1)

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

/admin/taxonomy/class-taxonomy-columns.php  
  1. class WPSEO_Taxonomy_Columns { 
  2.  
  3. /** 
  4. * @var WPSEO_Metabox_Analysis_SEO 
  5. */ 
  6. private $analysis_seo; 
  7.  
  8. /** 
  9. * WPSEO_Taxonomy_Columns constructor. 
  10. */ 
  11. public function __construct() { 
  12.  
  13. $this->taxonomy = $this->get_taxonomy(); 
  14.  
  15. if ( ! empty( $this->taxonomy ) ) { 
  16. add_filter( 'manage_edit-' . $this->taxonomy . '_columns', array( $this, 'add_columns' ) ); 
  17. add_filter( 'manage_' . $this->taxonomy . '_custom_column', array( $this, 'parse_column' ), 10, 3 ); 
  18.  
  19. $this->analysis_seo = new WPSEO_Metabox_Analysis_SEO(); 
  20. $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability(); 
  21.  
  22. /** 
  23. * Adds an SEO score column to the terms table, right after the description column. 
  24. * @param array $columns Current set columns. 
  25. * @return array 
  26. */ 
  27. public function add_columns( array $columns ) { 
  28.  
  29. if ( $this->is_metabox_hidden() === true ) { 
  30. return $columns; 
  31.  
  32. $new_columns = array(); 
  33.  
  34. foreach ( $columns as $column_name => $column_value ) { 
  35. $new_columns[ $column_name ] = $column_value; 
  36.  
  37. if ( $column_name === 'description' && $this->analysis_seo->is_enabled() ) { 
  38. $new_columns['wpseo_score'] = __( 'SEO', 'wordpress-seo' ); 
  39.  
  40. if ( $column_name === 'description' && $this->analysis_readability->is_enabled() ) { 
  41. $new_columns['wpseo_score_readability'] = __( 'Readability', 'wordpress-seo' ); 
  42.  
  43. return $new_columns; 
  44.  
  45. /** 
  46. * Parses the column. 
  47. * @param string $content The current content of the column. 
  48. * @param string $column_name The name of the column. 
  49. * @param integer $term_id ID of requested taxonomy. 
  50. * @return string 
  51. */ 
  52. public function parse_column( $content, $column_name, $term_id ) { 
  53.  
  54. switch ( $column_name ) { 
  55. case 'wpseo_score': 
  56. return $this->get_score_value( $term_id ); 
  57.  
  58. break; 
  59.  
  60. case 'wpseo_score_readability': 
  61. return $this->get_score_readability_value( $term_id ); 
  62. break; 
  63.  
  64. return $content; 
  65.  
  66.  
  67. /** 
  68. * Returns the posted/get taxonomy value if it is set. 
  69. * @return string|null 
  70. */ 
  71. private function get_taxonomy() { 
  72. if ( defined( 'DOING_AJAX' ) && DOING_AJAX === true ) { 
  73. return FILTER_INPUT( INPUT_POST, 'taxonomy' ); 
  74.  
  75. return FILTER_INPUT( INPUT_GET, 'taxonomy' ); 
  76.  
  77. /** 
  78. * Parses the value for the score column. 
  79. * @param integer $term_id ID of requested term. 
  80. * @return string 
  81. */ 
  82. private function get_score_value( $term_id ) { 
  83. $term = get_term( $term_id, $this->taxonomy ); 
  84.  
  85. // When the term isn't indexable. 
  86. if ( ! $this->is_indexable( $term ) ) { 
  87. return $this->create_score_icon( 
  88. new WPSEO_Rank( WPSEO_Rank::NO_INDEX ),  
  89. __( 'Term is set to noindex.', 'wordpress-seo' ) 
  90. ); 
  91.  
  92. // When there is a focus key word. 
  93. if ( $focus_keyword = $this->get_focus_keyword( $term ) ) { 
  94. $score = (int) WPSEO_Taxonomy_Meta::get_term_meta( $term_id, $this->taxonomy, 'linkdex' ); 
  95. $rank = WPSEO_Rank::from_numeric_score( $score ); 
  96.  
  97. return $this->create_score_icon( $rank, $rank->get_label() ); 
  98.  
  99. // Default icon. 
  100. return $this->create_score_icon( 
  101. new WPSEO_Rank( WPSEO_Rank::NO_FOCUS ),  
  102. __( 'Focus keyword not set.', 'wordpress-seo' ) 
  103. ); 
  104.  
  105. /** 
  106. * Parses the value for the readability score column. 
  107. * @param int $term_id ID of the requested term. 
  108. * @return string The HTML for the readability score indicator. 
  109. */ 
  110. private function get_score_readability_value( $term_id ) { 
  111. $score = (int) WPSEO_Taxonomy_Meta::get_term_meta( $term_id, $this->taxonomy, 'content_score' ); 
  112. $rank = WPSEO_Rank::from_numeric_score( $score ); 
  113.  
  114. return $this->create_score_icon( $rank ); 
  115.  
  116. /** 
  117. * Creates an icon by the given values. 
  118. * @param WPSEO_Rank $rank The ranking object. 
  119. * @param string $title Optional. The title to show. Defaults to the rank label. 
  120. * @return string The HTML for a score icon. 
  121. */ 
  122. private function create_score_icon( WPSEO_Rank $rank, $title = '' ) { 
  123. if ( empty( $title ) ) { 
  124. $title = $rank->get_label(); 
  125.  
  126. return '<div aria-hidden="true" title="' . esc_attr( $title ) . '" class="wpseo-score-icon ' . esc_attr( $rank->get_css_class() ) . '"></div><span class="screen-reader-text">' . $title . '</span>'; 
  127.  
  128. /** 
  129. * Check if the taxonomy is indexable. 
  130. * @param mixed $term The current term. 
  131. * @return bool 
  132. */ 
  133. private function is_indexable( $term ) { 
  134. static $options; 
  135.  
  136. // Saving the options once, because it's static. 
  137. if ( $options === null ) { 
  138. $options = WPSEO_Options::get_all(); 
  139.  
  140. // When the no_index value is not empty and not default, check if its value is index. 
  141. $no_index = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $this->taxonomy, 'noindex' ); 
  142. if ( ! empty( $no_index ) && $no_index !== 'default' ) { 
  143. return ( $no_index === 'index' ); 
  144.  
  145. // Check if the default for taxonomy is empty (this will be index). 
  146. $no_index_key = 'noindex-tax-' . $term->taxonomy; 
  147. if ( is_object( $term ) && ( isset( $options[ $no_index_key ] ) ) ) { 
  148. return ( empty( $options[ $no_index_key ] ) ); 
  149.  
  150. return true; 
  151.  
  152. /** 
  153. * Returns the focus keyword if this is set, otherwise it will give the term name. 
  154. * @param stdClass|WP_Term $term The current term. 
  155. * @return string 
  156. */ 
  157. private function get_focus_keyword( $term ) { 
  158. if ( $focus_keyword = WPSEO_Taxonomy_Meta::get_term_meta( 'focuskw', $term->term_id, $term->taxonomy ) ) { 
  159. return $focus_keyword; 
  160.  
  161. return $term->name; 
  162.  
  163. /** 
  164. * Checks if a taxonomy is being added via a POST method. If not, it defaults to a GET request. 
  165. * @return int 
  166. */ 
  167. private function get_taxonomy_input_type() { 
  168.  
  169. if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) { 
  170. return INPUT_POST; 
  171.  
  172. return INPUT_GET; 
  173.  
  174. /** 
  175. * Test whether the metabox should be hidden either by choice of the admin 
  176. * @since 3.1 
  177. * @param string $taxonomy (optional) The post type to test, defaults to the current post post_type. 
  178. * @return bool Whether or not the meta box (and associated columns etc) should be hidden 
  179. */ 
  180. private function is_metabox_hidden( $taxonomy = null ) { 
  181. $get_taxonomy_type = filter_input( $this->get_taxonomy_input_type(), 'taxonomy' ); 
  182.  
  183. if ( ! isset( $taxonomy ) && $get_taxonomy_type ) { 
  184. $taxonomy = sanitize_text_field( $get_taxonomy_type ); 
  185.  
  186. if ( isset( $taxonomy ) ) { 
  187. // Don't make static as taxonomies may still be added during the run. 
  188. $custom_taxonomies = get_taxonomies( array( 'public' => true ), 'names' ); 
  189. $options = get_option( 'wpseo_titles' ); 
  190.  
  191. return ( ( isset( $options[ 'hideeditbox-tax-' . $taxonomy ] ) && $options[ 'hideeditbox-tax-' . $taxonomy ] === true ) || in_array( $taxonomy, $custom_taxonomies ) === false ); 
  192.  
  193. return false;