| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Network-aware option storage helper for the n-gram cache rebuild |
| 9 |
* subsystem. |
| 10 |
* |
| 11 |
* In a multisite network-activated installation, n-gram rebuild |
| 12 |
* progress (offset, current site, total sites, initialization flag) |
| 13 |
* must be shared across all sites in the network so each scheduled |
| 14 |
* batch sees the same state. In a per-site install, the same options |
| 15 |
* live in `wp_options`. |
| 16 |
* |
| 17 |
* This collaborator picks the right WordPress option API (site option |
| 18 |
* vs option) based on `is_plugin_active_for_network(ABJ404_FILE)` and |
| 19 |
* surfaces a stable get/set contract to callers (scheduler, async |
| 20 |
* batch runner, orchestrator). |
| 21 |
*/ |
| 22 |
class ABJ_404_Solution_NGramNetworkOptionStore { |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns true when the plugin is network-activated in a multisite |
| 26 |
* install — i.e. one shared activation across all sites. |
| 27 |
* |
| 28 |
* Falls through to false on single-site (no `is_multisite()`). |
| 29 |
* |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
public function isNetworkActivated() { |
| 33 |
if (!is_multisite()) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
if (!function_exists('is_plugin_active_for_network')) { |
| 38 |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 39 |
} |
| 40 |
|
| 41 |
return is_plugin_active_for_network(plugin_basename(ABJ404_FILE)); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Read an option from the network-wide store when network-activated, |
| 46 |
* otherwise from the site-local store. |
| 47 |
* |
| 48 |
* @param string $option_name |
| 49 |
* @param mixed $default |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function getOption($option_name, $default = false) { |
| 53 |
if ($this->isNetworkActivated()) { |
| 54 |
return get_site_option($option_name, $default); |
| 55 |
} |
| 56 |
return get_option($option_name, $default); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Write an option to the network-wide store when network-activated, |
| 61 |
* otherwise to the site-local store. |
| 62 |
* |
| 63 |
* @param string $option_name |
| 64 |
* @param mixed $value |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function updateOption($option_name, $value) { |
| 68 |
if ($this->isNetworkActivated()) { |
| 69 |
return update_site_option($option_name, $value); |
| 70 |
} |
| 71 |
return update_option($option_name, $value); |
| 72 |
} |
| 73 |
} |
| 74 |
|