alpipego/resizefly-addon-template

使用此模板创建ResizeFly的新插件

安装: 7

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:wordpress-plugin

dev-master 2019-02-23 08:48 UTC

This package is auto-updated.

Last update: 2024-09-24 05:56:12 UTC


README

使用此模板注册ResizeFly插件的附加组件。

安装

  • 通过Composer
    使用以下命令创建新项目
composer create-project alpipego/resizefly-addon-template:dev-master  --no-install --remove-vcs YOUR_ADDON_NAME
  • 手动
    检出此存储库
git checkout git@github.com:alpipego/resizefly-addon-template.git YOUR_ADDON_NAME

确保更新所有文件中的命名空间和 composer.json

配置

WordPress插件头部

更新插件头部,查看https://codex.wordpress.org/File_Header#Plugin_File_Header_Example以获取示例

ResizeFly附加组件配置

更新namenicenameversionmin_version,以正确注册您的附加组件到父插件。请特别注意不要使用已安装的附加组件的name,因为这会覆盖它。

$addon = [
    'name'        => 'addon_template', // short name, only lowercase letters and underscores
    'nicename'    => 'Add-on Template', // Nice name, for use in UI
    'file'        => __FILE__, // Reference to this file, don't change
    'path'        => realpath( plugin_dir_path( __FILE__ ) ) . '/', // Path to this add-on, don't change
    'url'         => plugin_dir_url( __FILE__ ), // URL to this add-on, don't change
    'version'     => '1.0.0', // Version string, should match add-on version above
    'min_version' => '3.1.0', // Required minimum version of ResizeFly plugin, should match required version in composer.json
];

您的附加组件现在已注册到父插件,并且可以访问它的DI容器,即您可以将插件(甚至是其他附加组件的)类注入到您的代码中。

依赖注入容器

ResizeFly使用基于自定义Pimple的依赖注入(DI)容器。注册您的类

$plugin[ $addon['name'] ] = function ( $plugin ) use ( $addon ) {
    return new Addon( $plugin['addons'][$addon['name']] );
};

附加组件配置被传递回Addon()类。

附加组件

然后您可以使用$config数组来注册图像的自定义脚本等。

<?php

namespace Alpipego\Resizefly\AddonTemplate;

use Alpipego\Resizefly\Addon\AddonInterface;

final class Addon implements AddonInterface {
    private $config;

    /**
     * Addon constructor.
     *
     * @param array $config
     */
    public function __construct( $config ) {
        $this->config = $config;
    }

    /**
     * Set up add-on.
     * Register custom script
     */
    public function run() {
        add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
    }
    
    /**
     * Enqueue script, reusing add-on configuration as much as possible 
     */
    public function enqueueScripts(){
        wp_enqueue_script($this->config['name'], $this->config['url']. '/js/'.$this->config['name'].'.js');
    }
}