class-sitemap-cache-data.php 2.4 KB

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