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