Having:
pluginname.php
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die();
}
define('PLUGINNAME_PLUGIN_URL', plugin_dir_url( __FILE__ ));
define('PLUGINNAME_PLUGIN_DIR', plugin_dir_path(__FILE__));
require_once( plugin_dir_path( __FILE__ ) . 'class-pluginname.php' );
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( __FILE__, array( 'PluginName', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'PluginName', 'deactivate' ) );
PluginName::get_instance();
class-pluginname.php
/**
* Initialize the plugin
*
* @since 1.0.0
*/
private function __construct()
{
#add_action('init', array($this, 'widget_add_vars'));
#add_action('params', array($this, 'catch_widget_query'));
add_action('dailyExport', array($this, '_generateJson'));
if (!wp_next_scheduled('dailyExport')) {
wp_schedule_event( time(), 'daily', 'dailyExport' );
}
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance()
{
// If the single instance hasn't been set, set it now.
if ( null == self::$instance )
self::$instance = new self;
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*/
public static function activate()
{
self::_generateJson();
wp_schedule_event(time(), 'daily', 'dailyExport');
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*/
public static function deactivate()
{
wp_clear_scheduled_hook('dailyExport');
}
public function dailyExport()
{
return self::_generateJson();
}
I was wondering, if you think that is right to define as static
the activate
and deactivate
functions, and if you think that I properly work with the cron event, to schedule daily events.
I wrote my plugin, based on this template https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate