wpdb = $wpdb; $this->tableUserSettings = $wpdb->prefix . Database::$tableUserSettings; } public function save($key, $value, $userId = null) { $field = $this->get($key, $userId); return $field ? $this->update($key, $value, $userId) : $this->add($key, $value, $userId); } public function add($key, $value, $userId = null) { $result = $this->wpdb->insert("{$this->tableUserSettings}", array( 'param' => $key, 'value' => $value, 'userId' => $userId, )); return $result !== false; } public function get($key, $userId = null) { return $userId !== null ? $this->wpdb->get_row( $this->wpdb->prepare("SELECT id FROM {$this->tableUserSettings} WHERE `param` = %s AND `userId` = %d", array($key, $userId)) ) : $this->wpdb->get_row( $this->wpdb->prepare("SELECT id FROM {$this->tableUserSettings} WHERE `param` = %s AND `userId` IS NULL", $key) ); } public function update($key, $value, $userId = null) { $result = $this->wpdb->update("{$this->tableUserSettings}", array('value' => $value), array('param' => $key, 'userId' => $userId) ); return $result !== false; } public function getAll($userId = null) { return $userId !== null ? $this->wpdb->get_results( $this->wpdb->prepare("SELECT * FROM {$this->tableUserSettings} WHERE `userId` = %d;", $userId), ARRAY_A ) : $this->wpdb->get_results( "SELECT * FROM {$this->tableUserSettings} WHERE `userId` IS NULL;", ARRAY_A ); } }