diff --git a/classes/Services/Restrictions.php b/classes/Services/Restrictions.php index 5a3a5289..bce1868a 100644 --- a/classes/Services/Restrictions.php +++ b/classes/Services/Restrictions.php @@ -29,6 +29,12 @@ class Restrictions { */ public $restrictions; + /** + * Simple internal request based cache. + * + * @var array + */ + private $restrictions_cache; /** * Get a list of all restrictions. @@ -191,6 +197,60 @@ public function get_cache_key( $post_id = null ) { return apply_filters( 'content_control/get_cache_key', $cache_key, $post_id, $user_id, $context ); } + /** + * Get from cache. + * + * @param string $cache_key Cache key. + * + * @return mixed|null + */ + public function get_from_cache( $cache_key ) { + /** + * Allow preloading from cache. + * + * @param mixed|null $cache Cache. + * @param string $cache_key Cache key. + * @param Restrictions $this Restrictions instance. + * + * @return mixed|null + * + * @since 2.4.0 + */ + $cache = apply_filters( 'content_control/get_restrictions_from_cache', null, $cache_key, $this ); + + if ( $cache ) { + return $cache; + } + + return $this->restrictions_cache[ $cache_key ] ?? null; + } + + /** + * Set in cache. + * + * @param string $cache_key Cache key. + * @param mixed $value Value to set. + * + * @return void + */ + public function set_in_cache( $cache_key, $value ) { + $this->restrictions_cache[ $cache_key ] = $value; + + /** + * Filter the cache. + * + * @param array $cache Cache. + * @param string $cache_key Cache key. + * @param Restrictions $this Restrictions instance. + * + * @return array + * + * @since 2.4.0 + */ + do_action( 'content_control/set_restrictions_in_cache', $cache_key, $value, $this ); + } + + /** * Get all applicable restrictions for the current post. *