| @@ -1,27 +1,81 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +defined('ABSPATH') or die; | |
| 4 | + | |
| 3 | 5 | use FluentSupport\Framework\Foundation\Application; |
| 4 | 6 | use FluentSupport\App\Hooks\Handlers\ActivationHandler; |
| 5 | 7 | use FluentSupport\App\Hooks\Handlers\DeactivationHandler; |
| 6 | 8 | |
| 7 | -return function($file) { | |
| 9 | +return function ($file) { | |
| 8 | 10 | |
| 9 | - register_activation_hook($file, function() { | |
| 11 | + require_once FLUENT_SUPPORT_PLUGIN_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php'; | |
| 12 | + | |
| 13 | + register_activation_hook($file, function () { | |
| 10 | 14 | (new ActivationHandler)->handle(); |
| 11 | 15 | }); |
| 12 | 16 | |
| 13 | - register_deactivation_hook($file, function() { | |
| 17 | + register_deactivation_hook($file, function () { | |
| 14 | 18 | (new DeactivationHandler)->handle(); |
| 15 | 19 | }); |
| 16 | 20 | |
| 17 | - add_action('plugins_loaded', function() use ($file) { | |
| 21 | + add_action('plugins_loaded', function () use ($file) { | |
| 18 | 22 | $application = new Application($file); |
| 23 | + | |
| 24 | + \FluentSupport\Database\DBMigrator::maybeMigrateDBChanges(); | |
| 25 | + | |
| 19 | 26 | do_action('fluent_support_loaded', $application); |
| 20 | 27 | do_action('fluent_support_addons_loaded', $application); |
| 21 | 28 | |
| 22 | - add_action('init', function () { | |
| 23 | - load_plugin_textdomain('fluent-support', false, 'fluent-support/language/'); | |
| 29 | + // add_action('init', function () { | |
| 30 | + // load_plugin_textdomain('fluent-support', false, 'fluent-support/language/'); | |
| 31 | + // }); | |
| 32 | + | |
| 33 | + add_action('fluent_support/admin_app_loaded', function () { | |
| 34 | + if (!wp_next_scheduled('fluent_support_hourly_tasks')) { | |
| 35 | + wp_schedule_event(time(), 'hourly', 'fluent_support_hourly_tasks'); | |
| 36 | + } | |
| 37 | + | |
| 38 | + if (!wp_next_scheduled('fluent_support_daily_tasks')) { | |
| 39 | + wp_schedule_event(time(), 'daily', 'fluent_support_daily_tasks'); | |
| 40 | + } | |
| 41 | + | |
| 42 | + if (!wp_next_scheduled('fluent_support_weekly_tasks')) { | |
| 43 | + wp_schedule_event(time(), 'weekly', 'fluent_support_weekly_tasks'); | |
| 44 | + } | |
| 45 | + | |
| 46 | + global $wpdb; | |
| 47 | + $table = esc_sql($wpdb->prefix . 'fs_tickets'); | |
| 48 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $table is sanitized via esc_sql(); no user input. | |
| 49 | + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM `{$table}` LIKE 'serial_number'"); | |
| 50 | + if ($column_exists) { | |
| 51 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 52 | + $has_null = $wpdb->get_var("SELECT id FROM `{$table}` WHERE `serial_number` IS NULL LIMIT 1"); | |
| 53 | + if ($has_null) { | |
| 54 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 55 | + $wpdb->query("UPDATE `{$table}` SET `serial_number` = `id` WHERE `serial_number` IS NULL"); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + /* | |
| 60 | + * The below schedule is powered by Action Scheduler by WooCommerce | |
| 61 | + * It will run every 30 minutes. Only schedule it while something is | |
| 62 | + * actually listening (currently only Fluent Support Pro's storage | |
| 63 | + * token refresh / email piping retry) — otherwise it fires with no | |
| 64 | + * callback to run. Any future free-tier listener added via | |
| 65 | + * add_action('fluent_support_half_hourly', ...) makes this schedule | |
| 66 | + * itself automatically, with no change needed here. | |
| 67 | + */ | |
| 68 | + $hasListener = has_action('fluent_support_half_hourly'); | |
| 69 | + $nextRun = as_next_scheduled_action('fluent_support_half_hourly', [], 'fluent-support'); | |
| 70 | + | |
| 71 | + if ($hasListener) { | |
| 72 | + if (false === $nextRun) { | |
| 73 | + as_schedule_recurring_action(time(), 1800, 'fluent_support_half_hourly', [], 'fluent-support', true); | |
| 74 | + } | |
| 75 | + } elseif (false !== $nextRun) { | |
| 76 | + as_unschedule_all_actions('fluent_support_half_hourly', [], 'fluent-support'); | |
| 77 | + } | |
| 24 | 78 | }); |
| 25 | 79 | |
| 26 | 80 | }); |
| 27 | 81 | }; |