• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6638054992

25 Oct 2023 08:55AM UTC coverage: 49.106%. First build
6638054992

Pull #20653

github

vraja-pro
Merge remote-tracking branch 'origin/feature/upgrade-react-and-tests' into feature/upgrade-react-and-tests
Pull Request #20653: Feature/upgrade react and tests

64 of 157 new or added lines in 34 files covered. (40.76%)

13135 of 26748 relevant lines covered (49.11%)

3.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/admin/taxonomy/class-taxonomy-columns.php
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() ) {
×
NEW
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">' .
×
NEW
88
                                                                                        __( 'SEO score', 'wordpress-seo' ) . '</span></span></span>';
×
89
                        }
90

91
                        if ( $column_name === 'description' && $this->analysis_readability->is_enabled() ) {
×
NEW
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">' .
×
NEW
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
                else {
135
                        if ( isset( $_GET['taxonomy'] ) && is_string( $_GET['taxonomy'] ) ) {
×
136
                                return sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) );
×
137
                        }
138
                }
139
                // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
140
                return null;
×
141
        }
142

143
        /**
144
         * Returns the posted/get taxonomy value if it is set.
145
         *
146
         * @return string|null
147
         */
148
        private function get_taxonomy() {
149
                // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
150
                if ( wp_doing_ajax() ) {
×
151
                        if ( isset( $_POST['taxonomy'] ) && is_string( $_POST['taxonomy'] ) ) {
×
152
                                return sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) );
×
153
                        }
154
                }
155
                else {
156
                        if ( isset( $_GET['taxonomy'] ) && is_string( $_GET['taxonomy'] ) ) {
×
157
                                return sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) );
×
158
                        }
159
                }
160
                // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
161
                return null;
×
162
        }
163

164
        /**
165
         * Parses the value for the score column.
166
         *
167
         * @param int $term_id ID of requested term.
168
         *
169
         * @return string
170
         */
171
        private function get_score_value( $term_id ) {
172
                $indexable = $this->indexable_repository->find_by_id_and_type( (int) $term_id, 'term' );
×
173

174
                return $this->score_icon_helper->for_seo( $indexable, '', __( 'Term is set to noindex.', 'wordpress-seo' ) );
×
175
        }
176

177
        /**
178
         * Parses the value for the readability score column.
179
         *
180
         * @param int $term_id ID of the requested term.
181
         *
182
         * @return string The HTML for the readability score indicator.
183
         */
184
        private function get_score_readability_value( $term_id ) {
185
                $score = (int) WPSEO_Taxonomy_Meta::get_term_meta( $term_id, $this->taxonomy, 'content_score' );
×
186

187
                return $this->score_icon_helper->for_readability( $score );
×
188
        }
189

190
        /**
191
         * Check if the taxonomy is indexable.
192
         *
193
         * @param mixed $term The current term.
194
         *
195
         * @return bool Whether the term is indexable.
196
         */
197
        private function is_indexable( $term ) {
198
                // When the no_index value is not empty and not default, check if its value is index.
199
                $no_index = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $this->taxonomy, 'noindex' );
×
200

201
                // Check if the default for taxonomy is empty (this will be index).
202
                if ( ! empty( $no_index ) && $no_index !== 'default' ) {
×
203
                        return ( $no_index === 'index' );
×
204
                }
205

206
                if ( is_object( $term ) ) {
×
207
                        $no_index_key = 'noindex-tax-' . $term->taxonomy;
×
208

209
                        // If the option is false, this means we want to index it.
210
                        return WPSEO_Options::get( $no_index_key, false ) === false;
×
211
                }
212

213
                return true;
×
214
        }
215

216
        /**
217
         * Wraps the WPSEO_Metabox check to determine whether the metabox should be displayed either by
218
         * choice of the admin or because the taxonomy is not public.
219
         *
220
         * @since 7.0
221
         *
222
         * @param string|null $taxonomy Optional. The taxonomy to test, defaults to the current taxonomy.
223
         *
224
         * @return bool Whether the meta box (and associated columns etc) should be hidden.
225
         */
226
        private function display_metabox( $taxonomy = null ) {
227
                $current_taxonomy = $this->get_current_taxonomy();
×
228

229
                if ( ! isset( $taxonomy ) && ! empty( $current_taxonomy ) ) {
×
230
                        $taxonomy = $current_taxonomy;
×
231
                }
232

233
                return WPSEO_Utils::is_metabox_active( $taxonomy, 'taxonomy' );
×
234
        }
235
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc