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