| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.13 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Admin; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Installs FireBox on the current site. |
| 21 |
* |
| 22 |
* This is the single entry point for "make this site ready to run FireBox". It |
| 23 |
* is called from the activation hook, for every site of a network activation, |
| 24 |
* when a new site is added to a network, and as a self-heal whenever the stored |
| 25 |
* version drifts from the running one. It must therefore be idempotent. |
| 26 |
*/ |
| 27 |
class SiteInstaller |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Installs, or repairs, FireBox on the current site. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function install($run_migrations = true) |
| 35 |
{ |
| 36 |
$activation = new PluginActivation(firebox()->hook_data); |
| 37 |
|
| 38 |
// Read before anything is created: a site with neither a stored version |
| 39 |
// nor tables is new and has nothing to migrate, while one with tables |
| 40 |
// but no version predates the version option and needs every migration. |
| 41 |
$installed_version = get_option('firebox_version', ''); |
| 42 |
|
| 43 |
if (!$installed_version && $activation->tablesInstalled()) |
| 44 |
{ |
| 45 |
$installed_version = '1.0.0'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Migrations first, install second. |
| 50 |
* |
| 51 |
* Stamping the version is what stops this running again, and a |
| 52 |
* migration that fatals half way through must not leave the site |
| 53 |
* looking up to date — it would never be retried. Running them first |
| 54 |
* means a failure leaves the old version in place and the next request |
| 55 |
* tries again. |
| 56 |
*/ |
| 57 |
if ($installed_version && $run_migrations) |
| 58 |
{ |
| 59 |
(new \FireBox\Core\Migrator($installed_version))->run(); |
| 60 |
} |
| 61 |
|
| 62 |
$activation->start(); |
| 63 |
|
| 64 |
// Migrations still owed: leave the version alone so the next request |
| 65 |
// picks the site up and finishes the job. |
| 66 |
if ($installed_version && !$run_migrations) |
| 67 |
{ |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
update_option('firebox_version', FBOX_VERSION); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Installs FireBox on the current site during plugin activation. |
| 76 |
* |
| 77 |
* Activation is a hostile place to run code. WordPress includes the plugin |
| 78 |
* file for the first time inside activate_plugin(), long after `init` has |
| 79 |
* fired — and `init` is where the framework registers its autoloader, so |
| 80 |
* none of the FPFramework classes the migrations rely on can be loaded. |
| 81 |
* (This is why the install steps below require the few framework files they |
| 82 |
* need by hand.) |
| 83 |
* |
| 84 |
* So activation installs, and never migrates. A site that owes migrations |
| 85 |
* keeps its stored version, and the self-heal on its next request — with |
| 86 |
* the framework loaded — runs them. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public static function installOnActivation() |
| 91 |
{ |
| 92 |
self::install(false); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Installs FireBox on every site of the network. |
| 97 |
* |
| 98 |
* @param int $limit Sites to install per run. The rest are picked up by |
| 99 |
* the next run, or lazily by self-heal. |
| 100 |
* |
| 101 |
* @return int Number of sites installed. |
| 102 |
*/ |
| 103 |
public static function installNetwork($limit = 50) |
| 104 |
{ |
| 105 |
if (!is_multisite()) |
| 106 |
{ |
| 107 |
self::installOnActivation(); |
| 108 |
|
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
$offset = (int) get_site_option('firebox_network_install_offset', 0); |
| 113 |
|
| 114 |
$sites = get_sites([ |
| 115 |
'fields' => 'ids', |
| 116 |
'number' => $limit, |
| 117 |
'offset' => $offset, |
| 118 |
'orderby' => 'id' |
| 119 |
]); |
| 120 |
|
| 121 |
foreach ($sites as $site_id) |
| 122 |
{ |
| 123 |
switch_to_blog($site_id); |
| 124 |
self::installOnActivation(); |
| 125 |
restore_current_blog(); |
| 126 |
} |
| 127 |
|
| 128 |
$installed = count($sites); |
| 129 |
|
| 130 |
if ($installed < $limit) |
| 131 |
{ |
| 132 |
// Reached the end of the network. |
| 133 |
delete_site_option('firebox_network_install_offset'); |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
update_site_option('firebox_network_install_offset', $offset + $installed); |
| 138 |
|
| 139 |
if (!wp_next_scheduled('firebox_network_install')) |
| 140 |
{ |
| 141 |
wp_schedule_single_event(time() + 60, 'firebox_network_install'); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $installed; |
| 146 |
} |
| 147 |
} |
| 148 |
|