2017 © Pedro Peláez
 

library wp-settings-api

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

image

dwnload/wp-settings-api

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  • Saturday, January 6, 2018
  • by thefrosty
  • Repository
  • 1 Watchers
  • 2 Stars
  • 42 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

Dwnload WordPress Settings Api

PHP from Packagist Latest Stable Version Total Downloads License Build Status codecov, (*1)

It's a PHP class wrapper for handling WordPress Settings API., (*2)

Package Installation (via Composer)

To install this package, edit your composer.json file:, (*3)

{
    "require": {
        "dwnload/wp-settings-api": "^3.8"
    }
}

Now run:, (*4)

$ composer install dwnload/wp-settings-api, (*5)

Usage Example

@see examples/Example.php, (*6)

Suggested package

⭐️ frontpack/composer-assets-plugin, (*7)

As of v3.2.1, this shouldn't be needed any more (unless you are filtering local assets only). Assets will now be loaded via the jsdelivr CDN., (*8)

Otherwise, since this is a PHP package and not a WordPress plugin the assets included can't be loaded properly. In order to have the settings page inherit the styles and use the proper JS, you've got to copy the /assets directory to your plugin or theme. Then add the following to filter the asset src to your directory:, (*9)

<?php declare(strict_types=1);

namespace Vendor\Package;

use Dwnload\WpSettingsApi\Api\Script;
use Dwnload\WpSettingsApi\Api\Style;
use Dwnload\WpSettingsApi\WpSettingsApi;
use TheFrosty\WpUtilities\Plugin\AbstractHookProvider;
use TheFrosty\WpUtilities\Plugin\HooksTrait;

/**
 * Class WpSettingsApi
 *
 * @package Dwnload\WpSettingsApi
 */
class WpSettingsApiScripts extends AbstractHookProvider
{


    public function addHooks(array $scripts): void
    {
        \add_filter(WpSettingsApi::FILTER_PREFIX . 'admin_scripts', [$this, 'adminScripts']);
        \add_filter(WpSettingsApi::FILTER_PREFIX . 'admin_styles', [$this, 'adminStyles']);
    }

    /**
     * The default script needs to be moved from the vendor directory somewhere into our app since the
     * vendor directory is outside of the doc root.
     * @param Script[] $scripts
     * @return array
     */
    public function adminScripts(array $scripts): array
    {
        \array_walk($scripts, function (Script $script, int $key) use (&$scripts) {
            switch ($script->getHandle()) {
                case WpSettingsApi::ADMIN_SCRIPT_HANDLE:
                /**
                 * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider`
                 * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP
                 * function that will point to the asset.
                 * (Should match `frontpack/composer-assets-plugin configs`)
                 */
                $scripts[$key]->setSrc($this->getPlugin()->getUrl('assets/js/admin.js'));
                break;
            case WpSettingsApi::ADMIN_MEDIA_HANDLE:
                /**
                 * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider`
                 * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP
                 * function that will point to the asset.
                 * (Should match `frontpack/composer-assets-plugin configs`)
                 */
                $scripts[$key]->setSrc($this->getPlugin()->getUrl('assets/js/wp-media-uploader.js'));
                break;
            }
            $this->registerScript($script);
        });

        return $scripts;
    }

    /**
     * The default style needs to be moved from the vendor directory somewhere into our app since the
     * vendor directory is outside of the doc root.
     * @param Style[] $styles
     * @return array
     */
    public function adminStyles(array $styles): array
    {
        \array_walk($styles, function (Style $style, int $key) use (&$styles) {
            if ($style->getHandle() === WpSettingsApi::ADMIN_STYLE_HANDLE) {
                /**
                 * If you're not using the `TheFrosty\WpUtilities\Plugin\AbstractHookProvider`
                 * use `plugins_url()` in place of the `$this->getPlugin()->getUrl` or any other WP
                 * function that will point to the asset.
                 */
                $styles[$key]->setSrc($this->getPlugin()->getUrl('assets/css/admin.css'));
                $this->registerStyle($style);
            }
        });

        return $styles;
    }

    /**
     * If the script is not registered before being returned back to the filter the src still uses
     * the vendor directory file path.
     * @param Script $script
     */
    private function registerScript(Script $script): void
    {
        \wp_register_script(
            $script->getHandle(),
            $script->getSrc(),
            $script->getDependencies(),
            $script->getVersion(),
            $script->getInFooter()
        );
    }

    /**
     * If the style is not registered before being returned back to the filter the src still uses
     * the vendor directory file path.
     * @param Style $style
     */
    private function registerStyle(Style $style): void
    {
        \wp_register_style(
            $style->getHandle(),
            $style->getSrc(),
            $style->getDependencies(),
            $style->getVersion(),
            $style->getMedia()
        );
    }
}

The Versions

06/01 2018

dev-master

9999999-dev

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

06/01 2018

2.2.0

2.2.0.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

06/01 2018

dev-develop

dev-develop

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

23/08 2017

2.1.5

2.1.5.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

23/08 2017

2.1.4

2.1.4.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

16/08 2017

2.1.3

2.1.3.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/08 2017

2.1.2

2.1.2.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/08 2017

2.1.1

2.1.1.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

15/08 2017

2.1.0

2.1.0.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

28/07 2017

2.0.3

2.0.3.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

MIT

The Requires

 

The Development Requires

28/07 2017

2.0.2

2.0.2.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

The Requires

 

The Development Requires

28/07 2017

2.0.1

2.0.1.0

A PHP class abstraction that removes all the headaches of the WordPress settings API under the hood and builds a nice options panel on the fly.

  Sources   Download

The Requires

 

The Development Requires