is_full_flag = false; $this->timestamp = $time; $this->finder = new Jetpack_Sitemap_Finder(); $this->writer = new XMLWriter(); $this->writer->openMemory(); $this->writer->setIndent( true ); $this->writer->startDocument( '1.0', 'UTF-8' ); $this->item_capacity = max( 1, (int) $item_limit ); $this->byte_capacity = max( 1, (int) $byte_limit ); $this->initialize_buffer(); } /** * Initialize the buffer with any required headers or setup. * This should be implemented by child classes. * * @access protected * @since 14.6 */ abstract protected function initialize_buffer(); /** * Append an item to the buffer. * * @since 14.6 * * @param array $array The item to be added. * @return bool True if the append succeeded, False if not. */ public function append( $array ) { if ( $array === null ) { return true; } if ( $this->is_full_flag ) { return false; } if ( 0 >= $this->item_capacity || 0 >= $this->byte_capacity ) { $this->is_full_flag = true; return false; } $current_content = $this->writer->outputMemory( true ); $this->content .= $current_content; $this->append_item( $array ); $new_content = $this->writer->outputMemory( true ); $this->content .= $new_content; // Update capacities after successful append $this->item_capacity -= 1; $this->byte_capacity -= strlen( $new_content ); // Check both capacity limits if ( 0 >= $this->item_capacity || $this->byte_capacity <= 0 ) { $this->is_full_flag = true; } return true; } /** * Append a specific item to the buffer. * This should be implemented by child classes. * * @access protected * @since 14.6 * @param array $array The item to be added. */ abstract protected function append_item( $array ); /** * Retrieve the contents of the buffer. * * @since 14.6 * @return string The contents of the buffer. */ public function contents() { $this->writer->endElement(); // End root element (urlset/sitemapindex) $this->writer->endDocument(); $final_content = $this->writer->outputMemory( true ); $this->content .= $final_content; if ( empty( $this->content ) ) { // If buffer is empty, return a minimal valid XML structure return "\n"; } return $this->content; } /** * Detect whether the buffer is full. * * @since 14.6 * @return bool True if the buffer is full, false otherwise. */ public function is_full() { return $this->is_full_flag; } /** * Detect whether the buffer is empty. * * @since 14.6 * @return bool True if the buffer is empty, false otherwise. */ public function is_empty() { $current = $this->writer->outputMemory( true ); $this->content .= $current; return empty( $this->content ); } /** * Update the timestamp of the buffer. * * @since 14.6 * @param string $new_time A datetime string in 'YYYY-MM-DD hh:mm:ss' format. */ public function view_time( $new_time ) { $this->timestamp = max( $this->timestamp, $new_time ); } /** * Retrieve the timestamp of the buffer. * * @since 14.6 * @return string A datetime string in 'YYYY-MM-DD hh:mm:ss' format. */ public function last_modified() { return $this->timestamp; } /** * Compatibility method for the old DOMDocument implementation. * This is only here to satisfy the jetpack_print_sitemap filter. * * @since 14.6 * @return null */ public function get_document() { return null; } }