| 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_DB_VERSION = '1.0'; |
| 16 |
const WTOTEM_TABLE_SETTINGS = 'wtotem_settings'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Creating a database with plugin settings. |
| 20 |
*/ |
| 21 |
public static function install () { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 25 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name) { |
| 26 |
|
| 27 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 28 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 29 |
name tinytext NOT NULL, |
| 30 |
value longtext, |
| 31 |
UNIQUE KEY id (id) |
| 32 |
);"; |
| 33 |
|
| 34 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 35 |
dbDelta($sql); |
| 36 |
|
| 37 |
update_option("wtotem_db_version", self::WTOTEM_DB_VERSION); |
| 38 |
} |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add (or update) option to the settings table. |
| 44 |
*/ |
| 45 |
public static function setOption ($name, $value) { |
| 46 |
global $wpdb; |
| 47 |
|
| 48 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 49 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name){ |
| 50 |
self::install(); |
| 51 |
} |
| 52 |
|
| 53 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 54 |
|
| 55 |
$option = array( |
| 56 |
'name' => $name, |
| 57 |
'value' => $value, |
| 58 |
); |
| 59 |
|
| 60 |
if($current = self::getOption($name, true)){ |
| 61 |
$option['id'] = $current['id']; |
| 62 |
} |
| 63 |
|
| 64 |
$wpdb->replace( $table_name, $option ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Delete option to the settings table. |
| 69 |
*/ |
| 70 |
public static function deleteOption ($name) { |
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 74 |
$wpdb->delete( $table_name, array( 'name' => $name ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Getting values from the settings table. |
| 79 |
* |
| 80 |
* @param string $name |
| 81 |
* Option name. |
| 82 |
* @param string $need_id |
| 83 |
* If you need to return id. |
| 84 |
* |
| 85 |
* @return string|array |
| 86 |
*/ |
| 87 |
public static function getOption ($name, $need_id = false) { |
| 88 |
global $wpdb; |
| 89 |
|
| 90 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 91 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name){ |
| 92 |
self::install(); |
| 93 |
} |
| 94 |
|
| 95 |
$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS); |
| 96 |
|
| 97 |
$option = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE name = '%s'", $name ) ); |
| 98 |
|
| 99 |
if($option) { |
| 100 |
if($need_id){ |
| 101 |
return [ |
| 102 |
'id' => $option->id, |
| 103 |
'value' => $option->value, |
| 104 |
]; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $option ? $option->value : ''; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Deleting wtotem tables. |
| 113 |
*/ |
| 114 |
public static function uninstall() { |
| 115 |
global $wpdb; |
| 116 |
|
| 117 |
$table_name = $wpdb->prefix . self::WTOTEM_TABLE_SETTINGS; |
| 118 |
$wpdb->query( "DROP TABLE IF EXISTS $table_name"); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the table with the site prefix added. |
| 124 |
* |
| 125 |
* @param string $table |
| 126 |
* Table name. |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public static function add_prefix($table) { |
| 130 |
global $wpdb; |
| 131 |
return $wpdb->prefix . $table; |
| 132 |
} |
| 133 |
|
| 134 |
} |