Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Post_Transient.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * A Class to handle Transients for posts, useful for caching complex structures |
| 5 | * It uses the same logic as WordPress Transient, but instead of options it will |
| 6 | * use the Post Meta as the table |
| 7 | * |
| 8 | * @since 4.1 |
| 9 | */ |
| 10 | class Tribe__Post_Transient { |
| 11 | |
| 12 | /** |
| 13 | * Get (and instantiate, if necessary) the instance of the class |
| 14 | * |
| 15 | * @since 4.1 |
| 16 | * @static |
| 17 | * @return self |
| 18 | * |
| 19 | */ |
| 20 | public static function instance() { |
| 21 | return tribe( 'post-transient' ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Fetches the Transient Data |
| 26 | * |
| 27 | * @since 4.1 |
| 28 | * |
| 29 | * @param int $post_id The Post ID, can also be a WP_Post |
| 30 | * @param string $transient Post Meta to Fetch |
| 31 | * |
| 32 | * @return mixed Value stored on the Post Transient. |
| 33 | */ |
| 34 | public function get( $post_id, $transient ) { |
| 35 | global $_wp_using_ext_object_cache; |
| 36 | |
| 37 | if ( is_numeric( $post_id ) ) { |
| 38 | $post_id = (int) $post_id; |
| 39 | } else { |
| 40 | $post = get_post( $post_id ); |
| 41 | $post_id = $post->ID; |
| 42 | } |
| 43 | |
| 44 | if ( has_filter( 'tribe_pre_post_meta_transient_' . $transient ) ) { |
| 45 | /** |
| 46 | * Attach an action before getting the new Transient |
| 47 | * |
| 48 | * @since 4.1 |
| 49 | * |
| 50 | * @param int $post_id Post ID |
| 51 | * @param string $transient The Post Meta Key |
| 52 | */ |
| 53 | $pre = apply_filters( 'tribe_pre_post_meta_transient_' . $transient, $post_id, $transient ); |
| 54 | if ( false !== $pre ) { |
| 55 | return $pre; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if ( $_wp_using_ext_object_cache ) { |
| 60 | $value = wp_cache_get( "tribe_{$transient}-{$post_id}", "tribe_post_meta_transient-{$post_id}" ); |
| 61 | } else { |
| 62 | $meta_timeout = '_transient_timeout_' . $transient; |
| 63 | $meta = '_transient_' . $transient; |
| 64 | $value = get_post_meta( $post_id, $meta, false ); |
| 65 | |
| 66 | // if there aren't any values, communicate that it did not fetch data from post transient |
| 67 | if ( ! is_array( $value ) || 0 === count( $value ) ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // grab the first value, because that's all we care about |
| 72 | $value = current( $value ); |
| 73 | |
| 74 | if ( $value && ! defined( 'WP_INSTALLING' ) ) { |
| 75 | if ( get_post_meta( $post_id, $meta_timeout, true ) < time() ) { |
| 76 | $this->delete( $post_id, $transient ); |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Attach an action after getting the new Transient |
| 85 | * |
| 86 | * @since 4.1 |
| 87 | * |
| 88 | * @param int $post_id Post ID |
| 89 | * @param string $transient The Post Meta Key |
| 90 | */ |
| 91 | return has_filter( 'tribe_post_meta_transient_' . $transient ) |
| 92 | ? apply_filters( 'tribe_post_meta_transient_' . $transient, $value, $post_id ) |
| 93 | : $value; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Delete a post meta transient. |
| 98 | * |
| 99 | * @since 4.1 |
| 100 | * |
| 101 | * @param int $post_id The Post ID, can also be a WP_Post. |
| 102 | * @param string $transient Post Meta to Delete. |
| 103 | * @param string $value Only delete if the value Matches. |
| 104 | * |
| 105 | * @return boolean If we were able to delete the transient. |
| 106 | */ |
| 107 | public function delete( $post_id, $transient, $value = null ) { |
| 108 | global $_wp_using_ext_object_cache; |
| 109 | |
| 110 | if ( is_numeric( $post_id ) ) { |
| 111 | $post_id = (int) $post_id; |
| 112 | } else { |
| 113 | $post = get_post( $post_id ); |
| 114 | $post_id = $post->ID; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Use this to pre attach an action to deleting a Post Transient |
| 119 | * |
| 120 | * @since 4.1 |
| 121 | * |
| 122 | * @param int $post_id Post ID |
| 123 | * @param string $transient The Post Meta Key |
| 124 | */ |
| 125 | do_action( 'tribe_delete_post_meta_transient_' . $transient, $post_id, $transient ); |
| 126 | |
| 127 | if ( $_wp_using_ext_object_cache ) { |
| 128 | $result = wp_cache_delete( "tribe_{$transient}-{$post_id}", "tribe_post_meta_transient-{$post_id}" ); |
| 129 | } else { |
| 130 | $meta_timeout = '_transient_timeout_' . $transient; |
| 131 | $meta = '_transient_' . $transient; |
| 132 | $result = delete_post_meta( $post_id, $meta, $value ); |
| 133 | if ( $result ) { |
| 134 | delete_post_meta( $post_id, $meta_timeout, $value ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if ( $result ) { |
| 139 | /** |
| 140 | * Use this to attach an Action to when the Transient is deleted |
| 141 | * |
| 142 | * @since 4.1 |
| 143 | * |
| 144 | * @param int $post_id Post ID |
| 145 | * @param string $transient The Post Meta Key |
| 146 | */ |
| 147 | do_action( 'tribe_deleted_post_meta_transient', $transient, $post_id, $transient ); |
| 148 | } |
| 149 | |
| 150 | return $result; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets a new value for the Transient. |
| 155 | * |
| 156 | * @since 4.1 |
| 157 | * |
| 158 | * @param int $post_id The Post ID, can also be a WP_Post. |
| 159 | * @param string $transient Post Meta to set. |
| 160 | * @param string $value Only delete if the value Matches. |
| 161 | * @param int $expiration How long this transient will be valid, in seconds. |
| 162 | * |
| 163 | * @return int|false Meta ID on success, false on failure. |
| 164 | */ |
| 165 | public function set( $post_id, $transient, $value, $expiration = 0 ) { |
| 166 | global $_wp_using_ext_object_cache; |
| 167 | |
| 168 | if ( is_numeric( $post_id ) ) { |
| 169 | $post_id = (int) $post_id; |
| 170 | } else { |
| 171 | $post = get_post( $post_id ); |
| 172 | $post_id = $post->ID; |
| 173 | } |
| 174 | |
| 175 | $this->delete( $post_id, $transient ); |
| 176 | |
| 177 | /** |
| 178 | * Attach an action before setting the new Transient |
| 179 | * |
| 180 | * @since 4.1 |
| 181 | * |
| 182 | * @param int $post_id Post ID |
| 183 | * @param string $transient The Post Meta Key |
| 184 | */ |
| 185 | if ( has_filter( 'tribe_pre_set_post_meta_transient_' . $transient ) ) { |
| 186 | $value = apply_filters( 'tribe_pre_set_post_meta_transient_' . $transient, $value, $post_id, $transient ); |
| 187 | } |
| 188 | |
| 189 | if ( $_wp_using_ext_object_cache ) { |
| 190 | $result = wp_cache_set( "tribe_{$transient}-{$post_id}", $value, "tribe_post_meta_transient-{$post_id}", $expiration ); |
| 191 | } else { |
| 192 | $meta_timeout = '_transient_timeout_' . $transient; |
| 193 | $meta = '_transient_' . $transient; |
| 194 | if ( $expiration ) { |
| 195 | add_post_meta( $post_id, $meta_timeout, time() + $expiration, true ); |
| 196 | } |
| 197 | $result = add_post_meta( $post_id, $meta, $value, true ); |
| 198 | } |
| 199 | |
| 200 | if ( $result ) { |
| 201 | /** |
| 202 | * Attach an action after setting the new Transient |
| 203 | * |
| 204 | * @since 4.1 |
| 205 | * |
| 206 | * @param int $post_id Post ID |
| 207 | * @param string $transient The Post Meta Key |
| 208 | */ |
| 209 | do_action( 'tribe_set_post_meta_transient_' . $transient, $post_id, $transient ); |
| 210 | } |
| 211 | |
| 212 | return $result; |
| 213 | } |
| 214 | } |
| 215 |