PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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 4.2.0 4.2.1 All 138 releases
learnpress / inc / Databases / class-thim-cache-db.php

class-thim-cache-db.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/Databases/class-thim-cache-db.php

154 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Thim_Cache_DB
4 *
5 * @author tungnx
6 * @version 1.0.0
7 * @since 4.2.2
8 */
9 defined( 'ABSPATH' ) || exit();
10
11 if ( class_exists( 'Thim_Cache_DB' ) ) {
12 return;
13 }
14
15 class Thim_Cache_DB {
16 private static $_instance = null;
17 private $action = null; // one of insert/update
18 const ACTION_INSERT = 'insert';
19 const ACTION_UPDATE = 'update';
20
21 /**
22 * Set action for thim cache (one of insert/update)
23 * Default is null
24 *
25 * @param string|null $action
26 * @description Null for not set manual action insert/update
27 * @description Else it will be auto check exist key_cache to insert/update
28 * @return Thim_Cache_DB
29 */
30 public function set_action( $action ): Thim_Cache_DB {
31 $this->action = $action;
32 return $this;
33 }
34
35 /**
36 * Singleton
37 */
38 public static function instance(): Thim_Cache_DB {
39 if ( is_null( self::$_instance ) ) {
40 self::$_instance = new static();
41 }
42
43 return self::$_instance;
44 }
45
46 /**
47 * @var wpdb $wpdb
48 */
49 public $wpdb;
50 public $table_name;
51
52 protected function __construct() {
53 $this->wpdb = $GLOBALS['wpdb'];
54 $this->table_name = $this->wpdb->prefix . 'thim_cache';
55 }
56
57 /**
58 * Get value by key_cache
59 *
60 * @param string $key_cache
61 *
62 * @return bool|string
63 */
64 public function get_value( string $key_cache ) {
65 $sql = $this->wpdb->prepare(
66 "SELECT `value` FROM {$this->table_name} WHERE `key_cache` = %s",
67 $key_cache
68 );
69
70 $result = $this->wpdb->get_var( $sql );
71 if ( is_null( $result ) ) {
72 return false;
73 }
74
75 return $result;
76 }
77
78 /**
79 * Get value by key_cache
80 *
81 * @param string $key_cache
82 * @param string $value
83 * @param int $expire timestamp
84 * @return bool|int|mysqli_result|resource|null
85 */
86 public function set_value( string $key_cache, string $value, int $expire = 0 ) {
87 $action = self::ACTION_INSERT;
88
89 // Auto check exist key_cache
90 $value_old = $this->get_value( $key_cache );
91 if ( false !== $value_old ) {
92 $action = self::ACTION_UPDATE;
93 }
94
95 if ( self::ACTION_UPDATE === $action ) { // Update
96 $sql = $this->wpdb->prepare(
97 "UPDATE {$this->table_name} SET value = %s, expiration = %d WHERE key_cache = %s",
98 $value,
99 $expire,
100 $key_cache
101 );
102 } else { // Insert
103 $sql = $this->wpdb->prepare(
104 "INSERT INTO {$this->table_name} (key_cache, value, expiration) VALUES (%s, %s, %d)",
105 $key_cache,
106 $value,
107 $expire
108 );
109 }
110
111 return $this->wpdb->query( $sql );
112 }
113
114 /**
115 * Delete value by key_cache
116 *
117 * @param string $key_cache
118 *
119 * @return bool|int|mysqli_result|resource|null
120 * @throws Exception
121 */
122 public function remove_cache( string $key_cache ) {
123 $sql = $this->wpdb->prepare(
124 "DELETE FROM {$this->table_name} WHERE key_cache = %s",
125 $key_cache
126 );
127
128 $result = $this->wpdb->query( $sql );
129
130 if ( $this->wpdb->last_error ) {
131 throw new Exception( $this->wpdb->last_error );
132 }
133
134 return $result;
135 }
136
137 /**
138 * Delete all cache.
139 *
140 * @return bool|int|mysqli_result|resource|null
141 * @throws Exception
142 */
143 public function remove_all_cache() {
144 $sql = "TRUNCATE TABLE {$this->table_name}";
145 $result = $this->wpdb->query( $sql );
146
147 if ( $this->wpdb->last_error ) {
148 throw new Exception( $this->wpdb->last_error );
149 }
150
151 return $result;
152 }
153 }
154