• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Jetpack a2z

Jetpack a2z

Jetpack a2z

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Jetpack_Sitemap_Builder::build_one_sitemap_index() – Build and store a single page sitemap index. Return false if no index is built.

You appear to be a bot. Output may be restricted

Description

Build and store a single page sitemap index. Return false if no index is built.

Side effect: Create/update a sitemap index row.

Usage

$bool|array = Jetpack_Sitemap_Builder::build_one_sitemap_index( $number, $from_id, $datetime, $index_type );

Parameters

$number
( int ) required – The number of the current sitemap index.
$from_id
( int ) required – The greatest lower bound of the IDs of the sitemaps to be included.
$datetime
( string ) required – Datetime of previous sitemap in 'YYYY-MM-DD hh:mm:ss' format.
$index_type
( string ) required – Sitemap index type.
$last_id
( int ) required – The ID of the last item to be successfully added to the buffer.
$any_left
( bool ) required – 'true' if there are items which haven't been saved to a sitemap, 'false' otherwise.
$last_modified
( string ) required – The most recent timestamp to appear on the sitemap. }

Returns

bool|array @args {

Source

File name: jetpack/modules/sitemaps/sitemap-builder.php
Lines:

1 to 91 of 91
  private function build_one_sitemap_index( $number, $from_id, $datetime, $index_type ) {
    $last_sitemap_id   = $from_id;
    $any_sitemaps_left = true;

    // Check the datetime format.
    $datetime = jp_sitemap_datetime( $datetime );

    $sitemap_type = jp_sitemap_child_type_of( $index_type );

    if ( $this->logger ) {
      $index_debug_name = jp_sitemap_filename( $index_type, $number );
      $this->logger->report( "-- Building $index_debug_name" );
    }

    $buffer = new Jetpack_Sitemap_Buffer_Master(
      JP_SITEMAP_MAX_ITEMS,
      JP_SITEMAP_MAX_BYTES,
      $datetime
    );

    // Add pointer to the previous sitemap index (unless we're at the first one).
    if ( 1 !== $number ) {
      $i              = $number - 1;
      $prev_index_url = $this->finder->construct_sitemap_url(
        jp_sitemap_filename( $index_type, $i )
      );

      $item_array = array(
        'sitemap' => array(
          'loc'     => $prev_index_url,
          'lastmod' => $datetime,
        ),
      );

      $buffer->append( $item_array );
    }

    // Add as many items to the buffer as possible.
    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
      );

      // If there were no posts to get, make a note.
      if ( null == $posts ) { // WPCS: loose comparison ok.
        $any_sitemaps_left = false;
        break;
      }

      // Otherwise, loop through each post in the batch.
      foreach ( $posts as $post ) {
        // Generate the sitemap XML for the post.
        $current_item = $this->sitemap_row_to_index_item( (array) $post );

        // Try adding this item to the buffer.
        if ( true === $buffer->append( $current_item['xml'] ) ) {
          $last_sitemap_id = $post['ID'];
          $buffer->view_time( $current_item['last_modified'] );
        } else {
          // Otherwise stop looping through posts.
          break;
        }
      }
    }

    // If no items were added, return false.
    if ( true === $buffer->is_empty() ) {
      return false;
    }

    $this->librarian->store_sitemap_data(
      $number,
      $index_type,
      $buffer->contents(),
      $buffer->last_modified()
    );

    /*
		 * Now report back with the ID of the last sitemap post ID to
		 * be successfully added, whether there are any sitemap posts
		 * left, and the most recent modification time seen.
		 */
    return array(
      'last_id'       => $last_sitemap_id,
      'any_left'      => $any_sitemaps_left,
      'last_modified' => $buffer->last_modified(),
    );
  }
 

 View on GitHub View on Trac

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Related

Published: 27 November 2019 | Last updated: 27 November 2019

Primary Sidebar

Information

Function name: Jetpack_Sitemap_Builder::build_one_sitemap_index
Class ref: Jetpack_Sitemap_Builder
Plugin ref: Jetpack
Version: 9.2
Sourcefile: modules/sitemaps/sitemap-builder.php
File ref: modules/sitemaps/sitemap-builder.php
Deprecated?: No
API Letters: B,J,O,S

Footer

Jetpack a2z
Jetpack a2z
Jetpack a2z
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  jetpack.wp-a2z.org
© Copyright Jetpack a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites