sitemap-buffer-fallback.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * The fallback buffer for users with no XML support.
  4. *
  5. * @since 5.3.0
  6. * @package Jetpack
  7. */
  8. /**
  9. * A buffer for constructing master sitemap xml files.
  10. *
  11. * @since 5.1.0
  12. */
  13. abstract class Jetpack_Sitemap_Buffer_Fallback extends Jetpack_Sitemap_Buffer {
  14. /**
  15. * The buffer contents.
  16. *
  17. * @access protected
  18. * @since 5.3.0
  19. * @var string The buffer contents.
  20. */
  21. protected $buffer;
  22. public function __construct( $item_limit, $byte_limit, $time = '1970-01-01 00:00:00' ) {
  23. $this->is_full_flag = false;
  24. $this->is_empty_flag = true;
  25. $this->timestamp = $time;
  26. $this->finder = new Jetpack_Sitemap_Finder();
  27. $this->item_capacity = max( 1, intval( $item_limit ) );
  28. $this->byte_capacity = max( 1, intval( $byte_limit ) ) - strlen( $this->contents() );
  29. }
  30. /**
  31. * Append an item to the buffer, if there is room for it,
  32. * and set is_empty_flag to false. If there is no room,
  33. * we set is_full_flag to true. If $item is null,
  34. * don't do anything and report success.
  35. *
  36. * @since 5.3.0
  37. *
  38. * @param array $array The item to be added.
  39. *
  40. * @return bool True if the append succeeded, False if not.
  41. */
  42. public function append( $array ) {
  43. if ( is_null( $array ) ) {
  44. return true;
  45. }
  46. if ( $this->is_full_flag ) {
  47. return false;
  48. }
  49. if ( 0 >= $this->item_capacity || 0 >= $this->byte_capacity ) {
  50. $this->is_full_flag = true;
  51. return false;
  52. } else {
  53. $this->item_capacity -= 1;
  54. $added_string = $this->array_to_xml_string( $array );
  55. $this->buffer .= $added_string;
  56. $this->is_empty_flag = false;
  57. mbstring_binary_safe_encoding(); // So we can safely use strlen().
  58. $this->byte_capacity -= strlen( $added_string );
  59. reset_mbstring_encoding();
  60. return true;
  61. }
  62. }
  63. /**
  64. * Detect whether the buffer is empty.
  65. *
  66. * @since 5.3.0
  67. *
  68. * @return bool True if the buffer is empty, false otherwise.
  69. */
  70. public function is_empty() {
  71. return $this->is_empty_flag;
  72. }
  73. /**
  74. * Retrieve the contents of the buffer.
  75. *
  76. * @since 5.3.0
  77. *
  78. * @return string The contents of the buffer (with the footer included).
  79. */
  80. public function contents() {
  81. $root = $this->get_root_element();
  82. return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . $root[0] . $this->buffer . $root[1] . PHP_EOL;
  83. }
  84. /**
  85. * Legacy implementation of array to XML conversion without using DOMDocument.
  86. *
  87. * @param Array $array
  88. * @return String $result
  89. */
  90. public function array_to_xml_string( $array, $parent = null, $root = null ) {
  91. $string = '';
  92. foreach ( $array as $key => $value ) {
  93. // Only allow a-z, A-Z, colon, underscore, and hyphen.
  94. $tag = preg_replace( '/[^a-zA-Z:_-]/', '_', $key );
  95. if ( is_array( $value ) ) {
  96. $string .= "<$tag>";
  97. $string .= $this->array_to_xml_string( $value );
  98. $string .= "</$tag>";
  99. } elseif ( is_null( $value ) ) {
  100. $string .= "<$tag />";
  101. } else {
  102. $string .= "<$tag>" . htmlspecialchars( $value ) . "</$tag>";
  103. }
  104. }
  105. return $string;
  106. }
  107. /**
  108. * Render an associative array of XML attribute key/value pairs.
  109. *
  110. * @access public
  111. * @since 5.3.0
  112. *
  113. * @param array $array Key/value array of attributes.
  114. *
  115. * @return string The rendered attribute string.
  116. */
  117. public static function array_to_xml_attr_string( $array ) {
  118. $string = '';
  119. foreach ( $array as $key => $value ) {
  120. $key = preg_replace( '/[^a-zA-Z:_-]/', '_', $key );
  121. $string .= ' ' . $key . '="' . esc_attr( $value ) . '"';
  122. }
  123. return $string;
  124. }
  125. }