PluginProbe
WebTotem Security / 2.4.12
WebTotem Security v2.4.12
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / DB.php

DB.php in WebTotem Security 2.4.12, at lib/DB.php

134 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }