is_full_flag = false; $this->timestamp = $time; $this->finder = new Jetpack_Sitemap_Finder(); $this->doc = new DOMDocument( '1.0', 'UTF-8' ); $this->item_capacity = max( 1, intval( $item_limit ) ); $this->byte_capacity = max( 1, intval( $byte_limit ) ) - strlen( $this->doc->saveXML() ); } /** * Returns a DOM element that contains all sitemap elements. * * @access protected * @since 5.3.0 * @return DOMElement $root */ abstract protected function get_root_element(); /** * Append an item to the buffer, if there is room for it, * and set is_empty_flag to false. If there is no room, * we set is_full_flag to true. If $item is null, * don't do anything and report success. * * @since 4.8.0 * * @param string $item The item to be added. * * @return bool True if the append succeeded, False if not. */ public function try_to_add_item( $item ) { _deprecated_function( 'Jetpack_Sitemap_Buffer::try_to_add_item', '5.3.0', 'Jetpack_Sitemap_Buffer::append' ); $this->append( $item ); } /** * Append an item to the buffer, if there is room for it, * and set is_empty_flag to false. If there is no room, * we set is_full_flag to true. If $item is null, * don't do anything and report success. * * @since 5.3.0 * * @param array $array The item to be added. * * @return bool True if the append succeeded, False if not. */ public function append( $array ) { if ( is_null( $array ) ) { 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; } else { $this->item_capacity -= 1; $added_element = $this->array_to_xml_string( $array, $this->get_root_element(), $this->doc ); $this->byte_capacity -= strlen( $this->doc->saveXML( $added_element ) ); return true; } } /** * Retrieve the contents of the buffer. * * @since 4.8.0 * * @return string The contents of the buffer (with the footer included). */ public function contents() { if( $this->is_empty() ) { // The sitemap should have at least the root element added to the DOM $this->get_root_element(); } return $this->doc->saveXML(); } /** * Retrieve the document object. * * @since 5.3.0 * @return DOMDocument $doc */ public function get_document() { return $this->doc; } /** * Detect whether the buffer is full. * * @since 4.8.0 * * @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 4.8.0 * * @return bool True if the buffer is empty, false otherwise. */ public function is_empty() { return ( ! isset( $this->root ) || ! $this->root->hasChildNodes() ); } /** * Update the timestamp of the buffer. * * @since 4.8.0 * * @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 4.8.0 * * @return string A datetime string in 'YYYY-MM-DD hh:mm:ss' format. */ public function last_modified() { return $this->timestamp; } /** * Render an associative array as an XML string. This is needed because * SimpleXMLElement only handles valid XML, but we sometimes want to * pass around (possibly invalid) fragments. Note that 'null' values make * a tag self-closing; this is only sometimes correct (depending on the * version of HTML/XML); see the list of 'void tags'. * * Example: * * array( * 'html' => array( | * 'head' => array( | * 'title' => 'Woo!', | Woo! * ), | * 'body' => array( ==> | * 'h2' => 'Some thing', |

Some thing

* 'p' => 'it's all up ons', |

it's all up ons

* 'br' => null, |
* ), | * ), | * ) * * @access protected * @since 3.9.0 * @since 4.8.0 Rename, add $depth parameter, and change return type. * @since 5.3.0 Refactor, remove $depth parameter, add $parent and $root, make access protected. * * @param array $array A recursive associative array of tag/child relationships. * @param DOMElement $parent (optional) an element to which new children should be added. * @param DOMDocument $root (optional) the parent document. * * @return string|DOMDocument The rendered XML string or an object if root element is specified. */ protected function array_to_xml_string( $array, $parent = null, $root = null ) { $return_string = false; if ( null === $parent ) { $return_string = true; $parent = $root = new DOMDocument(); } if ( is_array( $array ) ) { foreach ( $array as $key => $value ) { $element = $root->createElement( $key ); $parent->appendChild( $element ); if ( is_array( $value ) ) { foreach ( $value as $child_key => $child_value ) { $child = $root->createElement( $child_key ); $element->appendChild( $child ); $child->appendChild( self::array_to_xml_string( $child_value, $child, $root ) ); } } else { $element->appendChild( $root->createTextNode( $value ) ); } } } else { $element = $root->createTextNode( $array ); $parent->appendChild( $element ); } if ( $return_string ) { return $root->saveHTML(); } else { return $element; } } }