Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Mar 4, 2022
0 parents commit cf5806a
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# WordPress Options

A WordPress helper class for managing plugin options.

## Installation

```shell
composer require wp-forge/wp-options
```

## Usage

### Setting Options

```php
<?php

use WP_Forge\Options\Options;

$options = new Options('my_plugin_options');

// Pass the option name and option value as parameters.
$options->set('name', 'value');
```

### Getting Options

```php
<?php

use WP_Forge\Options\Options;

$options = new Options('my_plugin_options');

// Pass the option name and a default value as parameters.
// If a default value is not provided, `null` will be the default return value.
$options->get('name', 'default');
```

### Deleting Options

```php
<?php

use WP_Forge\Options\Options;

$options = new Options('my_plugin_options');

// Pass the option name to be deleted as a parameter.
$options->delete('name');
```

### Checking if an Option Exists

```php
<?php

use WP_Forge\Options\Options;

$options = new Options('my_plugin_options');

// Pass the option name as a parameter.
$options->has('name');
```

### Saving Options

By default, options will save automatically on the `shutdown` hook.

However, if you'd like to force a save, you can do it like this:

```php
<?php

use WP_Forge\Options\Options;

$options = new Options('my_plugin_options');

// Pass the option name and option value as parameters
$options->save();
```
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "wp-forge/wp-options",
"description": "A WordPress helper class for managing plugin options.",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Micah Wood",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"WP_Forge\\Options\\": "includes"
}
}
}
133 changes: 133 additions & 0 deletions includes/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace WP_Forge\Options;

/**
* Class Options
*
* A class for handling the fetching, saving and manipulation of options for WordPress plugins.
* All options data is stored in a single database option, but this class allows you to individually
* set or get options within it.
*/
class Options {

/**
* The name where our option is stored in the database.
*
* @var string
*/
protected $optionName;

/**
* Stores all options
*
* @var array
*/
protected $options = [];

/**
* Tracks whether a save is necessary.
*
* @var bool
*/
protected $shouldSave = false;

/**
* Class constructor.
*
* @param string $name Option name
*/
public function __construct( string $name ) {
$this->optionName = $name;
$this->options = $this->fetch();
add_action( 'shutdown', [ $this, 'maybeSave' ] );
}

/**
* Fetch options from the database.
*
* @return array
*/
public function fetch() {
return (array) get_option( $this->optionName, [] );
}

/**
* Check if an option exists.
*
* @param string $name Option name
*
* @return bool
*/
public function has( string $name ) {
return isset( $this->options[ $name ] );
}

/**
* Get an option by name.
*
* @param string $name Option name
* @param mixed $default Fallback value
*
* @return mixed
*/
public function get( string $name, $default = null ) {
return $this->has( $name ) ? $this->options[ $name ] : $default;
}

/**
* Set an option by name.
*
* @param string $name Option name
* @param mixed $value Option value
*/
public function set( string $name, $value ) {
if ( ! $this->has( $name ) || $this->get( $name ) !== $value ) {
$this->options[ $name ] = $value;
$this->shouldSave = true;
}
}

/**
* Delete an option by name.
*
* @param string $name Option name
*/
public function delete( string $name ) {
if ( $this->has( $name ) ) {
unset( $this->options[ $name ] );
$this->shouldSave = true;
}
}

/**
* Populate all options at once.
*
* @param array $data Option data
*/
public function populate( array $data ) {
$this->options = $data;
$this->shouldSave = true;
}

/**
* Save options to the database.
*
* @return bool
*/
public function save() {
$this->shouldSave = false;

return update_option( $this->optionName, $this->options, true );
}

/**
* Only save the options to the database if something changed.
*/
public function maybeSave() {
if ( $this->shouldSave ) {
$this->save();
}
}

}

0 comments on commit cf5806a

Please sign in to comment.