PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / trunk
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI vtrunk
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / includes / cache.php

cache.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI trunk, at includes/cache.php

206 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress Cache Class
4 *
5 * Used to store commonly used query results
6 *
7 * @package GamiPress\Cache
8 * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
9 * @since 1.4.7
10 */
11 // Exit if accessed directly
12 if( !defined( 'ABSPATH' ) ) exit;
13
14 /**
15 * Get a cached element.
16 *
17 * @since 1.4.7
18 * @updated 1.6.1 Added support to return cached value stored in options table
19 *
20 * @param string $key Cache key
21 * @param mixed $default Default value in case the cache is not found
22 * @param bool $stored Whatever if the cache has been stored previously in the database or not
23 *
24 * @return mixed
25 */
26 function gamipress_get_cache( $key = '', $default = null, $stored = true ) {
27
28 if( isset( GamiPress()->cache[$key] ) ) {
29 return GamiPress()->cache[$key];
30 } else if( $stored ) {
31
32 // If GamiPress is installed network wide, get cache from network options
33 if( gamipress_is_network_wide_active() ) {
34 $cached = get_site_option( 'gamipress_cache_' . $key );
35 } else {
36 $cached = get_option( 'gamipress_cache_' . $key );
37 }
38
39 // If has been cached on options, then return the cached value
40 if( $cached !== false ) {
41
42 GamiPress()->cache[$key] = $cached;
43
44 return GamiPress()->cache[$key];
45
46 }
47
48 }
49
50 return $default;
51
52 }
53
54 /**
55 * Set a cached element.
56 *
57 * @since 1.4.7
58 * @updated 1.6.1 Added $save parameter
59 *
60 * @param string $key
61 * @param mixed $value
62 * @param bool $save
63 *
64 * @return bool
65 */
66 function gamipress_set_cache( $key = '', $value = '', $save = false ) {
67
68 // Just keep value on a floating cache
69 // To make it persistent pass $save as true or use gamipress_save_cache() function
70 GamiPress()->cache[$key] = $value;
71
72 if( $save === true ) {
73 return gamipress_save_cache( $key, $value );
74 }
75
76 return true;
77
78 }
79
80 /**
81 * Save a cached element.
82 *
83 * @since 1.6.1
84 *
85 * @param string $key
86 * @param mixed $value
87 *
88 * @return bool
89 */
90 function gamipress_save_cache( $key = '', $value = '' ) {
91
92 // Allow to make value optional but just if element has been already cached
93 if( empty( $value ) && isset( GamiPress()->cache[$key] ) ) {
94 $value = GamiPress()->cache[$key];
95 }
96
97 // Update the floating cache
98 GamiPress()->cache[$key] = $value;
99
100 // If GamiPress is installed network wide, save cache on network options
101 if( gamipress_is_network_wide_active() ) {
102 return update_site_option( 'gamipress_cache_' . $key, $value );
103 } else {
104 return update_option( 'gamipress_cache_' . $key, $value, false );
105 }
106
107 }
108
109 /**
110 * Delete a cached element.
111 *
112 * @since 1.6.1
113 *
114 * @param string $key
115 *
116 * @return bool
117 */
118 function gamipress_delete_cache( $key = '' ) {
119
120 if( isset( GamiPress()->cache[$key] ) ) {
121 unset( GamiPress()->cache[$key] );
122 }
123
124 // If GamiPress is installed network wide, delete cache on network options
125 if( gamipress_is_network_wide_active() ) {
126 return delete_site_option( 'gamipress_cache_' . $key );
127 } else {
128 return delete_option( 'gamipress_cache_' . $key );
129 }
130
131 }
132
133 /**
134 * Flush the entire GamiPress cache
135 *
136 * @since 1.9.9.2
137 */
138 function gamipress_flush_cache() {
139
140 global $wpdb;
141
142 if( gamipress_is_network_wide_active() ) {
143 // Multi site installs
144 $wpdb->query( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE 'gamipress_cache_%'");
145 } else {
146 // Single site installs
147 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'gamipress_cache_%'" );
148 }
149
150 }
151
152 /**
153 * Preview achievement earned email action
154 *
155 * @since 1.3.0
156 */
157 function gamipress_clear_cache_action() {
158
159 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
160 return;
161 }
162
163 if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'clear_cache' ) ) {
164 exit;
165 }
166
167 // Clear the GamiPress cache
168 gamipress_flush_cache();
169
170 wp_redirect( admin_url( 'admin.php?page=gamipress_settings&gamipress-message=cache_cleared' ) );
171 exit;
172
173 }
174 add_action( 'gamipress_action_get_clear_cache', 'gamipress_clear_cache_action' );
175
176 /**
177 * GamiPress admin notices
178 *
179 * @since 1.5.9
180 */
181 function gamipress_clear_cache_notices() {
182
183 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
184 return;
185 }
186
187 if( ! isset( $_GET['gamipress-message'] ) ) {
188 return;
189 }
190
191 if( $_GET['gamipress-message'] !== 'cache_cleared' ) {
192 return;
193 }
194
195 ?>
196
197 <div class="notice notice-success is-dismissible gamipress-notice">
198 <p>
199 <?php _e( '<strong>GamiPress cache</strong> cleared successfully!', 'gamipress' ); ?>
200 </p>
201 </div>
202
203 <?php
204
205 }
206 add_action( 'admin_notices', 'gamipress_clear_cache_notices' );