Skip to content

Commit

Permalink
Add cache methods and filters
Browse files Browse the repository at this point in the history
  • Loading branch information
danieliser committed Jun 17, 2024
1 parent 30898fe commit ce68cb7
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions classes/Services/Restrictions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class Restrictions {
*/
public $restrictions;

/**
* Simple internal request based cache.
*
* @var array<string,mixed>
*/
private $restrictions_cache;

/**
* Get a list of all restrictions.
Expand Down Expand Up @@ -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 );

Check failure on line 219 in classes/Services/Restrictions.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

@param tag must not be named $this. Choose a descriptive alias, for example $instance.

Check failure on line 219 in classes/Services/Restrictions.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

PHPDoc tag @param has invalid value (Restrictions $this Restrictions instance.): Unexpected token "$this", expected variable at offset 146

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<string,mixed> $cache Cache.
* @param string $cache_key Cache key.
* @param Restrictions $this Restrictions instance.
*
* @return array<string,mixed>
*
* @since 2.4.0
*/
do_action( 'content_control/set_restrictions_in_cache', $cache_key, $value, $this );

Check failure on line 250 in classes/Services/Restrictions.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

@param tag must not be named $this. Choose a descriptive alias, for example $instance.
}


/**
* Get all applicable restrictions for the current post.
*
Expand Down

0 comments on commit ce68cb7

Please sign in to comment.