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