Completed
Push — master ( 48bc46...861ddb )
by Jonathan
04:46 queued 34s
created

Object_Sync_Sf_Deactivate::clear_schedule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 8
rs 10
c 2
b 2
f 0
1
<?php
2
/**
3
 * Class file for the Object_Sync_Sf_Deactivate class.
4
 *
5
 * @file
6
 */
7
8
if ( ! class_exists( 'Object_Sync_Salesforce' ) ) {
9
	die();
10
}
11
12
/**
13
 * What to do when the plugin is deactivated
14
 */
15
class Object_Sync_Sf_Deactivate {
16
17
	protected $wpdb;
18
	protected $version;
19
	protected $slug;
20
	protected $schedulable_classes;
21
	protected $option_prefix;
22
	protected $queue;
23
24
	/**
25
	* Constructor which sets up deactivate hooks
26
	* @param object $wpdb
27
	* @param string $version
28
	* @param string $slug
29
	* @param array $schedulable_classes
30
	* @param string $option_prefix
31
	* @param object $queue
32
	*
33
	*/
34
	public function __construct( $wpdb, $version, $slug, $schedulable_classes, $option_prefix = '', $queue = '' ) {
35
		$this->wpdb                = $wpdb;
36
		$this->version             = $version;
37
		$this->slug                = $slug;
38
		$this->option_prefix       = isset( $option_prefix ) ? $option_prefix : 'object_sync_for_salesforce_';
39
		$this->schedulable_classes = $schedulable_classes;
40
		$this->queue               = $queue;
41
42
		$this->action_group_suffix = '_check_records';
0 ignored issues
show
Bug Best Practice introduced by
The property action_group_suffix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
		$delete_data               = (int) get_option( $this->option_prefix . 'delete_data_on_uninstall', 0 );
44
		if ( 1 === $delete_data ) {
45
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'wordpress_salesforce_drop_tables' ) );
46
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'clear_schedule' ) );
47
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'delete_log_post_type' ) );
48
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'remove_roles_capabilities' ) );
49
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'flush_plugin_cache' )
50
			);
51
			register_deactivation_hook( dirname( __DIR__ ) . '/' . $slug . '.php', array( $this, 'delete_plugin_options' ) );
52
		}
53
	}
54
55
	/**
56
	* Drop database tables for Salesforce
57
	* This removes the tables for fieldmaps (between types of objects) and object maps (between indidual instances of objects)
58
	*
59
	*/
60
	public function wordpress_salesforce_drop_tables() {
61
		$field_map_table  = $this->wpdb->prefix . 'object_sync_sf_field_map';
62
		$object_map_table = $this->wpdb->prefix . 'object_sync_sf_object_map';
63
		$this->wpdb->query( 'DROP TABLE IF EXISTS ' . $field_map_table );
64
		$this->wpdb->query( 'DROP TABLE IF EXISTS ' . $object_map_table );
65
		delete_option( $this->option_prefix . 'db_version' );
66
	}
67
68
	/**
69
	* Clear the scheduled tasks
70
	* This removes all the scheduled tasks that are included in the plugin's $schedulable_classes array
71
	*
72
	*/
73
	public function clear_schedule() {
74
		if ( '' === $this->queue ) {
75
			return;
76
		}
77
		foreach ( $this->schedulable_classes as $key => $value ) {
78
			$schedule_name     = $key;
79
			$action_group_name = $schedule_name . $this->action_group_suffix;
80
			$this->queue->cancel( $action_group_name );
81
		}
82
	}
83
84
	/**
85
	* Delete the log post type
86
	* This removes the log post type
87
	*
88
	*/
89
	public function delete_log_post_type() {
90
		unregister_post_type( 'wp_log' );
91
	}
92
93
	/**
94
	* Remove roles and capabilities
95
	* This removes the configure_salesforce capability from the admin role
96
	*
97
	* It also allows other plugins to remove the capability from other roles
98
	*
99
	*/
100
	public function remove_roles_capabilities() {
101
102
		// by default, only administrators can configure the plugin
103
		$role = get_role( 'administrator' );
104
		$role->remove_cap( 'configure_salesforce' );
105
106
		// hook that allows other roles to configure the plugin as well
107
		$roles = apply_filters( $this->option_prefix . 'roles_configure_salesforce', null );
108
109
		// for each role that we have, remove the configure salesforce capability
110
		if ( null !== $roles ) {
111
			foreach ( $roles as $role ) {
112
				$role->remove_cap( 'configure_salesforce' );
113
			}
114
		}
115
116
	}
117
118
	/**
119
	* Flush the plugin cache
120
	*
121
	*/
122
	public function flush_plugin_cache() {
123
		$sfwp_transients = new Object_Sync_Sf_WordPress_Transient( 'sfwp_transients' );
124
		$sfwp_transients->flush();
125
	}
126
127
	/**
128
	* Clear the plugin options
129
	*
130
	*/
131
	public function delete_plugin_options() {
132
		$table          = $this->wpdb->prefix . 'options';
133
		$plugin_options = $this->wpdb->get_results( 'SELECT option_name FROM ' . $table . ' WHERE option_name LIKE "object_sync_for_salesforce_%"', ARRAY_A );
134
		foreach ( $plugin_options as $option ) {
135
			delete_option( $option['option_name'] );
136
		}
137
	}
138
139
}
140