| 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 The magic methods take precedence over the Serializable interface. |
| 178 |
* This means that in practice, this method will now only be called on PHP < 7.4. |
| 179 |
* For PHP 7.4 and higher, the magic methods will be used instead.} |
| 180 |
* |
| 181 |
* {@internal The Serializable interface is being phased out, in favour of the magic methods. |
| 182 |
* This method should be deprecated and removed and the class should no longer |
| 183 |
* implement the `Serializable` interface. |
| 184 |
* This change, however, can't be made until the minimum PHP version goes up to PHP 7.4 or higher.} |
| 185 |
* |
| 186 |
* @link http://php.net/manual/en/serializable.unserialize.php |
| 187 |
* @link https://wiki.php.net/rfc/phase_out_serializable |
| 188 |
* |
| 189 |
* @since 5.1.0 |
| 190 |
* |
| 191 |
* @param string $data The string representation of the object in C or O-format. |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function unserialize( $data ) { |
| 196 |
|
| 197 |
$data = unserialize( $data ); |
| 198 |
$this->__unserialize( $data ); |
| 199 |
} |
| 200 |
} |
| 201 |
|