PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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-home-page-builder.php

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

146 lines 4.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\Options_Helper;
7 use Yoast\WP\SEO\Helpers\Post_Helper;
8 use Yoast\WP\SEO\Helpers\Url_Helper;
9 use Yoast\WP\SEO\Models\Indexable;
10 use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
11
12 /**
13 * Homepage Builder for the indexables.
14 *
15 * Formats the homepage meta to indexable format.
16 *
17 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
18 */
19 class Indexable_Home_Page_Builder {
20
21 use Indexable_Social_Image_Trait;
22
23 /**
24 * The options helper.
25 *
26 * @var Options_Helper
27 */
28 protected $options;
29
30 /**
31 * The URL helper.
32 *
33 * @var Url_Helper
34 */
35 protected $url_helper;
36
37 /**
38 * The latest version of the Indexable-Home-Page-Builder.
39 *
40 * @var int
41 */
42 protected $version;
43
44 /**
45 * Holds the taxonomy helper instance.
46 *
47 * @var Post_Helper
48 */
49 protected $post_helper;
50
51 /**
52 * The WPDB instance.
53 *
54 * @var wpdb
55 */
56 protected $wpdb;
57
58 /**
59 * Indexable_Home_Page_Builder constructor.
60 *
61 * @param Options_Helper $options The options helper.
62 * @param Url_Helper $url_helper The url helper.
63 * @param Indexable_Builder_Versions $versions Knows the latest version of each Indexable type.
64 * @param Post_Helper $post_helper The post helper.
65 * @param wpdb $wpdb The WPDB instance.
66 */
67 public function __construct(
68 Options_Helper $options,
69 Url_Helper $url_helper,
70 Indexable_Builder_Versions $versions,
71 Post_Helper $post_helper,
72 wpdb $wpdb
73 ) {
74 $this->options = $options;
75 $this->url_helper = $url_helper;
76 $this->version = $versions->get_latest_version_for_type( 'home-page' );
77 $this->post_helper = $post_helper;
78 $this->wpdb = $wpdb;
79 }
80
81 /**
82 * Formats the data.
83 *
84 * @param Indexable $indexable The indexable to format.
85 *
86 * @return Indexable The extended indexable.
87 */
88 public function build( $indexable ) {
89 $indexable->object_type = 'home-page';
90 $indexable->title = $this->options->get( 'title-home-wpseo' );
91 $indexable->breadcrumb_title = $this->options->get( 'breadcrumbs-home' );
92 $indexable->permalink = $this->url_helper->home();
93 $indexable->blog_id = \get_current_blog_id();
94 $indexable->description = $this->options->get( 'metadesc-home-wpseo' );
95 if ( empty( $indexable->description ) ) {
96 $indexable->description = \get_bloginfo( 'description' );
97 }
98
99 $indexable->is_robots_noindex = \get_option( 'blog_public' ) === '0';
100
101 $indexable->open_graph_title = $this->options->get( 'open_graph_frontpage_title' );
102 $indexable->open_graph_image = $this->options->get( 'open_graph_frontpage_image' );
103 $indexable->open_graph_image_id = $this->options->get( 'open_graph_frontpage_image_id' );
104 $indexable->open_graph_description = $this->options->get( 'open_graph_frontpage_desc' );
105
106 // Reset the OG image source & meta.
107 $indexable->open_graph_image_source = null;
108 $indexable->open_graph_image_meta = null;
109
110 // When the image or image id is set.
111 if ( $indexable->open_graph_image || $indexable->open_graph_image_id ) {
112 $indexable->open_graph_image_source = 'set-by-user';
113
114 $this->set_open_graph_image_meta_data( $indexable );
115 }
116
117 $timestamps = $this->get_object_timestamps();
118 $indexable->object_published_at = $timestamps->published_at;
119 $indexable->object_last_modified = $timestamps->last_modified;
120
121 $indexable->version = $this->version;
122
123 return $indexable;
124 }
125
126 /**
127 * Returns the timestamps for the homepage.
128 *
129 * @return object An object with last_modified and published_at timestamps.
130 */
131 protected function get_object_timestamps() {
132 $post_statuses = $this->post_helper->get_public_post_statuses();
133
134 $sql = "
135 SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
136 FROM {$this->wpdb->posts} AS p
137 WHERE p.post_status IN (" . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
138 AND p.post_password = ''
139 AND p.post_type = 'post'
140 ";
141
142 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare.
143 return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $post_statuses ) );
144 }
145 }
146