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); $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); $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 all tables. */ public static function uninstall() { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS `" . self::add_prefix(self::WTOTEM_TABLE_SETTINGS) . "`"); } /** * 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; } }