| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Helpers\Score_Icon_Helper; |
| 9 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class adds columns to the taxonomy table. |
| 13 |
*/ |
| 14 |
class WPSEO_Taxonomy_Columns { |
| 15 |
|
| 16 |
/** |
| 17 |
* The SEO analysis. |
| 18 |
* |
| 19 |
* @var WPSEO_Metabox_Analysis_SEO |
| 20 |
*/ |
| 21 |
private $analysis_seo; |
| 22 |
|
| 23 |
/** |
| 24 |
* The readability analysis. |
| 25 |
* |
| 26 |
* @var WPSEO_Metabox_Analysis_Readability |
| 27 |
*/ |
| 28 |
private $analysis_readability; |
| 29 |
|
| 30 |
/** |
| 31 |
* The current taxonomy. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $taxonomy; |
| 36 |
|
| 37 |
/** |
| 38 |
* Holds the Indexable_Repository. |
| 39 |
* |
| 40 |
* @var Indexable_Repository |
| 41 |
*/ |
| 42 |
protected $indexable_repository; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds the Score_Icon_Helper. |
| 46 |
* |
| 47 |
* @var Score_Icon_Helper |
| 48 |
*/ |
| 49 |
protected $score_icon_helper; |
| 50 |
|
| 51 |
/** |
| 52 |
* WPSEO_Taxonomy_Columns constructor. |
| 53 |
*/ |
| 54 |
public function __construct() { |
| 55 |
|
| 56 |
$this->taxonomy = $this->get_taxonomy(); |
| 57 |
|
| 58 |
if ( ! empty( $this->taxonomy ) ) { |
| 59 |
add_filter( 'manage_edit-' . $this->taxonomy . '_columns', [ $this, 'add_columns' ] ); |
| 60 |
add_filter( 'manage_' . $this->taxonomy . '_custom_column', [ $this, 'parse_column' ], 10, 3 ); |
| 61 |
} |
| 62 |
|
| 63 |
$this->analysis_seo = new WPSEO_Metabox_Analysis_SEO(); |
| 64 |
$this->analysis_readability = new WPSEO_Metabox_Analysis_Readability(); |
| 65 |
$this->indexable_repository = YoastSEO()->classes->get( Indexable_Repository::class ); |
| 66 |
$this->score_icon_helper = YoastSEO()->helpers->score_icon; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Adds an SEO score column to the terms table, right after the description column. |
| 71 |
* |
| 72 |
* @param array $columns Current set columns. |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
public function add_columns( array $columns ) { |
| 77 |
if ( $this->display_metabox( $this->taxonomy ) === false ) { |
| 78 |
return $columns; |
| 79 |
} |
| 80 |
|
| 81 |
$new_columns = []; |
| 82 |
|
| 83 |
foreach ( $columns as $column_name => $column_value ) { |
| 84 |
$new_columns[ $column_name ] = $column_value; |
| 85 |
|
| 86 |
if ( $column_name === 'description' && $this->analysis_seo->is_enabled() ) { |
| 87 |
$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">' |
| 88 |
. __( 'SEO score', 'wordpress-seo' ) . '</span></span></span>'; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $column_name === 'description' && $this->analysis_readability->is_enabled() ) { |
| 92 |
$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">' |
| 93 |
. __( 'Readability score', 'wordpress-seo' ) . '</span></span></span>'; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $new_columns; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Parses the column. |
| 102 |
* |
| 103 |
* @param string $content The current content of the column. |
| 104 |
* @param string $column_name The name of the column. |
| 105 |
* @param int $term_id ID of requested taxonomy. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function parse_column( $content, $column_name, $term_id ) { |
| 110 |
|
| 111 |
switch ( $column_name ) { |
| 112 |
case 'wpseo-score': |
| 113 |
return $this->get_score_value( $term_id ); |
| 114 |
|
| 115 |
case 'wpseo-score-readability': |
| 116 |
return $this->get_score_readability_value( $term_id ); |
| 117 |
} |
| 118 |
|
| 119 |
return $content; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves the taxonomy from the $_GET or $_POST variable. |
| 124 |
* |
| 125 |
* @return string|null The current taxonomy or null when it is not set. |
| 126 |
*/ |
| 127 |
public function get_current_taxonomy() { |
| 128 |
// phpcs:disable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 129 |
if ( ! empty( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) { |
| 130 |
if ( isset( $_POST['taxonomy'] ) && is_string( $_POST['taxonomy'] ) ) { |
| 131 |
return sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ); |
| 132 |
} |
| 133 |
} |
| 134 |
elseif ( isset( $_GET['taxonomy'] ) && is_string( $_GET['taxonomy'] ) ) { |
| 135 |
return sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); |
| 136 |
} |
| 137 |
// phpcs:enable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended |
| 138 |
return null; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the posted/get taxonomy value if it is set. |
| 143 |
* |
| 144 |
* @return string|null |
| 145 |
*/ |
| 146 |
private function get_taxonomy() { |
| 147 |
// phpcs:disable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 148 |
if ( wp_doing_ajax() ) { |
| 149 |
if ( isset( $_POST['taxonomy'] ) && is_string( $_POST['taxonomy'] ) ) { |
| 150 |
return sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ); |
| 151 |
} |
| 152 |
} |
| 153 |
elseif ( isset( $_GET['taxonomy'] ) && is_string( $_GET['taxonomy'] ) ) { |
| 154 |
return sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); |
| 155 |
} |
| 156 |
// phpcs:enable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Parses the value for the score column. |
| 162 |
* |
| 163 |
* @param int $term_id ID of requested term. |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
private function get_score_value( $term_id ) { |
| 168 |
$indexable = $this->indexable_repository->find_by_id_and_type( (int) $term_id, 'term' ); |
| 169 |
|
| 170 |
return $this->score_icon_helper->for_seo( $indexable, '', __( 'Term is set to noindex.', 'wordpress-seo' ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Parses the value for the readability score column. |
| 175 |
* |
| 176 |
* @param int $term_id ID of the requested term. |
| 177 |
* |
| 178 |
* @return string The HTML for the readability score indicator. |
| 179 |
*/ |
| 180 |
private function get_score_readability_value( $term_id ) { |
| 181 |
$score = (int) WPSEO_Taxonomy_Meta::get_term_meta( $term_id, $this->taxonomy, 'content_score' ); |
| 182 |
|
| 183 |
return $this->score_icon_helper->for_readability( $score ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Check if the taxonomy is indexable. |
| 188 |
* |
| 189 |
* @param mixed $term The current term. |
| 190 |
* |
| 191 |
* @return bool Whether the term is indexable. |
| 192 |
*/ |
| 193 |
private function is_indexable( $term ) { |
| 194 |
// When the no_index value is not empty and not default, check if its value is index. |
| 195 |
$no_index = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $this->taxonomy, 'noindex' ); |
| 196 |
|
| 197 |
// Check if the default for taxonomy is empty (this will be index). |
| 198 |
if ( ! empty( $no_index ) && $no_index !== 'default' ) { |
| 199 |
return ( $no_index === 'index' ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( is_object( $term ) ) { |
| 203 |
$no_index_key = 'noindex-tax-' . $term->taxonomy; |
| 204 |
|
| 205 |
// If the option is false, this means we want to index it. |
| 206 |
return WPSEO_Options::get( $no_index_key, false ) === false; |
| 207 |
} |
| 208 |
|
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Wraps the WPSEO_Metabox check to determine whether the metabox should be displayed either by |
| 214 |
* choice of the admin or because the taxonomy is not public. |
| 215 |
* |
| 216 |
* @since 7.0 |
| 217 |
* |
| 218 |
* @param string|null $taxonomy Optional. The taxonomy to test, defaults to the current taxonomy. |
| 219 |
* |
| 220 |
* @return bool Whether the meta box (and associated columns etc) should be hidden. |
| 221 |
*/ |
| 222 |
private function display_metabox( $taxonomy = null ) { |
| 223 |
$current_taxonomy = $this->get_current_taxonomy(); |
| 224 |
|
| 225 |
if ( ! isset( $taxonomy ) && ! empty( $current_taxonomy ) ) { |
| 226 |
$taxonomy = $current_taxonomy; |
| 227 |
} |
| 228 |
|
| 229 |
return WPSEO_Utils::is_metabox_active( $taxonomy, 'taxonomy' ); |
| 230 |
} |
| 231 |
} |
| 232 |
|