PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / sitemaps / class-sitemap-cache-data.php

class-sitemap-cache-data.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at inc/sitemaps/class-sitemap-cache-data.php

217 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\XML_Sitemaps
6 */
7
8 /**
9 * Sitemap Cache Data object, manages sitemap data stored in cache.
10 */
11 class WPSEO_Sitemap_Cache_Data implements Serializable, WPSEO_Sitemap_Cache_Data_Interface {
12
13 /**
14 * Sitemap XML data.
15 *
16 * @var string
17 */
18 private $sitemap = '';
19
20 /**
21 * Status of the sitemap, usable or not.
22 *
23 * @var string
24 */
25 private $status = self::UNKNOWN;
26
27 /**
28 * Set the sitemap XML data
29 *
30 * @param string $sitemap XML Content of the sitemap.
31 *
32 * @return void
33 */
34 public function set_sitemap( $sitemap ) {
35
36 if ( ! is_string( $sitemap ) ) {
37 $sitemap = '';
38 }
39
40 $this->sitemap = $sitemap;
41
42 /*
43 * Empty sitemap is not usable.
44 */
45 if ( ! empty( $sitemap ) ) {
46 $this->set_status( self::OK );
47 }
48 else {
49 $this->set_status( self::ERROR );
50 }
51 }
52
53 /**
54 * Set the status of the sitemap, is it usable.
55 *
56 * @param bool|string $usable Is the sitemap usable or not.
57 *
58 * @return void
59 */
60 public function set_status( $usable ) {
61
62 if ( $usable === self::OK ) {
63 $this->status = self::OK;
64
65 return;
66 }
67
68 if ( $usable === self::ERROR ) {
69 $this->status = self::ERROR;
70 $this->sitemap = '';
71
72 return;
73 }
74
75 $this->status = self::UNKNOWN;
76 }
77
78 /**
79 * Is the sitemap usable.
80 *
81 * @return bool True if usable, False if bad or unknown.
82 */
83 public function is_usable() {
84
85 return $this->status === self::OK;
86 }
87
88 /**
89 * Get the XML content of the sitemap.
90 *
91 * @return string The content of the sitemap.
92 */
93 public function get_sitemap() {
94
95 return $this->sitemap;
96 }
97
98 /**
99 * Get the status of the sitemap.
100 *
101 * @return string Status of the sitemap, 'ok'/'error'/'unknown'.
102 */
103 public function get_status() {
104
105 return $this->status;
106 }
107
108 /**
109 * String representation of object.
110 *
111 * {@internal This magic method is only "magic" as of PHP 7.4 in which the magic method was introduced.}
112 *
113 * @link https://www.php.net/language.oop5.magic#object.serialize
114 * @link https://wiki.php.net/rfc/custom_object_serialization
115 *
116 * @since 17.8.0
117 *
118 * @return array The data to be serialized.
119 */
120 public function __serialize() { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__serializeFound
121
122 $data = [
123 'status' => $this->status,
124 'xml' => $this->sitemap,
125 ];
126
127 return $data;
128 }
129
130 /**
131 * Constructs the object.
132 *
133 * {@internal This magic method is only "magic" as of PHP 7.4 in which the magic method was introduced.}
134 *
135 * @link https://www.php.net/language.oop5.magic#object.serialize
136 * @link https://wiki.php.net/rfc/custom_object_serialization
137 *
138 * @since 17.8.0
139 *
140 * @param array $data The unserialized data to use to (re)construct the object.
141 *
142 * @return void
143 */
144 public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
145
146 $this->set_sitemap( $data['xml'] );
147 $this->set_status( $data['status'] );
148 }
149
150 /**
151 * String representation of object.
152 *
153 * {@internal The magic methods take precedence over the Serializable interface.
154 * This means that in practice, this method will now only be called on PHP < 7.4.
155 * For PHP 7.4 and higher, the magic methods will be used instead.}
156 *
157 * {@internal The Serializable interface is being phased out, in favour of the magic methods.
158 * This method should be deprecated and removed and the class should no longer
159 * implement the `Serializable` interface.
160 * This change, however, can't be made until the minimum PHP version goes up to PHP 7.4 or higher.}
161 *
162 * @link http://php.net/manual/en/serializable.serialize.php
163 * @link https://wiki.php.net/rfc/phase_out_serializable
164 *
165 * @since 5.1.0
166 *
167 * @return string The string representation of the object or null in C-format.
168 */
169 public function serialize() {
170
171 return serialize( $this->__serialize() );
172 }
173
174 /**
175 * Constructs the object.
176 *
177 * {@internal Unlike `serialize()` above, this method is NOT superseded by its magic counterpart
178 * on PHP 7.4 and higher, so it is live code on every supported PHP version. PHP selects the
179 * handler based on the *format* of the payload being read, not on the PHP version: `O:`-format
180 * payloads are routed to `__unserialize()`, whereas `C:`-format payloads are dispatched here,
181 * because this class implements `Serializable`. Verified on PHP 7.4 through 8.3.
182 * There is no call to this method anywhere in the codebase; the engine invokes it.}
183 *
184 * {@internal `$data` must be treated as untrusted. `C:`-format payloads survive WordPress
185 * unaltered on the way into the database, because `is_serialized()` has no case for `C` and
186 * `maybe_serialize()` therefore stores such a string verbatim rather than escaping it. Any code
187 * path that unserializes third-party data can consequently reach this method with a crafted
188 * payload.
189 * The nested call is restricted with `allowed_classes => false` for that reason: a legitimate
190 * payload only ever contains the two strings produced by `__serialize()`, so instantiating a
191 * class here is never valid, and permitting it turns this method into an object-injection sink
192 * that ends in an arbitrary `__destruct()`. Do not relax that restriction.}
193 *
194 * {@internal The Serializable interface is being phased out, in favour of the magic methods.
195 * This method should be deprecated and removed and the class should no longer implement the
196 * `Serializable` interface. The minimum supported PHP version has since risen to 7.4, so that is
197 * now possible, but it is deliberately left as a separate change: dropping the interface alters
198 * how sitemap caches originally written on PHP < 7.4 are handled, as PHP then returns an empty
199 * instance for those payloads instead of a populated one.}
200 *
201 * @link http://php.net/manual/en/serializable.unserialize.php
202 * @link https://wiki.php.net/rfc/phase_out_serializable
203 *
204 * @since 5.1.0
205 *
206 * @param string $data Untrusted serialized representation of the data array, as carried in the
207 * body of a `C:`-format payload naming this class.
208 *
209 * @return void
210 */
211 public function unserialize( $data ) {
212
213 $data = unserialize( $data, [ 'allowed_classes' => false ] );
214 $this->__unserialize( $data );
215 }
216 }
217