PluginProbe
Themify Builder / trunk
Themify Builder vtrunk
7.8.1 7.8.0 7.7.9 7.7.8 7.7.7 7.7.6 7.7.5 7.7.4 7.7.3 7.7.2 trunk 7.6.0 7.6.1 7.6.2 7.6.3 7.6.4 7.6.5 7.6.6 7.6.7 7.6.8 7.6.9 7.7.0 7.7.1
themify-builder / themify / class-themify-storage.php

class-themify-storage.php in Themify Builder trunk, at themify/class-themify-storage.php

122 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Themify_Storage
4 {
5
6 private static $table = null;
7
8 public static function init(){
9 if (self::$table === null) {
10 global $wpdb;
11 $errors = $wpdb->show_errors;
12 try {
13 self::$table = $wpdb->prefix . 'tf_storage';
14 $q = 'CREATE TABLE IF NOT EXISTS ' . self::$table . ' (
15 `key` CHAR(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL PRIMARY KEY,
16 `value` MEDIUMTEXT NOT NULL,
17 `expire` INT UNSIGNED,
18 KEY(expire)
19 ) ENGINE=InnoDB ' . $wpdb->get_charset_collate() . ';';
20 $wpdb->hide_errors();
21 $res = $wpdb->query($q);
22 if ($res === false) {
23 self::$table = false;
24 }
25 unset($q, $res);
26 } catch (Throwable $e) {
27 self::$table = false;
28 }
29 finally {
30 if ($errors) {
31 $wpdb->show_errors();
32 }
33 }
34 }
35 return self::$table;
36 }
37
38 public static function cleanDb(){
39 if (self::init() !== false) {
40 $q = 'DELETE FROM %s WHERE `expire` IS NOT NULL AND `expire`<' . time();
41 return self::query($q);
42 }
43 return false;
44 }
45
46 public static function query(string $q){
47 if (self::init() !== false) {
48 global $wpdb;
49 return $wpdb->query(sprintf($q, self::$table));
50 }
51 return false;
52 }
53
54 public static function get(string $key,string $prefix = ''){
55 $k = self::getHash($key, $prefix);
56 if (self::init() !== false) {
57 global $wpdb;
58 $res = $wpdb->get_row('SELECT `value`,`expire` FROM ' . self::$table . ' WHERE `key`=\'' . esc_sql($k) . '\' LIMIT 1');
59 unset($k);
60 if (!empty($res)) {
61 if (!empty($res->expire) && time() > $res->expire) {
62 self::delete($key, $prefix);
63 }
64 elseif (isset($res->value)) {
65 return $res->value;
66 }
67 }
68 return false;
69 }
70 return get_transient($k);
71 }
72
73 public static function set(string $key, $v, $exp = null,string $prefix = ''){
74 $k = self::getHash($key, $prefix);
75 if (is_array($v)) {
76 $v = json_encode($v);
77 } elseif ($v === true || $v === false) {
78 $v = $v === true ? '1' : '0';
79 }
80 if (self::init() !== false) {
81 global $wpdb;
82 $exp = $exp === null?'DEFAULT':((int)$exp + time());
83 $q = 'INSERT INTO ' . self::$table . ' (`key`,`value`,`expire`) VALUES (\'' . esc_sql($k) . '\',\'' . esc_sql($v) . '\',' . $exp . ') ON DUPLICATE KEY UPDATE `value`=VALUES(value),`expire`=VALUES(expire)';
84 return $wpdb->query($q);
85 }
86 return set_transient($k, $v, $exp);
87 }
88
89 public static function delete(string $k,string $prefix = ''){
90 $k = self::getHash($k, $prefix);
91 if (self::init() !== false) {
92 global $wpdb;
93 $q = 'DELETE FROM ' . self::$table . ' WHERE `key`=\'' . esc_sql($k) . '\' LIMIT 1';
94 return $wpdb->query($q);
95 }
96 return delete_transient($k);
97 }
98
99 public static function getHash(string $k,string $prefix = ''):string{
100 static $h = null;
101 if ($h === null) {
102 $hashs = hash_algos();
103 $h = 'fnv164';
104 if (in_array('xxh3', $hashs, true)) {
105 $h = 'xxh3';
106 } elseif (in_array('fnv1a64', $hashs, true)) {
107 $h = 'fnv1a64';
108 }
109 unset($hashs);
110 }
111 $k = hash($h, $k);
112 if ($prefix !== '') {
113 $k = substr_replace($k, $prefix, 0, strlen($prefix));
114 }
115 return $k;
116 }
117
118 public static function deleteByPrefix(string $prefix){
119 return self::query('DELETE FROM %s WHERE `key` LIKE "' . esc_sql($prefix) . '%%' . '"');
120 }
121 }
122