| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This class handles the data for the option where the Ryte data is stored. |
| 10 |
*/ |
| 11 |
class WPSEO_Ryte_Option { |
| 12 |
|
| 13 |
/** |
| 14 |
* Indicates the data is not fetched. |
| 15 |
* |
| 16 |
* @var int |
| 17 |
*/ |
| 18 |
const NOT_FETCHED = 99; |
| 19 |
|
| 20 |
/** |
| 21 |
* Indicates the option is indexable. |
| 22 |
* |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
const IS_INDEXABLE = 1; |
| 26 |
|
| 27 |
/** |
| 28 |
* Indicates the option is not indexable. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
const IS_NOT_INDEXABLE = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* Indicates the data could not be fetched. |
| 36 |
* |
| 37 |
* @var int |
| 38 |
*/ |
| 39 |
const CANNOT_FETCH = -1; |
| 40 |
|
| 41 |
/** |
| 42 |
* The name of the option where data will be stored. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
const OPTION_NAME = 'wpseo_ryte'; |
| 47 |
|
| 48 |
/** |
| 49 |
* The key of the status in the option. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
const STATUS = 'status'; |
| 54 |
|
| 55 |
/** |
| 56 |
* The key of the last fetch date in the option. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
const LAST_FETCH = 'last_fetch'; |
| 61 |
|
| 62 |
/** |
| 63 |
* The limit for fetching the status manually. |
| 64 |
* |
| 65 |
* @var int |
| 66 |
*/ |
| 67 |
const FETCH_LIMIT = 15; |
| 68 |
|
| 69 |
/** |
| 70 |
* The Ryte option stored in the database. |
| 71 |
* |
| 72 |
* @var array |
| 73 |
*/ |
| 74 |
private $ryte_option; |
| 75 |
|
| 76 |
/** |
| 77 |
* Setting the object by setting the properties. |
| 78 |
*/ |
| 79 |
public function __construct() { |
| 80 |
$this->ryte_option = $this->get_option(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Getting the status from the option. |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function get_status() { |
| 89 |
if ( array_key_exists( self::STATUS, $this->ryte_option ) ) { |
| 90 |
return $this->ryte_option[ self::STATUS ]; |
| 91 |
} |
| 92 |
|
| 93 |
return self::CANNOT_FETCH; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Saving the status to the options. |
| 98 |
* |
| 99 |
* @param string $status The status to save. |
| 100 |
*/ |
| 101 |
public function set_status( $status ) { |
| 102 |
$this->ryte_option[ self::STATUS ] = $status; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Saving the last fetch timestamp to the options. |
| 107 |
* |
| 108 |
* @param int $timestamp Timestamp with the new value. |
| 109 |
*/ |
| 110 |
public function set_last_fetch( $timestamp ) { |
| 111 |
$this->ryte_option[ self::LAST_FETCH ] = $timestamp; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Determines whether the indexability status should be fetched. |
| 116 |
* |
| 117 |
* If LAST_FETCH isn't set, we assume the indexability status hasn't been fetched |
| 118 |
* yet and return true. Then, we check whether the last fetch is within the |
| 119 |
* FETCH_LIMIT time interval (15 seconds) to avoid too many consecutive API calls. |
| 120 |
* |
| 121 |
* @return bool Whether the indexability status should be fetched. |
| 122 |
*/ |
| 123 |
public function should_be_fetched() { |
| 124 |
if ( ! isset( $this->ryte_option[ self::LAST_FETCH ] ) ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
return ( ( time() - $this->ryte_option[ self::LAST_FETCH ] ) > self::FETCH_LIMIT ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Saving the option with the current data. |
| 133 |
*/ |
| 134 |
public function save_option() { |
| 135 |
update_option( self::OPTION_NAME, $this->ryte_option ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns the value of the onpage_enabled status. |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
*/ |
| 143 |
public function is_enabled() { |
| 144 |
return WPSEO_Options::get( 'ryte_indexability' ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Getting the option with the Ryte data. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
private function get_option() { |
| 153 |
$default = [ |
| 154 |
self::STATUS => self::NOT_FETCHED, |
| 155 |
self::LAST_FETCH => 0, |
| 156 |
]; |
| 157 |
|
| 158 |
return get_option( self::OPTION_NAME, $default ); |
| 159 |
} |
| 160 |
} |
| 161 |
|