class-onpage-option.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class handles the data for the option where the Ryte data is stored.
  9. */
  10. class WPSEO_OnPage_Option {
  11. /**
  12. * Indicates the data is not fetched.
  13. *
  14. * @var int
  15. */
  16. const NOT_FETCHED = 99;
  17. /**
  18. * Indicates the option is indexable.
  19. *
  20. * @var int
  21. */
  22. const IS_INDEXABLE = 1;
  23. /**
  24. * Indicates the option is not indexable.
  25. *
  26. * @var int
  27. */
  28. const IS_NOT_INDEXABLE = 0;
  29. /**
  30. * Indicates the data could not be fetched.
  31. *
  32. * @var int
  33. */
  34. const CANNOT_FETCH = -1;
  35. /**
  36. * The name of the option where data will be stored.
  37. *
  38. * @var string
  39. */
  40. const OPTION_NAME = 'wpseo_onpage';
  41. /**
  42. * The key of the status in the option.
  43. *
  44. * @var string
  45. */
  46. const STATUS = 'status';
  47. /**
  48. * The key of the last fetch date in the option.
  49. *
  50. * @var string
  51. */
  52. const LAST_FETCH = 'last_fetch';
  53. /**
  54. * The limit for fetching the status manually.
  55. *
  56. * @var int
  57. */
  58. const FETCH_LIMIT = 15;
  59. /**
  60. * The Ryte option stored in the database.
  61. *
  62. * @var array
  63. */
  64. private $onpage_option;
  65. /**
  66. * Setting the object by setting the properties.
  67. */
  68. public function __construct() {
  69. $this->onpage_option = $this->get_option();
  70. }
  71. /**
  72. * Getting the status from the option.
  73. *
  74. * @return string
  75. */
  76. public function get_status() {
  77. if ( array_key_exists( self::STATUS, $this->onpage_option ) ) {
  78. return $this->onpage_option[ self::STATUS ];
  79. }
  80. return self::CANNOT_FETCH;
  81. }
  82. /**
  83. * Saving the status to the options.
  84. *
  85. * @param string $status The status to save.
  86. */
  87. public function set_status( $status ) {
  88. $this->onpage_option[ self::STATUS ] = $status;
  89. }
  90. /**
  91. * Saving the last fetch timestamp to the options.
  92. *
  93. * @param integer $timestamp Timestamp with the new value.
  94. */
  95. public function set_last_fetch( $timestamp ) {
  96. $this->onpage_option[ self::LAST_FETCH ] = $timestamp;
  97. }
  98. /**
  99. * Check if the last fetch is within the time of 60 minutes.
  100. *
  101. * @return bool
  102. */
  103. public function should_be_fetched() {
  104. return ( ( time() - $this->onpage_option[ self::LAST_FETCH ] ) > self::FETCH_LIMIT );
  105. }
  106. /**
  107. * Saving the option with the current data.
  108. */
  109. public function save_option() {
  110. update_option( self::OPTION_NAME, $this->onpage_option );
  111. }
  112. /**
  113. * Returns the value of the onpage_enabled status.
  114. *
  115. * @return bool
  116. */
  117. public function is_enabled() {
  118. return WPSEO_Options::get( 'onpage_indexability' );
  119. }
  120. /**
  121. * Getting the option with the Ryte data.
  122. *
  123. * @return array
  124. */
  125. private function get_option() {
  126. $default = [
  127. self::STATUS => self::NOT_FETCHED,
  128. self::LAST_FETCH => 0,
  129. ];
  130. return get_option( self::OPTION_NAME, $default );
  131. }
  132. }