定制 dwnload/wp-settings-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dwnload/wp-settings-api

Composer 安装命令:

composer require 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.

README 文档

README

PHP from Packagist Latest Stable Version Total Downloads License Build Status codecov

It's a PHP class wrapper for handling WordPress Settings API.

Package Installation (via Composer)

To install this package, edit your composer.json file:

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

Now run:

$ composer install dwnload/wp-settings-api

Usage Example

@see examples/Example.php

Suggested package

⭐️ frontpack/composer-assets-plugin

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.

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:

<?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()
        );
    }
}

dwnload/wp-settings-api 适用场景与选型建议

dwnload/wp-settings-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.75k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2017 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dwnload/wp-settings-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dwnload/wp-settings-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 dwnload/wp-settings-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 19
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-23