class-taxonomy-columns.php 7.1 KB

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