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

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