PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
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-post-type-archive-builder.php

indexable-post-type-archive-builder.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.9, at src/builders/indexable-post-type-archive-builder.php

150 lines 4.3 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\Options_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 * Post type archive builder for the indexables.
13 *
14 * Formats the post type archive meta to indexable format.
15 *
16 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
17 */
18 class Indexable_Post_Type_Archive_Builder {
19
20 /**
21 * The options helper.
22 *
23 * @var Options_Helper
24 */
25 protected $options;
26
27 /**
28 * The latest version of the Indexable_Post_Type_Archive_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_Post_Type_Archive_Builder constructor.
50 *
51 * @param Options_Helper $options The options helper.
52 * @param Indexable_Builder_Versions $versions The latest version of each Indexable builder.
53 * @param Post_Helper $post_helper The post helper.
54 * @param wpdb $wpdb The WPDB instance.
55 */
56 public function __construct(
57 Options_Helper $options,
58 Indexable_Builder_Versions $versions,
59 Post_Helper $post_helper,
60 wpdb $wpdb
61 ) {
62 $this->options = $options;
63 $this->version = $versions->get_latest_version_for_type( 'post-type-archive' );
64 $this->post_helper = $post_helper;
65 $this->wpdb = $wpdb;
66 }
67
68 /**
69 * Formats the data.
70 *
71 * @param string $post_type The post type to build the indexable for.
72 * @param Indexable $indexable The indexable to format.
73 *
74 * @return Indexable The extended indexable.
75 */
76 public function build( $post_type, Indexable $indexable ) {
77 $indexable->object_type = 'post-type-archive';
78 $indexable->object_sub_type = $post_type;
79 $indexable->title = $this->options->get( 'title-ptarchive-' . $post_type );
80 $indexable->description = $this->options->get( 'metadesc-ptarchive-' . $post_type );
81 $indexable->breadcrumb_title = $this->get_breadcrumb_title( $post_type );
82 $indexable->permalink = \get_post_type_archive_link( $post_type );
83 $indexable->is_robots_noindex = $this->options->get( 'noindex-ptarchive-' . $post_type );
84 $indexable->is_public = ( (int) $indexable->is_robots_noindex !== 1 );
85 $indexable->blog_id = \get_current_blog_id();
86 $indexable->version = $this->version;
87
88 $timestamps = $this->get_object_timestamps( $post_type );
89 $indexable->object_published_at = $timestamps->published_at;
90 $indexable->object_last_modified = $timestamps->last_modified;
91
92 return $indexable;
93 }
94
95 /**
96 * Returns the fallback breadcrumb title for a given post.
97 *
98 * @param string $post_type The post type to get the fallback breadcrumb title for.
99 *
100 * @return string
101 */
102 private function get_breadcrumb_title( $post_type ) {
103 $options_breadcrumb_title = $this->options->get( 'bctitle-ptarchive-' . $post_type );
104
105 if ( $options_breadcrumb_title !== '' ) {
106 return $options_breadcrumb_title;
107 }
108
109 $post_type_obj = \get_post_type_object( $post_type );
110
111 if ( ! \is_object( $post_type_obj ) ) {
112 return '';
113 }
114
115 if ( isset( $post_type_obj->label ) && $post_type_obj->label !== '' ) {
116 return $post_type_obj->label;
117 }
118
119 if ( isset( $post_type_obj->labels->menu_name ) && $post_type_obj->labels->menu_name !== '' ) {
120 return $post_type_obj->labels->menu_name;
121 }
122
123 return $post_type_obj->name;
124 }
125
126 /**
127 * Returns the timestamps for a given post type.
128 *
129 * @param string $post_type The post type.
130 *
131 * @return object An object with last_modified and published_at timestamps.
132 */
133 protected function get_object_timestamps( $post_type ) {
134 $post_statuses = $this->post_helper->get_public_post_statuses();
135
136 $sql = "
137 SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
138 FROM {$this->wpdb->posts} AS p
139 WHERE p.post_status IN (" . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
140 AND p.post_password = ''
141 AND p.post_type = %s
142 ";
143
144 $replacements = \array_merge( $post_statuses, [ $post_type ] );
145
146 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare.
147 return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $replacements ) );
148 }
149 }
150