| 1 |
<?php if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
|
| 3 |
use FluentCommunity\App\Hooks\Handlers\ActivationHandler; |
| 4 |
use FluentCommunity\App\Hooks\Handlers\DeactivationHandler; |
| 5 |
use FluentCommunity\Framework\Foundation\Application; |
| 6 |
|
| 7 |
return function ($file) { |
| 8 |
|
| 9 |
$app = new Application($file); |
| 10 |
|
| 11 |
register_activation_hook($file, function () use ($app) { |
| 12 |
($app->make(ActivationHandler::class))->handle(); |
| 13 |
|
| 14 |
if (function_exists('\as_next_scheduled_action')) { |
| 15 |
if (!\as_next_scheduled_action('fluent_community_scheduled_hour_jobs')) { |
| 16 |
\as_schedule_recurring_action(time(), 3600, 'fluent_community_scheduled_hour_jobs', [], 'fluent-community', true); |
| 17 |
} |
| 18 |
|
| 19 |
if (!\as_next_scheduled_action('fluent_community_daily_jobs')) { |
| 20 |
\as_schedule_recurring_action(time(), 86400, 'fluent_community_daily_jobs', [], 'fluent-community', true); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
}); |
| 25 |
|
| 26 |
register_deactivation_hook($file, function () use ($app) { |
| 27 |
($app->make(DeactivationHandler::class))->handle(); |
| 28 |
}); |
| 29 |
|
| 30 |
require_once FLUENT_COMMUNITY_PLUGIN_DIR . 'vendor/woocommerce/action-scheduler/action-scheduler.php'; |
| 31 |
require_once FLUENT_COMMUNITY_PLUGIN_DIR . 'app/Functions/helpers.php'; |
| 32 |
|
| 33 |
if (file_exists(FLUENT_COMMUNITY_PLUGIN_DIR . 'Modules/modules_init.php')) { |
| 34 |
require_once FLUENT_COMMUNITY_PLUGIN_DIR . 'Modules/modules_init.php'; |
| 35 |
} |
| 36 |
|
| 37 |
add_action('plugins_loaded', function () use ($app) { |
| 38 |
do_action('fluent_community/portal_loaded', $app); |
| 39 |
|
| 40 |
add_action('init', function () use ($app) { |
| 41 |
/* |
| 42 |
* Transitional: repair a stale schema on ANY request, not only on the |
| 43 |
* ones a privileged user makes. |
| 44 |
* |
| 45 |
* Both entry points below are gated on a capability, so on an |
| 46 |
* upgrading site a newly added table does not exist until someone who |
| 47 |
* can activate plugins loads wp-admin. Cron, REST and ordinary member |
| 48 |
* requests all reach the plugin before that and read a table that is |
| 49 |
* not there yet. Multisite is worse: activate_plugins maps to |
| 50 |
* manage_network_plugins there, so only a super admin, visiting each |
| 51 |
* subsite in turn, ever migrates it. |
| 52 |
* |
| 53 |
* Ahead of on_wp_init deliberately, so the request that performs the |
| 54 |
* migration is also the first to benefit from it. |
| 55 |
* |
| 56 |
* Safe under the concurrency this exposes it to: dbDelta sits behind a |
| 57 |
* table-exists check, and the preference backfill replays as a no-op |
| 58 |
* (its INSERT is ON DUPLICATE KEY UPDATE with a self-assignment), so a |
| 59 |
* burst of traffic straight after an update duplicates work rather |
| 60 |
* than corrupting anything. |
| 61 |
* |
| 62 |
* Remove once 2.9.x is broadly adopted; the capability-gated entry |
| 63 |
* points are enough on a site that is already current. |
| 64 |
*/ |
| 65 |
fluent_community_maybe_migrate_db(); |
| 66 |
|
| 67 |
do_action('fluent_community/on_wp_init', $app); |
| 68 |
}); |
| 69 |
}); |
| 70 |
|
| 71 |
/* |
| 72 |
* A stale schema is repaired from three places. The init hook above is the one |
| 73 |
* that actually closes the gap, because it needs no privileged user; the two |
| 74 |
* below predate it and are kept as belt and braces. Portal render catches |
| 75 |
* sites where an admin browses the community; admin_init catches the |
| 76 |
* plugin-update case, where the first request after the new code lands is a |
| 77 |
* wp-admin page. A missing index only slows things down, but a missing table |
| 78 |
* is fatal, so more than one entry point matters. |
| 79 |
* |
| 80 |
* The version option is autoloaded so the check below costs nothing on the |
| 81 |
* requests where there is nothing to do - which, after the first one, is all |
| 82 |
* of them. Note that update_option() returns early when the value is |
| 83 |
* unchanged, so the autoload flag only flips on a site whose version string |
| 84 |
* actually moves; one already stamped at the current version keeps the old |
| 85 |
* flag until the next DB version bump. |
| 86 |
*/ |
| 87 |
if (!function_exists('fluent_community_maybe_migrate_db')) { |
| 88 |
function fluent_community_maybe_migrate_db() |
| 89 |
{ |
| 90 |
$currentDBVersion = get_option('fluent_community_db_version'); |
| 91 |
|
| 92 |
if ($currentDBVersion && version_compare($currentDBVersion, FLUENT_COMMUNITY_DB_VERSION, '>=')) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if (get_transient('fluent_community_db_migration_lock')) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
set_transient('fluent_community_db_migration_lock', 1, 5 * MINUTE_IN_SECONDS); |
| 101 |
\FluentCommunity\Database\DBMigrator::run(); |
| 102 |
update_option('fluent_community_db_version', FLUENT_COMMUNITY_DB_VERSION, true); |
| 103 |
delete_transient('fluent_community_db_migration_lock'); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
add_action('admin_init', function () { |
| 108 |
if (current_user_can('activate_plugins')) { |
| 109 |
fluent_community_maybe_migrate_db(); |
| 110 |
} |
| 111 |
}); |
| 112 |
|
| 113 |
add_action('fluent_community/portal_render_for_user', function () { |
| 114 |
if (!\FluentCommunity\App\Services\Helper::isSiteAdmin()) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (!\as_next_scheduled_action('fluent_community_scheduled_hour_jobs')) { |
| 119 |
as_schedule_recurring_action(time(), 3600, 'fluent_community_scheduled_hour_jobs', [], 'fluent-community'); |
| 120 |
} |
| 121 |
|
| 122 |
if (!\as_next_scheduled_action('fluent_community_daily_jobs')) { |
| 123 |
\as_schedule_recurring_action(time(), 86400, 'fluent_community_daily_jobs', [], 'fluent-community'); |
| 124 |
} |
| 125 |
/* |
| 126 |
* We will remove this after final release |
| 127 |
*/ |
| 128 |
fluent_community_maybe_migrate_db(); |
| 129 |
|
| 130 |
|
| 131 |
if (defined('FLUENT_COMMUNITY_PRO_VERSION')) { |
| 132 |
add_filter('fluent_community/portal_notices', function ($notices) { |
| 133 |
if (FLUENT_COMMUNITY_MIN_PRO_VERSION !== FLUENT_COMMUNITY_PRO_VERSION && version_compare(FLUENT_COMMUNITY_MIN_PRO_VERSION, FLUENT_COMMUNITY_PRO_VERSION, '>')) { |
| 134 |
$updateUrl = admin_url('plugins.php?s=fluent-community&plugin_status=all&fluent-fluent-community-pro-check-update=' . time()); |
| 135 |
$notices[] = '<div style="padding: 10px; background-color: var(--fcom-primary-bg, white);" class="error"><b>' . esc_html__('Heads UP:', 'fluent-community') . ' </b> ' . esc_html__('FluentCommunityPro Plugin needs to be updated to the latest version.', 'fluent-community') . ' <a href="' . esc_url($updateUrl) . '">' . esc_html__('Click here to update', 'fluent-community') . '</a></div>'; |
| 136 |
} |
| 137 |
return $notices; |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
}); |
| 142 |
}; |
| 143 |
|