-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDeactivator.php
92 lines (81 loc) · 2 KB
/
Deactivator.php
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
<?php
/**
* Fired during plugin deactivation.
*
* This class defines all code necessary to run during the plugin's deactivation.
*
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WooCommercePOS
*/
namespace WCPOS\WooCommercePOS;
/**
* Fired during plugin deactivation.
*/
class Deactivator {
public function __construct() {
register_deactivation_hook( PLUGIN_FILE, array( $this, 'deactivate' ) );
}
/**
* Fired when the plugin is deactivated.
*
* @param $network_wide
*/
public function deactivate( $network_wide ): void {
if ( \function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids
$blog_ids = $this->get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
$this->single_deactivate();
restore_current_blog();
}
} else {
$this->single_deactivate();
}
} else {
$this->single_deactivate();
}
}
/**
* Fired when the plugin is deactivated.
*/
public function single_deactivate(): void {
// remove pos capabilities
$this->remove_pos_capability();
// remove pos rewrite rule
flush_rewrite_rules( false ); // false will not overwrite .htaccess
}
/**
* Get all blog ids of blogs in the current network that are:
* - not archived
* - not spam
* - not deleted.
*/
private static function get_blog_ids() {
global $wpdb;
// get an array of blog ids
$sql = "SELECT blog_id FROM $wpdb->blogs
WHERE archived = '0' AND spam = '0'
AND deleted = '0'";
return $wpdb->get_col( $sql );
}
/**
* remove default pos capabilities to administrator and
* shop_manager roles.
*/
private static function remove_pos_capability(): void {
$roles = array( 'administrator', 'shop_manager' );
$caps = array( 'manage_woocommerce_pos', 'access_woocommerce_pos' );
foreach ( $roles as $slug ) {
$role = get_role( $slug );
if ( $role ) {
foreach ( $caps as $cap ) {
$role->remove_cap( $cap );
}
}
}
}
}