使用某种控制器,并将对两个类的调用组合在每个操作的一个回调中。控制器应该负责将业务逻辑真正分配给事件(操作),而不是类之外的代码<基本要点是:插件控制器不应更改数据,只有模型才能更改数据。
以下示例显示了如何创建一个表(我在这里使用一个选项来保持简单)和一个选项:
<?php # -*- coding: utf-8 -*-
namespace WPSE;
/**
* Plugin Name: Multiple De/Activation Objects
*/
\\register_activation_hook(
__FILE__,
array ( Front_Controller::get_instance(), \'activate\' )
);
\\register_deactivation_hook(
__FILE__,
array ( Front_Controller::get_instance(), \'deactivate\' )
);
class Front_Controller
{
protected static $instance = NULL;
protected $db_name = \'wpse_db\';
protected $option_name = \'wpse_option\';
public static function get_instance()
{
if ( NULL === self::$instance )
self::$instance = new self;
return self::$instance;
}
public function activate()
{
$db = new Db( $this->db_name );
$db->create_table();
$option = new Option( $this->option_name );
$option->add();
}
public function deactivate()
{
$db = new Db( $this->db_name );
$db->delete_table();
$option = new Option( $this->option_name );
$option->remove();
}
}
class Db
{
protected $db_name;
public function __construct( $db_name )
{
$this->db_name = $db_name;
}
public function create_table()
{
// install table, for this demonstration, we use an option
return \\add_option( $this->db_name, \'exists\' );
}
public function delete_table()
{
// uninstall table
return \\delete_option( $this->db_name );
}
}
class Option
{
protected $option_name;
public function __construct( $option_name )
{
$this->option_name = $option_name;
}
public function add()
{
// add option
return \\add_option( $this->option_name, \'exists\' );
}
public function remove()
{
// delete option
return \\delete_option( $this->option_name );
}
}
您可以使用第二个插件测试是否成功:
add_action( \'admin_footer\', function() {
$db = get_option( \'wpse_db\', \'missing\' );
$option = get_option( \'wpse_option\', \'missing\' );
print "<code>\\$db = $db, \\$option = $option</code>";
});
以下链接供进一步阅读: