2015-09-05 117 views
5

快速放入,我试图建立一个使用WordPress的'生态系统',我有一个核心插件,然后额外的插件插件。WordPress PSR-4名称空间插件访问另一个命名空间插件

更深入的是,每个附加插件都需要核心插件才能运行。我已经使用WordPress标准编码和文件结构实践实现了这一点。我重做这个项目现在使用命名空间PSR-4,作曲家,闺房,然后通过作曲家

"autoload": { 
    "psr-4": { 
     "CorePlugin\\Library\\": "library" 
    } 
} 

标准的WordPress安装

| 
|__www 
    | 
    |___wp-admin 
    | 
    |___wp-content 
    | | 
    | |___plugins 
    | | | 
    | | |___my-core-plugin 
    | | | | 
    | | | |___library 
    | | | | | 
    | | | | |___class-post-register.php 
    | | | | 
    | | | |___vendor 
    | | | | | 
    | | | | |___autoload.php 
    | | | | 
    | | | |___composer.json 
    | | | | 
    | | | |___core.php 
    | | | 
    | | |___my-first-addon-plugin 
    | | | | 
    | | | |___library 
    | | | | 
    | | | |___vendor 
    | | | | | 
    | | | | |___autoload.php 
    | | | | 
    | | | |___composer.json 
    | | | | 
    | | | |___core.php 
    | | | 
    | | |___my-second-addon-plugin 
    | | | | 
    | | | |___library 
    | | | | 
    | | | |___vendor 
    | | | | | 
    | | | | |___autoload.php 
    | | | | 
    | | | |___composer.json 
    | | | | 
    | | | |___core.php 
    | | | 
    | |___themes 
    | | | 
    | | |___my-custom-theme 
    | | 
    | wp-includes 

核心插件PSR4示例核心插件类

<?php 

namespace CorePlugin\library; 

class Post_Register { 

    private __construct() { 
     // ... code 
    } 

    private init() { 

    } 

    private register($data) { 
     // .. code to register a custom post for example. 
    } 

} 

首先通过的作曲家的附加插件

下面附加的插件PSR4

"autoload": { 
    "psr-4": { 
     "FirstAddon\\Library\\": "library" 
    } 
} 

类是我很困惑。我想在不同的命名空间使用一个类从核心插件,我得到的错误:

Fatal error: Class 'CorePlugin\Library\Post_Register' not found in...

两个插件自动加载各自的作曲家生成自动加载的文件,所以我虽然我能够use命名空间。在我深入研究PHP手册的这一部分(http://php.net/manual/en/language.namespaces.php)之前,我来这里问了一下,我可能会尝试子命名空间。

<?php 

namespace FirstAddon; 

use CorePlugin\Library\Post_Register; 

class First_Addon { 

    private __construct() { 
     // ... code 
    } 

    private init() { 

    } 

    private another_function() { 

    } 

} 

而且,我很犹豫使用子命名空间与括号中,因为,例如,在laravel,use FOO \吧;和use bar \ foo;像这样。

<?php namespace App\Services; 

use App\User; 
use Validator; 
use Illuminate\Contracts\Auth\Registrar as RegistrarContract; 

class Registrar implements RegistrarContract { 

回答

0

我相信你已经从这里走了,但我想我会回答,以防其他人试图让插件依赖于对方。我在我的插件中使用类和名称空间。我的插件重用彼此的类。

命名空间插件

首先,它基本上可以归结为在WordPress的加载你的插件的顺序。我自己来自C#/ Java,最初由WP如何做事情而感到困惑。你想确保你想使用的插件已经加载。做到这一点的最好方法是通过延迟挂钩来实例化类 - 这是在加载插件后发生的。一个例子可以是

add_action('plugins_loaded', function() { new whatever() }); 

,然后让构造函数使用类中的其他插件(或任何你需要它):

function __construct() { 
    $other_plugin_class = new \Namespace\To\Other\Plugin\MyClass(); 
} 

依赖

如果插件是依赖于每个其他,并且需要采取不同的行动,具体取决于启用了哪些插件,哪些不是,您可以这样做:

if (! function_exists('is_plugin_active')) 
    require_once(ABSPATH . '/wp-admin/includes/plugin.php'); 

if (is_admin() and is_plugin_active('myplugin1/plugin1.php')) { 
    add_action('admin_menu', array($this, 'add_a_menu_depending_on_another_plugin'), 99); 
} 

首先它确保我们需要的功能被加载,然后检查是否启用了相关的插件。

自动装入

,我使用内置的自动加载值得一提:

spl_autoload_register('my_autoloader'); 
function my_autoloader($class_name) { 
    if (false !== strpos($class_name, 'Nuvobook')) { 
    $classes_dir = plugin_dir_path(__DIR__); 
    $class_file = strtolower(str_replace('_', '-', str_replace('\\', DIRECTORY_SEPARATOR, $class_name)) . '.php'); 
    include_once $classes_dir . $class_file; 
    } 
} 

..这My_Class我的班命名约定映射到我的名,class.php

它所有作品奇妙。

希望能帮助别人。

相关问题