# wt-security/2.4.13/lib/DB.php

WebTotem Security, version 2.4.13. 134 lines.

- Page: https://pluginprobe.com/plugins/wt-security/2.4.13/code/lib/DB.php
- Raw: https://pluginprobe.com/plugins/wt-security/2.4.13/raw/lib/DB.php
- Modified: 2022-08-23T11:56:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wt-security/2.4.13/code/lib/DB.php#L10-L20`.

```php
<?php
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
	if (!headers_sent()) {
		/* Report invalid access if possible. */
		header('HTTP/1.1 403 Forbidden');
	}
	exit(1);
}

/**
 * WebTotem Database class for Wordpress.
 */
class WebTotemDB {

	const WTOTEM_DB_VERSION = '1.0';
	const WTOTEM_TABLE_SETTINGS = 'wtotem_settings';

	/**
	 * Creating a database with plugin settings.
	 */
	public static function install () {
		global $wpdb;

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
		if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

			$sql = "CREATE TABLE " . $table_name . " (
			  id mediumint(9) NOT NULL AUTO_INCREMENT,
			  name tinytext NOT NULL,
			  value longtext,
			  UNIQUE KEY id (id)
			);";

			require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
			dbDelta($sql);

			update_option("wtotem_db_version", self::WTOTEM_DB_VERSION);
		}
		return true;
	}

	/**
	 * Add (or update) option to the settings table.
	 */
	public static function setOption ($name, $value) {
		global $wpdb;

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
		if($wpdb->get_var("show tables like '$table_name'") != $table_name){
			self::install();
		}

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);

		$option = array(
			'name' => $name,
			'value' => $value,
		);

		if($current = self::getOption($name, true)){
			$option['id'] = $current['id'];
		}

		$wpdb->replace( $table_name, $option );
	}

	/**
	 * Delete option to the settings table.
	 */
	public static function deleteOption ($name) {
		global $wpdb;

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
		$wpdb->delete( $table_name, array( 'name' => $name ) );
	}

	/**
	 * Getting values from the settings table.
	 *
	 * @param string $name
	 *    Option name.
	 * @param string $need_id
	 *    If you need to return id.
	 *
	 * @return string|array
	 */
	public static function getOption ($name, $need_id = false) {
		global $wpdb;

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
		if($wpdb->get_var("show tables like '$table_name'") != $table_name){
			self::install();
		}

		$table_name = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);

		$option = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE name = '%s'", $name ) );

		if($option) {
			if($need_id){
				return [
					'id' => $option->id,
					'value' => $option->value,
				];
			}
		}

		return $option ? $option->value : '';
	}

	/**
	 * Deleting wtotem tables.
	 */
	public static function uninstall() {
		global $wpdb;

		$table_name = $wpdb->prefix . self::WTOTEM_TABLE_SETTINGS;
		$wpdb->query( "DROP TABLE IF EXISTS $table_name");

	}

	/**
	 * 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;
	}

}
```
