PageRenderTime 25ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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