| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility callbacks. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Utilities; |
| 9 |
|
| 10 |
use function WPE\FaustWP\Detect_Conflicts\delete_conflicts_dismissed; |
| 11 |
use function WPE\FaustWP\Settings\{ |
| 12 |
get_secret_key, |
| 13 |
faustwp_get_settings, |
| 14 |
faustwp_update_setting, |
| 15 |
maybe_set_default_settings |
| 16 |
}; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
register_activation_hook( FAUSTWP_FILE, __NAMESPACE__ . '\\handle_activation' ); |
| 23 |
/** |
| 24 |
* Callback for WordPress register_activation_hook() function. |
| 25 |
* |
| 26 |
* 1. Deactivate the WPE Headless plugin if it's active. |
| 27 |
* 2. Set default settings if no settings exist. |
| 28 |
* 3. Set secret_key if does not exist. |
| 29 |
* 4. Flush rewrite rules. |
| 30 |
* |
| 31 |
* @todo is flush_rewrite_rules() needed? |
| 32 |
* |
| 33 |
* @link https://developer.wordpress.org/reference/functions/register_activation_hook/ |
| 34 |
* |
| 35 |
* @param bool $network_active True if plugin is network activated. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
function handle_activation( $network_active ) { |
| 40 |
if ( is_plugin_active( 'wpe-headless/wpe-headless.php' ) ) { |
| 41 |
deactivate_plugins( 'wpe-headless/wpe-headless.php', true ); |
| 42 |
} |
| 43 |
|
| 44 |
// Handle network activation of plugin within multisite. |
| 45 |
if ( $network_active ) { |
| 46 |
$args = array( |
| 47 |
'fields' => 'ids', |
| 48 |
); |
| 49 |
$sites = get_sites( $args ); |
| 50 |
|
| 51 |
foreach ( $sites as $site ) { |
| 52 |
switch_to_blog( $site ); |
| 53 |
maybe_set_default_settings(); |
| 54 |
restore_current_blog(); |
| 55 |
} |
| 56 |
|
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
maybe_set_default_settings(); |
| 61 |
} |
| 62 |
|
| 63 |
register_deactivation_hook( FAUSTWP_FILE, __NAMESPACE__ . '\\handle_deactivation' ); |
| 64 |
/** |
| 65 |
* Callback for WordPress register_deactivation_hook() function. |
| 66 |
* |
| 67 |
* Clear conflict dismissals and flush rewrites on plugin deactivation. |
| 68 |
* |
| 69 |
* @todo is flush_rewrite_rules() needed? |
| 70 |
* |
| 71 |
* @link https://developer.wordpress.org/reference/functions/register_deactivation_hook/ |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
function handle_deactivation() { |
| 76 |
delete_conflicts_dismissed(); |
| 77 |
flush_rewrite_rules(); |
| 78 |
} |
| 79 |
|
| 80 |
add_action( 'wp_initialize_site', __NAMESPACE__ . '\\handle_new_site_creation' ); |
| 81 |
/** |
| 82 |
* Fires when a site's initialization routine should be executed. |
| 83 |
* |
| 84 |
* @link https://developer.wordpress.org/reference/hooks/wp_initialize_site/ |
| 85 |
* |
| 86 |
* @param WP_Site $new_site New site object. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
function handle_new_site_creation( $new_site ) { |
| 91 |
switch_to_blog( $new_site->blog_id ); |
| 92 |
maybe_set_default_settings(); |
| 93 |
restore_current_blog(); |
| 94 |
} |
| 95 |
|