diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2019-01-01 22:18:11 -0500 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2019-01-01 22:18:11 -0500 |
commit | 018bd442ec1e04ba78a6628763414eb60b359398 (patch) | |
tree | 448cde462397af33e5a964ba5d0803b73c65040e /plugins/jetpack/modules/sitemaps/sitemap-builder.php | |
parent | Update easy-table 1.8 (diff) | |
download | blogs-gentoo-018bd442ec1e04ba78a6628763414eb60b359398.tar.gz blogs-gentoo-018bd442ec1e04ba78a6628763414eb60b359398.tar.bz2 blogs-gentoo-018bd442ec1e04ba78a6628763414eb60b359398.zip |
Update jetpack 6.8.1
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'plugins/jetpack/modules/sitemaps/sitemap-builder.php')
-rw-r--r-- | plugins/jetpack/modules/sitemaps/sitemap-builder.php | 256 |
1 files changed, 175 insertions, 81 deletions
diff --git a/plugins/jetpack/modules/sitemaps/sitemap-builder.php b/plugins/jetpack/modules/sitemaps/sitemap-builder.php index b83a7c49..c74b1f03 100644 --- a/plugins/jetpack/modules/sitemaps/sitemap-builder.php +++ b/plugins/jetpack/modules/sitemaps/sitemap-builder.php @@ -7,6 +7,7 @@ * @author Automattic */ +/* Include sitemap subclasses, if not already, and include proper buffer based on phpxml's availability. */ require_once dirname( __FILE__ ) . '/sitemap-constants.php'; require_once dirname( __FILE__ ) . '/sitemap-buffer.php'; @@ -34,6 +35,38 @@ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { } /** + * Simple class for rendering an empty sitemap with a short TTL + */ +class Jetpack_Sitemap_Buffer_Empty extends Jetpack_Sitemap_Buffer { + + public function __construct() { + parent::__construct( JP_SITEMAP_MAX_ITEMS, JP_SITEMAP_MAX_BYTES, '1970-01-01 00:00:00' ); + + $this->doc->appendChild( + $this->doc->createComment( "generator='jetpack-" . JETPACK__VERSION . "'" ) + ); + + $this->doc->appendChild( + $this->doc->createProcessingInstruction( + 'xml-stylesheet', + 'type="text/xsl" href="' . $this->finder->construct_sitemap_url( 'sitemap-index.xsl' ) . '"' + ) + ); + } + + protected function get_root_element() { + if ( ! isset( $this->root ) ) { + $this->root = $this->doc->createElement( 'sitemapindex' ); + $this->root->setAttribute( 'xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9' ); + $this->doc->appendChild( $this->root ); + $this->byte_capacity -= strlen( $this->doc->saveXML( $this->root ) ); + } + + return $this->root; + } +} + +/** * The Jetpack_Sitemap_Builder object handles the construction of * all sitemap files (except the XSL files, which are handled by * Jetpack_Sitemap_Stylist.) Other than the constructor, there are @@ -78,7 +111,7 @@ class Jetpack_Sitemap_Builder { */ public function __construct() { $this->librarian = new Jetpack_Sitemap_Librarian(); - $this->finder = new Jetpack_Sitemap_Finder(); + $this->finder = new Jetpack_Sitemap_Finder(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $this->logger = new Jetpack_Sitemap_Logger(); @@ -118,10 +151,7 @@ class Jetpack_Sitemap_Builder { if ( ! class_exists( 'DOMDocument' ) ) { $this->logger->report( __( - '-- WARNING: Jetpack can not load necessary XML manipulation libraries. ' - . 'This can happen if XML support in PHP is not enabled on your server. ' - . 'XML support is highly recommended for WordPress and Jetpack, please enable ' - . 'it or contact your hosting provider about it.', + 'Jetpack can not load necessary XML manipulation libraries. Please ask your hosting provider to refer to our server requirements at https://jetpack.com/support/server-requirements/ .', 'jetpack' ), true @@ -235,7 +265,7 @@ class Jetpack_Sitemap_Builder { $finished = true; break; - } // End switch(). + } // End switch. // Unlock the state. Jetpack_Sitemap_State::unlock(); @@ -266,12 +296,14 @@ class Jetpack_Sitemap_Builder { if ( false === $result ) { // If no sitemap was generated, advance to the next type. - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $index_type, - 'last-added' => 0, - 'number' => 0, - 'last-modified' => '1970-01-01 00:00:00', - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $index_type, + 'last-added' => 0, + 'number' => 0, + 'last-modified' => '1970-01-01 00:00:00', + ) + ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $sitemap_type" ); @@ -279,19 +311,22 @@ class Jetpack_Sitemap_Builder { // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( - $state['number'], $sitemap_type + $state['number'], + $sitemap_type ); return; } // Otherwise, update the state. - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $state['sitemap-type'], - 'last-added' => $result['last_id'], - 'number' => $state['number'] + 1, - 'last-modified' => $result['last_modified'], - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $state['sitemap-type'], + 'last-added' => $result['last_id'], + 'number' => $state['number'] + 1, + 'last-modified' => $result['last_modified'], + ) + ); if ( true === $result['any_left'] ) { // If there's more work to be done with this type, return. @@ -299,12 +334,14 @@ class Jetpack_Sitemap_Builder { } // Otherwise, advance state to the next sitemap type. - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $index_type, - 'last-added' => 0, - 'number' => 0, - 'last-modified' => '1970-01-01 00:00:00', - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $index_type, + 'last-added' => 0, + 'number' => 0, + 'last-modified' => '1970-01-01 00:00:00', + ) + ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $sitemap_type" ); @@ -312,7 +349,8 @@ class Jetpack_Sitemap_Builder { // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( - $state['number'] + 1, $sitemap_type + $state['number'] + 1, + $sitemap_type ); } @@ -330,12 +368,14 @@ class Jetpack_Sitemap_Builder { // If only 0 or 1 sitemaps were built, advance to the next type and return. if ( 1 >= $state['max'][ $sitemap_type ]['number'] ) { - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $next_type, - 'last-added' => 0, - 'number' => 0, - 'last-modified' => '1970-01-01 00:00:00', - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $next_type, + 'last-added' => 0, + 'number' => 0, + 'last-modified' => '1970-01-01 00:00:00', + ) + ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); @@ -343,7 +383,8 @@ class Jetpack_Sitemap_Builder { // There are no indices of this type. $this->librarian->delete_numbered_sitemap_rows_after( - 0, $index_type + 0, + $index_type ); return; @@ -359,12 +400,14 @@ class Jetpack_Sitemap_Builder { // If no index was built, advance to the next type and return. if ( false === $result ) { - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $next_type, - 'last-added' => 0, - 'number' => 0, - 'last-modified' => '1970-01-01 00:00:00', - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $next_type, + 'last-added' => 0, + 'number' => 0, + 'last-modified' => '1970-01-01 00:00:00', + ) + ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); @@ -372,19 +415,22 @@ class Jetpack_Sitemap_Builder { // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( - $state['number'], $index_type + $state['number'], + $index_type ); return; } // Otherwise, check in the state. - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $index_type, - 'last-added' => $result['last_id'], - 'number' => $state['number'] + 1, - 'last-modified' => $result['last_modified'], - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $index_type, + 'last-added' => $result['last_id'], + 'number' => $state['number'] + 1, + 'last-modified' => $result['last_modified'], + ) + ); // If there are still sitemaps left to index, return. if ( true === $result['any_left'] ) { @@ -392,12 +438,14 @@ class Jetpack_Sitemap_Builder { } // Otherwise, advance to the next type. - Jetpack_Sitemap_State::check_in( array( - 'sitemap-type' => $next_type, - 'last-added' => 0, - 'number' => 0, - 'last-modified' => '1970-01-01 00:00:00', - ) ); + Jetpack_Sitemap_State::check_in( + array( + 'sitemap-type' => $next_type, + 'last-added' => 0, + 'number' => 0, + 'last-modified' => '1970-01-01 00:00:00', + ) + ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); @@ -405,10 +453,9 @@ class Jetpack_Sitemap_Builder { // We're done generating indices of this type. $this->librarian->delete_numbered_sitemap_rows_after( - $state['number'] + 1, $index_type + $state['number'] + 1, + $index_type ); - - return; } /** @@ -419,6 +466,9 @@ class Jetpack_Sitemap_Builder { * @since 4.8.0 */ private function build_master_sitemap( $max ) { + $page = array(); + $image = array(); + $video = array(); if ( $this->logger ) { $this->logger->report( '-- Building Master Sitemap.' ); } @@ -430,10 +480,10 @@ class Jetpack_Sitemap_Builder { if ( 0 < $max[ JP_PAGE_SITEMAP_TYPE ]['number'] ) { if ( 1 === $max[ JP_PAGE_SITEMAP_TYPE ]['number'] ) { - $page['filename'] = jp_sitemap_filename( JP_PAGE_SITEMAP_TYPE, 1 ); + $page['filename'] = jp_sitemap_filename( JP_PAGE_SITEMAP_TYPE, 1 ); $page['last_modified'] = jp_sitemap_datetime( $max[ JP_PAGE_SITEMAP_TYPE ]['lastmod'] ); } else { - $page['filename'] = jp_sitemap_filename( + $page['filename'] = jp_sitemap_filename( JP_PAGE_SITEMAP_INDEX_TYPE, $max[ JP_PAGE_SITEMAP_INDEX_TYPE ]['number'] ); @@ -452,10 +502,10 @@ class Jetpack_Sitemap_Builder { if ( 0 < $max[ JP_IMAGE_SITEMAP_TYPE ]['number'] ) { if ( 1 === $max[ JP_IMAGE_SITEMAP_TYPE ]['number'] ) { - $image['filename'] = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, 1 ); + $image['filename'] = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, 1 ); $image['last_modified'] = jp_sitemap_datetime( $max[ JP_IMAGE_SITEMAP_TYPE ]['lastmod'] ); } else { - $image['filename'] = jp_sitemap_filename( + $image['filename'] = jp_sitemap_filename( JP_IMAGE_SITEMAP_INDEX_TYPE, $max[ JP_IMAGE_SITEMAP_INDEX_TYPE ]['number'] ); @@ -474,10 +524,10 @@ class Jetpack_Sitemap_Builder { if ( 0 < $max[ JP_VIDEO_SITEMAP_TYPE ]['number'] ) { if ( 1 === $max[ JP_VIDEO_SITEMAP_TYPE ]['number'] ) { - $video['filename'] = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, 1 ); + $video['filename'] = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, 1 ); $video['last_modified'] = jp_sitemap_datetime( $max[ JP_VIDEO_SITEMAP_TYPE ]['lastmod'] ); } else { - $video['filename'] = jp_sitemap_filename( + $video['filename'] = jp_sitemap_filename( JP_VIDEO_SITEMAP_INDEX_TYPE, $max[ JP_VIDEO_SITEMAP_INDEX_TYPE ]['number'] ); @@ -533,8 +583,8 @@ class Jetpack_Sitemap_Builder { JP_SITEMAP_MAX_BYTES ); - // Add entry for the main page (only if we're at the first one). - if ( 1 === $number ) { + // Add entry for the main page (only if we're at the first one) and it isn't already going to be included as a page. + if ( 1 === $number && 'page' !== get_option( 'show_on_front' ) ) { $item_array = array( 'url' => array( 'loc' => home_url(), @@ -559,7 +609,8 @@ class Jetpack_Sitemap_Builder { // Add as many items to the buffer as possible. while ( $last_post_id >= 0 && false === $buffer->is_full() ) { $posts = $this->librarian->query_posts_after_id( - $last_post_id, JP_SITEMAP_BATCH_SIZE + $last_post_id, + JP_SITEMAP_BATCH_SIZE ); if ( null == $posts ) { // WPCS: loose comparison ok. @@ -692,7 +743,7 @@ class Jetpack_Sitemap_Builder { * } */ public function build_one_image_sitemap( $number, $from_id ) { - $last_post_id = $from_id; + $last_post_id = $from_id; $any_posts_left = true; if ( $this->logger ) { @@ -708,7 +759,8 @@ class Jetpack_Sitemap_Builder { // Add as many items to the buffer as possible. while ( false === $buffer->is_full() ) { $posts = $this->librarian->query_images_after_id( - $last_post_id, JP_SITEMAP_BATCH_SIZE + $last_post_id, + JP_SITEMAP_BATCH_SIZE ); if ( null == $posts ) { // WPCS: loose comparison ok. @@ -770,7 +822,7 @@ class Jetpack_Sitemap_Builder { * } */ public function build_one_video_sitemap( $number, $from_id ) { - $last_post_id = $from_id; + $last_post_id = $from_id; $any_posts_left = true; if ( $this->logger ) { @@ -786,7 +838,8 @@ class Jetpack_Sitemap_Builder { // Add as many items to the buffer as possible. while ( false === $buffer->is_full() ) { $posts = $this->librarian->query_videos_after_id( - $last_post_id, JP_SITEMAP_BATCH_SIZE + $last_post_id, + JP_SITEMAP_BATCH_SIZE ); if ( null == $posts ) { // WPCS: loose comparison ok. @@ -872,7 +925,7 @@ class Jetpack_Sitemap_Builder { // Add pointer to the previous sitemap index (unless we're at the first one). if ( 1 !== $number ) { - $i = $number - 1; + $i = $number - 1; $prev_index_url = $this->finder->construct_sitemap_url( jp_sitemap_filename( $index_type, $i ) ); @@ -891,7 +944,9 @@ class Jetpack_Sitemap_Builder { while ( false === $buffer->is_full() ) { // Retrieve a batch of posts (in order). $posts = $this->librarian->query_sitemaps_after_id( - $sitemap_type, $last_sitemap_id, JP_SITEMAP_BATCH_SIZE + $sitemap_type, + $last_sitemap_id, + JP_SITEMAP_BATCH_SIZE ); // If there were no posts to get, make a note. @@ -968,6 +1023,21 @@ class Jetpack_Sitemap_Builder { ); } + + /** + * This is served instead of a 404 when the master sitemap is requested + * but not yet generated. + * + * @access public + * @since 6.7.0 + * + * @return string The empty sitemap xml. + */ + public function empty_sitemap_xml() { + $empty_sitemap = new Jetpack_Sitemap_Buffer_Empty(); + return $empty_sitemap->contents(); + } + /** * Build and return the news sitemap xml. Note that the result of this * function is cached in the transient 'jetpack_news_sitemap_xml'. @@ -1026,7 +1096,7 @@ class Jetpack_Sitemap_Builder { $the_stored_news_sitemap, JP_NEWS_SITEMAP_INTERVAL ); - } // End if(). + } // End if. return $the_stored_news_sitemap; } @@ -1040,7 +1110,9 @@ class Jetpack_Sitemap_Builder { * * @param WP_Post $post The post to be processed. * - * @return array An array representing the post URL. + * @return array + * @type array $xml An XML fragment representing the post URL. + * @type string $last_modified Date post was last modified. */ private function post_to_sitemap_item( $post ) { @@ -1051,8 +1123,8 @@ class Jetpack_Sitemap_Builder { * * @since 3.9.0 * - * @param bool $skip Current boolean. False by default, so no post is skipped. - * @param WP_POST $post Current post object. + * @param bool $skip Current boolean. False by default, so no post is skipped. + * @param object $post Current post in the form of a $wpdb result object. Not WP_Post. */ if ( true === apply_filters( 'jetpack_sitemap_skip_post', false, $post ) ) { return array( @@ -1117,7 +1189,9 @@ class Jetpack_Sitemap_Builder { * * @param WP_Post $post The image post to be processed. * - * @return string An XML fragment representing the post URL. + * @return array + * @type array $xml An XML fragment representing the post URL. + * @type string $last_modified Date post was last modified. */ private function image_post_to_sitemap_item( $post ) { @@ -1140,6 +1214,15 @@ class Jetpack_Sitemap_Builder { $url = wp_get_attachment_url( $post->ID ); + // Do not include the image if the attached parent is not published. + // Unattached will be published. Otherwise, will inherit parent status. + if ( 'publish' !== get_post_status( $post ) ) { + return array( + 'xml' => null, + 'last_modified' => null, + ); + } + $parent_url = get_permalink( get_post( $post->post_parent ) ); if ( '' == $parent_url ) { // WPCS: loose comparison ok. $parent_url = get_permalink( $post ); @@ -1155,7 +1238,7 @@ class Jetpack_Sitemap_Builder { ), ); - $item_array['url']['image:image']['image:title'] = $post->post_title; + $item_array['url']['image:image']['image:title'] = $post->post_title; $item_array['url']['image:image']['image:caption'] = $post->post_excerpt; /** @@ -1192,7 +1275,9 @@ class Jetpack_Sitemap_Builder { * * @param WP_Post $post The video post to be processed. * - * @return string An XML fragment representing the post URL. + * @return array + * @type array $xml An XML fragment representing the post URL. + * @type string $last_modified Date post was last modified. */ private function video_post_to_sitemap_item( $post ) { @@ -1213,6 +1298,15 @@ class Jetpack_Sitemap_Builder { ); } + // Do not include the video if the attached parent is not published. + // Unattached will be published. Otherwise, will inherit parent status. + if ( 'publish' !== get_post_status( $post ) ) { + return array( + 'xml' => null, + 'last_modified' => null, + ); + } + $parent_url = esc_url( get_permalink( get_post( $post->post_parent ) ) ); if ( '' == $parent_url ) { // WPCS: loose comparison ok. $parent_url = esc_url( get_permalink( $post ) ); @@ -1321,11 +1415,11 @@ class Jetpack_Sitemap_Builder { $item_array = array( 'url' => array( - 'loc' => $url, - 'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ), + 'loc' => $url, + 'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ), 'news:news' => array( - 'news:publication' => array( - 'news:name' => get_bloginfo( 'name' ), + 'news:publication' => array( + 'news:name' => html_entity_decode( get_bloginfo( 'name' ) ), 'news:language' => $language, ), /** This filter is already documented in core/wp-includes/feed.php */ |