PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | inc/cache/class-lp-cache.php +228 -216 4.3.9.1 → 4.4.9 View file →
@@ -1,216 +1,228 @@
1 -<?php
2 -
3 -/**
4 - * Class LP_Cache
5 - *
6 - * @author tungnx
7 - * @since 4.0.8
8 - * @version 1.0.2
9 - */
10 -defined( 'ABSPATH' ) || exit();
11 -
12 -class LP_Cache {
13 - /**
14 - * @var string Key group parent
15 - */
16 - protected $key_group_parent = 'learn_press';
17 - /**
18 - * @var string Key group child(external)
19 - */
20 - protected $key_group_child = '';
21 - /**
22 - * @var string Add key group parent with key group child
23 - */
24 - protected $key_group = '';
25 - /**
26 - * @var string Add key group parent with key group child
27 - */
28 - protected $has_thim_cache = false;
29 -
30 - /**
31 - * If set $has_thim_cache = true, will use thim cache
32 - * Set/Update will check key from table thim_cache
33 - * else only WP Cache
34 - */
35 - public function __construct( $has_thim_cache = false ) {
36 - $this->key_group = $this->key_group_parent . '/' . $this->key_group_child;
37 - $this->has_thim_cache = $has_thim_cache;
38 - }
39 -
40 - /**
41 - * Set cache
42 - *
43 - * @param string $key
44 - * @param mixed $data
45 - * @param int $expire
46 - *
47 - * @since 4.0.8
48 - * @version 1.0.2
49 - */
50 - public function set_cache( string $key, $data, int $expire = 0 ) {
51 - try {
52 - // Cache WP
53 - wp_cache_set( $key, $data, $this->key_group, $expire );
54 - // Cache thim_cache
55 - if ( $this->can_handle_with_thim_cache() ) {
56 - $key = "{$this->key_group}/{$key}";
57 - Thim_Cache_DB::instance()->set_value( $key, $data, $expire );
58 - }
59 - } catch ( Throwable $e ) {
60 - error_log( __METHOD__ . ': ' . $e->getMessage() );
61 - }
62 - }
63 -
64 - /**
65 - * Get cache
66 - *
67 - * @param string $key
68 - * @return false|mixed
69 - */
70 - public function get_cache( string $key ) {
71 - // Get WP Cache
72 - $cache = wp_cache_get( $key, $this->key_group );
73 - // Get thim_cache
74 - if ( false === $cache && $this->can_handle_with_thim_cache() ) {
75 - $key = "{$this->key_group}/{$key}";
76 - $cache = Thim_Cache_DB::instance()->get_value( $key );
77 - /*if ( is_string( $cache ) ) {
78 - $cache = wp_unslash( $cache );
79 - }*/
80 - }
81 -
82 - return $cache;
83 - }
84 -
85 - /**
86 - * Set value for first load page on one process
87 - * Apply for query call same many times.
88 - *
89 - * @param string $type
90 - * @param string $key
91 - * @param $val mixed
92 - *
93 - * @author tungnx
94 - * @version 1.0.0
95 - * @sicne 4.1.4.1
96 - * @return false|mixed|string
97 - */
98 - public static function cache_load_first( string $type = 'get', string $key = '', $val = '' ) {
99 - static $first_set_value = array();
100 -
101 - if ( 'get' === $type ) {
102 - if ( ! array_key_exists( $key, $first_set_value ) ) {
103 - return false;
104 - } else {
105 - return $first_set_value[ $key ];
106 - }
107 - } elseif ( 'set' === $type ) {
108 - $first_set_value[ $key ] = $val;
109 -
110 - return $first_set_value[ $key ];
111 - } elseif ( 'clear' === $type ) {
112 - unset( $first_set_value[ $key ] );
113 - }
114 -
115 - return $first_set_value;
116 - }
117 -
118 - /**
119 - * Clear cache by key
120 - *
121 - * @param $key
122 - */
123 - public function clear( $key ) {
124 - try {
125 - if ( empty( $key ) ) {
126 - return;
127 - }
128 -
129 - wp_cache_delete( $key, $this->key_group );
130 - if ( $this->can_handle_with_thim_cache() ) {
131 - $key = "{$this->key_group}/{$key}";
132 - Thim_Cache_DB::instance()->remove_cache( $key );
133 - }
134 - } catch ( Throwable $e ) {
135 - error_log( $e->getMessage() );
136 - }
137 - }
138 -
139 - /**
140 - * Store list keys cache to a group.
141 - *
142 - * @param string $key_group
143 - * @param string $key_cache_new
144 - * @since 4.2.5.4
145 - * @version 1.0.0
146 - */
147 - public function save_cache_keys( string $key_group, string $key_cache_new ) {
148 - try {
149 - $keys = $this->get_cache( $key_group );
150 - if ( false === $keys ) {
151 - $keys_cache = array();
152 - } else {
153 - $keys_cache = LP_Helper::json_decode( $keys, true );
154 - }
155 -
156 - $keys_cache[] = $key_cache_new;
157 - $this->set_cache( $key_group, json_encode( $keys_cache ) );
158 - } catch ( Throwable $e ) {
159 - error_log( __METHOD__ . ': ' . $e->getMessage() );
160 - }
161 - }
162 -
163 - /**
164 - * Clear cache on group store list keys.
165 - *
166 - * @param string $key_group
167 - * @since 4.2.5.4
168 - * @version 1.0.0
169 - */
170 - public function clear_cache_on_group( string $key_group ) {
171 - try {
172 - $keys_cache_of_group_str = $this->get_cache( $key_group );
173 - if ( false === $keys_cache_of_group_str ) {
174 - return;
175 - }
176 - $keys_cache_of_group = LP_Helper::json_decode( $keys_cache_of_group_str, true );
177 - if ( ! empty( $keys_cache_of_group ) ) {
178 - foreach ( $keys_cache_of_group as $key ) {
179 - // Clear cache by key
180 - $this->clear( $key );
181 - }
182 - // Clear cache store list keys
183 - $this->clear( $key_group );
184 - }
185 - } catch ( Throwable $e ) {
186 - error_log( __METHOD__ . ': ' . $e->getMessage() );
187 - }
188 - }
189 -
190 - /**
191 - * Check can handle with thim cache
192 - *
193 - * @since 4.2.5.4
194 - * @version 1.0.0
195 - * @return bool
196 - */
197 - public function can_handle_with_thim_cache(): bool {
198 - return $this->has_thim_cache && LP_Settings::is_created_tb_thim_cache();
199 - }
200 -
201 - /**
202 - * Clear all cache
203 - *
204 - * @since 4.0.8
205 - * @version 1.0.1
206 - * @return void
207 - */
208 - public function clear_all() {
209 - try {
210 - wp_cache_flush();
211 - Thim_Cache_DB::instance()->remove_all_cache();
212 - } catch ( Throwable $e ) {
213 - error_log( __METHOD__ . ': ' . $e->getMessage() );
214 - }
215 - }
216 -}
1 +<?php
2 +
3 +/**
4 + * Class LP_Cache
5 + *
6 + * @author tungnx
7 + * @since 4.0.8
8 + * @version 1.0.2
9 + */
10 +defined( 'ABSPATH' ) || exit();
11 +
12 +class LP_Cache {
13 + /**
14 + * @var string Key group parent
15 + */
16 + protected $key_group_parent = 'learn_press';
17 + /**
18 + * @var string Key group child(external)
19 + */
20 + protected $key_group_child = '';
21 + /**
22 + * @var string Add key group parent with key group child
23 + */
24 + protected $key_group = '';
25 + /**
26 + * @var string Add key group parent with key group child
27 + */
28 + protected $has_thim_cache = false;
29 +
30 + /**
31 + * If set $has_thim_cache = true, will use thim cache
32 + * Set/Update will check key from table thim_cache
33 + * else only WP Cache
34 + */
35 + public function __construct( $has_thim_cache = false ) {
36 + $this->key_group = $this->key_group_parent;
37 +
38 + if ( ! empty( $this->key_group_child ) ) {
39 + $this->key_group .= '/' . $this->key_group_child;
40 + }
41 +
42 + $this->has_thim_cache = $has_thim_cache;
43 + }
44 +
45 + /**
46 + * Set cache
47 + *
48 + * @param string $key
49 + * @param mixed $data
50 + * @param int $expire
51 + *
52 + * @since 4.0.8
53 + * @version 1.0.2
54 + */
55 + public function set_cache( string $key, $data, int $expire = 0 ) {
56 + try {
57 + // Cache WP
58 + wp_cache_set( $key, $data, $this->key_group, $expire );
59 + // Cache thim_cache
60 + if ( $this->can_handle_with_thim_cache() ) {
61 + $key = "{$this->key_group}/{$key}";
62 +
63 + if ( ! is_string( $data ) ) {
64 + $data = wp_json_encode( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
65 + }
66 +
67 + $expiration = $expire > 0 ? time() + $expire : 0;
68 + Thim_Cache_DB::instance()->set_value( $key, $data, $expiration );
69 + }
70 + } catch ( Throwable $e ) {
71 + LP_Debug::error_log( $e );
72 + }
73 + }
74 +
75 + /**
76 + * Get cache
77 + *
78 + * @param string $key
79 + * @return false|mixed
80 + */
81 + public function get_cache( string $key ) {
82 + // Get WP Cache
83 + $cache = wp_cache_get( $key, $this->key_group );
84 + // Get thim_cache
85 + if ( false === $cache && $this->can_handle_with_thim_cache() ) {
86 + $key = "{$this->key_group}/{$key}";
87 + $cache = Thim_Cache_DB::instance()->get_value( $key );
88 + try {
89 + $cache = LP_Helper::json_decode( $cache, true );
90 + } catch ( Throwable $e ) {
91 + }
92 + }
93 +
94 + return $cache;
95 + }
96 +
97 + /**
98 + * Set value for first load page on one process
99 + * Apply for query call same many times.
100 + *
101 + * @param string $type
102 + * @param string $key
103 + * @param $val mixed
104 + *
105 + * @author tungnx
106 + * @version 1.0.0
107 + * @sicne 4.1.4.1
108 + * @return false|mixed|string
109 + */
110 + public static function cache_load_first( string $type = 'get', string $key = '', $val = '' ) {
111 + static $first_set_value = array();
112 +
113 + if ( 'get' === $type ) {
114 + if ( ! array_key_exists( $key, $first_set_value ) ) {
115 + return false;
116 + } else {
117 + return $first_set_value[ $key ];
118 + }
119 + } elseif ( 'set' === $type ) {
120 + $first_set_value[ $key ] = $val;
121 +
122 + return $first_set_value[ $key ];
123 + } elseif ( 'clear' === $type ) {
124 + unset( $first_set_value[ $key ] );
125 + }
126 +
127 + return $first_set_value;
128 + }
129 +
130 + /**
131 + * Clear cache by key
132 + *
133 + * @param $key
134 + */
135 + public function clear( $key ) {
136 + try {
137 + if ( empty( $key ) ) {
138 + return;
139 + }
140 +
141 + wp_cache_delete( $key, $this->key_group );
142 + if ( $this->can_handle_with_thim_cache() ) {
143 + $key = "{$this->key_group}/{$key}";
144 + Thim_Cache_DB::instance()->remove_cache( $key );
145 + }
146 + } catch ( Throwable $e ) {
147 + error_log( $e->getMessage() );
148 + }
149 + }
150 +
151 + /**
152 + * Store list keys cache to a group.
153 + *
154 + * @param string $key_group
155 + * @param string $key_cache_new
156 + * @since 4.2.5.4
157 + * @version 1.0.0
158 + */
159 + public function save_cache_keys( string $key_group, string $key_cache_new ) {
160 + try {
161 + $keys = $this->get_cache( $key_group );
162 + if ( false === $keys ) {
163 + $keys_cache = array();
164 + } else {
165 + $keys_cache = LP_Helper::json_decode( $keys, true );
166 + }
167 +
168 + $keys_cache[] = $key_cache_new;
169 + $this->set_cache( $key_group, json_encode( $keys_cache ) );
170 + } catch ( Throwable $e ) {
171 + error_log( __METHOD__ . ': ' . $e->getMessage() );
172 + }
173 + }
174 +
175 + /**
176 + * Clear cache on group store list keys.
177 + *
178 + * @param string $key_group
179 + * @since 4.2.5.4
180 + * @version 1.0.0
181 + */
182 + public function clear_cache_on_group( string $key_group ) {
183 + try {
184 + $keys_cache_of_group_str = $this->get_cache( $key_group );
185 + if ( false === $keys_cache_of_group_str ) {
186 + return;
187 + }
188 + $keys_cache_of_group = LP_Helper::json_decode( $keys_cache_of_group_str, true );
189 + if ( ! empty( $keys_cache_of_group ) ) {
190 + foreach ( $keys_cache_of_group as $key ) {
191 + // Clear cache by key
192 + $this->clear( $key );
193 + }
194 + // Clear cache store list keys
195 + $this->clear( $key_group );
196 + }
197 + } catch ( Throwable $e ) {
198 + error_log( __METHOD__ . ': ' . $e->getMessage() );
199 + }
200 + }
201 +
202 + /**
203 + * Check can handle with thim cache
204 + *
205 + * @since 4.2.5.4
206 + * @version 1.0.0
207 + * @return bool
208 + */
209 + public function can_handle_with_thim_cache(): bool {
210 + return $this->has_thim_cache && LP_Settings::is_created_tb_thim_cache();
211 + }
212 +
213 + /**
214 + * Clear all cache
215 + *
216 + * @since 4.0.8
217 + * @version 1.0.1
218 + * @return void
219 + */
220 + public function clear_all() {
221 + try {
222 + wp_cache_flush();
223 + Thim_Cache_DB::instance()->remove_all_cache();
224 + } catch ( Throwable $e ) {
225 + error_log( __METHOD__ . ': ' . $e->getMessage() );
226 + }
227 + }
228 +}