| 1 |
<?php |
| 2 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 3 |
if (!headers_sent()) { |
| 4 |
/* Report invalid access if possible. */ |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
exit(1); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* WebTotem Database class for Wordpress. |
| 12 |
*/ |
| 13 |
class WebTotemDB { |
| 14 |
|
| 15 |
const WTOTEM_TABLE_SETTINGS = 'wtotem_settings'; |
| 16 |
const WTOTEM_TABLE_BLOCKED_LIST = 'wtotem_blocked_list'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Creating a database with plugin settings. |
| 20 |
*/ |
| 21 |
public static function install () { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 25 |
|
| 26 |
$settings_table = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 27 |
if($wpdb->get_var("show tables like '$settings_table'") != $settings_table) { |
| 28 |
|
| 29 |
$sql = "CREATE TABLE " . $settings_table . " ( |
| 30 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 31 |
name tinytext NOT NULL, |
| 32 |
value longtext, |
| 33 |
UNIQUE KEY id (id) |
| 34 |
);"; |
| 35 |
|
| 36 |
dbDelta($sql); |
| 37 |
} |
| 38 |
|
| 39 |
$blocked_list_table = self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST); |
| 40 |
if($wpdb->get_var("show tables like '$blocked_list_table'") != $blocked_list_table) { |
| 41 |
|
| 42 |
$sql = "CREATE TABLE " . $blocked_list_table . " ( |
| 43 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 44 |
ip tinytext NOT NULL, |
| 45 |
reason tinytext, |
| 46 |
blockedTime tinytext, |
| 47 |
UNIQUE KEY id (id) |
| 48 |
);"; |
| 49 |
|
| 50 |
dbDelta($sql); |
| 51 |
} |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add (or update) data to the table. |
| 58 |
*/ |
| 59 |
public static function setData ($options, $table, $where = false) { |
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$table_name = self::getTable($table); |
| 63 |
|
| 64 |
if($where && $current = self::getData($where, $table)){ |
| 65 |
$options['id'] = $current['id']; |
| 66 |
} |
| 67 |
|
| 68 |
$wpdb->replace( $table_name, $options ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Delete data from the table. |
| 73 |
*/ |
| 74 |
public static function deleteData ($params, $table) { |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
$table_name = self::getTable($table); |
| 78 |
$wpdb->delete( $table_name, $params ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Getting values from the table. |
| 83 |
* |
| 84 |
* @param array $options |
| 85 |
* Option name. |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public static function getData ($options, $table) { |
| 90 |
global $wpdb; |
| 91 |
$table_name = self::getTable($table); |
| 92 |
|
| 93 |
foreach ($options as $key => $value){ |
| 94 |
$where[] = $key . " = '" . $value . "'"; |
| 95 |
} |
| 96 |
$where = implode(' AND ', $where); |
| 97 |
|
| 98 |
$options = $wpdb->get_row( "SELECT * FROM $table_name WHERE $where" ); |
| 99 |
|
| 100 |
return (array) $options ?: []; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Deleting wtotem tables. |
| 105 |
*/ |
| 106 |
public static function uninstall() { |
| 107 |
$tables = [ self::WTOTEM_TABLE_SETTINGS, self::WTOTEM_TABLE_BLOCKED_LIST ]; |
| 108 |
foreach ($tables as $table) { |
| 109 |
global $wpdb; |
| 110 |
$wpdb->query('DROP TABLE IF EXISTS `' . self::add_prefix($table) . '`'); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns the table with the site prefix added. |
| 116 |
* |
| 117 |
* @param string $table |
| 118 |
* Table name. |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
public static function add_prefix($table) { |
| 122 |
global $wpdb; |
| 123 |
return $wpdb->prefix . $table; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get table name. |
| 128 |
*/ |
| 129 |
private static function getTable($name) { |
| 130 |
switch ($name) { |
| 131 |
case 'settings': |
| 132 |
return self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 133 |
case 'blocked_list': |
| 134 |
return self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST); |
| 135 |
} |
| 136 |
|
| 137 |
throw new \OutOfBoundsException('Unknown key: ' . $name); |
| 138 |
} |
| 139 |
|
| 140 |
} |