Object_Sync_Sf_Deactivate::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.7333
1
<?php
2
/**
3
 * What to do when the plugin is deactivated
4
 *
5
 * @class   Object_Sync_Sf_Deactivate
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Deactivate class.
13
 */
14
class Object_Sync_Sf_Deactivate {
15
16
	/**
17
	 * Current version of the plugin
18
	 *
19
	 * @var string
20
	 */
21
	public $version;
22
23
	/**
24
	 * The main plugin file
25
	 *
26
	 * @var string
27
	 */
28
	public $file;
29
30
	/**
31
	 * Global object of `$wpdb`, the WordPress database
32
	 *
33
	 * @var object
34
	 */
35
	public $wpdb;
36
37
	/**
38
	 * The plugin's slug so we can include it when necessary
39
	 *
40
	 * @var string
41
	 */
42
	public $slug;
43
44
	/**
45
	 * The plugin's prefix when saving options to the database
46
	 *
47
	 * @var string
48
	 */
49
	public $option_prefix;
50
51
	/**
52
	 * Suffix for group name in ActionScheduler
53
	 *
54
	 * @var string
55
	 */
56
	public $action_group_suffix;
57
58
	/**
59
	 * Array of what classes in the plugin can be scheduled to occur with `wp_cron` events
60
	 *
61
	 * @var array
62
	 */
63
	public $schedulable_classes;
64
65
	/**
66
	 * Object_Sync_Sf_Queue class
67
	 *
68
	 * @var object
69
	 */
70
	public $queue;
71
72
	/**
73
	 * Constructor for deactivate class
74
	 */
75
	public function __construct() {
76
		$this->version             = object_sync_for_salesforce()->version;
77
		$this->file                = object_sync_for_salesforce()->file;
78
		$this->wpdb                = object_sync_for_salesforce()->wpdb;
79
		$this->slug                = object_sync_for_salesforce()->slug;
80
		$this->option_prefix       = object_sync_for_salesforce()->option_prefix;
81
		$this->action_group_suffix = object_sync_for_salesforce()->action_group_suffix;
82
83
		$this->schedulable_classes = object_sync_for_salesforce()->schedulable_classes;
84
		$this->queue               = object_sync_for_salesforce()->queue;
85
86
		$delete_data = (int) get_option( $this->option_prefix . 'delete_data_on_uninstall', 0 );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
		$delete_data = (int) /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'delete_data_on_uninstall', 0 );
Loading history...
87
		if ( 1 === $delete_data ) {
88
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'wordpress_salesforce_drop_tables' ) );
0 ignored issues
show
Bug introduced by
The function register_deactivation_hook was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
			/** @scrutinizer ignore-call */ 
89
   register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'wordpress_salesforce_drop_tables' ) );
Loading history...
89
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'clear_schedule' ) );
90
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'delete_log_post_type' ) );
91
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'remove_roles_capabilities' ) );
92
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'flush_plugin_cache' ) );
93
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'delete_plugin_options' ) );
94
		}
95
	}
96
97
	/**
98
	 * Drop database tables for Salesforce
99
	 * This removes the tables for fieldmaps (between types of objects) and object maps (between indidual instances of objects)
100
	 */
101
	public function wordpress_salesforce_drop_tables() {
102
		$field_map_table  = $this->wpdb->prefix . 'object_sync_sf_field_map';
103
		$object_map_table = $this->wpdb->prefix . 'object_sync_sf_object_map';
104
		$this->wpdb->query( 'DROP TABLE IF EXISTS ' . $field_map_table );
105
		$this->wpdb->query( 'DROP TABLE IF EXISTS ' . $object_map_table );
106
		delete_option( $this->option_prefix . 'db_version' );
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
		/** @scrutinizer ignore-call */ 
107
  delete_option( $this->option_prefix . 'db_version' );
Loading history...
107
	}
108
109
	/**
110
	 * Clear the scheduled tasks
111
	 * This removes all the scheduled tasks that are included in the plugin's $schedulable_classes array
112
	 */
113
	public function clear_schedule() {
114
		if ( ! empty( $this->schedulable_classes ) ) {
115
			foreach ( $this->schedulable_classes as $key => $value ) {
116
				$schedule_name     = $key;
117
				$action_group_name = $schedule_name . $this->action_group_suffix;
118
				$this->queue->cancel( $action_group_name );
119
			}
120
		}
121
	}
122
123
	/**
124
	 * Delete the log post type
125
	 * This removes the log post type
126
	 */
127
	public function delete_log_post_type() {
128
		unregister_post_type( 'wp_log' );
0 ignored issues
show
Bug introduced by
The function unregister_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
		/** @scrutinizer ignore-call */ 
129
  unregister_post_type( 'wp_log' );
Loading history...
129
	}
130
131
	/**
132
	 * Remove roles and capabilities
133
	 * This removes the configure_salesforce capability from the admin role
134
	 *
135
	 * It also allows other plugins to remove the capability from other roles
136
	 */
137
	public function remove_roles_capabilities() {
138
139
		// by default, only administrators can configure the plugin.
140
		$role = get_role( 'administrator' );
0 ignored issues
show
Bug introduced by
The function get_role was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
		$role = /** @scrutinizer ignore-call */ get_role( 'administrator' );
Loading history...
141
		$role->remove_cap( 'configure_salesforce' );
142
143
		// hook that allows other roles to configure the plugin as well.
144
		$roles = apply_filters( $this->option_prefix . 'roles_configure_salesforce', null );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
		$roles = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'roles_configure_salesforce', null );
Loading history...
145
146
		// for each role that we have, remove the configure salesforce capability.
147
		if ( null !== $roles ) {
148
			foreach ( $roles as $role ) {
149
				$role->remove_cap( 'configure_salesforce' );
150
			}
151
		}
152
153
	}
154
155
	/**
156
	 * Flush the plugin cache
157
	 */
158
	public function flush_plugin_cache() {
159
		$sfwp_transients = new Object_Sync_Sf_WordPress_Transient( 'sfwp_transients' );
160
		$sfwp_transients->flush();
161
	}
162
163
	/**
164
	 * Clear the plugin options
165
	 */
166
	public function delete_plugin_options() {
167
		$wpdb           = $this->wpdb;
168
		$plugin_options = $wpdb->get_results( $wpdb->prepare( "SELECT option_name FROM `{$wpdb->base_prefix}options` WHERE option_name LIKE %s;", $wpdb->esc_like( $this->option_prefix ) . '%' ), ARRAY_A );
0 ignored issues
show
Bug introduced by
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
169
170
		foreach ( $plugin_options as $option ) {
171
			delete_option( $option['option_name'] );
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
			/** @scrutinizer ignore-call */ 
172
   delete_option( $option['option_name'] );
Loading history...
172
		}
173
	}
174
175
}
176