class-wp-feed-cache-transient.php 2.5 KB

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