Jetpack_Sitemap_Buffer

A buffer for constructing sitemap xml files.

Defined (1)

The class is defined in the following location(s).

/modules/sitemaps/sitemap-buffer.php  
  1. class Jetpack_Sitemap_Buffer { 
  2.  
  3. /** 
  4. * Largest number of items the buffer can hold. 
  5. * @access private 
  6. * @since 4.8.0 
  7. * @var int $item_capacity The item capacity. 
  8. */ 
  9. private $item_capacity; 
  10.  
  11. /** 
  12. * Largest number of bytes the buffer can hold. 
  13. * @access private 
  14. * @since 4.8.0 
  15. * @var int $byte_capacity The byte capacity. 
  16. */ 
  17. private $byte_capacity; 
  18.  
  19. /** 
  20. * Footer text of the buffer; stored here so it can be appended when the buffer is full. 
  21. * @access private 
  22. * @since 4.8.0 
  23. * @var string $footer_text The footer text. 
  24. */ 
  25. private $footer_text; 
  26.  
  27. /** 
  28. * The buffer contents. 
  29. * @access private 
  30. * @since 4.8.0 
  31. * @var string The buffer contents. 
  32. */ 
  33. private $buffer; 
  34.  
  35. /** 
  36. * Flag which detects when the buffer is full. 
  37. * @access private 
  38. * @since 4.8.0 
  39. * @var bool $is_full_flag The flag value. This flag is set to false on construction and only flipped to true if we've tried to add something and failed. 
  40. */ 
  41. private $is_full_flag; 
  42.  
  43. /** 
  44. * Flag which detects when the buffer is empty. 
  45. * @access private 
  46. * @since 4.8.0 
  47. * @var bool $is_empty_flag The flag value. This flag is set to true on construction and only flipped to false if we've tried to add something and succeeded. 
  48. */ 
  49. private $is_empty_flag; 
  50.  
  51. /** 
  52. * The most recent timestamp seen by the buffer. 
  53. * @access private 
  54. * @since 4.8.0 
  55. * @var string $timestamp Must be in 'YYYY-MM-DD hh:mm:ss' format. 
  56. */ 
  57. private $timestamp; 
  58.  
  59. /** 
  60. * Construct a new Jetpack_Sitemap_Buffer. 
  61. * @since 4.8.0 
  62. * @param int $item_limit The maximum size of the buffer in items. 
  63. * @param int $byte_limit The maximum size of the buffer in bytes. 
  64. * @param string $header The string to prepend to the entire buffer. 
  65. * @param string $footer The string to append to the entire buffer. 
  66. * @param string $time The initial datetime of the buffer. Must be in 'YYYY-MM-DD hh:mm:ss' format. 
  67. */ 
  68. public function __construct( 
  69. $item_limit,  
  70. $byte_limit,  
  71. $header = '',  
  72. $footer = '',  
  73. $time 
  74. ) { 
  75. $this->item_capacity = max( 1, intval( $item_limit ) ); 
  76.  
  77. mbstring_binary_safe_encoding(); // So we can safely use strlen(). 
  78. $this->byte_capacity = max( 1, intval( $byte_limit ) ) - strlen( $header ) - strlen( $footer ); 
  79. reset_mbstring_encoding(); 
  80.  
  81. $this->footer_text = $footer; 
  82. $this->buffer = $header; 
  83. $this->is_full_flag = false; 
  84. $this->is_empty_flag = true; 
  85. $this->timestamp = $time; 
  86. return; 
  87.  
  88. /** 
  89. * Append an item to the buffer, if there is room for it,  
  90. * and set is_empty_flag to false. If there is no room,  
  91. * we set is_full_flag to true. If $item is null,  
  92. * don't do anything and report success. 
  93. * @since 4.8.0 
  94. * @param string $item The item to be added. 
  95. * @return bool True if the append succeeded, False if not. 
  96. */ 
  97. public function try_to_add_item( $item ) { 
  98. if ( is_null( $item ) ) { 
  99. return true; 
  100. } else { 
  101.  
  102. mbstring_binary_safe_encoding(); // So we can safely use strlen(). 
  103. $item_size = strlen( $item ); // Size in bytes. 
  104. reset_mbstring_encoding(); 
  105.  
  106. if ( 0 >= $this->item_capacity || 0 > $this->byte_capacity - $item_size ) { 
  107. $this->is_full_flag = true; 
  108. return false; 
  109. } else { 
  110. $this->is_empty_flag = false; 
  111. $this->item_capacity -= 1; 
  112. $this->byte_capacity -= $item_size; 
  113. $this->buffer .= $item; 
  114. return true; 
  115.  
  116. /** 
  117. * Retrieve the contents of the buffer. 
  118. * @since 4.8.0 
  119. * @return string The contents of the buffer (with the footer included). 
  120. */ 
  121. public function contents() { 
  122. return $this->buffer . $this->footer_text; 
  123.  
  124. /** 
  125. * Detect whether the buffer is full. 
  126. * @since 4.8.0 
  127. * @return bool True if the buffer is full, false otherwise. 
  128. */ 
  129. public function is_full() { 
  130. return $this->is_full_flag; 
  131.  
  132. /** 
  133. * Detect whether the buffer is empty. 
  134. * @since 4.8.0 
  135. * @return bool True if the buffer is empty, false otherwise. 
  136. */ 
  137. public function is_empty() { 
  138. return $this->is_empty_flag; 
  139.  
  140. /** 
  141. * Update the timestamp of the buffer. 
  142. * @since 4.8.0 
  143. * @param string $new_time A datetime string in 'YYYY-MM-DD hh:mm:ss' format. 
  144. */ 
  145. public function view_time( $new_time ) { 
  146. $this->timestamp = max( $this->timestamp, $new_time ); 
  147. return; 
  148.  
  149. /** 
  150. * Retrieve the timestamp of the buffer. 
  151. * @since 4.8.0 
  152. * @return string A datetime string in 'YYYY-MM-DD hh:mm:ss' format. 
  153. */ 
  154. public function last_modified() { 
  155. return $this->timestamp; 
  156.  
  157. /** 
  158. * Render an associative array as an XML string. This is needed because 
  159. * SimpleXMLElement only handles valid XML, but we sometimes want to 
  160. * pass around (possibly invalid) fragments. Note that 'null' values make 
  161. * a tag self-closing; this is only sometimes correct (depending on the 
  162. * version of HTML/XML); see the list of 'void tags'. 
  163. * Example: 
  164. * array( 
  165. * 'html' => array( |<html xmlns="foo"> 
  166. * 'head' => array( | <head> 
  167. * 'title' => 'Woo!', | <title>Woo!</title> 
  168. * ), | </head> 
  169. * 'body' => array( ==> | <body> 
  170. * 'h2' => 'Some thing', | <h2>Some thing</h2> 
  171. * 'p' => 'it's all up ons', | <p>it's all up ons</p> 
  172. * 'br' => null, | <br /> 
  173. * ), | </body> 
  174. * ), |</html> 
  175. * ) 
  176. * @access public 
  177. * @since 3.9.0 
  178. * @since 4.8.0 Rename, add $depth parameter, and change return type. 
  179. * @param array $array A recursive associative array of tag/child relationships. 
  180. * @param string $depth String to prepend to each line. For internal use only. 
  181. * @return string The rendered XML string. 
  182. */ 
  183. public static function array_to_xml_string( $array, $depth = '' ) { 
  184. $string = ''; 
  185.  
  186. foreach ( $array as $key => $value ) { 
  187.  
  188. // Only allow a-z, A-Z, colon, underscore, and hyphen. 
  189. $tag = preg_replace( '/[^a-zA-Z:_-]/', '_', $key ); 
  190.  
  191. if ( is_array( $value ) ) { 
  192. $string .= $depth . "<$tag>\n"; 
  193. $string .= self::array_to_xml_string( $value, $depth . ' ' ); 
  194. $string .= $depth . "</$tag>\n"; 
  195. } elseif ( is_null( $value ) ) { 
  196. $string .= $depth . "<$tag />\n"; 
  197. } else { 
  198. $string .= $depth . "<$tag>" . ent2ncr( $value ) . "</$tag>\n"; 
  199.  
  200. return $string; 
  201.  
  202. /** 
  203. * Render an associative array of XML attribute key/value pairs. 
  204. * @access public 
  205. * @since 4.8.0 
  206. * @param array $array Key/value array of attributes. 
  207. * @return string The rendered attribute string. 
  208. */ 
  209. public static function array_to_xml_attr_string( $array ) { 
  210. $string = ''; 
  211.  
  212. foreach ( $array as $key => $value ) { 
  213. $key = preg_replace( '/[^a-zA-Z:_-]/', '_', $key ); 
  214. $string .= ' ' . $key . '="' . esc_attr( $value ) . '"'; 
  215.  
  216. return $string; 
  217.