You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

142 lines
2.6 KiB

  1. <?php
  2. /**
  3. * Feed API: WP_Feed_Cache_Transient class
  4. *
  5. * @package WordPress
  6. * @subpackage Feed
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to implement feed cache transients.
  11. *
  12. * @since 2.8.0
  13. */
  14. class WP_Feed_Cache_Transient {
  15. /**
  16. * Holds the transient name.
  17. *
  18. * @since 2.8.0
  19. * @access public
  20. * @var string
  21. */
  22. public $name;
  23. /**
  24. * Holds the transient mod name.
  25. *
  26. * @since 2.8.0
  27. * @access public
  28. * @var string
  29. */
  30. public $mod_name;
  31. /**
  32. * Holds the cache duration in seconds.
  33. *
  34. * Defaults to 43200 seconds (12 hours).
  35. *
  36. * @since 2.8.0
  37. * @access public
  38. * @var int
  39. */
  40. public $lifetime = 43200;
  41. /**
  42. * Constructor.
  43. *
  44. * @since 2.8.0
  45. * @since 3.2.0 Updated to use a PHP5 constructor.
  46. * @access public
  47. *
  48. * @param string $location URL location (scheme is used to determine handler).
  49. * @param string $filename Unique identifier for cache object.
  50. * @param string $extension 'spi' or 'spc'.
  51. */
  52. public function __construct($location, $filename, $extension) {
  53. $this->name = 'feed_' . $filename;
  54. $this->mod_name = 'feed_mod_' . $filename;
  55. $lifetime = $this->lifetime;
  56. /**
  57. * Filters the transient lifetime of the feed cache.
  58. *
  59. * @since 2.8.0
  60. *
  61. * @param int $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
  62. * @param string $filename Unique identifier for the cache object.
  63. */
  64. $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename);
  65. }
  66. /**
  67. * Sets the transient.
  68. *
  69. * @since 2.8.0
  70. * @access public
  71. *
  72. * @param SimplePie $data Data to save.
  73. * @return true Always true.
  74. */
  75. public function save($data) {
  76. if ( $data instanceof SimplePie ) {
  77. $data = $data->data;
  78. }
  79. set_transient($this->name, $data, $this->lifetime);
  80. set_transient($this->mod_name, time(), $this->lifetime);
  81. return true;
  82. }
  83. /**
  84. * Gets the transient.
  85. *
  86. * @since 2.8.0
  87. * @access public
  88. *
  89. * @return mixed Transient value.
  90. */
  91. public function load() {
  92. return get_transient($this->name);
  93. }
  94. /**
  95. * Gets mod transient.
  96. *
  97. * @since 2.8.0
  98. * @access public
  99. *
  100. * @return mixed Transient value.
  101. */
  102. public function mtime() {
  103. return get_transient($this->mod_name);
  104. }
  105. /**
  106. * Sets mod transient.
  107. *
  108. * @since 2.8.0
  109. * @access public
  110. *
  111. * @return bool False if value was not set and true if value was set.
  112. */
  113. public function touch() {
  114. return set_transient($this->mod_name, time(), $this->lifetime);
  115. }
  116. /**
  117. * Deletes transients.
  118. *
  119. * @since 2.8.0
  120. * @access public
  121. *
  122. * @return true Always true.
  123. */
  124. public function unlink() {
  125. delete_transient($this->name);
  126. delete_transient($this->mod_name);
  127. return true;
  128. }
  129. }