When working with classes and action, is a good practise, give an easy way to remove the action.
Using something like add_action( 'init', array ( $this, 'init' ));
removing this action, can be done, but is far from easy.
Another thing to consider, is that if you plain to use a plugin from theme, is a good idea insert some custom filters and actions hooks to customize the behavior of your plugin form themes.
So, in your case you can do something like this
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();
}
After that, the class can be something like this:
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...
}
}
And in your template file use just
<?php do_action('spektrix_list_events'); ?>
or
<?php do_action('spektrix_list_events', true); // getPrices = true ?>
Code is rough and untested, but should give you a direction to start.