| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fired during plugin deactivation. |
| 4 |
* |
| 5 |
* This class defines all code necessary to run during the plugin's deactivation. |
| 6 |
* |
| 7 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 8 |
* |
| 9 |
* @see http://wcpos.com |
| 10 |
* @package WooCommercePOS |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WCPOS\WooCommercePOS; |
| 14 |
|
| 15 |
/** |
| 16 |
* Fired during plugin deactivation. |
| 17 |
*/ |
| 18 |
class Deactivator { |
| 19 |
/** |
| 20 |
* Constructor. |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
register_deactivation_hook( PLUGIN_FILE, array( $this, 'deactivate' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Fired when the plugin is deactivated. |
| 28 |
* |
| 29 |
* @param bool $network_wide Whether to deactivate network-wide. |
| 30 |
*/ |
| 31 |
public function deactivate( bool $network_wide ): void { |
| 32 |
if ( \function_exists( 'is_multisite' ) && is_multisite() ) { |
| 33 |
if ( $network_wide ) { |
| 34 |
// Get all blog ids. |
| 35 |
$blog_ids = $this->get_blog_ids(); |
| 36 |
|
| 37 |
foreach ( $blog_ids as $blog_id ) { |
| 38 |
switch_to_blog( $blog_id ); |
| 39 |
$this->single_deactivate(); |
| 40 |
|
| 41 |
restore_current_blog(); |
| 42 |
} |
| 43 |
} else { |
| 44 |
$this->single_deactivate(); |
| 45 |
} |
| 46 |
} else { |
| 47 |
$this->single_deactivate(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Fired when the plugin is deactivated. |
| 53 |
*/ |
| 54 |
public function single_deactivate(): void { |
| 55 |
// Report churn before tearing anything down, while settings (and so the |
| 56 |
// consent state) are still readable. |
| 57 |
$lifecycle = new \WCPOS\WooCommercePOS\Services\Lifecycle_Events(); |
| 58 |
$lifecycle->report_deactivation(); |
| 59 |
$lifecycle->clear_schedule(); |
| 60 |
|
| 61 |
// remove pos capabilities. |
| 62 |
$this->remove_pos_capability(); |
| 63 |
|
| 64 |
// stop the print-job retention purge. |
| 65 |
wp_clear_scheduled_hook( \WCPOS\WooCommercePOS\Services\Print_Job_Service::PURGE_HOOK ); |
| 66 |
|
| 67 |
// stop the journal retention purge. |
| 68 |
wp_clear_scheduled_hook( \WCPOS\WooCommercePOS\Sync\Sync_Journal_Purge::PURGE_HOOK ); |
| 69 |
|
| 70 |
// remove pos rewrite rule. |
| 71 |
flush_rewrite_rules( false ); // false will not overwrite .htaccess. |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get all blog ids of blogs in the current network that are: |
| 76 |
* - not archived |
| 77 |
* - not spam |
| 78 |
* - not deleted. |
| 79 |
*/ |
| 80 |
private static function get_blog_ids() { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
// get an array of blog ids. |
| 84 |
$sql = "SELECT blog_id FROM $wpdb->blogs |
| 85 |
WHERE archived = '0' AND spam = '0' |
| 86 |
AND deleted = '0'"; |
| 87 |
|
| 88 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query contains no user input. |
| 89 |
return $wpdb->get_col( $sql ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Remove POS capabilities from all roles that received them during activation. |
| 94 |
*/ |
| 95 |
private static function remove_pos_capability(): void { |
| 96 |
$roles = array( 'administrator', 'shop_manager', 'cashier' ); |
| 97 |
$caps = array( 'manage_woocommerce_pos', 'access_woocommerce_pos' ); |
| 98 |
foreach ( $roles as $slug ) { |
| 99 |
$role = get_role( $slug ); |
| 100 |
if ( $role ) { |
| 101 |
foreach ( $caps as $cap ) { |
| 102 |
$role->remove_cap( $cap ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|