Settings Model - 10quality/wpmvc-addon-administrator GitHub Wiki

Settings are managed in a settings model, this model will also auto-generate admin pages based on the settings defined.

A setting model is an extension of the Option Model; a project can hold multiple settings models if needed.

Content

Create a new Option model

Since this is basically an Option Model, the model can be created using the following WordPress MVC command:

php ayuco create optionmodel:{model name} {model id}

In example:

php ayuco create optionmodel:Settings myapp_settings

Once the model is created, it will look like:

use WPMVC\MVC\Traits\FindTrait;
use WPMVC\MVC\Models\OptionModel as Model;

class Settings extends Model
{
    use FindTrait;
    /**
     * Property id.
     * @var string
     */
    protected $id = 'myapp_settings';
}

Convert to a Settings model

To convert it into a Settings model, it will need the following modifications:

  • Replace use WPMVC\MVC\Models\OptionModel for use WPMVC\Addons\Administrator\Abstracts\SettingsModel.
  • Add use WPMVC\Addons\Administrator\Traits\SettingsTrait and use the trait.
  • Add const ID nad use it on property $id.
  • Add the protected init() method.
use WPMVC\MVC\Traits\FindTrait;
use WPMVC\Addons\Administrator\Traits\SettingsTrait;
use WPMVC\Addons\Administrator\Abstracts\SettingsModel as Model;

class Settings extends Model
{
    use FindTrait, SettingsTrait;
    /**
     * Constant id.
     * @var string
     */
    const ID = 'myapp_settings';
    /**
     * Property id.
     * @var string
     */
    protected $id = self::ID;
    /**
     * Initializes.
     */
    protected function init()
    {
        // TODO
    }
}

Basic properties

Inside the init() method add a title and a description (optional) to your settings, in example:

// Name space and use statement...

class Settings extends Model
{
    // Class properties...

    protected function init()
    {
        $this->title = __( 'My App Settings', 'my-domain' );
        $this->description= __( 'My App plugin configuration and settings.', 'my-domain' );
    }
}

Enqueue

Enqueue additional scripts or styles inside the enqueue() method to load them in the settings page, in example:

// Name space and use statement...

class Settings extends Model
{
    // Class properties and methods...
    public function enqueue()
    {
        wp_enqueue_script(
            'my-custom-settings',
            assets_url( 'js/admin.js' )
        );
    }
}

Register a settings model

To enable settings page rendering and management, each created settings model needs to be registered into the Add-on. There are 2 ways to register models:

  1. Register via the config file
  2. Register via filter hook

Register via the config file

In the [project folder]/app/Config/app.php file, the following configuration must be added:

    'administrator_models' => [
        '[model_id]' => '[Namespace]\Models\[Model]',
    ],

[model_id] must be replaced with a model ID, [Namspace] with a project namespace and [Model] with a settings model class name, like in the next example:

    'administrator_models' => [
        'myapp_settings' => 'MyNamespace\Models\Settings',
    ],

Register via filter hook

Use the following filter hook to do so:

use [Namespace]\Models\Settings;

add_filter( 'administrator_models', function( $models ) {
    $models[Settings::ID] = Settings::class;
    return $models;
} );