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