Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.23% covered (danger)
30.23%
13 / 43
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Buffer_Fallback
32.50% covered (danger)
32.50%
13 / 40
50.00% covered (danger)
50.00%
3 / 6
74.28
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 append
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 is_empty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 contents
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 array_to_xml_string
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 array_to_xml_attr_string
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * The fallback buffer for users with no XML support.
4 *
5 * @since 5.3.0
6 * @package automattic/jetpack
7 */
8
9if ( ! defined( 'ABSPATH' ) ) {
10    exit( 0 );
11}
12
13require_once __DIR__ . '/sitemap-stylist.php';
14
15/**
16 * A buffer for constructing master sitemap xml files.
17 *
18 * @since 5.1.0
19 */
20abstract class Jetpack_Sitemap_Buffer_Fallback extends Jetpack_Sitemap_Buffer {
21
22    /**
23     * The buffer contents.
24     *
25     * @access protected
26     * @since 5.3.0
27     * @var string The buffer contents.
28     */
29    protected $buffer;
30
31    /**
32     * Jetpack_Sitemap_Buffer_Fallback constructor.
33     *
34     * @param int    $item_limit The maximum size of the buffer in items.
35     * @param int    $byte_limit The maximum size of the buffer in bytes.
36     * @param string $time The initial datetime of the buffer. Must be in 'YYYY-MM-DD hh:mm:ss' format.
37     */
38    public function __construct( $item_limit, $byte_limit, $time = '1970-01-01 00:00:00' ) {
39        $this->is_full_flag  = false;
40        $this->is_empty_flag = true;
41        $this->timestamp     = $time;
42
43        $this->finder = new Jetpack_Sitemap_Finder();
44
45        $this->item_capacity = max( 1, (int) $item_limit );
46        $this->byte_capacity = max( 1, (int) $byte_limit ) - strlen( $this->contents() );
47    }
48
49    /**
50     * Append an item to the buffer, if there is room for it,
51     * and set is_empty_flag to false. If there is no room,
52     * we set is_full_flag to true. If $item is null,
53     * don't do anything and report success.
54     *
55     * @since 5.3.0
56     *
57     * @param array $array The item to be added.
58     *
59     * @return bool True if the append succeeded, False if not.
60     */
61    public function append( $array ) {
62        if ( $array === null ) {
63            return true;
64        }
65
66        if ( $this->is_full_flag ) {
67            return false;
68        }
69
70        if ( 0 >= $this->item_capacity || 0 >= $this->byte_capacity ) {
71            $this->is_full_flag = true;
72            return false;
73        } else {
74            $this->item_capacity -= 1;
75            $added_string         = $this->array_to_xml_string( $array );
76            $this->buffer        .= $added_string;
77            $this->is_empty_flag  = false;
78
79            mbstring_binary_safe_encoding(); // So we can safely use strlen().
80            $this->byte_capacity -= strlen( $added_string );
81            reset_mbstring_encoding();
82
83            return true;
84        }
85    }
86
87    /**
88     * Detect whether the buffer is empty.
89     *
90     * @since 5.3.0
91     *
92     * @return bool True if the buffer is empty, false otherwise.
93     */
94    public function is_empty() {
95        return $this->is_empty_flag;
96    }
97
98    /**
99     * Retrieve the contents of the buffer.
100     *
101     * @since 5.3.0
102     *
103     * @return string The contents of the buffer (with the footer included).
104     */
105    public function contents() {
106        $root = $this->get_root_element();
107
108        return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . $root[0] . $this->buffer . $root[1] . PHP_EOL;
109    }
110
111    /**
112     * Legacy implementation of array to XML conversion without using DOMDocument.
113     *
114     * @param array       $array Item to append to buffer.
115     * @param DOMElement  $parent (optional) an element to which new children should be added.
116     * @param DOMDocument $root (optional) the parent document.
117     * @return String $result
118     */
119    public function array_to_xml_string( $array, $parent = null, $root = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
120        $string = '';
121
122        foreach ( $array as $key => $value ) {
123            // Only allow a-z, A-Z, colon, underscore, and hyphen.
124            $tag = preg_replace( '/[^a-zA-Z:_-]/', '_', $key );
125
126            if ( is_array( $value ) ) {
127                $string .= "<$tag>";
128                $string .= $this->array_to_xml_string( $value );
129                $string .= "</$tag>";
130            } elseif ( $value === null ) {
131                $string .= "<$tag />";
132            } else {
133                $string .= "<$tag>" . htmlspecialchars( $value, ENT_COMPAT ) . "</$tag>";
134            }
135        }
136
137        return $string;
138    }
139
140    /**
141     * Render an associative array of XML attribute key/value pairs.
142     *
143     * @access public
144     * @since 5.3.0
145     *
146     * @param array $array Key/value array of attributes.
147     *
148     * @return string The rendered attribute string.
149     */
150    public static function array_to_xml_attr_string( $array ) {
151        $string = '';
152
153        foreach ( $array as $key => $value ) {
154            $key     = preg_replace( '/[^a-zA-Z:_-]/', '_', $key );
155            $string .= ' ' . $key . '="' . esc_attr( $value ) . '"';
156        }
157
158        return $string;
159    }
160}