PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
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 18.5, at inc/sitemaps/class-sitemap-cache-data.php

199 lines 4.7 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 public function set_sitemap( $sitemap ) {
33
34 if ( ! is_string( $sitemap ) ) {
35 $sitemap = '';
36 }
37
38 $this->sitemap = $sitemap;
39
40 /*
41 * Empty sitemap is not usable.
42 */
43 if ( ! empty( $sitemap ) ) {
44 $this->set_status( self::OK );
45 }
46 else {
47 $this->set_status( self::ERROR );
48 }
49 }
50
51 /**
52 * Set the status of the sitemap, is it usable.
53 *
54 * @param bool|string $usable Is the sitemap usable or not.
55 *
56 * @return void
57 */
58 public function set_status( $usable ) {
59
60 if ( $usable === self::OK ) {
61 $this->status = self::OK;
62
63 return;
64 }
65
66 if ( $usable === self::ERROR ) {
67 $this->status = self::ERROR;
68 $this->sitemap = '';
69
70 return;
71 }
72
73 $this->status = self::UNKNOWN;
74 }
75
76 /**
77 * Is the sitemap usable.
78 *
79 * @return bool True if usable, False if bad or unknown.
80 */
81 public function is_usable() {
82
83 return $this->status === self::OK;
84 }
85
86 /**
87 * Get the XML content of the sitemap.
88 *
89 * @return string The content of the sitemap.
90 */
91 public function get_sitemap() {
92
93 return $this->sitemap;
94 }
95
96 /**
97 * Get the status of the sitemap.
98 *
99 * @return string Status of the sitemap, 'ok'/'error'/'unknown'.
100 */
101 public function get_status() {
102
103 return $this->status;
104 }
105
106 /**
107 * String representation of object.
108 *
109 * {@internal This magic method is only "magic" as of PHP 7.4 in which the magic method was introduced.}
110 *
111 * @link https://www.php.net/language.oop5.magic#object.serialize
112 * @link https://wiki.php.net/rfc/custom_object_serialization
113 *
114 * @since 17.8.0
115 *
116 * @return array The data to be serialized.
117 */
118 public function __serialize() { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__serializeFound
119
120 $data = [
121 'status' => $this->status,
122 'xml' => $this->sitemap,
123 ];
124
125 return $data;
126 }
127
128 /**
129 * Constructs the object.
130 *
131 * {@internal This magic method is only "magic" as of PHP 7.4 in which the magic method was introduced.}
132 *
133 * @link https://www.php.net/language.oop5.magic#object.serialize
134 * @link https://wiki.php.net/rfc/custom_object_serialization
135 *
136 * @since 17.8.0
137 *
138 * @param array $data The unserialized data to use to (re)construct the object.
139 *
140 * @return void
141 */
142 public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
143
144 $this->set_sitemap( $data['xml'] );
145 $this->set_status( $data['status'] );
146 }
147
148 /**
149 * String representation of object.
150 *
151 * {@internal The magic methods take precedence over the Serializable interface.
152 * This means that in practice, this method will now only be called on PHP < 7.4.
153 * For PHP 7.4 and higher, the magic methods will be used instead.}
154 *
155 * {@internal The Serializable interface is being phased out, in favour of the magic methods.
156 * This method should be deprecated and removed and the class should no longer
157 * implement the `Serializable` interface.
158 * This change, however, can't be made until the minimum PHP version goes up to PHP 7.4 or higher.}
159 *
160 * @link http://php.net/manual/en/serializable.serialize.php
161 * @link https://wiki.php.net/rfc/phase_out_serializable
162 *
163 * @since 5.1.0
164 *
165 * @return string The string representation of the object or null in C-format.
166 */
167 public function serialize() {
168
169 return serialize( $this->__serialize() );
170 }
171
172 /**
173 * Constructs the object.
174 *
175 * {@internal The magic methods take precedence over the Serializable interface.
176 * This means that in practice, this method will now only be called on PHP < 7.4.
177 * For PHP 7.4 and higher, the magic methods will be used instead.}
178 *
179 * {@internal The Serializable interface is being phased out, in favour of the magic methods.
180 * This method should be deprecated and removed and the class should no longer
181 * implement the `Serializable` interface.
182 * This change, however, can't be made until the minimum PHP version goes up to PHP 7.4 or higher.}
183 *
184 * @link http://php.net/manual/en/serializable.unserialize.php
185 * @link https://wiki.php.net/rfc/phase_out_serializable
186 *
187 * @since 5.1.0
188 *
189 * @param string $data The string representation of the object in C or O-format.
190 *
191 * @return void
192 */
193 public function unserialize( $data ) {
194
195 $data = unserialize( $data );
196 $this->__unserialize( $data );
197 }
198 }
199