| 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 |
public static function activate_plugin( $network_wide ) { |
| 29 |
self::do_multisite( $network_wide, [ __CLASS__, 'activate_site' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Deactivation wrapper. |
| 34 |
* |
| 35 |
* @param bool $network_wide Weather to deactivate network wide. |
| 36 |
*/ |
| 37 |
public static function deactivate_plugin( $network_wide ) { |
| 38 |
self::do_multisite( $network_wide, [ __CLASS__, 'deactivate_site' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Uninstall the plugin. |
| 43 |
*/ |
| 44 |
public static function uninstall_plugin() { |
| 45 |
self::do_multisite( true, [ __CLASS__, 'uninstall_site' ] ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle single & multisite processes. |
| 50 |
* |
| 51 |
* @param bool $network_wide Weather to do it network wide. |
| 52 |
* @param callable $method Callable method for each site. |
| 53 |
* @param array $args Array of extra args. |
| 54 |
*/ |
| 55 |
private static function do_multisite( $network_wide, $method, $args = [] ) { |
| 56 |
global $wpdb; |
| 57 |
|
| 58 |
if ( is_multisite() && $network_wide ) { |
| 59 |
$activated = get_site_option( 'content_control_activated', [] ); |
| 60 |
|
| 61 |
/* phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery */ |
| 62 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); |
| 63 |
|
| 64 |
// Try to reduce the chances of a timeout with a large number of sites. |
| 65 |
if ( \count( $blog_ids ) > 2 ) { |
| 66 |
ignore_user_abort( true ); |
| 67 |
|
| 68 |
if ( ! \ContentControl\is_func_disabled( 'set_time_limit' ) ) { |
| 69 |
/* phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged */ |
| 70 |
@set_time_limit( 0 ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( $blog_ids as $blog_id ) { |
| 75 |
switch_to_blog( $blog_id ); |
| 76 |
call_user_func_array( $method, [ $args ] ); |
| 77 |
|
| 78 |
$activated[] = $blog_id; |
| 79 |
|
| 80 |
restore_current_blog(); |
| 81 |
} |
| 82 |
|
| 83 |
update_site_option( 'content_control_activated', $activated ); |
| 84 |
} else { |
| 85 |
call_user_func_array( $method, [ $args ] ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Activate on single site. |
| 91 |
*/ |
| 92 |
public static function activate_site() { |
| 93 |
// Add a temporary option that will fire a hookable action on next load. |
| 94 |
\set_transient( '_content_control_installed', true, 3600 ); |
| 95 |
|
| 96 |
$version = plugin()->get( 'version' ); |
| 97 |
|
| 98 |
// Add version info. |
| 99 |
\add_option( 'content_control_version', [ |
| 100 |
'version' => $version, |
| 101 |
'upgraded_from' => null, |
| 102 |
'initial_version' => $version, |
| 103 |
'installed_on' => gmdate( 'Y-m-d H:i:s' ), |
| 104 |
] ); |
| 105 |
|
| 106 |
// Add data versions if missing. |
| 107 |
\add_option( 'content_control_data_versioning', \ContentControl\current_data_versions() ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Deactivate on single site. |
| 112 |
*/ |
| 113 |
public static function deactivate_site() { |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Uninstall single site. |
| 118 |
*/ |
| 119 |
public static function uninstall_site() { |
| 120 |
} |
| 121 |
} |
| 122 |
|