| @@ -9,8 +9,12 @@ | ||
| 9 | 9 | * @since 4.8.0 |
| 10 | 10 | * @package automattic/jetpack |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 13 | 17 | /* Ensure sitemap constants are available. */ |
| 14 | 18 | require_once __DIR__ . '/sitemap-constants.php'; |
| 15 | 19 | |
| 16 | 20 | /** |
| @@ -21,8 +25,20 @@ | ||
| 21 | 25 | */ |
| 22 | 26 | class Jetpack_Sitemap_Librarian { |
| 23 | 27 | |
| 24 | 28 | /** |
| 29 | + * Sanitized posts table column lists, keyed by table name. | |
| 30 | + * | |
| 31 | + * Keying by table name keeps a process that switches blogs from reusing | |
| 32 | + * one site's column list against another site's posts table. The cache | |
| 33 | + * is static because the librarian is constructed fresh on every request | |
| 34 | + * that builds a sitemap, while the underlying schema is not. | |
| 35 | + * | |
| 36 | + * @var array | |
| 37 | + */ | |
| 38 | + private static $post_columns_cache = array(); | |
| 39 | + | |
| 40 | + /** | |
| 25 | 41 | * Retrieve a single sitemap with given name and type. |
| 26 | 42 | * Returns null if no such sitemap exists. |
| 27 | 43 | * |
| 28 | 44 | * @access public |
| @@ -70,8 +86,13 @@ | ||
| 70 | 86 | * |
| 71 | 87 | * If a sitemap with that type and name does not exist, create it. |
| 72 | 88 | * If a sitemap with that type and name does exist, update it. |
| 73 | 89 | * |
| 90 | + * This method uses get_current_sitemap_post_id() for efficiency, | |
| 91 | + * as it only retrieves the post ID, which will be typically cached in the persistent object cache. | |
| 92 | + * This approach avoids loading unnecessary data (like post content) into memory, | |
| 93 | + * unlike using read_sitemap_data() which would retrieve the full post object. | |
| 94 | + * | |
| 74 | 95 | * @access public |
| 75 | 96 | * @since 4.8.0 |
| 76 | 97 | * |
| 77 | 98 | * @param string $index Index of the sitemap to be stored. |
| @@ -81,11 +102,11 @@ | ||
| 81 | 102 | */ |
| 82 | 103 | public function store_sitemap_data( $index, $type, $contents, $timestamp ) { |
| 83 | 104 | $name = jp_sitemap_filename( $type, $index ); |
| 84 | 105 | |
| 85 | - $the_post = $this->read_sitemap_data( $name, $type ); | |
| 106 | + $post_id = $this->get_current_sitemap_post_id( $name, $type ); | |
| 86 | 107 | |
| 87 | - if ( null === $the_post ) { | |
| 108 | + if ( null === $post_id ) { | |
| 88 | 109 | // Post does not exist. |
| 89 | 110 | wp_insert_post( |
| 90 | 111 | array( |
| 91 | 112 | 'post_title' => $name, |
| @@ -97,9 +118,9 @@ | ||
| 97 | 118 | } else { |
| 98 | 119 | // Post does exist. |
| 99 | 120 | wp_insert_post( |
| 100 | 121 | array( |
| 101 | - 'ID' => $the_post['id'], | |
| 122 | + 'ID' => $post_id, | |
| 102 | 123 | 'post_title' => $name, |
| 103 | 124 | 'post_content' => base64_encode( $contents ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 104 | 125 | 'post_type' => $type, |
| 105 | 126 | 'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( $timestamp ) ), |
| @@ -108,8 +129,28 @@ | ||
| 108 | 129 | } |
| 109 | 130 | } |
| 110 | 131 | |
| 111 | 132 | /** |
| 133 | + * Get the current sitemap post ID. | |
| 134 | + * | |
| 135 | + * @param string $name The name of the sitemap. | |
| 136 | + * @param string $type The type of the sitemap. | |
| 137 | + * @return int|null The post ID if it exists, null otherwise. | |
| 138 | + */ | |
| 139 | + private function get_current_sitemap_post_id( $name, $type ) { | |
| 140 | + $args = array( | |
| 141 | + 'post_type' => $type, | |
| 142 | + 'post_status' => 'draft', | |
| 143 | + 'posts_per_page' => 1, | |
| 144 | + 'title' => $name, | |
| 145 | + 'fields' => 'ids', | |
| 146 | + ); | |
| 147 | + | |
| 148 | + $query = new WP_Query( $args ); | |
| 149 | + $posts = $query->posts; | |
| 150 | + return is_array( $posts ) && $posts ? $posts[0] : null; | |
| 151 | + } | |
| 152 | + /** | |
| 112 | 153 | * Delete a sitemap by name and type. |
| 113 | 154 | * |
| 114 | 155 | * @access public |
| 115 | 156 | * @since 4.8.0 |
| @@ -211,8 +252,57 @@ | ||
| 211 | 252 | } |
| 212 | 253 | } |
| 213 | 254 | |
| 214 | 255 | /** |
| 256 | + * Retrieve the timestamps of named sitemaps of a given type, keyed by filename. | |
| 257 | + * | |
| 258 | + * Looking rows up by name is what lets a caller avoid assuming the Nth row of | |
| 259 | + * a type is file N, which an interrupted cleanup can make false by rewriting a | |
| 260 | + * row and moving it in ID order. Only the named rows are read, in batches, so | |
| 261 | + * the result is bounded by what was asked for rather than by how many sitemap | |
| 262 | + * rows the site has. Names with no stored row are absent from the result. | |
| 263 | + * | |
| 264 | + * @access public | |
| 265 | + * @since 16.2 | |
| 266 | + * | |
| 267 | + * @param string $type Type of the sitemap rows to retrieve. | |
| 268 | + * @param array $names Sitemap filenames to look for. | |
| 269 | + * | |
| 270 | + * @return array Map of sitemap filename to its 'YYYY-MM-DD hh:mm:ss' timestamp. | |
| 271 | + */ | |
| 272 | + public function query_sitemap_timestamps( $type, $names ) { | |
| 273 | + global $wpdb; | |
| 274 | + | |
| 275 | + $timestamps = array(); | |
| 276 | + | |
| 277 | + foreach ( array_chunk( (array) $names, JP_SITEMAP_BATCH_SIZE ) as $chunk ) { | |
| 278 | + $placeholders = implode( ', ', array_fill( 0, count( $chunk ), '%s' ) ); | |
| 279 | + | |
| 280 | + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $placeholders is a generated list of %s. | |
| 281 | + $sql = "SELECT post_title, post_date | |
| 282 | + FROM $wpdb->posts | |
| 283 | + WHERE post_type=%s | |
| 284 | + AND post_status=%s | |
| 285 | + AND post_title IN ( $placeholders );"; | |
| 286 | + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 287 | + | |
| 288 | + $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 289 | + $wpdb->prepare( | |
| 290 | + $sql, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared right here. | |
| 291 | + array_merge( array( $type, 'draft' ), array_values( $chunk ) ) | |
| 292 | + ), | |
| 293 | + ARRAY_A | |
| 294 | + ); | |
| 295 | + | |
| 296 | + foreach ( (array) $rows as $row ) { | |
| 297 | + $timestamps[ $row['post_title'] ] = $row['post_date']; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + return $timestamps; | |
| 302 | + } | |
| 303 | + | |
| 304 | + /** | |
| 215 | 305 | * Retrieve an array of sitemap rows (of a given type) sorted by ID. |
| 216 | 306 | * |
| 217 | 307 | * Returns the smallest $num_posts sitemap rows (measured by ID) |
| 218 | 308 | * of the given type which are larger than $from_id. |
| @@ -223,9 +313,11 @@ | ||
| 223 | 313 | * @param string $type Type of the sitemap rows to retrieve. |
| 224 | 314 | * @param int $from_id Greatest lower bound of retrieved sitemap post IDs. |
| 225 | 315 | * @param int $num_posts Largest number of sitemap posts to retrieve. |
| 226 | 316 | * |
| 227 | - * @return array The sitemaps, as an array of associative arrays. | |
| 317 | + * @return array The sitemaps, as an array of associative arrays with | |
| 318 | + * keys ID, post_title, and post_date. The post content is | |
| 319 | + * deliberately excluded to keep memory usage low. | |
| 228 | 320 | */ |
| 229 | 321 | public function query_sitemaps_after_id( $type, $from_id, $num_posts ) { |
| 230 | 322 | global $wpdb; |
| 231 | 323 | |
| @@ -230,9 +322,9 @@ | ||
| 230 | 322 | global $wpdb; |
| 231 | 323 | |
| 232 | 324 | return $wpdb->get_results( |
| 233 | 325 | $wpdb->prepare( |
| 234 | - "SELECT * | |
| 326 | + "SELECT ID, post_title, post_date | |
| 235 | 327 | FROM $wpdb->posts |
| 236 | 328 | WHERE post_type=%s |
| 237 | 329 | AND post_status=%s |
| 238 | 330 | AND ID>%d |
| @@ -273,12 +365,14 @@ | ||
| 273 | 365 | $post_types[ $i ] = $wpdb->prepare( '%s', $post_type ); |
| 274 | 366 | } |
| 275 | 367 | $post_types_list = implode( ',', $post_types ); |
| 276 | 368 | |
| 369 | + $columns_list = $this->get_sanitized_post_columns( $wpdb ); | |
| 370 | + | |
| 277 | 371 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WPCS: db call ok; no-cache ok. |
| 278 | 372 | return $wpdb->get_results( |
| 279 | 373 | $wpdb->prepare( |
| 280 | - "SELECT * | |
| 374 | + "SELECT $columns_list | |
| 281 | 375 | FROM $wpdb->posts |
| 282 | 376 | WHERE post_status='publish' |
| 283 | 377 | AND post_type IN ($post_types_list) |
| 284 | 378 | AND ID>%d |
| @@ -298,9 +392,9 @@ | ||
| 298 | 392 | * @since 4.8.0 |
| 299 | 393 | * |
| 300 | 394 | * @param int $post_id Post identifier. |
| 301 | 395 | * |
| 302 | - * @return int Timestamp in 'Y-m-d h:i:s' format (UTC) of the most recent comment on the given post, or null if no such comments exist. | |
| 396 | + * @return string Timestamp in 'Y-m-d h:i:s' format (UTC) of the most recent comment on the given post, or null if no such comments exist. | |
| 303 | 397 | */ |
| 304 | 398 | public function query_latest_approved_comment_time_on_post( $post_id ) { |
| 305 | 399 | global $wpdb; |
| 306 | 400 | |
| @@ -325,16 +419,20 @@ | ||
| 325 | 419 | * |
| 326 | 420 | * @param int $from_id Greatest lower bound of retrieved image post IDs. |
| 327 | 421 | * @param int $num_posts Largest number of image posts to retrieve. |
| 328 | 422 | * |
| 329 | - * @return array The posts. | |
| 423 | + * @return array The posts, without the post_content and | |
| 424 | + * post_content_filtered columns. | |
| 330 | 425 | */ |
| 331 | 426 | public function query_images_after_id( $from_id, $num_posts ) { |
| 332 | 427 | global $wpdb; |
| 333 | 428 | |
| 429 | + $columns_list = $this->get_sanitized_post_columns( $wpdb ); | |
| 430 | + | |
| 431 | + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WPCS: db call ok; no-cache ok. | |
| 334 | 432 | return $wpdb->get_results( |
| 335 | 433 | $wpdb->prepare( |
| 336 | - "SELECT * | |
| 434 | + "SELECT $columns_list | |
| 337 | 435 | FROM $wpdb->posts |
| 338 | 436 | WHERE post_type='attachment' |
| 339 | 437 | AND post_mime_type LIKE %s |
| 340 | 438 | AND ID>%d |
| @@ -343,9 +441,10 @@ | ||
| 343 | 441 | 'image/%', |
| 344 | 442 | $from_id, |
| 345 | 443 | $num_posts |
| 346 | 444 | ) |
| 347 | - ); // WPCS: db call ok; no-cache ok. | |
| 445 | + ); | |
| 446 | + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 348 | 447 | } |
| 349 | 448 | |
| 350 | 449 | /** |
| 351 | 450 | * Retrieve an array of video posts sorted by ID. |
| @@ -414,12 +513,14 @@ | ||
| 414 | 513 | } |
| 415 | 514 | |
| 416 | 515 | $post_types_list = implode( ',', $post_types ); |
| 417 | 516 | |
| 517 | + $columns_list = $this->get_sanitized_post_columns( $wpdb ); | |
| 518 | + | |
| 418 | 519 | // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WPCS: db call ok; no-cache ok. |
| 419 | 520 | return $wpdb->get_results( |
| 420 | 521 | $wpdb->prepare( |
| 421 | - "SELECT * | |
| 522 | + "SELECT $columns_list | |
| 422 | 523 | FROM $wpdb->posts |
| 423 | 524 | WHERE post_status='publish' |
| 424 | 525 | AND post_date >= '%s' |
| 425 | 526 | AND post_type IN ($post_types_list) |
| @@ -431,5 +532,36 @@ | ||
| 431 | 532 | ); |
| 432 | 533 | // phpcs:enable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder,WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 433 | 534 | } |
| 434 | 535 | |
| 536 | + /** | |
| 537 | + * Returns all columns from the posts table, | |
| 538 | + * except post_content and post_content_filtered. | |
| 539 | + * | |
| 540 | + * The column list is memoized in self::$post_columns_cache, since this is | |
| 541 | + * called once per batch while building sitemaps. | |
| 542 | + * | |
| 543 | + * A cached entry is only used when it is non-empty. SHOW COLUMNS returns | |
| 544 | + * no rows when the query fails, and treating that as a cache hit would | |
| 545 | + * leave every later query in the process with an empty column list. | |
| 546 | + * | |
| 547 | + * @param object $wpdb The WordPress database object. | |
| 548 | + * @return string The sanitized post columns. | |
| 549 | + */ | |
| 550 | + private function get_sanitized_post_columns( $wpdb ) { | |
| 551 | + $table = $wpdb->posts; | |
| 552 | + | |
| 553 | + if ( empty( self::$post_columns_cache[ $table ] ) ) { | |
| 554 | + $columns = array_filter( | |
| 555 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 556 | + $wpdb->get_col( "SHOW COLUMNS FROM $wpdb->posts" ), | |
| 557 | + function ( $column ) { | |
| 558 | + return $column !== 'post_content' && $column !== 'post_content_filtered'; | |
| 559 | + } | |
| 560 | + ); | |
| 561 | + | |
| 562 | + self::$post_columns_cache[ $table ] = implode( ',', array_map( 'esc_sql', $columns ) ); | |
| 563 | + } | |
| 564 | + | |
| 565 | + return self::$post_columns_cache[ $table ]; | |
| 566 | + } | |
| 435 | 567 | } |