Fragment_Cache::callback()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Rarst\Fragment_Cache;
4
5
/**
6
 * Abstract base class for implementation of fragment type handler.
7
 */
8
abstract class Fragment_Cache {
9
10
	/** @var bool $in_callback Static flag to catch and prevent nested caching calls.  */
11
	static $in_callback = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $in_callback.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13
	/** @var int $timeout Cache timeout duration.  */
14
	public $timeout;
15
16
	/** @var string $type Handler type name. */
17
	protected $type;
18
19
	/**
20
	 * Create object and set parameters from passed.
21
	 *
22
	 * @param array $args Configuration arguments.
23
	 */
24
	public function __construct( $args ) {
25
26
		$this->type    = $args['type'];
27
		$this->timeout = $args['timeout'];
28
	}
29
30
	/**
31
	 * Enable fragment handler.
32
	 */
33
	abstract public function enable();
34
35
	/**
36
	 * Disable fragment handler.
37
	 */
38
	abstract public function disable();
39
40
	/**
41
	 * Wrapper to retrieve data through TLC Transient.
42
	 *
43
	 * @param string $name Name of fragment.
44
	 * @param array  $args Arguments.
45
	 * @param mixed  $salt Optional salt data.
46
	 *
47
	 * @return mixed
48
	 */
49
	public function fetch( $name, $args, $salt = '' ) {
50
51
		global $current_user;
52
53
		static $empty_user;
54
55
		if ( self::$in_callback || apply_filters( 'fc_skip_cache', false, $this->type, $name, $args, $salt ) ) {
56
			return $this->callback( $name, $args );
57
		}
58
59
		// Anonymize front-end run for consistency.
60
		if ( is_user_logged_in() ) {
61
62
			if ( empty( $empty_user ) ) {
63
				$empty_user = new \WP_User( 0 );
64
			}
65
66
			$stored_user  = $current_user;
67
			$current_user = $empty_user;
68
		}
69
70
		$salt   = maybe_serialize( $salt );
71
		$output = tlc_transient( 'fragment-cache-' . $this->type . '-' . $name . $salt )
72
				->updates_with( array( $this, 'wrap_callback' ), array( $name, $args ) )
73
				->expires_in( $this->timeout )
74
				->get();
75
76
		if ( ! empty( $stored_user ) ) {
77
			$current_user = $stored_user;
78
		}
79
80
		return $output;
81
	}
82
83
	/**
84
	 * Wraps callback to correctly set execution flag.
85
	 *
86
	 * @param string $name Fragment name.
87
	 * @param array  $args Arguments.
88
	 *
89
	 * @return string
90
	 */
91
	public function wrap_callback( $name, $args ) {
92
93
		self::$in_callback = true;
94
		$output = $this->callback( $name, $args );
95
		self::$in_callback = false;
96
97
		return $output;
98
	}
99
100
	/**
101
	 * Used to generate data to be cached.
102
	 *
103
	 * @param string $name Fragment name.
104
	 * @param array  $args Arguments.
105
	 *
106
	 * @return string
107
	 */
108
	abstract protected function callback( $name, $args );
109
110
	/**
111
	 * Get human-readable HTML comment with timestamp to append to cached fragment.
112
	 *
113
	 * @param string $name Fragment name.
114
	 *
115
	 * @return string
116
	 */
117
	public function get_comment( $name ) {
118
119
		return '<!-- ' . esc_html( $name ) . ' ' . esc_html( $this->type ) . ' cached on ' . date_i18n( DATE_RSS ) . ' -->';
120
	}
121
}
122