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

Yoast / wordpress-seo / 6378904747

02 Oct 2023 10:24AM UTC coverage: 45.763% (+0.08%) from 45.688%
6378904747

push

github

igorschoester
Merge remote-tracking branch 'origin/trunk' into feature/upsell-updates

Conflicts:
	admin/class-premium-upsell-admin-block.php
	packages/js/src/components/fills/MetaboxFill.js
	packages/js/src/components/fills/SidebarFill.js
	packages/js/src/elementor/components/fills/ElementorFill.js

56 of 94 new or added lines in 15 files covered. (59.57%)

551 existing lines in 7 files now uncovered.

12201 of 26661 relevant lines covered (45.76%)

3.41 hits per line

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

0.0
/src/integrations/admin/link-count-columns-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WP_Query;
6
use wpdb;
7
use Yoast\WP\Lib\Model;
8
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action;
9
use Yoast\WP\SEO\Conditionals\Admin\Posts_Overview_Or_Ajax_Conditional;
10
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
11
use Yoast\WP\SEO\Conditionals\Should_Index_Links_Conditional;
12
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
13
use Yoast\WP\SEO\Integrations\Integration_Interface;
14

15
/**
16
 * Link_Count_Columns_Integration class.
17
 */
18
class Link_Count_Columns_Integration implements Integration_Interface {
19

20
        /**
21
         * Partial column name.
22
         *
23
         * @var string
24
         */
25
        const COLUMN_LINKED = 'linked';
26

27
        /**
28
         * Partial column name.
29
         *
30
         * @var string
31
         */
32
        const COLUMN_LINKS = 'links';
33

34
        /**
35
         * The post type helper.
36
         *
37
         * @var Post_Type_Helper
38
         */
39
        protected $post_type_helper;
40

41
        /**
42
         * The database object.
43
         *
44
         * @var wpdb
45
         */
46
        protected $wpdb;
47

48
        /**
49
         * The post link builder.
50
         *
51
         * @var Post_Link_Indexing_Action
52
         */
53
        protected $post_link_indexing_action;
54

55
        /**
56
         * The admin columns cache.
57
         *
58
         * @var Admin_Columns_Cache_Integration
59
         */
60
        protected $admin_columns_cache;
61

62
        /**
63
         * {@inheritDoc}
64
         */
65
        public static function get_conditionals() {
66
                return [
67
                        Admin_Conditional::class,
×
68
                        Posts_Overview_Or_Ajax_Conditional::class,
69
                        Should_Index_Links_Conditional::class,
70
                ];
71
        }
72

73
        /**
74
         * Link_Count_Columns_Integration constructor
75
         *
76
         * @param Post_Type_Helper                $post_type_helper          The post type helper.
77
         * @param wpdb                            $wpdb                      The wpdb object.
78
         * @param Post_Link_Indexing_Action       $post_link_indexing_action The post link indexing action.
79
         * @param Admin_Columns_Cache_Integration $admin_columns_cache       The admin columns cache.
80
         */
81
        public function __construct(
82
                Post_Type_Helper $post_type_helper,
83
                wpdb $wpdb,
84
                Post_Link_Indexing_Action $post_link_indexing_action,
85
                Admin_Columns_Cache_Integration $admin_columns_cache
86
        ) {
87
                $this->post_type_helper          = $post_type_helper;
×
88
                $this->wpdb                      = $wpdb;
×
89
                $this->post_link_indexing_action = $post_link_indexing_action;
×
90
                $this->admin_columns_cache       = $admin_columns_cache;
×
91
        }
92

93
        /**
94
         * {@inheritDoc}
95
         */
96
        public function register_hooks() {
97
                \add_filter( 'posts_clauses', [ $this, 'order_by_links' ], 1, 2 );
×
98
                \add_filter( 'posts_clauses', [ $this, 'order_by_linked' ], 1, 2 );
×
99

100
                \add_action( 'admin_init', [ $this, 'register_init_hooks' ] );
×
101

102
                // Adds a filter to exclude the attachments from the link count.
103
                \add_filter( 'wpseo_link_count_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] );
×
104
        }
105

106
        /**
107
         * Register hooks that need to be registered after `init` due to all post types not yet being registered.
108
         */
109
        public function register_init_hooks() {
110
                $public_post_types = \apply_filters( 'wpseo_link_count_post_types', $this->post_type_helper->get_accessible_post_types() );
×
111

112
                if ( ! \is_array( $public_post_types ) || empty( $public_post_types ) ) {
×
113
                        return;
×
114
                }
115

116
                foreach ( $public_post_types as $post_type ) {
×
117
                        \add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_post_columns' ] );
×
118
                        \add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'column_content' ], 10, 2 );
×
119
                        \add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'column_sort' ] );
×
120
                }
121
        }
122

123
        /**
124
         * Adds the columns for the post overview.
125
         *
126
         * @param array $columns Array with columns.
127
         *
128
         * @return array The extended array with columns.
129
         */
130
        public function add_post_columns( $columns ) {
131
                if ( ! \is_array( $columns ) ) {
×
132
                        return $columns;
×
133
                }
134

135
                $columns[ 'wpseo-' . self::COLUMN_LINKS ] = \sprintf(
×
136
                        '<span class="yoast-linked-to yoast-column-header-has-tooltip" data-tooltip-text="%1$s"><span class="screen-reader-text">%2$s</span></span>',
×
137
                        \esc_attr__( 'Number of outgoing internal links in this post. See "Yoast Columns" text in the help tab for more info.', 'wordpress-seo' ),
×
138
                        /* translators: Hidden accessibility text. */
UNCOV
139
                        \esc_html__( 'Outgoing internal links', 'wordpress-seo' )
×
140
                );
141

142
                if ( $this->post_link_indexing_action->get_total_unindexed() === 0 ) {
×
143
                        $columns[ 'wpseo-' . self::COLUMN_LINKED ] = \sprintf(
×
144
                                '<span class="yoast-linked-from yoast-column-header-has-tooltip" data-tooltip-text="%1$s"><span class="screen-reader-text">%2$s</span></span>',
×
145
                                \esc_attr__( 'Number of internal links linking to this post. See "Yoast Columns" text in the help tab for more info.', 'wordpress-seo' ),
×
146
                                /* translators: Hidden accessibility text. */
UNCOV
147
                                \esc_html__( 'Received internal links', 'wordpress-seo' )
×
148
                        );
149
                }
150

151
                return $columns;
×
152
        }
153

154
        /**
155
         * Modifies the query pieces to allow ordering column by links to post.
156
         *
157
         * @param array    $pieces Array of Query pieces.
158
         * @param WP_Query $query  The Query on which to apply.
159
         *
160
         * @return array
161
         */
162
        public function order_by_linked( $pieces, $query ) {
163
                if ( $query->get( 'orderby' ) !== 'wpseo-' . self::COLUMN_LINKED ) {
×
164
                        return $pieces;
×
165
                }
166

167
                return $this->build_sort_query_pieces( $pieces, $query, 'incoming_link_count' );
×
168
        }
169

170
        /**
171
         * Modifies the query pieces to allow ordering column by links to post.
172
         *
173
         * @param array    $pieces Array of Query pieces.
174
         * @param WP_Query $query  The Query on which to apply.
175
         *
176
         * @return array
177
         */
178
        public function order_by_links( $pieces, $query ) {
179
                if ( $query->get( 'orderby' ) !== 'wpseo-' . self::COLUMN_LINKS ) {
×
180
                        return $pieces;
×
181
                }
182

183
                return $this->build_sort_query_pieces( $pieces, $query, 'link_count' );
×
184
        }
185

186
        /**
187
         * Builds the pieces for a sorting query.
188
         *
189
         * @param array    $pieces Array of Query pieces.
190
         * @param WP_Query $query  The Query on which to apply.
191
         * @param string   $field  The field in the table to JOIN on.
192
         *
193
         * @return array Modified Query pieces.
194
         */
195
        protected function build_sort_query_pieces( $pieces, $query, $field ) {
196
                // We only want our code to run in the main WP query.
197
                if ( ! $query->is_main_query() ) {
×
198
                        return $pieces;
×
199
                }
200

201
                // Get the order query variable - ASC or DESC.
202
                $order = \strtoupper( $query->get( 'order' ) );
×
203

204
                // Make sure the order setting qualifies. If not, set default as ASC.
205
                if ( ! \in_array( $order, [ 'ASC', 'DESC' ], true ) ) {
×
206
                        $order = 'ASC';
×
207
                }
208

209
                $table = Model::get_table_name( 'Indexable' );
×
210

211
                $pieces['join']   .= " LEFT JOIN $table AS yoast_indexable ON yoast_indexable.object_id = {$this->wpdb->posts}.ID AND yoast_indexable.object_type = 'post' ";
×
212
                $pieces['orderby'] = "yoast_indexable.$field $order, FIELD( {$this->wpdb->posts}.post_status, 'publish' ) $order, {$pieces['orderby']}";
×
213

214
                return $pieces;
×
215
        }
216

217
        /**
218
         * Displays the column content for the given column.
219
         *
220
         * @param string $column_name Column to display the content for.
221
         * @param int    $post_id     Post to display the column content for.
222
         */
223
        public function column_content( $column_name, $post_id ) {
224
                $indexable = $this->admin_columns_cache->get_indexable( $post_id );
×
225
                // Nothing to output if we don't have the value.
226
                if ( $indexable === false ) {
×
227
                        return;
×
228
                }
229

230
                switch ( $column_name ) {
231
                        case 'wpseo-' . self::COLUMN_LINKS:
232
                                echo (int) $indexable->link_count;
×
233
                                return;
×
234
                        case 'wpseo-' . self::COLUMN_LINKED:
235
                                if ( \get_post_status( $post_id ) === 'publish' ) {
×
236
                                        echo (int) $indexable->incoming_link_count;
×
237
                                }
238
                }
239
        }
240

241
        /**
242
         * Sets the sortable columns.
243
         *
244
         * @param array $columns Array with sortable columns.
245
         *
246
         * @return array The extended array with sortable columns.
247
         */
248
        public function column_sort( array $columns ) {
249
                $columns[ 'wpseo-' . self::COLUMN_LINKS ]  = 'wpseo-' . self::COLUMN_LINKS;
×
250
                $columns[ 'wpseo-' . self::COLUMN_LINKED ] = 'wpseo-' . self::COLUMN_LINKED;
×
251

252
                return $columns;
×
253
        }
254
}
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

© 2025 Coveralls, Inc