class-onpage-option.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const NOT_FETCHED = 99;
  12. const IS_INDEXABLE = 1;
  13. const IS_NOT_INDEXABLE = 0;
  14. const CANNOT_FETCH = -1;
  15. /**
  16. * The name of the option where data will be stored
  17. */
  18. const OPTION_NAME = 'wpseo_onpage';
  19. /**
  20. * The key of the status in the option
  21. */
  22. const STATUS = 'status';
  23. /**
  24. * The key of the last fetch date in the option.
  25. */
  26. const LAST_FETCH = 'last_fetch';
  27. /**
  28. * The limit for fetching the status manually.
  29. */
  30. const FETCH_LIMIT = 15;
  31. /**
  32. * @var array The Ryte option stored in the database.
  33. */
  34. private $onpage_option;
  35. /**
  36. * Setting the object by setting the properties
  37. */
  38. public function __construct() {
  39. $this->onpage_option = $this->get_option();
  40. }
  41. /**
  42. * Getting the status from the option.
  43. *
  44. * @return string
  45. */
  46. public function get_status() {
  47. if ( array_key_exists( self::STATUS, $this->onpage_option ) ) {
  48. return $this->onpage_option[ self::STATUS ];
  49. }
  50. return self::CANNOT_FETCH;
  51. }
  52. /**
  53. * Saving the status to the options.
  54. *
  55. * @param string $status The status to save.
  56. */
  57. public function set_status( $status ) {
  58. $this->onpage_option[ self::STATUS ] = $status;
  59. }
  60. /**
  61. * Saving the last fetch timestamp to the options.
  62. *
  63. * @param integer $timestamp Timestamp with the new value.
  64. */
  65. public function set_last_fetch( $timestamp ) {
  66. $this->onpage_option[ self::LAST_FETCH ] = $timestamp;
  67. }
  68. /**
  69. * Check if the last fetch is within the time of 60 minutes
  70. *
  71. * @return bool
  72. */
  73. public function should_be_fetched() {
  74. return ( ( time() - $this->onpage_option[ self::LAST_FETCH ] ) > self::FETCH_LIMIT );
  75. }
  76. /**
  77. * Saving the option with the current data
  78. */
  79. public function save_option() {
  80. update_option( self::OPTION_NAME, $this->onpage_option );
  81. }
  82. /**
  83. * Returns the value of the onpage_enabled status
  84. *
  85. * @return bool
  86. */
  87. public function is_enabled() {
  88. return WPSEO_Options::get( 'onpage_indexability' );
  89. }
  90. /**
  91. * Getting the option with the Ryte data.
  92. *
  93. * @return array
  94. */
  95. private function get_option() {
  96. $default = array(
  97. self::STATUS => self::NOT_FETCHED,
  98. self::LAST_FETCH => 0,
  99. );
  100. return get_option( self::OPTION_NAME, $default );
  101. }
  102. }