item_capacity = max( 1, intval( $item_limit ) ); mbstring_binary_safe_encoding(); // So we can safely use strlen(). $this->byte_capacity = max( 1, intval( $byte_limit ) ) - strlen( $header ) - strlen( $footer ); reset_mbstring_encoding(); $this->footer_text = $footer; $this->buffer = $header; $this->is_full_flag = false; $this->is_empty_flag = true; $this->timestamp = $time; return; } /** * 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 ) { if ( is_null( $item ) ) { return true; } else { mbstring_binary_safe_encoding(); // So we can safely use strlen(). $item_size = strlen( $item ); // Size in bytes. reset_mbstring_encoding(); if ( 0 >= $this->item_capacity || 0 > $this->byte_capacity - $item_size ) { $this->is_full_flag = true; return false; } else { $this->is_empty_flag = false; $this->item_capacity -= 1; $this->byte_capacity -= $item_size; $this->buffer .= $item; 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() { return $this->buffer . $this->footer_text; } /** * 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 $this->is_empty_flag; } /** * 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 ); return; } /** * 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 public * @since 3.9.0 * @since 4.8.0 Rename, add $depth parameter, and change return type. * * @param array $array A recursive associative array of tag/child relationships. * @param string $depth String to prepend to each line. For internal use only. * * @return string The rendered XML string. */ public static function array_to_xml_string( $array, $depth = '' ) { $string = ''; foreach ( $array as $key => $value ) { // Only allow a-z, A-Z, colon, underscore, and hyphen. $tag = preg_replace( '/[^a-zA-Z:_-]/', '_', $key ); if ( is_array( $value ) ) { $string .= $depth . "<$tag>\n"; $string .= self::array_to_xml_string( $value, $depth . ' ' ); $string .= $depth . "\n"; } elseif ( is_null( $value ) ) { $string .= $depth . "<$tag />\n"; } else { $string .= $depth . "<$tag>" . ent2ncr( $value ) . "\n"; } } return $string; } /** * Render an associative array of XML attribute key/value pairs. * * @access public * @since 4.8.0 * * @param array $array Key/value array of attributes. * * @return string The rendered attribute string. */ public static function array_to_xml_attr_string( $array ) { $string = ''; foreach ( $array as $key => $value ) { $key = preg_replace( '/[^a-zA-Z:_-]/', '_', $key ); $string .= ' ' . $key . '="' . esc_attr( $value ) . '"'; } return $string; } }