2015-09-05 121 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

它所有作品奇妙。

希望能幫助別人。

相關問題