PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5.1
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 / builders / indexable-author-builder.php

indexable-author-builder.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5.1, at src/builders/indexable-author-builder.php

194 lines 5.2 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\Builders;
4
5 use wpdb;
6 use Yoast\WP\SEO\Helpers\Author_Archive_Helper;
7 use Yoast\WP\SEO\Helpers\Post_Helper;
8 use Yoast\WP\SEO\Models\Indexable;
9 use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
10
11 /**
12 * Author Builder for the indexables.
13 *
14 * Formats the author meta to indexable format.
15 */
16 class Indexable_Author_Builder {
17
18 use Indexable_Social_Image_Trait;
19
20 /**
21 * The author archive helper.
22 *
23 * @var Author_Archive_Helper
24 */
25 private $author_archive;
26
27 /**
28 * The latest version of the Indexable_Author_Builder.
29 *
30 * @var int
31 */
32 protected $version;
33
34 /**
35 * Holds the taxonomy helper instance.
36 *
37 * @var Post_Helper
38 */
39 protected $post_helper;
40
41 /**
42 * The WPDB instance.
43 *
44 * @var wpdb
45 */
46 protected $wpdb;
47
48 /**
49 * Indexable_Author_Builder constructor.
50 *
51 * @param Author_Archive_Helper $author_archive The author archive helper.
52 * @param Indexable_Builder_Versions $versions The Indexable version manager.
53 * @param Post_Helper $post_helper The post helper.
54 * @param wpdb $wpdb The WPDB instance.
55 */
56 public function __construct(
57 Author_Archive_Helper $author_archive,
58 Indexable_Builder_Versions $versions,
59 Post_Helper $post_helper,
60 wpdb $wpdb
61 ) {
62 $this->author_archive = $author_archive;
63 $this->version = $versions->get_latest_version_for_type( 'user' );
64 $this->post_helper = $post_helper;
65 $this->wpdb = $wpdb;
66 }
67
68 /**
69 * Formats the data.
70 *
71 * @param int $user_id The user to retrieve the indexable for.
72 * @param Indexable $indexable The indexable to format.
73 *
74 * @return Indexable The extended indexable.
75 */
76 public function build( $user_id, Indexable $indexable ) {
77 $meta_data = $this->get_meta_data( $user_id );
78
79 $indexable->object_id = $user_id;
80 $indexable->object_type = 'user';
81 $indexable->permalink = \get_author_posts_url( $user_id );
82 $indexable->title = $meta_data['wpseo_title'];
83 $indexable->description = $meta_data['wpseo_metadesc'];
84 $indexable->is_cornerstone = false;
85 $indexable->is_robots_noindex = ( $meta_data['wpseo_noindex_author'] === 'on' );
86 $indexable->is_robots_nofollow = null;
87 $indexable->is_robots_noarchive = null;
88 $indexable->is_robots_noimageindex = null;
89 $indexable->is_robots_nosnippet = null;
90 $indexable->is_public = ( $indexable->is_robots_noindex ) ? false : null;
91 $indexable->has_public_posts = $this->author_archive->author_has_public_posts( $user_id );
92 $indexable->blog_id = \get_current_blog_id();
93
94 $this->reset_social_images( $indexable );
95 $this->handle_social_images( $indexable );
96
97 $timestamps = $this->get_object_timestamps( $user_id );
98 $indexable->object_published_at = $timestamps->published_at;
99 $indexable->object_last_modified = $timestamps->last_modified;
100
101 $indexable->version = $this->version;
102
103 return $indexable;
104 }
105
106 /**
107 * Retrieves the meta data for this indexable.
108 *
109 * @param int $user_id The user to retrieve the meta data for.
110 *
111 * @return array List of meta entries.
112 */
113 protected function get_meta_data( $user_id ) {
114 $keys = [
115 'wpseo_title',
116 'wpseo_metadesc',
117 'wpseo_noindex_author',
118 ];
119
120 $output = [];
121 foreach ( $keys as $key ) {
122 $output[ $key ] = $this->get_author_meta( $user_id, $key );
123 }
124
125 return $output;
126 }
127
128 /**
129 * Retrieves the author meta.
130 *
131 * @param int $user_id The user to retrieve the indexable for.
132 * @param string $key The meta entry to retrieve.
133 *
134 * @return string|null The value of the meta field.
135 */
136 protected function get_author_meta( $user_id, $key ) {
137 $value = \get_the_author_meta( $key, $user_id );
138 if ( \is_string( $value ) && $value === '' ) {
139 return null;
140 }
141
142 return $value;
143 }
144
145 /**
146 * Finds an alternative image for the social image.
147 *
148 * @param Indexable $indexable The indexable.
149 *
150 * @return array|bool False when not found, array with data when found.
151 */
152 protected function find_alternative_image( Indexable $indexable ) {
153 $gravatar_image = \get_avatar_url(
154 $indexable->object_id,
155 [
156 'size' => 500,
157 'scheme' => 'https',
158 ]
159 );
160 if ( $gravatar_image ) {
161 return [
162 'image' => $gravatar_image,
163 'source' => 'gravatar-image',
164 ];
165 }
166
167 return false;
168 }
169
170 /**
171 * Returns the timestamps for a given author.
172 *
173 * @param int $author_id The author ID.
174 *
175 * @return object An object with last_modified and published_at timestamps.
176 */
177 protected function get_object_timestamps( $author_id ) {
178 $post_statuses = $this->post_helper->get_public_post_statuses();
179
180 $sql = "
181 SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
182 FROM {$this->wpdb->posts} AS p
183 WHERE p.post_status IN (" . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
184 AND p.post_password = ''
185 AND p.post_author = %d
186 ";
187
188 $replacements = \array_merge( $post_statuses, [ $author_id ] );
189
190 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare.
191 return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $replacements ) );
192 }
193 }
194