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

Yoast / wordpress-seo / 7257244093

19 Dec 2023 04:09AM UTC coverage: 49.388% (-0.002%) from 49.39%
7257244093

push

github

web-flow
Merge pull request #20987 from Yoast/JRF/docs/more-fixes

Docs: more fixes

15425 of 31232 relevant lines covered (49.39%)

4.07 hits per line

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

26.18
/admin/class-meta-columns.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin
6
 */
7

8
use Yoast\WP\SEO\Context\Meta_Tags_Context;
9
use Yoast\WP\SEO\Helpers\Score_Icon_Helper;
10
use Yoast\WP\SEO\Integrations\Admin\Admin_Columns_Cache_Integration;
11
use Yoast\WP\SEO\Surfaces\Values\Meta;
12

13
/**
14
 * Class WPSEO_Meta_Columns.
15
 */
16
class WPSEO_Meta_Columns {
17

18
        /**
19
         * Holds the context objects for each indexable.
20
         *
21
         * @var Meta_Tags_Context[]
22
         */
23
        protected $context = [];
24

25
        /**
26
         * Holds the SEO analysis.
27
         *
28
         * @var WPSEO_Metabox_Analysis_SEO
29
         */
30
        private $analysis_seo;
31

32
        /**
33
         * Holds the readability analysis.
34
         *
35
         * @var WPSEO_Metabox_Analysis_Readability
36
         */
37
        private $analysis_readability;
38

39
        /**
40
         * Admin columns cache.
41
         *
42
         * @var Admin_Columns_Cache_Integration
43
         */
44
        private $admin_columns_cache;
45

46
        /**
47
         * Holds the Score_Icon_Helper.
48
         *
49
         * @var Score_Icon_Helper
50
         */
51
        private $score_icon_helper;
52

53
        /**
54
         * When page analysis is enabled, just initialize the hooks.
55
         */
56
        public function __construct() {
×
57
                if ( apply_filters( 'wpseo_use_page_analysis', true ) === true ) {
×
58
                        add_action( 'admin_init', [ $this, 'setup_hooks' ] );
×
59
                }
60

61
                $this->analysis_seo         = new WPSEO_Metabox_Analysis_SEO();
×
62
                $this->analysis_readability = new WPSEO_Metabox_Analysis_Readability();
×
63
                $this->admin_columns_cache  = YoastSEO()->classes->get( Admin_Columns_Cache_Integration::class );
×
64
                $this->score_icon_helper    = YoastSEO()->helpers->score_icon;
×
65
        }
66

67
        /**
68
         * Sets up up the hooks.
69
         */
70
        public function setup_hooks() {
×
71
                $this->set_post_type_hooks();
×
72

73
                if ( $this->analysis_seo->is_enabled() ) {
×
74
                        add_action( 'restrict_manage_posts', [ $this, 'posts_filter_dropdown' ] );
×
75
                }
76

77
                if ( $this->analysis_readability->is_enabled() ) {
×
78
                        add_action( 'restrict_manage_posts', [ $this, 'posts_filter_dropdown_readability' ] );
×
79
                }
80

81
                add_filter( 'request', [ $this, 'column_sort_orderby' ] );
×
82
                add_filter( 'default_hidden_columns', [ $this, 'column_hidden' ], 10, 1 );
×
83
        }
84

85
        /**
86
         * Adds the column headings for the SEO plugin for edit posts / pages overview.
87
         *
88
         * @param array $columns Already existing columns.
89
         *
90
         * @return array Array containing the column headings.
91
         */
92
        public function column_heading( $columns ) {
12✔
93
                if ( $this->display_metabox() === false ) {
12✔
94
                        return $columns;
×
95
                }
96

97
                $added_columns = [];
12✔
98

99
                if ( $this->analysis_seo->is_enabled() ) {
12✔
100
                        $added_columns['wpseo-score'] = '<span class="yoast-column-seo-score yoast-column-header-has-tooltip" data-tooltip-text="'
12✔
101
                                                                                        . esc_attr__( 'SEO score', 'wordpress-seo' )
12✔
102
                                                                                        . '"><span class="screen-reader-text">'
12✔
103
                                                                                        . __( 'SEO score', 'wordpress-seo' )
12✔
104
                                                                                        . '</span></span></span>';
12✔
105
                }
106

107
                if ( $this->analysis_readability->is_enabled() ) {
12✔
108
                        $added_columns['wpseo-score-readability'] = '<span class="yoast-column-readability yoast-column-header-has-tooltip" data-tooltip-text="'
12✔
109
                                                                                                                . esc_attr__( 'Readability score', 'wordpress-seo' )
12✔
110
                                                                                                                . '"><span class="screen-reader-text">'
12✔
111
                                                                                                                . __( 'Readability score', 'wordpress-seo' )
12✔
112
                                                                                                                . '</span></span></span>';
12✔
113
                }
114

115
                $added_columns['wpseo-title']    = __( 'SEO Title', 'wordpress-seo' );
12✔
116
                $added_columns['wpseo-metadesc'] = __( 'Meta Desc.', 'wordpress-seo' );
12✔
117

118
                if ( $this->analysis_seo->is_enabled() ) {
12✔
119
                        $added_columns['wpseo-focuskw'] = __( 'Keyphrase', 'wordpress-seo' );
12✔
120
                }
121

122
                return array_merge( $columns, $added_columns );
12✔
123
        }
124

125
        /**
126
         * Displays the column content for the given column.
127
         *
128
         * @param string $column_name Column to display the content for.
129
         * @param int    $post_id     Post to display the column content for.
130
         */
131
        public function column_content( $column_name, $post_id ) {
×
132
                if ( $this->display_metabox() === false ) {
×
133
                        return;
×
134
                }
135

136
                switch ( $column_name ) {
137
                        case 'wpseo-score':
×
138
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Correctly escaped in render_score_indicator() method.
139
                                echo $this->parse_column_score( $post_id );
×
140

141
                                return;
×
142

143
                        case 'wpseo-score-readability':
×
144
                                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Correctly escaped in render_score_indicator() method.
145
                                echo $this->parse_column_score_readability( $post_id );
×
146

147
                                return;
×
148

149
                        case 'wpseo-title':
×
150
                                $meta = $this->get_meta( $post_id );
×
151
                                if ( $meta ) {
×
152
                                        echo esc_html( $meta->title );
×
153
                                }
154

155
                                return;
×
156

157
                        case 'wpseo-metadesc':
×
158
                                $metadesc_val = '';
×
159
                                $meta         = $this->get_meta( $post_id );
×
160
                                if ( $meta ) {
×
161
                                        $metadesc_val = $meta->meta_description;
×
162
                                }
163
                                if ( $metadesc_val === '' ) {
×
164
                                        echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">',
×
165
                                        /* translators: Hidden accessibility text. */
166
                                        esc_html__( 'Meta description not set.', 'wordpress-seo' ),
×
167
                                        '</span>';
×
168

169
                                        return;
×
170
                                }
171

172
                                echo esc_html( $metadesc_val );
×
173

174
                                return;
×
175

176
                        case 'wpseo-focuskw':
×
177
                                $focuskw_val = WPSEO_Meta::get_value( 'focuskw', $post_id );
×
178

179
                                if ( $focuskw_val === '' ) {
×
180
                                        echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">',
×
181
                                        /* translators: Hidden accessibility text. */
182
                                        esc_html__( 'Focus keyphrase not set.', 'wordpress-seo' ),
×
183
                                        '</span>';
×
184

185
                                        return;
×
186
                                }
187

188
                                echo esc_html( $focuskw_val );
×
189

190
                                return;
×
191
                }
192
        }
193

194
        /**
195
         * Indicates which of the SEO columns are sortable.
196
         *
197
         * @param array $columns Appended with their orderby variable.
198
         *
199
         * @return array Array containing the sortable columns.
200
         */
201
        public function column_sort( $columns ) {
×
202
                if ( $this->display_metabox() === false ) {
×
203
                        return $columns;
×
204
                }
205

206
                $columns['wpseo-metadesc'] = 'wpseo-metadesc';
×
207

208
                if ( $this->analysis_seo->is_enabled() ) {
×
209
                        $columns['wpseo-focuskw'] = 'wpseo-focuskw';
×
210
                        $columns['wpseo-score']   = 'wpseo-score';
×
211
                }
212

213
                if ( $this->analysis_readability->is_enabled() ) {
×
214
                        $columns['wpseo-score-readability'] = 'wpseo-score-readability';
×
215
                }
216

217
                return $columns;
×
218
        }
219

220
        /**
221
         * Hides the SEO title, meta description and focus keyword columns if the user hasn't chosen which columns to hide.
222
         *
223
         * @param array $hidden The hidden columns.
224
         *
225
         * @return array Array containing the columns to hide.
226
         */
227
        public function column_hidden( $hidden ) {
12✔
228
                if ( ! is_array( $hidden ) ) {
12✔
229
                        $hidden = [];
4✔
230
                }
231

232
                array_push( $hidden, 'wpseo-title', 'wpseo-metadesc' );
12✔
233

234
                if ( $this->analysis_seo->is_enabled() ) {
12✔
235
                        $hidden[] = 'wpseo-focuskw';
12✔
236
                }
237

238
                return $hidden;
12✔
239
        }
240

241
        /**
242
         * Adds a dropdown that allows filtering on the posts SEO Quality.
243
         */
244
        public function posts_filter_dropdown() {
×
245
                if ( ! $this->can_display_filter() ) {
×
246
                        return;
×
247
                }
248

249
                $ranks = WPSEO_Rank::get_all_ranks();
×
250

251
                /* translators: Hidden accessibility text. */
252
                echo '<label class="screen-reader-text" for="wpseo-filter">' . esc_html__( 'Filter by SEO Score', 'wordpress-seo' ) . '</label>';
×
253
                echo '<select name="seo_filter" id="wpseo-filter">';
×
254

255
                // phpcs:ignore WordPress.Security.EscapeOutput -- Output is correctly escaped in the generate_option() method.
256
                echo $this->generate_option( '', __( 'All SEO Scores', 'wordpress-seo' ) );
×
257

258
                foreach ( $ranks as $rank ) {
×
259
                        $selected = selected( $this->get_current_seo_filter(), $rank->get_rank(), false );
×
260

261
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Output is correctly escaped in the generate_option() method.
262
                        echo $this->generate_option( $rank->get_rank(), $rank->get_drop_down_label(), $selected );
×
263
                }
264

265
                echo '</select>';
×
266
        }
267

268
        /**
269
         * Adds a dropdown that allows filtering on the posts Readability Quality.
270
         *
271
         * @return void
272
         */
273
        public function posts_filter_dropdown_readability() {
×
274
                if ( ! $this->can_display_filter() ) {
×
275
                        return;
×
276
                }
277

278
                $ranks = WPSEO_Rank::get_all_readability_ranks();
×
279

280
                /* translators: Hidden accessibility text. */
281
                echo '<label class="screen-reader-text" for="wpseo-readability-filter">' . esc_html__( 'Filter by Readability Score', 'wordpress-seo' ) . '</label>';
×
282
                echo '<select name="readability_filter" id="wpseo-readability-filter">';
×
283

284
                // phpcs:ignore WordPress.Security.EscapeOutput -- Output is correctly escaped in the generate_option() method.
285
                echo $this->generate_option( '', __( 'All Readability Scores', 'wordpress-seo' ) );
×
286

287
                foreach ( $ranks as $rank ) {
×
288
                        $selected = selected( $this->get_current_readability_filter(), $rank->get_rank(), false );
×
289

290
                        // phpcs:ignore WordPress.Security.EscapeOutput -- Output is correctly escaped in the generate_option() method.
291
                        echo $this->generate_option( $rank->get_rank(), $rank->get_drop_down_readability_labels(), $selected );
×
292
                }
293

294
                echo '</select>';
×
295
        }
296

297
        /**
298
         * Generates an <option> element.
299
         *
300
         * @param string $value    The option's value.
301
         * @param string $label    The option's label.
302
         * @param string $selected HTML selected attribute for an option.
303
         *
304
         * @return string The generated <option> element.
305
         */
306
        protected function generate_option( $value, $label, $selected = '' ) {
×
307
                return '<option ' . $selected . ' value="' . esc_attr( $value ) . '">' . esc_html( $label ) . '</option>';
×
308
        }
309

310
        /**
311
         * Returns the meta object for a given post ID.
312
         *
313
         * @param int $post_id The post ID.
314
         *
315
         * @return Meta The meta object.
316
         */
317
        protected function get_meta( $post_id ) {
×
318
                $indexable = $this->admin_columns_cache->get_indexable( $post_id );
×
319

320
                return YoastSEO()->meta->for_indexable( $indexable, 'Post_Type' );
×
321
        }
322

323
        /**
324
         * Determines the SEO score filter to be later used in the meta query, based on the passed SEO filter.
325
         *
326
         * @param string $seo_filter The SEO filter to use to determine what further filter to apply.
327
         *
328
         * @return array The SEO score filter.
329
         */
330
        protected function determine_seo_filters( $seo_filter ) {
24✔
331
                if ( $seo_filter === WPSEO_Rank::NO_FOCUS ) {
24✔
332
                        return $this->create_no_focus_keyword_filter();
4✔
333
                }
334

335
                if ( $seo_filter === WPSEO_Rank::NO_INDEX ) {
20✔
336
                        return $this->create_no_index_filter();
4✔
337
                }
338

339
                $rank = new WPSEO_Rank( $seo_filter );
16✔
340

341
                return $this->create_seo_score_filter( $rank->get_starting_score(), $rank->get_end_score() );
16✔
342
        }
343

344
        /**
345
         * Determines the Readability score filter to the meta query, based on the passed Readability filter.
346
         *
347
         * @param string $readability_filter The Readability filter to use to determine what further filter to apply.
348
         *
349
         * @return array The Readability score filter.
350
         */
351
        protected function determine_readability_filters( $readability_filter ) {
12✔
352
                $rank = new WPSEO_Rank( $readability_filter );
12✔
353

354
                return $this->create_readability_score_filter( $rank->get_starting_score(), $rank->get_end_score() );
12✔
355
        }
356

357
        /**
358
         * Creates a keyword filter for the meta query, based on the passed Keyword filter.
359
         *
360
         * @param string $keyword_filter The keyword filter to use.
361
         *
362
         * @return array The keyword filter.
363
         */
364
        protected function get_keyword_filter( $keyword_filter ) {
×
365
                return [
366
                        'post_type' => get_query_var( 'post_type', 'post' ),
×
367
                        'key'       => WPSEO_Meta::$meta_prefix . 'focuskw',
×
368
                        'value'     => sanitize_text_field( $keyword_filter ),
×
369
                ];
370
        }
371

372
        /**
373
         * Determines whether the passed filter is considered to be valid.
374
         *
375
         * @param mixed $filter The filter to check against.
376
         *
377
         * @return bool Whether the filter is considered valid.
378
         */
379
        protected function is_valid_filter( $filter ) {
8✔
380
                return ! empty( $filter ) && is_string( $filter );
8✔
381
        }
382

383
        /**
384
         * Collects the filters and merges them into a single array.
385
         *
386
         * @return array Array containing all the applicable filters.
387
         */
388
        protected function collect_filters() {
×
389
                $active_filters = [];
×
390

391
                $seo_filter             = $this->get_current_seo_filter();
×
392
                $readability_filter     = $this->get_current_readability_filter();
×
393
                $current_keyword_filter = $this->get_current_keyword_filter();
×
394

395
                if ( $this->is_valid_filter( $seo_filter ) ) {
×
396
                        $active_filters = array_merge(
×
397
                                $active_filters,
×
398
                                $this->determine_seo_filters( $seo_filter )
×
399
                        );
400
                }
401

402
                if ( $this->is_valid_filter( $readability_filter ) ) {
×
403
                        $active_filters = array_merge(
×
404
                                $active_filters,
×
405
                                $this->determine_readability_filters( $readability_filter )
×
406
                        );
407
                }
408

409
                if ( $this->is_valid_filter( $current_keyword_filter ) ) {
×
410
                        /**
411
                         * Adapt the meta query used to filter the post overview on keyphrase.
412
                         *
413
                         * @internal
414
                         *
415
                         * @param array $keyphrase      The keyphrase used in the filter.
416
                         * @param array $keyword_filter The current keyword filter.
417
                         */
418
                        $keyphrase_filter = apply_filters(
×
419
                                'wpseo_change_keyphrase_filter_in_request',
×
420
                                $this->get_keyword_filter( $current_keyword_filter ),
×
421
                                $current_keyword_filter
×
422
                        );
423

424
                        if ( is_array( $keyphrase_filter ) ) {
×
425
                                $active_filters = array_merge(
×
426
                                        $active_filters,
×
427
                                        [ $keyphrase_filter ]
×
428
                                );
429
                        }
430
                }
431

432
                /**
433
                 * Adapt the active applicable filters on the posts overview.
434
                 *
435
                 * @internal
436
                 *
437
                 * @param array $active_filters The current applicable filters.
438
                 */
439
                return apply_filters( 'wpseo_change_applicable_filters', $active_filters );
×
440
        }
441

442
        /**
443
         * Modify the query based on the filters that are being passed.
444
         *
445
         * @param array $vars Query variables that need to be modified based on the filters.
446
         *
447
         * @return array Array containing the meta query to use for filtering the posts overview.
448
         */
449
        public function column_sort_orderby( $vars ) {
×
450
                $collected_filters = $this->collect_filters();
×
451

452
                $order_by_column = $vars['orderby'];
×
453
                if ( isset( $order_by_column ) ) {
×
454
                        // Based on the selected column, create a meta query.
455
                        $order_by = $this->filter_order_by( $order_by_column );
×
456

457
                        /**
458
                         * Adapt the order by part of the query on the posts overview.
459
                         *
460
                         * @internal
461
                         *
462
                         * @param array  $order_by        The current order by.
463
                         * @param string $order_by_column The current order by column.
464
                         */
465
                        $order_by = apply_filters( 'wpseo_change_order_by', $order_by, $order_by_column );
×
466

467
                        $vars = array_merge( $vars, $order_by );
×
468
                }
469

470
                return $this->build_filter_query( $vars, $collected_filters );
×
471
        }
472

473
        /**
474
         * Retrieves the meta robots query values to be used within the meta query.
475
         *
476
         * @return array Array containing the query parameters regarding meta robots.
477
         */
478
        protected function get_meta_robots_query_values() {
×
479
                return [
480
                        'relation' => 'OR',
×
481
                        [
482
                                'key'     => WPSEO_Meta::$meta_prefix . 'meta-robots-noindex',
×
483
                                'compare' => 'NOT EXISTS',
×
484
                        ],
485
                        [
486
                                'key'     => WPSEO_Meta::$meta_prefix . 'meta-robots-noindex',
×
487
                                'value'   => '1',
×
488
                                'compare' => '!=',
×
489
                        ],
490
                ];
491
        }
492

493
        /**
494
         * Determines the score filters to be used. If more than one is passed, it created an AND statement for the query.
495
         *
496
         * @param array $score_filters Array containing the score filters.
497
         *
498
         * @return array Array containing the score filters that need to be applied to the meta query.
499
         */
500
        protected function determine_score_filters( $score_filters ) {
×
501
                if ( count( $score_filters ) > 1 ) {
×
502
                        return array_merge( [ 'relation' => 'AND' ], $score_filters );
×
503
                }
504

505
                return $score_filters;
×
506
        }
507

508
        /**
509
         * Retrieves the post type from the $_GET variable.
510
         *
511
         * @return string|null The sanitized current post type or null when the variable is not set in $_GET.
512
         */
513
        public function get_current_post_type() {
12✔
514
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
515
                if ( isset( $_GET['post_type'] ) && is_string( $_GET['post_type'] ) ) {
12✔
516
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
517
                        return sanitize_text_field( wp_unslash( $_GET['post_type'] ) );
4✔
518
                }
519
                return null;
8✔
520
        }
521

522
        /**
523
         * Retrieves the SEO filter from the $_GET variable.
524
         *
525
         * @return string|null The sanitized seo filter or null when the variable is not set in $_GET.
526
         */
527
        public function get_current_seo_filter() {
12✔
528
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
529
                if ( isset( $_GET['seo_filter'] ) && is_string( $_GET['seo_filter'] ) ) {
12✔
530
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
531
                        return sanitize_text_field( wp_unslash( $_GET['seo_filter'] ) );
4✔
532
                }
533
                return null;
8✔
534
        }
535

536
        /**
537
         * Retrieves the Readability filter from the $_GET variable.
538
         *
539
         * @return string|null The sanitized readability filter or null when the variable is not set in $_GET.
540
         */
541
        public function get_current_readability_filter() {
12✔
542
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
543
                if ( isset( $_GET['readability_filter'] ) && is_string( $_GET['readability_filter'] ) ) {
12✔
544
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
545
                        return sanitize_text_field( wp_unslash( $_GET['readability_filter'] ) );
4✔
546
                }
547
                return null;
8✔
548
        }
549

550
        /**
551
         * Retrieves the keyword filter from the $_GET variable.
552
         *
553
         * @return string|null The sanitized seo keyword filter or null when the variable is not set in $_GET.
554
         */
555
        public function get_current_keyword_filter() {
12✔
556
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
557
                if ( isset( $_GET['seo_kw_filter'] ) && is_string( $_GET['seo_kw_filter'] ) ) {
12✔
558
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
559
                        return sanitize_text_field( wp_unslash( $_GET['seo_kw_filter'] ) );
4✔
560
                }
561
                return null;
8✔
562
        }
563

564
        /**
565
         * Uses the vars to create a complete filter query that can later be executed to filter out posts.
566
         *
567
         * @param array $vars    Array containing the variables that will be used in the meta query.
568
         * @param array $filters Array containing the filters that we need to apply in the meta query.
569
         *
570
         * @return array Array containing the complete filter query.
571
         */
572
        protected function build_filter_query( $vars, $filters ) {
16✔
573
                // If no filters were applied, just return everything.
574
                if ( count( $filters ) === 0 ) {
16✔
575
                        return $vars;
4✔
576
                }
577

578
                $result               = [ 'meta_query' => [] ];
12✔
579
                $result['meta_query'] = array_merge( $result['meta_query'], [ $this->determine_score_filters( $filters ) ] );
12✔
580

581
                $current_seo_filter = $this->get_current_seo_filter();
12✔
582

583
                // This only applies for the SEO score filter because it can because the SEO score can be altered by the no-index option.
584
                if ( $this->is_valid_filter( $current_seo_filter ) && ! in_array( $current_seo_filter, [ WPSEO_Rank::NO_INDEX, WPSEO_Rank::NO_FOCUS ], true ) ) {
12✔
585
                        $result['meta_query'] = array_merge( $result['meta_query'], [ $this->get_meta_robots_query_values() ] );
×
586
                }
587

588
                return array_merge( $vars, $result );
12✔
589
        }
590

591
        /**
592
         * Creates a Readability score filter.
593
         *
594
         * @param number $low  The lower boundary of the score.
595
         * @param number $high The higher boundary of the score.
596
         *
597
         * @return array The Readability Score filter.
598
         */
599
        protected function create_readability_score_filter( $low, $high ) {
×
600
                return [
601
                        [
602
                                'key'     => WPSEO_Meta::$meta_prefix . 'content_score',
×
603
                                'value'   => [ $low, $high ],
×
604
                                'type'    => 'numeric',
×
605
                                'compare' => 'BETWEEN',
×
606
                        ],
607
                ];
608
        }
609

610
        /**
611
         * Creates an SEO score filter.
612
         *
613
         * @param number $low  The lower boundary of the score.
614
         * @param number $high The higher boundary of the score.
615
         *
616
         * @return array The SEO score filter.
617
         */
618
        protected function create_seo_score_filter( $low, $high ) {
×
619
                return [
620
                        [
621
                                'key'     => WPSEO_Meta::$meta_prefix . 'linkdex',
×
622
                                'value'   => [ $low, $high ],
×
623
                                'type'    => 'numeric',
×
624
                                'compare' => 'BETWEEN',
×
625
                        ],
626
                ];
627
        }
628

629
        /**
630
         * Creates a filter to retrieve posts that were set to no-index.
631
         *
632
         * @return array Array containin the no-index filter.
633
         */
634
        protected function create_no_index_filter() {
×
635
                return [
636
                        [
637
                                'key'     => WPSEO_Meta::$meta_prefix . 'meta-robots-noindex',
×
638
                                'value'   => '1',
×
639
                                'compare' => '=',
×
640
                        ],
641
                ];
642
        }
643

644
        /**
645
         * Creates a filter to retrieve posts that have no keyword set.
646
         *
647
         * @return array Array containing the no focus keyword filter.
648
         */
649
        protected function create_no_focus_keyword_filter() {
×
650
                return [
651
                        [
652
                                'key'     => WPSEO_Meta::$meta_prefix . 'meta-robots-noindex',
×
653
                                'value'   => 'needs-a-value-anyway',
×
654
                                'compare' => 'NOT EXISTS',
×
655
                        ],
656
                        [
657
                                'key'     => WPSEO_Meta::$meta_prefix . 'linkdex',
×
658
                                'value'   => 'needs-a-value-anyway',
×
659
                                'compare' => 'NOT EXISTS',
×
660
                        ],
661
                ];
662
        }
663

664
        /**
665
         * Determines whether a particular post_id is of an indexable post type.
666
         *
667
         * @param string $post_id The post ID to check.
668
         *
669
         * @return bool Whether or not it is indexable.
670
         */
671
        protected function is_indexable( $post_id ) {
16✔
672
                if ( ! empty( $post_id ) && ! $this->uses_default_indexing( $post_id ) ) {
16✔
673
                        return WPSEO_Meta::get_value( 'meta-robots-noindex', $post_id ) === '2';
4✔
674
                }
675

676
                $post = get_post( $post_id );
12✔
677

678
                if ( is_object( $post ) ) {
12✔
679
                        // If the option is false, this means we want to index it.
680
                        return WPSEO_Options::get( 'noindex-' . $post->post_type, false ) === false;
8✔
681
                }
682

683
                return true;
4✔
684
        }
685

686
        /**
687
         * Determines whether the given post ID uses the default indexing settings.
688
         *
689
         * @param int $post_id The post ID to check.
690
         *
691
         * @return bool Whether or not the default indexing is being used for the post.
692
         */
693
        protected function uses_default_indexing( $post_id ) {
8✔
694
                return WPSEO_Meta::get_value( 'meta-robots-noindex', $post_id ) === '0';
8✔
695
        }
696

697
        /**
698
         * Returns filters when $order_by is matched in the if-statement.
699
         *
700
         * @param string $order_by The ID of the column by which to order the posts.
701
         *
702
         * @return array Array containing the order filters.
703
         */
704
        private function filter_order_by( $order_by ) {
×
705
                switch ( $order_by ) {
706
                        case 'wpseo-metadesc':
×
707
                                return [
708
                                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Reason: Only used when user requests sorting.
709
                                        'meta_key' => WPSEO_Meta::$meta_prefix . 'metadesc',
×
710
                                        'orderby'  => 'meta_value',
×
711
                                ];
712

713
                        case 'wpseo-focuskw':
×
714
                                return [
715
                                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Reason: Only used when user requests sorting.
716
                                        'meta_key' => WPSEO_Meta::$meta_prefix . 'focuskw',
×
717
                                        'orderby'  => 'meta_value',
×
718
                                ];
719

720
                        case 'wpseo-score':
×
721
                                return [
722
                                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Reason: Only used when user requests sorting.
723
                                        'meta_key' => WPSEO_Meta::$meta_prefix . 'linkdex',
×
724
                                        'orderby'  => 'meta_value_num',
×
725
                                ];
726

727
                        case 'wpseo-score-readability':
×
728
                                return [
729
                                        // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Reason: Only used when user requests sorting.
730
                                        'meta_key' => WPSEO_Meta::$meta_prefix . 'content_score',
×
731
                                        'orderby'  => 'meta_value_num',
×
732
                                ];
733
                }
734

735
                return [];
×
736
        }
737

738
        /**
739
         * Parses the score column.
740
         *
741
         * @param int $post_id The ID of the post for which to show the score.
742
         *
743
         * @return string The HTML for the SEO score indicator.
744
         */
745
        private function parse_column_score( $post_id ) {
×
746
                $meta = $this->get_meta( $post_id );
×
747

748
                if ( $meta ) {
×
749
                        return $this->score_icon_helper->for_seo( $meta->indexable, '', __( 'Post is set to noindex.', 'wordpress-seo' ) );
×
750
                }
751
        }
752

753
        /**
754
         * Parsing the readability score column.
755
         *
756
         * @param int $post_id The ID of the post for which to show the readability score.
757
         *
758
         * @return string The HTML for the readability score indicator.
759
         */
760
        private function parse_column_score_readability( $post_id ) {
×
761
                $meta = $this->get_meta( $post_id );
×
762
                if ( $meta ) {
×
763
                        return $this->score_icon_helper->for_readability( $meta->indexable->readability_score );
×
764
                }
765
        }
766

767
        /**
768
         * Sets up the hooks for the post_types.
769
         */
770
        private function set_post_type_hooks() {
×
771
                $post_types = WPSEO_Post_Type::get_accessible_post_types();
×
772

773
                if ( ! is_array( $post_types ) || $post_types === [] ) {
×
774
                        return;
×
775
                }
776

777
                foreach ( $post_types as $post_type ) {
×
778
                        if ( $this->display_metabox( $post_type ) === false ) {
×
779
                                continue;
×
780
                        }
781

782
                        add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'column_heading' ], 10, 1 );
×
783
                        add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'column_content' ], 10, 2 );
×
784
                        add_action( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'column_sort' ], 10, 2 );
×
785
                }
786

787
                unset( $post_type );
×
788
        }
789

790
        /**
791
         * Wraps the WPSEO_Metabox check to determine whether the metabox should be displayed either by
792
         * choice of the admin or because the post type is not a public post type.
793
         *
794
         * @since 7.0
795
         *
796
         * @param string|null $post_type Optional. The post type to test, defaults to the current post post_type.
797
         *
798
         * @return bool Whether or not the meta box (and associated columns etc) should be hidden.
799
         */
800
        private function display_metabox( $post_type = null ) {
×
801
                $current_post_type = $this->get_current_post_type();
×
802

803
                if ( ! isset( $post_type ) && ! empty( $current_post_type ) ) {
×
804
                        $post_type = $current_post_type;
×
805
                }
806

807
                return WPSEO_Utils::is_metabox_active( $post_type, 'post_type' );
×
808
        }
809

810
        /**
811
         * Determines whether or not filter dropdowns should be displayed.
812
         *
813
         * @return bool Whether or the current page can display the filter drop downs.
814
         */
815
        public function can_display_filter() {
×
816
                if ( $GLOBALS['pagenow'] === 'upload.php' ) {
×
817
                        return false;
×
818
                }
819

820
                if ( $this->display_metabox() === false ) {
×
821
                        return false;
×
822
                }
823

824
                $screen = get_current_screen();
×
825
                if ( $screen === null ) {
×
826
                        return false;
×
827
                }
828

829
                return WPSEO_Post_Type::is_post_type_accessible( $screen->post_type );
×
830
        }
831
}
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