get_var("show tables like '$settings_table'") != $settings_table) { $sql = "CREATE TABLE " . $settings_table . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, name tinytext NOT NULL, value longtext, UNIQUE KEY id (id) );"; dbDelta($sql); } $blocked_list_table = self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST); if($wpdb->get_var("show tables like '$blocked_list_table'") != $blocked_list_table) { $sql = "CREATE TABLE " . $blocked_list_table . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, ip tinytext NOT NULL, reason tinytext, blockedTime tinytext, UNIQUE KEY id (id) );"; dbDelta($sql); } return true; } /** * Add (or update) data to the table. */ public static function setData ($options, $table, $where = false) { global $wpdb; $table_name = self::getTable($table); if($where && $current = self::getData($where, $table)){ $options['id'] = $current['id']; } $wpdb->replace( $table_name, $options ); } /** * Delete data from the table. */ public static function deleteData ($params, $table) { global $wpdb; $table_name = self::getTable($table); $wpdb->delete( $table_name, $params ); } /** * Getting values from the table. * * @param array $options * Option name. * * @return array */ public static function getData ($options, $table) { global $wpdb; $table_name = self::getTable($table); foreach ($options as $key => $value){ $where[] = $key . " = '" . $value . "'"; } $where = implode(' AND ', $where); $options = $wpdb->get_row( "SELECT * FROM $table_name WHERE $where" ); return (array) $options ?: []; } /** * Deleting wtotem tables. */ public static function uninstall() { $tables = [ self::WTOTEM_TABLE_SETTINGS, self::WTOTEM_TABLE_BLOCKED_LIST ]; foreach ($tables as $table) { global $wpdb; $wpdb->query('DROP TABLE IF EXISTS `' . self::add_prefix($table) . '`'); } } /** * Returns the table with the site prefix added. * * @param string $table * Table name. * @return string */ public static function add_prefix($table) { global $wpdb; return $wpdb->prefix . $table; } /** * Get table name. */ private static function getTable($name) { switch ($name) { case 'settings': return self::add_prefix(self::WTOTEM_TABLE_SETTINGS); case 'blocked_list': return self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST); } throw new \OutOfBoundsException('Unknown key: ' . $name); } }