当使用类和动作时,这是一种很好的练习,请提供一种简单的方法来删除动作。
使用类似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, 但应该给你一个开始的方向。