/wp-content/plugins/wordpress-seo/inc/sitemaps/class-sitemap-cache-data.php

https://bitbucket.org/carloskikea/helpet · PHP · 131 lines · 50 code · 24 blank · 57 comment · 5 complexity · 8bf66463510ec68d0c1e38dbb289c9ad MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\XML_Sitemaps
  6. */
  7. /**
  8. * Sitemap Cache Data object, manages sitemap data stored in cache
  9. */
  10. class WPSEO_Sitemap_Cache_Data implements WPSEO_Sitemap_Cache_Data_Interface, Serializable {
  11. /** @var string Sitemap XML data. */
  12. private $sitemap = '';
  13. /** @var string Status of the sitemap, usable or not. */
  14. private $status = self::UNKNOWN;
  15. /**
  16. * Set the sitemap XML data
  17. *
  18. * @param string $sitemap XML Content of the sitemap.
  19. */
  20. public function set_sitemap( $sitemap ) {
  21. if ( ! is_string( $sitemap ) ) {
  22. $sitemap = '';
  23. }
  24. $this->sitemap = $sitemap;
  25. /**
  26. * Empty sitemap is not usable.
  27. */
  28. if ( ! empty( $sitemap ) ) {
  29. $this->set_status( self::OK );
  30. }
  31. else {
  32. $this->set_status( self::ERROR );
  33. }
  34. }
  35. /**
  36. * Set the status of the sitemap, is it usable.
  37. *
  38. * @param bool|string $valid Is the sitemap valid or not.
  39. *
  40. * @return void
  41. */
  42. public function set_status( $valid ) {
  43. if ( self::OK === $valid ) {
  44. $this->status = self::OK;
  45. return;
  46. }
  47. if ( self::ERROR === $valid ) {
  48. $this->status = self::ERROR;
  49. $this->sitemap = '';
  50. return;
  51. }
  52. $this->status = self::UNKNOWN;
  53. }
  54. /**
  55. * Is the sitemap usable.
  56. *
  57. * @return bool True if usable, False if bad or unknown.
  58. */
  59. public function is_usable() {
  60. return self::OK === $this->status;
  61. }
  62. /**
  63. * Get the XML content of the sitemap.
  64. *
  65. * @return string The content of the sitemap.
  66. */
  67. public function get_sitemap() {
  68. return $this->sitemap;
  69. }
  70. /**
  71. * Get the status of the sitemap.
  72. *
  73. * @return string Status of the sitemap, 'ok'/'error'/'unknown'
  74. */
  75. public function get_status() {
  76. return $this->status;
  77. }
  78. /**
  79. * String representation of object
  80. *
  81. * @link http://php.net/manual/en/serializable.serialize.php
  82. * @return string the string representation of the object or null
  83. * @since 5.1.0
  84. */
  85. public function serialize() {
  86. $data = array(
  87. 'status' => $this->status,
  88. 'xml' => $this->sitemap,
  89. );
  90. return serialize( $data );
  91. }
  92. /**
  93. * Constructs the object
  94. *
  95. * @link http://php.net/manual/en/serializable.unserialize.php
  96. *
  97. * @param string $serialized The string representation of the object.
  98. *
  99. * @return void
  100. * @since 5.1.0
  101. */
  102. public function unserialize( $serialized ) {
  103. $data = unserialize( $serialized );
  104. $this->set_sitemap( $data['xml'] );
  105. $this->set_status( $data['status'] );
  106. }
  107. }