| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Builders; |
| 4 | 4 | |
| 5 | -use wpdb; | |
| 6 | 5 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 | 6 | use Yoast\WP\SEO\Helpers\Post_Helper; |
| 8 | 7 | use Yoast\WP\SEO\Helpers\Url_Helper; |
| 9 | 8 | use Yoast\WP\SEO\Models\Indexable; |
| @@ -12,10 +11,8 @@ | ||
| 12 | 11 | /** |
| 13 | 12 | * Homepage Builder for the indexables. |
| 14 | 13 | * |
| 15 | 14 | * Formats the homepage meta to indexable format. |
| 16 | - * | |
| 17 | - * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded | |
| 18 | 15 | */ |
| 19 | 16 | class Indexable_Home_Page_Builder { |
| 20 | 17 | |
| 21 | 18 | use Indexable_Social_Image_Trait; |
| @@ -48,15 +45,8 @@ | ||
| 48 | 45 | */ |
| 49 | 46 | protected $post_helper; |
| 50 | 47 | |
| 51 | 48 | /** |
| 52 | - * The WPDB instance. | |
| 53 | - * | |
| 54 | - * @var wpdb | |
| 55 | - */ | |
| 56 | - protected $wpdb; | |
| 57 | - | |
| 58 | - /** | |
| 59 | 49 | * Indexable_Home_Page_Builder constructor. |
| 60 | 50 | * |
| 61 | 51 | * @param Options_Helper $options The options helper. |
| 62 | 52 | * @param Url_Helper $url_helper The url helper. |
| @@ -61,22 +51,19 @@ | ||
| 61 | 51 | * @param Options_Helper $options The options helper. |
| 62 | 52 | * @param Url_Helper $url_helper The url helper. |
| 63 | 53 | * @param Indexable_Builder_Versions $versions Knows the latest version of each Indexable type. |
| 64 | 54 | * @param Post_Helper $post_helper The post helper. |
| 65 | - * @param wpdb $wpdb The WPDB instance. | |
| 66 | 55 | */ |
| 67 | 56 | public function __construct( |
| 68 | 57 | Options_Helper $options, |
| 69 | 58 | Url_Helper $url_helper, |
| 70 | 59 | Indexable_Builder_Versions $versions, |
| 71 | - Post_Helper $post_helper, | |
| 72 | - wpdb $wpdb | |
| 60 | + Post_Helper $post_helper | |
| 73 | 61 | ) { |
| 74 | 62 | $this->options = $options; |
| 75 | 63 | $this->url_helper = $url_helper; |
| 76 | 64 | $this->version = $versions->get_latest_version_for_type( 'home-page' ); |
| 77 | 65 | $this->post_helper = $post_helper; |
| 78 | - $this->wpdb = $wpdb; | |
| 79 | 66 | } |
| 80 | 67 | |
| 81 | 68 | /** |
| 82 | 69 | * Formats the data. |
| @@ -128,18 +115,34 @@ | ||
| 128 | 115 | * |
| 129 | 116 | * @return object An object with last_modified and published_at timestamps. |
| 130 | 117 | */ |
| 131 | 118 | protected function get_object_timestamps() { |
| 119 | + global $wpdb; | |
| 132 | 120 | $post_statuses = $this->post_helper->get_public_post_statuses(); |
| 133 | 121 | |
| 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 | - "; | |
| 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'; | |
| 141 | 130 | |
| 142 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare. | |
| 143 | - return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $post_statuses ) ); | |
| 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 | |
| 144 | 147 | } |
| 145 | 148 | } |