class-taxonomy-columns.php 7.2 KB

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