PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / indexing / post-link-indexing-action.php

post-link-indexing-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at src/actions/indexing/post-link-indexing-action.php

145 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Actions\Indexing;
4
5 use Yoast\WP\Lib\Model;
6 use Yoast\WP\SEO\Helpers\Post_Type_Helper;
7
8 /**
9 * Reindexing action for post link indexables.
10 *
11 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
12 */
13 class Post_Link_Indexing_Action extends Abstract_Link_Indexing_Action {
14
15 /**
16 * The transient name.
17 *
18 * @var string
19 */
20 const UNINDEXED_COUNT_TRANSIENT = 'wpseo_unindexed_post_link_count';
21
22 /**
23 * The transient cache key for limited counts.
24 *
25 * @var string
26 */
27 const UNINDEXED_LIMITED_COUNT_TRANSIENT = self::UNINDEXED_COUNT_TRANSIENT . '_limited';
28
29 /**
30 * The post type helper.
31 *
32 * @var Post_Type_Helper
33 */
34 protected $post_type_helper;
35
36 /**
37 * Sets the required helper.
38 *
39 * @required
40 *
41 * @param Post_Type_Helper $post_type_helper The post type helper.
42 *
43 * @return void
44 */
45 public function set_helper( Post_Type_Helper $post_type_helper ) {
46 $this->post_type_helper = $post_type_helper;
47 }
48
49 /**
50 * Returns objects to be indexed.
51 *
52 * @return array Objects to be indexed.
53 */
54 protected function get_objects() {
55 $query = $this->get_select_query( $this->get_limit() );
56
57 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Function get_select_query returns a prepared query.
58 $posts = $this->wpdb->get_results( $query );
59
60 return \array_map(
61 static function ( $post ) {
62 return (object) [
63 'id' => (int) $post->ID,
64 'type' => 'post',
65 'content' => $post->post_content,
66 ];
67 },
68 $posts
69 );
70 }
71
72 /**
73 * Builds a query for counting the number of unindexed post links.
74 *
75 * @return string The prepared query string.
76 */
77 protected function get_count_query() {
78 $public_post_types = $this->post_type_helper->get_accessible_post_types();
79 $indexable_table = Model::get_table_name( 'Indexable' );
80 $links_table = Model::get_table_name( 'SEO_Links' );
81
82 // Warning: If this query is changed, makes sure to update the query in get_select_query as well.
83 return $this->wpdb->prepare(
84 "SELECT COUNT(P.ID)
85 FROM {$this->wpdb->posts} AS P
86 LEFT JOIN $indexable_table AS I
87 ON P.ID = I.object_id
88 AND I.link_count IS NOT NULL
89 AND I.object_type = 'post'
90 LEFT JOIN $links_table AS L
91 ON L.post_id = P.ID
92 AND L.target_indexable_id IS NULL
93 AND L.type = 'internal'
94 AND L.target_post_id IS NOT NULL
95 AND L.target_post_id != 0
96 WHERE ( I.object_id IS NULL OR L.post_id IS NOT NULL )
97 AND P.post_status = 'publish'
98 AND P.post_type IN (" . \implode( ', ', \array_fill( 0, \count( $public_post_types ), '%s' ) ) . ')',
99 $public_post_types
100 );
101 }
102
103 /**
104 * Builds a query for selecting the ID's of unindexed post links.
105 *
106 * @param int|false $limit The maximum number of post link IDs to return.
107 *
108 * @return string The prepared query string.
109 */
110 protected function get_select_query( $limit = false ) {
111 $public_post_types = $this->post_type_helper->get_accessible_post_types();
112 $indexable_table = Model::get_table_name( 'Indexable' );
113 $links_table = Model::get_table_name( 'SEO_Links' );
114 $replacements = $public_post_types;
115
116 $limit_query = '';
117 if ( $limit ) {
118 $limit_query = 'LIMIT %d';
119 $replacements[] = $limit;
120 }
121
122 // Warning: If this query is changed, makes sure to update the query in get_count_query as well.
123 return $this->wpdb->prepare(
124 "
125 SELECT P.ID, P.post_content
126 FROM {$this->wpdb->posts} AS P
127 LEFT JOIN $indexable_table AS I
128 ON P.ID = I.object_id
129 AND I.link_count IS NOT NULL
130 AND I.object_type = 'post'
131 LEFT JOIN $links_table AS L
132 ON L.post_id = P.ID
133 AND L.target_indexable_id IS NULL
134 AND L.type = 'internal'
135 AND L.target_post_id IS NOT NULL
136 AND L.target_post_id != 0
137 WHERE ( I.object_id IS NULL OR L.post_id IS NOT NULL )
138 AND P.post_status = 'publish'
139 AND P.post_type IN (" . \implode( ', ', \array_fill( 0, \count( $public_post_types ), '%s' ) ) . ")
140 $limit_query",
141 $replacements
142 );
143 }
144 }
145