在插件类中注册和使用返回结果的操作

时间:2013-09-17 作者:codecowboy

我有一个插件类,它包含一个函数spektrix\\u list\\u events()

class SpektrixPlugin {

    public function __construct(){

        add_action( \'init\', array ( $this, \'init\' ));
    }

    public function init()
    {
        add_action( \'spektrix_list_events\', array ( $this, \'spektrix_list_events\' ));
    }

    public function spektrix_list_events($getPrices = FALSE) {
        $api = new SpektrixApiClient();
        return $api->getAllEvents($getPrices);

    }
}

$SpektrixEvents = new SpektrixPlugin;
add_action(\'init\', array($SpektrixEvents, \'init\'));
在模板文件中,我希望能够调用do\\u action(\'spektrix\\u list\\u events\'),但似乎无法正常工作。我试着听从建议here

其他问题-使用add\\u action()是否是建议的方法?

UPDATE:

实际上正在调用该方法,但没有返回任何结果。那么,应该如何调用返回一些输出以供显示的插件类方法呢?

3 个回复
SO网友:cybmeta

某些操作挂钩需要针对特定事件启动。请尝试以下代码:

class SpektrixPlugin {

    public function __construct(){
        add_action(\'init\', array(&$this, \'init\'));
    }

    public function spektrix_list_events($getPrices = FALSE) {
        $api = new SpektrixApiClient();
        return $api->getAllEvents($getPrices);
    }

    public function init(){
        add_action( \'spektrix_list_events\', array ( $this, \'spektrix_list_events\' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin();
我已经测试了此代码,它可以正常工作:

class SpektrixPlugin {

    public function __construct(){
        add_action(\'init\', array(&$this, \'init\'));
    }

    public function spektrix_list_events() {
        echo \'test\';
    }

    public function init(){
        add_action( \'spektrix_list_events\', array ( $this, \'spektrix_list_events\' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin();

 //this print \'test\', so it works.
 do_action(\'spektrix_list_events\');
但是。。。。我一直在阅读Wordpress文档do_action() 它清楚地表明do_action() 将调用函数/方法,但it won\'t return anything. 引用Wordpress关于do_action():

此函数的工作方式与apply\\u filters()类似,只是不返回任何内容,只调用函数或方法。

所以,你应该检查apply_filters() 函数的工作方式与do_action() 但可以返回被调用方法返回的值,或查找其他实现。do_action() 不适合返回值。

usgin apply\\u过滤器示例:

class SpektrixPlugin {

    public function __construct(){
        add_action(\'init\', array(&$this, \'init\'));
    }

    public function spektrix_list_events() {
        $myvar = "Hey, I\'m works";
        return $myvar;
    }

    public function init(){
        add_filter( \'spektrix_list_events\', array ( $this, \'spektrix_list_events\' ));
    }

 }

 $SpektrixEvents = new SpektrixPlugin;

 $test = apply_filters(\'spektrix_list_events\',\'\');
 var_dump($test);
无论如何,我认为这种获取数据的方法并不合适。虽然apply_filters() 将用于获取值,如上例所示,该函数专门设计用于过滤值,而不是获取值。我想你能做的就是get 方法来获取所需的值,然后可以对该值应用筛选器或对其执行操作。

SO网友:gmazzap

当使用类和动作时,这是一种很好的练习,请提供一种简单的方法来删除动作。

使用类似add_action( \'init\', array ( $this, \'init\' )); 可以删除此操作,但far from easy.

另一件需要考虑的事情是,如果您直接使用来自主题的插件,最好插入一些自定义过滤器和操作挂钩来自定义插件表单主题的行为。

所以,在你的情况下,你可以这样做

add_action(\'init\', \'initSpektrixPlugin\');

function initSpektrixPlugin() {
  // if the class isn\'t in the main plugin file, you can require class file here, e.g.:
  // require_once( plugin_dir_path(__FILE__) . \'inc/SpektrixPlugin.class.php\' );
  SpektrixPlugin::init();
}
之后,课程可以是这样的:

class SpektrixPlugin {

    protected static $events = array();

    public static function init()
    {
        add_action( \'spektrix_list_events\', array ( __CLASS__, \'printEvents\' ) );
    }

    protected static function getEvents( $getPrices = false )
    {
        $cached = self::getCachedEvents( $getPrices );
        if ( $cached ) 
           // last \'true\' param means we are returning cached result
           return apply_filters(\'spektrix_events\', $cached, $getPrices, true);
        // requiring the api client class from here, you include it
        // only if you don\'t already have cached results
        require_once( plugin_dir_path(__FILE__) . \'SpektrixApiClient.php\' );
        $api = new SpektrixApiClient();
        $events = self::setCachedEvents( $api->getAllEvents($getPrices), $getPrices);
        // a custom action to customize behavior from theme
        do_action(\'spektrix_events_getted\', $events, $getPrices);
        // return events after a custom filter, to customize behavior from theme
        // last \'false\' param means we are returning not cached result
        return apply_filters(\'spektrix_events\', $events, $getPrices, false);
    }

    protected static function getCachedEvents( $getPrices = false )
    {
      $key = $getPrices ? 1 : 0;
      if ( isset( self::$events[$key] ) ) return self::$events[$key];
      return false;
    }

    protected static function setCachedEvents( $events, $getPrices = false )
    {
      $key = $getPrices ? 1 : 0;
      self::$events[$key] = $events;
      return $events; 
    }

    public static function printEvents( $getPrices = false )
    {
        $events = self::getEvents( $getPrices );

        // debug
        echo \'<pre>\';
        print_r($events);
        echo \'</pre><br />\';
        // end debug

        // print here your events..
        // I don\'t know if it\'s a string, an object, an array...
    }

}
在模板文件中使用

<?php do_action(\'spektrix_list_events\'); ?>

<?php do_action(\'spektrix_list_events\', true); // getPrices = true ?>
代码很粗糙untested, 但应该给你一个开始的方向。

SO网友:sun

如果您想让任何功能正常工作,请将其与您的适当需求挂钩。我认为在您的情况下,您希望执行函数spektrix_list_events(). 它在类中,所以在挂钩中使用数组add_action(\'appropriate hook\',array(\'SpektrixPlugin\',\'spektrix_list_events\'));希望这会有所帮助。

结束

相关推荐

Control Loop Within Loop

我试图控制一个循环中的一个循环。我想做的是显示所有帖子,并每3篇帖子插入一个自定义帖子类型。所以它会像这样“后CPT后CPT后CPT后CPT…”我是这样做的global $loop_counter; $loop_counter = 0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> ?> // DO STUFF <?php $loop_counter+