| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin installer. |
| 4 |
* |
| 5 |
* @copyright (c) 2021, Code Atlantic LLC. |
| 6 |
* |
| 7 |
* @package ContentControl\Plugin |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace ContentControl\Plugin; |
| 11 |
|
| 12 |
use function ContentControl\plugin; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Install |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
class Install { |
| 22 |
|
| 23 |
/** |
| 24 |
* Activation wrapper. |
| 25 |
* |
| 26 |
* @param bool $network_wide Weather to activate network wide. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function activate_plugin( $network_wide ) { |
| 31 |
self::do_multisite( $network_wide, [ __CLASS__, 'activate_site' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Deactivation wrapper. |
| 36 |
* |
| 37 |
* @param bool $network_wide Weather to deactivate network wide. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public static function deactivate_plugin( $network_wide ) { |
| 42 |
self::do_multisite( $network_wide, [ __CLASS__, 'deactivate_site' ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Uninstall the plugin. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public static function uninstall_plugin() { |
| 51 |
self::do_multisite( true, [ __CLASS__, 'uninstall_site' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Handle single & multisite processes. |
| 56 |
* |
| 57 |
* @param bool $network_wide Weather to do it network wide. |
| 58 |
* @param callable $method Callable method for each site. |
| 59 |
* @param array<string,mixed> $args Array of extra args. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
private static function do_multisite( $network_wide, $method, $args = [] ) { |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
if ( is_multisite() && $network_wide ) { |
| 67 |
$activated = get_site_option( 'content_control_activated', [] ); |
| 68 |
|
| 69 |
/* phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery */ |
| 70 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); |
| 71 |
|
| 72 |
// Try to reduce the chances of a timeout with a large number of sites. |
| 73 |
if ( \count( $blog_ids ) > 2 ) { |
| 74 |
ignore_user_abort( true ); |
| 75 |
|
| 76 |
if ( ! \ContentControl\is_func_disabled( 'set_time_limit' ) ) { |
| 77 |
/* phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged */ |
| 78 |
@set_time_limit( 0 ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
foreach ( $blog_ids as $blog_id ) { |
| 83 |
switch_to_blog( $blog_id ); |
| 84 |
call_user_func_array( $method, [ $args ] ); |
| 85 |
|
| 86 |
$activated[] = $blog_id; |
| 87 |
|
| 88 |
restore_current_blog(); |
| 89 |
} |
| 90 |
|
| 91 |
update_site_option( 'content_control_activated', $activated ); |
| 92 |
} else { |
| 93 |
call_user_func_array( $method, [ $args ] ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Activate on single site. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public static function activate_site() { |
| 103 |
// Add a temporary option that will fire a hookable action on next load. |
| 104 |
\set_transient( '_content_control_installed', true, 3600 ); |
| 105 |
|
| 106 |
$version = plugin()->get( 'version' ); |
| 107 |
|
| 108 |
// Add version info. |
| 109 |
\add_option( 'content_control_version', [ |
| 110 |
'version' => $version, |
| 111 |
'upgraded_from' => null, |
| 112 |
'initial_version' => $version, |
| 113 |
'installed_on' => gmdate( 'Y-m-d H:i:s' ), |
| 114 |
] ); |
| 115 |
|
| 116 |
// Add data versions if missing. |
| 117 |
\add_option( 'content_control_data_versioning', \ContentControl\current_data_versions() ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Deactivate on single site. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public static function deactivate_site() { |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Uninstall single site. |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public static function uninstall_site() { |
| 134 |
} |
| 135 |
} |
| 136 |
|