summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2017-09-20 09:54:07 -0400
committerAnthony G. Basile <blueness@gentoo.org>2017-09-20 09:54:07 -0400
commitbb31fa2d44dbbe425fa9ce92d4e7be61a1146dfb (patch)
tree33456f5889d315ad2a520fcd33fe491e612588b4 /plugins/jetpack/modules/sitemaps/sitemap-builder.php
parentakismet 4.0 (diff)
downloadblogs-gentoo-bb31fa2d44dbbe425fa9ce92d4e7be61a1146dfb.tar.gz
blogs-gentoo-bb31fa2d44dbbe425fa9ce92d4e7be61a1146dfb.tar.bz2
blogs-gentoo-bb31fa2d44dbbe425fa9ce92d4e7be61a1146dfb.zip
jetpack 5.3
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.php317
1 files changed, 73 insertions, 244 deletions
diff --git a/plugins/jetpack/modules/sitemaps/sitemap-builder.php b/plugins/jetpack/modules/sitemaps/sitemap-builder.php
index cfd7b9a1..9cf91c24 100644
--- a/plugins/jetpack/modules/sitemaps/sitemap-builder.php
+++ b/plugins/jetpack/modules/sitemaps/sitemap-builder.php
@@ -9,6 +9,22 @@
require_once dirname( __FILE__ ) . '/sitemap-constants.php';
require_once dirname( __FILE__ ) . '/sitemap-buffer.php';
+
+if ( ! class_exists( 'DOMDocument' ) ) {
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-fallback.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-image-fallback.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-master-fallback.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-news-fallback.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-page-fallback.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-video-fallback.php';
+} else {
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-image.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-master.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-news.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-page.php';
+ require_once dirname( __FILE__ ) . '/sitemap-buffer-video.php';
+}
+
require_once dirname( __FILE__ ) . '/sitemap-librarian.php';
require_once dirname( __FILE__ ) . '/sitemap-finder.php';
require_once dirname( __FILE__ ) . '/sitemap-state.php';
@@ -87,8 +103,6 @@ class Jetpack_Sitemap_Builder {
array( 'post', 'page' )
)
);
-
- return;
}
/**
@@ -101,6 +115,17 @@ class Jetpack_Sitemap_Builder {
public function update_sitemap() {
if ( $this->logger ) {
$this->logger->report( '-- Updating...' );
+ 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'
+ )
+ );
+ }
}
for ( $i = 1; $i <= JP_SITEMAP_UPDATE_SIZE; $i++ ) {
@@ -201,12 +226,10 @@ class Jetpack_Sitemap_Builder {
JP_PAGE_SITEMAP_TYPE
);
die();
- }
+ } // End switch().
// Unlock the state.
Jetpack_Sitemap_State::unlock();
-
- return;
}
/**
@@ -280,8 +303,6 @@ class Jetpack_Sitemap_Builder {
$this->librarian->delete_numbered_sitemap_rows_after(
$state['number'] + 1, $sitemap_type
);
-
- return;
}
/**
@@ -387,29 +408,13 @@ class Jetpack_Sitemap_Builder {
* @since 4.8.0
*/
private function build_master_sitemap( $max ) {
- $sitemap_index_xsl_url = $this->finder->construct_sitemap_url( 'sitemap-index.xsl' );
- $jetpack_version = JETPACK__VERSION;
-
if ( $this->logger ) {
$this->logger->report( '-- Building Master Sitemap.' );
}
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_Master(
JP_SITEMAP_MAX_ITEMS,
- JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$sitemap_index_xsl_url}'?>
-<sitemapindex xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>
-HEADER
- ,
- <<<FOOTER
-</sitemapindex>\n
-FOOTER
- ,
- /* epoch */
- '1970-01-01 00:00:00'
+ JP_SITEMAP_MAX_BYTES
);
if ( 0 < $max[ JP_PAGE_SITEMAP_TYPE ]['number'] ) {
@@ -424,14 +429,14 @@ FOOTER
$page['last_modified'] = jp_sitemap_datetime( $max[ JP_PAGE_SITEMAP_INDEX_TYPE ]['lastmod'] );
}
- $buffer->try_to_add_item( Jetpack_Sitemap_Buffer::array_to_xml_string(
+ $buffer->append(
array(
'sitemap' => array(
'loc' => $this->finder->construct_sitemap_url( $page['filename'] ),
'lastmod' => $page['last_modified'],
),
)
- ) );
+ );
}
if ( 0 < $max[ JP_IMAGE_SITEMAP_TYPE ]['number'] ) {
@@ -446,14 +451,14 @@ FOOTER
$image['last_modified'] = jp_sitemap_datetime( $max[ JP_IMAGE_SITEMAP_INDEX_TYPE ]['lastmod'] );
}
- $buffer->try_to_add_item( Jetpack_Sitemap_Buffer::array_to_xml_string(
+ $buffer->append(
array(
'sitemap' => array(
'loc' => $this->finder->construct_sitemap_url( $image['filename'] ),
'lastmod' => $image['last_modified'],
),
)
- ) );
+ );
}
if ( 0 < $max[ JP_VIDEO_SITEMAP_TYPE ]['number'] ) {
@@ -468,14 +473,14 @@ FOOTER
$video['last_modified'] = $max[ JP_VIDEO_SITEMAP_INDEX_TYPE ]['lastmod'];
}
- $buffer->try_to_add_item( Jetpack_Sitemap_Buffer::array_to_xml_string(
+ $buffer->append(
array(
'sitemap' => array(
'loc' => $this->finder->construct_sitemap_url( $video['filename'] ),
'lastmod' => $video['last_modified'],
),
)
- ) );
+ );
}
$this->librarian->store_sitemap_data(
@@ -484,8 +489,6 @@ FOOTER
$buffer->contents(),
''
);
-
- return;
}
/**
@@ -514,46 +517,9 @@ FOOTER
$this->logger->report( "-- Building $debug_name" );
}
- $sitemap_xsl_url = $this->finder->construct_sitemap_url( 'sitemap.xsl' );
-
- $jetpack_version = JETPACK__VERSION;
-
- $namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
- /**
- * Filter the attribute value pairs used for namespace and namespace URI mappings.
- *
- * @module sitemaps
- *
- * @since 3.9.0
- *
- * @param array $namespaces Associative array with namespaces and namespace URIs.
- */
- apply_filters(
- 'jetpack_sitemap_ns',
- array(
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
- 'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
- 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
- )
- )
- );
-
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_Page(
JP_SITEMAP_MAX_ITEMS,
- JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$sitemap_xsl_url}'?>
-<urlset{$namespaces}>\n
-HEADER
- ,
- <<<FOOTER
-</urlset>\n
-FOOTER
- ,
- /* epoch */
- '1970-01-01 00:00:00'
+ JP_SITEMAP_MAX_BYTES
);
// Add entry for the main page (only if we're at the first one).
@@ -576,7 +542,7 @@ FOOTER
*/
$item_array = apply_filters( 'jetpack_sitemap_url_home', $item_array );
- $buffer->try_to_add_item( Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ) );
+ $buffer->append( $item_array );
}
// Add as many items to the buffer as possible.
@@ -593,7 +559,7 @@ FOOTER
foreach ( $posts as $post ) {
$current_item = $this->post_to_sitemap_item( $post );
- if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
+ if ( true === $buffer->append( $current_item['xml'] ) ) {
$last_post_id = $post->ID;
$buffer->view_time( $current_item['last_modified'] );
} else {
@@ -613,13 +579,14 @@ FOOTER
* @module sitemaps
*
* @since 3.9.0
+ * @since 5.3.0 returns an element of DOMDocument type instead of SimpleXMLElement
*
- * @param SimpleXMLElement $tree Data tree for sitemap.
+ * @param DOMDocument $doc Data tree for sitemap.
* @param string $last_modified Date of last modification.
*/
$tree = apply_filters(
'jetpack_print_sitemap',
- simplexml_load_string( $buffer->contents() ),
+ $buffer->get_document(),
$buffer->last_modified()
);
@@ -627,7 +594,7 @@ FOOTER
$this->librarian->store_sitemap_data(
$number,
JP_PAGE_SITEMAP_TYPE,
- $tree->asXML(),
+ $buffer->contents(),
$buffer->last_modified()
);
@@ -668,47 +635,9 @@ FOOTER
$this->logger->report( "-- Building $debug_name" );
}
- $image_sitemap_xsl_url = $this->finder->construct_sitemap_url( 'image-sitemap.xsl' );
-
- $jetpack_version = JETPACK__VERSION;
-
- $namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
- /**
- * Filter the XML namespaces included in image sitemaps.
- *
- * @module sitemaps
- *
- * @since 4.8.0
- *
- * @param array $namespaces Associative array with namespaces and namespace URIs.
- */
- apply_filters(
- 'jetpack_sitemap_image_ns',
- array(
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
- 'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
- 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
- 'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1',
- )
- )
- );
-
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_Image(
JP_SITEMAP_MAX_ITEMS,
- JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$image_sitemap_xsl_url}'?>
-<urlset{$namespaces}>\n
-HEADER
- ,
- <<<FOOTER
-</urlset>\n
-FOOTER
- ,
- /* epoch */
- '1970-01-01 00:00:00'
+ JP_SITEMAP_MAX_BYTES
);
// Add as many items to the buffer as possible.
@@ -725,7 +654,7 @@ FOOTER
foreach ( $posts as $post ) {
$current_item = $this->image_post_to_sitemap_item( $post );
- if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
+ if ( true === $buffer->append( $current_item['xml'] ) ) {
$last_post_id = $post->ID;
$buffer->view_time( $current_item['last_modified'] );
} else {
@@ -784,47 +713,9 @@ FOOTER
$this->logger->report( "-- Building $debug_name" );
}
- $video_sitemap_xsl_url = $this->finder->construct_sitemap_url( 'video-sitemap.xsl' );
-
- $jetpack_version = JETPACK__VERSION;
-
- $namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
- /**
- * Filter the XML namespaces included in video sitemaps.
- *
- * @module sitemaps
- *
- * @since 4.8.0
- *
- * @param array $namespaces Associative array with namespaces and namespace URIs.
- */
- apply_filters(
- 'jetpack_sitemap_video_ns',
- array(
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
- 'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
- 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
- 'xmlns:video' => 'http://www.google.com/schemas/sitemap-video/1.1',
- )
- )
- );
-
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_Video(
JP_SITEMAP_MAX_ITEMS,
- JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$video_sitemap_xsl_url}'?>
-<urlset{$namespaces}>\n
-HEADER
- ,
- <<<FOOTER
-</urlset>\n
-FOOTER
- ,
- /* epoch */
- '1970-01-01 00:00:00'
+ JP_SITEMAP_MAX_BYTES
);
// Add as many items to the buffer as possible.
@@ -841,7 +732,7 @@ FOOTER
foreach ( $posts as $post ) {
$current_item = $this->video_post_to_sitemap_item( $post );
- if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
+ if ( true === $buffer->append( $current_item['xml'] ) ) {
$last_post_id = $post->ID;
$buffer->view_time( $current_item['last_modified'] );
} else {
@@ -908,25 +799,9 @@ FOOTER
$this->logger->report( "-- Building $index_debug_name" );
}
- $sitemap_index_xsl_url = $this->finder->construct_sitemap_url( 'sitemap-index.xsl' );
-
- $jetpack_version = JETPACK__VERSION;
-
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_Master(
JP_SITEMAP_MAX_ITEMS,
JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$sitemap_index_xsl_url}'?>
-<sitemapindex xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n
-HEADER
- ,
- <<<FOOTER
-</sitemapindex>\n
-FOOTER
- ,
- /* initial last_modified value */
$datetime
);
@@ -944,7 +819,7 @@ FOOTER
),
);
- $buffer->try_to_add_item( Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ) );
+ $buffer->append( $item_array );
}
// Add as many items to the buffer as possible.
@@ -966,7 +841,7 @@ FOOTER
$current_item = $this->sitemap_row_to_index_item( (array) $post );
// Try adding this item to the buffer.
- if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
+ if ( true === $buffer->append( $current_item['xml'] ) ) {
$last_sitemap_id = $post['ID'];
$buffer->view_time( $current_item['last_modified'] );
} else {
@@ -1023,7 +898,7 @@ FOOTER
);
return array(
- 'xml' => Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ),
+ 'xml' => $item_array,
'last_modified' => $row['post_date'],
);
}
@@ -1046,10 +921,6 @@ FOOTER
$this->logger->report( 'Beginning news sitemap generation.' );
}
- $news_sitemap_xsl_url = $this->finder->construct_sitemap_url( 'news-sitemap.xsl' );
-
- $jetpack_version = JETPACK__VERSION;
-
/**
* Filter limit of entries to include in news sitemap.
*
@@ -1064,43 +935,9 @@ FOOTER
JP_NEWS_SITEMAP_MAX_ITEMS
);
- $namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
- /**
- * Filter the attribute value pairs used for namespace and namespace URI mappings.
- *
- * @module sitemaps
- *
- * @since 4.8.0
- *
- * @param array $namespaces Associative array with namespaces and namespace URIs.
- */
- apply_filters(
- 'jetpack_sitemap_news_ns',
- array(
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
- 'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
- 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
- 'xmlns:news' => 'http://www.google.com/schemas/sitemap-news/0.9',
- )
- )
- );
-
- $buffer = new Jetpack_Sitemap_Buffer(
+ $buffer = new Jetpack_Sitemap_Buffer_News(
min( $item_limit, JP_NEWS_SITEMAP_MAX_ITEMS ),
- JP_SITEMAP_MAX_BYTES,
- <<<HEADER
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- generator='jetpack-{$jetpack_version}' -->
-<?xml-stylesheet type='text/xsl' href='{$news_sitemap_xsl_url}'?>
-<urlset{$namespaces}>\n
-HEADER
- ,
- <<<FOOTER
-</urlset>\n
-FOOTER
- ,
- /* epoch */
- '1970-01-01 00:00:00'
+ JP_SITEMAP_MAX_BYTES
);
$posts = $this->librarian->query_most_recent_posts( JP_NEWS_SITEMAP_MAX_ITEMS );
@@ -1108,7 +945,7 @@ FOOTER
foreach ( $posts as $post ) {
$current_item = $this->post_to_news_sitemap_item( $post );
- if ( false === $buffer->try_to_add_item( $current_item['xml'] ) ) {
+ if ( false === $buffer->append( $current_item['xml'] ) ) {
break;
}
}
@@ -1124,7 +961,7 @@ FOOTER
$the_stored_news_sitemap,
JP_NEWS_SITEMAP_INTERVAL
);
- }
+ } // End if().
return $the_stored_news_sitemap;
}
@@ -1138,7 +975,7 @@ FOOTER
*
* @param WP_Post $post The post to be processed.
*
- * @return string An XML fragment representing the post URL.
+ * @return array An array representing the post URL.
*/
private function post_to_sitemap_item( $post ) {
@@ -1200,7 +1037,7 @@ FOOTER
$item_array = apply_filters( 'jetpack_sitemap_url', $item_array, $post->ID );
return array(
- 'xml' => Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ),
+ 'xml' => $item_array,
'last_modified' => $last_modified,
);
}
@@ -1236,11 +1073,11 @@ FOOTER
);
}
- $url = esc_url( wp_get_attachment_url( $post->ID ) );
+ $url = wp_get_attachment_url( $post->ID );
- $parent_url = esc_url( get_permalink( get_post( $post->post_parent ) ) );
+ $parent_url = get_permalink( get_post( $post->post_parent ) );
if ( '' == $parent_url ) { // WPCS: loose comparison ok.
- $parent_url = esc_url( get_permalink( $post ) );
+ $parent_url = get_permalink( $post );
}
$item_array = array(
@@ -1252,16 +1089,9 @@ FOOTER
),
),
);
- /** This filter is already documented in core/wp-includes/feed.php */
- $title = apply_filters( 'the_title_rss', $post->post_title );
- if ( '' !== $title ) {
- $item_array['url']['image:image']['image:title'] = htmlentities( $title );
- }
- /** This filter is already documented in core/wp-includes/feed.php */
- $caption = apply_filters( 'the_excerpt_rss', $post->post_excerpt );
- if ( '' !== $caption ) {
- $item_array['url']['image:image']['image:caption'] = "<![CDATA[" . $caption . "]]>";
- }
+
+ $item_array['url']['image:image']['image:title'] = $post->post_title;
+ $item_array['url']['image:image']['image:caption'] = $post->post_excerpt;
/**
* Filter associative array with data to build <url> node
@@ -1281,7 +1111,7 @@ FOOTER
);
return array(
- 'xml' => Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ),
+ 'xml' => $item_array,
'last_modified' => $post->post_modified_gmt,
);
}
@@ -1323,11 +1153,11 @@ FOOTER
$parent_url = esc_url( get_permalink( $post ) );
}
- // Prepare the content like get_the_content_feed()
+ // Prepare the content like get_the_content_feed().
$content = $post->post_content;
/** This filter is already documented in core/wp-includes/post-template.php */
$content = apply_filters( 'the_content', $content );
- $content = str_replace(']]>', ']]&gt;', $content);
+
/** This filter is already documented in core/wp-includes/feed.php */
$content = apply_filters( 'the_content_feed', $content, 'rss2' );
@@ -1339,7 +1169,7 @@ FOOTER
/** This filter is already documented in core/wp-includes/feed.php */
'video:title' => apply_filters( 'the_title_rss', $post->post_title ),
'video:thumbnail_loc' => '',
- 'video:description' => '<![CDATA[' . $content . ']]>',
+ 'video:description' => $content,
'video:content_loc' => esc_url( wp_get_attachment_url( $post->ID ) ),
),
),
@@ -1366,7 +1196,7 @@ FOOTER
);
return array(
- 'xml' => Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ),
+ 'xml' => $item_array,
'last_modified' => $post->post_modified_gmt,
);
}
@@ -1430,7 +1260,7 @@ FOOTER
'lastmod' => jp_sitemap_datetime( $post->post_modified_gmt ),
'news:news' => array(
'news:publication' => array(
- 'news:name' => esc_html( get_bloginfo( 'name' ) ),
+ 'news:name' => get_bloginfo( 'name' ),
'news:language' => $language,
),
/** This filter is already documented in core/wp-includes/feed.php */
@@ -1459,8 +1289,7 @@ FOOTER
);
return array(
- 'xml' => Jetpack_Sitemap_Buffer::array_to_xml_string( $item_array ),
+ 'xml' => $item_array,
);
}
-
}