y ) ) { $this->data[ $key ] = apply_filters( $this->get_filter_name( 'set', $key ), $value, $this->data ); return $this->update_data(); } if ( $key ) { $this->data[ $key ] = $value; } return $this; } /** * Remove an option by key. * * @param string $key Options array key. */ public function remove( string $key ) { if ( ! $key ) { return false; } unset( $this->data[ $key ] ); return $this->update_data(); } /** * Get the option key * * @return string Option key. */ public function get_option_key(): string { return $this->option_key; } /** * Set the option key. * * @param string $option_key Option name in the database. */ public function set_option_key( string $option_key ) { $this->option_key = $option_key; $this->set_data(); return $this; } /** * Gets all options. * * @return array */ public function get_data() { return (array) $this->get(); } /** * Sets the options data. * * @param array $data The options array. * @param boolean $unslash Whether to unslash the data. */ public function set_data( array $data = [], $unslash = false ) { if ( empty( $data ) && ! empty( $this->option_key ) ) { $default = $this->store_as_json ? '' : []; $data = get_option( $this->option_key, $default ); if ( $this->store_as_json ) { $data = json_decode( $data, true ); } // Do not unslash data from the database. $unslash = false; } if ( $unslash ) { $data = wp_unslash( $data ); } $this->data = (array) $data; return $this; } /** * Updates the options in the database. * * @param boolean $unslash Whether to unslash the data. */ public function update_data( bool $unslash = false ) { // Make sure we have something to work upon. if ( empty( $this->option_key ) ) { return false; } $data = $this->get_data(); if ( $unslash ) { $data = wp_unslash( $data ); } if ( $this->store_as_json ) { $data = wp_json_encode( $data ); } return update_option( $this->option_key, $data ); } /** * Magic method for accessing options as object props. * * @param string $key Options array key. * * @return mixed Value of the option */ public function __get( string $key ) { return $this->get( $key ); } /** * Magic method for setting options as object props. * * @param string $key Options array key. * @param mixed $value Option value. */ public function __set( string $key, $value ) { return $this->set( $key, $value ); } /** * Magic method for un-setting options as object props. * * @param string $key Options array key. */ public function __unset( string $key ) { return $this->remove( $key ); } /** * Magic method to check for existence of a key. * * @param string $key Options array key. */ public function __isset( string $key ) { return $this->exists( $key ); } /** * Allows the object being called as a function * to retrieve an option. * * @param string $key Options array key. * * @return mixed Option value. */ public function __invoke( string $key ) { return $this->get( $key ); } /** * Allows the object being treated as string * * @return string json encoded. */ public function __toString(): string { return wp_json_encode( $this->get_data() ); } /** * Determines whether an offset value exists. * * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php * * @param mixed $offset An offset to check for. * @return bool True if the offset exists, false otherwise. */ public function offsetExists( $offset ): bool { return $this->exists( $offset ); } /** * Retrieves a value at a specified offset. * * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php * * @param mixed $offset The offset to retrieve. * @return mixed If set, the value at the specified offset, false otherwise. */ public function offsetGet( $offset ): mixed { return $this->get( $offset ); } /** * Sets a value at a specified offset. * * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php * * @param mixed $offset The offset to assign the value to. * @param mixed $value The value to set. */ public function offsetSet( $offset, $value ): void { $this->set( $offset, $value ); } /** * Unsets a specified offset. * * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php * * @param mixed $offset The offset to unset. */ public function offsetUnset( $offset ): void { $this->remove( $offset ); } /** * Returns the current element. * * @link https://secure.php.net/manual/en/iterator.current.php * * @return mixed */ public function current(): mixed { return current( $this->data ); } /** * Moves forward to the next element. * * @link https://secure.php.net/manual/en/iterator.next.php */ public function next(): void { next( $this->data ); } /** * Returns the key of the current element. * * @link https://secure.php.net/manual/en/iterator.key.php * * @return mixed */ public function key(): mixed { return key( $this->data ); } /** * Checks if current position is valid. * * @link https://secure.php.net/manual/en/iterator.valid.php * * @return boolean */ public function valid(): bool { return key( $this->data ) !== null; } /** * Rewinds the Iterator to the first element. * * @link https://secure.php.net/manual/en/iterator.rewind.php */ public function rewind(): void { reset( $this->data ); } }