* * @see http://wcpos.com * @package WooCommercePOS */ namespace WCPOS\WooCommercePOS; /** * Fired during plugin deactivation. */ class Deactivator { /** * Constructor. */ public function __construct() { register_deactivation_hook( PLUGIN_FILE, array( $this, 'deactivate' ) ); } /** * Fired when the plugin is deactivated. * * @param bool $network_wide Whether to deactivate network-wide. */ public function deactivate( bool $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 { // Report churn before tearing anything down, while settings (and so the // consent state) are still readable. $lifecycle = new \WCPOS\WooCommercePOS\Services\Lifecycle_Events(); $lifecycle->report_deactivation(); $lifecycle->clear_schedule(); // remove pos capabilities. $this->remove_pos_capability(); // stop the print-job retention purge. wp_clear_scheduled_hook( \WCPOS\WooCommercePOS\Services\Print_Job_Service::PURGE_HOOK ); // stop the journal retention purge. wp_clear_scheduled_hook( \WCPOS\WooCommercePOS\Sync\Sync_Journal_Purge::PURGE_HOOK ); // 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'"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query contains no user input. return $wpdb->get_col( $sql ); } /** * Remove POS capabilities from all roles that received them during activation. */ private static function remove_pos_capability(): void { $roles = array( 'administrator', 'shop_manager', 'cashier' ); $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 ); } } } } }