*/ public static function crons(): array { /** @var array $crons */ $crons = (array) apply_filters('DayTidy/F__DayTidy/Plugin/Hooks__crons/crons', [ 'DayTidy/C__HourlySchedule' => 'hourly', ]); return $crons; } /** * Schedule DayTidy cron jobs if they are missing. * * @param mixed $time * @return void */ public static function scheduleCrons($time = null): void { $time = is_numeric($time) && (int) $time > 0 ? (int) $time : time(); foreach (self::crons() as $hook => $recurrence) { if (!is_string($hook) || !is_string($recurrence) || !$hook || !$recurrence) continue; if (wp_next_scheduled($hook)) continue; wp_schedule_event($time, $recurrence, $hook); } } /** * Clear DayTidy cron jobs. * * @return void */ public static function clearCrons(): void { foreach (array_keys(self::crons()) as $hook) { if (!is_string($hook) || !$hook) continue; wp_clear_scheduled_hook($hook); } } protected ?PluginProfile $profile; /** * Constructor * @param PluginProfile|null $profile */ public function __construct(?PluginProfile $profile = null) { $this->profile = $profile ? $profile : Profile::getInstance(); // Plugin Basename $basename = $this->profile->basename(); register_activation_hook($basename, [$this, 'activated']); register_deactivation_hook($basename, [$this, 'deactivated']); register_uninstall_hook($basename, [static::class, 'uninstalled']); } /** * Runs on plugin activation * @param boolean $network * @return void */ public function activated(bool $network = false):void { $current_blog_id = get_current_blog_id(); // Plugin activated only for one blog if (!function_exists('is_multisite') or (function_exists('is_multisite') and !is_multisite())) $network = false; if (!$network) { $this->install($current_blog_id); // Add WordPress rewrite-rule flushing to the to-do list RewriteRules::todo(); // Don't run rest of the function return; } // Database $db = new Db(); /** * Plugin activated for all blogs * @var array $blogs */ $blogs = $db->select("SELECT `blog_id` FROM `#__blogs`", 'loadColumn'); foreach ($blogs as $blog_id) { switch_to_blog($blog_id); $this->install($blog_id); } switch_to_blog($current_blog_id); // Add WordPress rewrite-rule flushing to the to-do list RewriteRules::todo(); } /** * Install the plugin on certain blog * @param int $blog_id * @return void */ public function install(int $blog_id = 1): void { // Time $time = time(); // Save Installation Time update_option('daytidy_installed_at', $time, false); // Create Database CreateTables::run(); // Add DayTidy roles and capabilities Capabilities::sync(); // Add cron jobs self::scheduleCrons($time); } /** * Runs on plugin deactivation * @param boolean $network * @return void */ public function deactivated(bool $network = false): void { /** * Refresh WordPress rewrite rules * We cannot use DayTidy\RewriteRules here because plugin is deactivated and it won't run */ flush_rewrite_rules(); AnnouncementManager::unregister('daytidy', $network); // Clear cron jobs self::clearCrons(); } /** * Runs on plugin uninstall * @return void */ public static function uninstalled(): void { // Database $db = new Db(); // Getting current blog $current_blog_id = get_current_blog_id(); // Single WordPress Installation if (!function_exists('is_multisite') or (function_exists('is_multisite') and !is_multisite())) { static::purge($current_blog_id); /** * Refresh WordPress rewrite rules * We cannot use DayTidy\RewriteRules here because plugin is removed and it won't run */ flush_rewrite_rules(); // Don't run rest of the function return; } /** * WordPress is multisite, so purge the plugin from all blogs * @var array $blogs */ $blogs = $db->select("SELECT `blog_id` FROM `#__blogs`", 'loadColumn'); foreach ($blogs as $blog_id) { switch_to_blog($blog_id); static::purge($blog_id); } // Switch back to current blog switch_to_blog($current_blog_id); /** * Refresh WordPress rewrite rules * We cannot use DayTidy\RewriteRules here because plugin is removed and it won't run */ flush_rewrite_rules(); } /** * Remove DayTidy from a blog * @param int $blog_id * @return void */ public static function purge(int $blog_id = 1): void { DataRetention::clearScheduledEvents(); // Decide whether to delete data $delete = apply_filters('DayTidy/F__DayTidy/Plugin/Hooks__purge/status', DataRetention::shouldDeleteData($blog_id), $blog_id); // Delete DayTidy data if ($delete) { DataRetention::cleanup($blog_id); } } /** * A function to deactivate the plugin programmatically * @return void */ public function deactivate(): void { // Include Function if (!function_exists('deactivate_plugins')) require_once ABSPATH . 'wp-admin/includes/plugin.php'; // Deactivate deactivate_plugins($this->profile->basename()); } }