| 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 |
|