带有注册激活挂钩的__命名空间__

时间:2016-05-08 作者:orionrush

我现在在wordpress中教自己名称空间和OOP,对我来说太简单了。

在插件的底部,我正在加载激活和停用挂钩。目前,停用挂钩可以工作,但激活挂钩不能,它只是默默地失败(WP_DEBUG 已打开)。

基文件声明一个名称空间和一个常量,然后需要类文件来激活和停用,然后注册挂钩。激活和停用在功能上都是相同的,每个都需要并实例化一个错误日志记录类,并向一个方法发送消息。

我已经把这两方面的文件都翻了一遍register_deactivation_hook 对于register_activation_hook (特别是撰稿人指出)我还没能解决这个问题,所以如果你能想到为什么一个有效而另一个无效,我将不胜感激。

文件结构如下:

/plugin/
    plugin.php   
/plugin/includes/
         ActivatorClass.php
         DeactivatorClass.php
         ErrorLogClass.php
/插件/插件。php

<?php
namespace company\\pluginName;

/**
 * Plugin Name:       Plugin Name
 * Plugin URI:        http://url
 * Description:       A demo plugin 
 * Version:           0.0.1
 * Author:            me
 */


function pluginInit() {

    // Machine file path
    define( \'PLUGIN_PATH\', realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR );

    /**
    * Runs during plugin activation.
    * Documented in includes/ActivatorClass.php
    */
    require_once PLUGIN_PATH . \'includes/ActivatorClass.php\';
    register_activation_hook( __FILE__, array( __NAMESPACE__ . \'\\\\ActivatorClass\', \'activate\' ) );

    /**
    * Runs during plugin deactivation.
    * Documented in includes/DeactivatorClass.php
    */
    require_once PLUGIN_PATH  . \'includes/DeactivatorClass.php\';
    register_deactivation_hook( __FILE__, array( __NAMESPACE__ . \'\\\\DeactivatorClass\', \'deactivate\' ) );
}

add_action( \'plugins_loaded\', __NAMESPACE__ . \'\\\\pluginInit\' );
插件/包含/激活类。php

 <?php
 namespace company\\pluginName;

 /**
  * Fired during plugin activation
  *
  * This class defines all code necessary to run during the plugin\'s activation.
  *
  * @since      0.0.1
  * @package    plugin\\pluginName\\
  * @author     me
  */
  class ActivatorClass {

      /**
       * Fired during plugin activation
       *
       * @since    0.0.1
       */
      public static function activate() {
          require ( PLUGIN_PATH. \'/includes/ErrorLogClass.php\');
          $activate = new ErrorLog();
          $activate->log(\'Plugin Activated\');
      }
  }
/plugin/includes/ErrorLogClass。php

<?php
namespace company\\pluginName;

/**
* Class ErrorLog
*
* @since      0.0.1
* @package    plugin\\pluginName\\
* @author     me
*/

 class ErrorLog {

    /**
     * The error(s) to be logged
     * @var array | string
     */
    protected $log;

    /**
     * The logging method
     *
     * @param $log
     */
    public static function log( $log ) {
        if ( true === WP_DEBUG ) {
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
 }

1 个回复
最合适的回答,由SO网友:Nathan Johnson 整理而成

插件激活挂钩在问题中提供的代码中不起作用的原因是在插件激活时plugins_loaded 吊钩从未运行过。自register_activation_hook 钩子是从plugins_loaded 它从未在激活时运行。由于它再也没有发射过,这种挂钩方式会导致register_activation_hook 从不开火。

解决方案是从主插件文件注册激活挂钩,而不是附加到任何挂钩。事实上,这是运行激活挂钩的唯一方法。在一个插件被激活后,只有另外两个钩子运行:特定的插件激活钩子和shutdown. 可以将挂钩添加到shutdown 他们会在插件激活时触发,但会在插件激活挂钩后触发。

/**
 * Plugin Name: WordPress Namespace OOP Plugin Example
 */
declare( strict_types = 1 );
namespace StackExchange\\WordPress;

//* Start bootstraping the plugin
require( __DIR__ . \'/includes/plugin.php\' );
$WordPressStackExchangePlugin = new plugin();
\\add_action( \'plugins_loaded\', [ $WordPressStackExchangePlugin, \'plugins_loaded\' ] );

//* Register activation hook
\\register_activation_hook( __FILE__, [ __NAMESPACE__ . \'\\\\ActivatorClass\', \'activate\' ] );
然后激活挂钩将从StackExchange\\WordPress\\ActivatorClass类调用静态方法activate。

备注:

严格的键入要求PHP>7.05.4和__DIR__ 要求PHP>5.3