Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Last active March 23, 2020 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaborio001/51c8c808ef1bc09e284ea012e6e72dbf to your computer and use it in GitHub Desktop.
Save csaborio001/51c8c808ef1bc09e284ea012e6e72dbf to your computer and use it in GitHub Desktop.
<?php
/**
* Logic activates / deactivates the plugin.
*
* @package compeer-matching-plugin
*/
namespace ScorpioTek;
class PluginActivation {
private $hook_name;
private $frequency;
private $file_base;
public function __construct( $caller_file_name, $hook_name, $frequency ) {
$this->set_hook_name( $hook_name );
$this->set_frequency( $frequency );
$this->set_file_base( plugin_dir_path( dirname( __FILE__ ) ) . $caller_file_name );
}
public static function get_instance( $caller_file_name, $hook_name, $frequency ) {
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = new PluginActivation( $caller_file_name, $hook_name, $frequency );
}
return $plugin;
}
public function init() {
\register_activation_hook( $this->get_file_base(), array( $this, 'activate_plugin' ) );
\register_deactivation_hook( $this->get_file_base(), array( $this, 'deactivate_plugin' ) );
}
public function activate_plugin() {
$hook_name = $this->get_hook_name();
if ( WP_DEBUG ) {
error_log( "Hook to activate {$hook_name} plugin invoked, will try to set up the cron event." );
}
if ( ! wp_next_scheduled( $hook_name ) ) {
$result = wp_schedule_event( time(), $this->get_frequency(), $hook_name );
if ( WP_DEBUG ) {
if ( true === $result ) {
error_log( "Cron task with name {$hook_name} successfully scheduled." );
} else {
error_log( "Cron task with name {$hook_name} was not scheduled." );
}
}
} else {
if ( WP_DEBUG ) {
error_log( "Hook {$hook_name} was already scheduled to run, no need to schedule the event." );
}
}
}
public function deactivate_plugin() {
wp_clear_scheduled_hook( $this->get_hook_name() );
}
public function set_cron_callback( $class_instance, $callback_name ) {
$hook_name = $this->get_hook_name();
if ( method_exists( $class_instance, $callback_name ) ) {
add_action( $this->get_hook_name(), array( $class_instance, $callback_name ) );
if ( WP_DEBUG ) {
error_log( "Callback function {$callback_name} was successfully set for hook {$hook_name}." );
}
} else {
if ( WP_DEBUG ) {
error_log( "Callback function {$callback_name} was NOT set for hook {$hook_name}." );
}
}
}
/**
* Setter for the hook_name property.
*
* @param string $hook_name the new value of the hook_name property.
*/
public function set_hook_name( $hook_name ) {
$this->hook_name = $hook_name;
return $this->get_hook_name();
}
/**
* Getter for the hook_name property.
*
* @return string - the value of the hook_name property.
*/
public function get_hook_name() {
return $this->hook_name;
}
/**
* Setter for the frequency property.
*
* @param string $frequency the new value of the frequency property.
*/
public function set_frequency( $frequency ) {
$this->frequency = $frequency;
return $this->get_frequency();
}
/**
* Getter for the frequency property.
*
* @return string - the value of the frequency property.
*/
public function get_frequency() {
return $this->frequency;
}
/**
* Setter for the file_base property.
*
* @param string $file_base the new value of the file_base property.
*/
public function set_file_base( $file_base ) {
$this->file_base = $file_base;
return $this->get_file_base();
}
/**
* Getter for the file_base property.
*
* @return string - the value of the file_base property.
*/
public function get_file_base() {
return $this->file_base;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment