Total Complexity | 40 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
Complex classes like Object_Sync_Sf_Activate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Object_Sync_Sf_Activate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Object_Sync_Sf_Activate { |
||
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 | * The version of this plugin's database setup |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | public $user_installed_version; |
||
78 | |||
79 | /** |
||
80 | * Constructor for activate class |
||
81 | */ |
||
82 | public function __construct() { |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Activation hooks |
||
101 | */ |
||
102 | private function add_actions() { |
||
103 | |||
104 | // on initial activation, run these hooks. |
||
105 | register_activation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'php_requirements' ) ); |
||
106 | // to maybe add later: |
||
107 | // register_activation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'require_ssl' ) ); |
||
108 | // if we determine we need to check for SSL on activation. |
||
109 | register_activation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'wordpress_salesforce_tables' ) ); |
||
110 | register_activation_hook( dirname( __DIR__ ) . '/' . $this->slug . '.php', array( $this, 'add_roles_capabilities' ) ); |
||
111 | |||
112 | // this should run when the user is in the admin area to make sure the database gets updated. |
||
113 | add_action( 'admin_init', array( $this, 'wordpress_salesforce_update_db_check' ), 10 ); |
||
114 | |||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Check for the minimum required version of php |
||
119 | */ |
||
120 | public function php_requirements() { |
||
121 | if ( version_compare( PHP_VERSION, '5.6.20', '<' ) ) { |
||
122 | deactivate_plugins( plugin_basename( __FILE__ ) ); |
||
123 | wp_die( '<strong>This plugin requires PHP Version 5.6.20</strong> <br />Please contact your host to upgrade PHP on your server, and then retry activating the plugin.' ); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Require SSL because otherwise the plugin will not authorize |
||
129 | */ |
||
130 | public function require_ssl() { |
||
131 | // although we might instead have to run this on plugin initalization rather than activation. |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Create database tables for Salesforce |
||
136 | * This creates tables for fieldmaps (between types of objects) and object maps (between individual instances of objects). |
||
137 | * Important requirement for developers: when you update the SQL for either of these tables, also update it in the documentation file located at /docs/troubleshooting-unable-to-create-database-tables.md and make sure that is included in your commit(s). |
||
138 | */ |
||
139 | public function wordpress_salesforce_tables() { |
||
140 | |||
141 | $charset_collate = $this->wpdb->get_charset_collate(); |
||
142 | |||
143 | $field_map_table = $this->wpdb->prefix . 'object_sync_sf_field_map'; |
||
144 | $field_map_sql = "CREATE TABLE $field_map_table ( |
||
145 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
||
146 | label varchar(64) NOT NULL DEFAULT '', |
||
147 | name varchar(64) NOT NULL DEFAULT '', |
||
148 | fieldmap_status varchar(10) NOT NULL DEFAULT 'active', |
||
149 | wordpress_object varchar(128) NOT NULL DEFAULT '', |
||
150 | salesforce_object varchar(255) NOT NULL DEFAULT '', |
||
151 | salesforce_record_types_allowed longblob, |
||
152 | salesforce_record_type_default varchar(255) NOT NULL DEFAULT '', |
||
153 | fields longtext NOT NULL, |
||
154 | pull_trigger_field varchar(128) NOT NULL DEFAULT 'LastModifiedDate', |
||
155 | sync_triggers text NOT NULL, |
||
156 | push_async tinyint(1) NOT NULL DEFAULT '0', |
||
157 | push_drafts tinyint(1) NOT NULL DEFAULT '0', |
||
158 | pull_to_drafts tinyint(1) NOT NULL DEFAULT '0', |
||
159 | always_delete_object_maps_on_delete tinyint(1) NOT NULL DEFAULT '0', |
||
160 | weight tinyint(1) NOT NULL DEFAULT '0', |
||
161 | version varchar(255) NOT NULL DEFAULT '', |
||
162 | PRIMARY KEY (id), |
||
163 | UNIQUE KEY name (name), |
||
164 | KEY name_sf_type_wordpress_type (wordpress_object,salesforce_object), |
||
165 | KEY fieldmap_status (fieldmap_status) |
||
166 | ) ENGINE=InnoDB $charset_collate"; |
||
167 | |||
168 | $object_map_table = $this->wpdb->prefix . 'object_sync_sf_object_map'; |
||
169 | $object_map_sql = "CREATE TABLE $object_map_table ( |
||
170 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
||
171 | wordpress_id varchar(32) NOT NULL, |
||
172 | salesforce_id varbinary(32) NOT NULL DEFAULT '', |
||
173 | wordpress_object varchar(128) NOT NULL DEFAULT '', |
||
174 | created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
175 | object_updated datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
176 | last_sync datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
177 | last_sync_action varchar(128) DEFAULT NULL, |
||
178 | last_sync_status tinyint(1) NOT NULL DEFAULT '0', |
||
179 | last_sync_message varchar(255) DEFAULT NULL, |
||
180 | PRIMARY KEY (id), |
||
181 | KEY wordpress_object (wordpress_object,wordpress_id), |
||
182 | KEY salesforce_object (salesforce_id) |
||
183 | ) $charset_collate"; |
||
184 | |||
185 | if ( ! function_exists( 'dbDelta' ) ) { |
||
186 | if ( ! is_admin() ) { |
||
187 | return false; |
||
188 | } |
||
189 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
||
190 | } |
||
191 | |||
192 | // Note: see https://wordpress.stackexchange.com/questions/67345/how-to-implement-wordpress-plugin-update-that-modifies-the-database |
||
193 | // When we run the dbDelta method below, "it checks if the table exists. What's more, it checks the column types. So if the table doesn't exist, it creates it, if it does, but some column types have changed it updates them, and if a column doesn't exists - it adds it." |
||
194 | // This does not remove columns if we remove columns, so we'll need to expand beyond this in the future if that happens, although I think the schema is pretty solid now. |
||
195 | $result_field_map = dbDelta( $field_map_sql ); |
||
196 | $result_object_map = dbDelta( $object_map_sql ); |
||
197 | |||
198 | $remove_key_version = '1.8.0'; |
||
199 | if ( '' !== $this->user_installed_version && version_compare( $this->user_installed_version, $remove_key_version, '<' ) ) { |
||
200 | $wpdb = $this->wpdb; |
||
201 | $wpdb->query( $wpdb->prepare( 'ALTER TABLE %s DROP INDEX salesforce', $object_map_table ) ); |
||
202 | $wpdb->query( $wpdb->prepare( 'ALTER TABLE %s DROP INDEX salesforce_wordpress', $object_map_table ) ); |
||
203 | $result_key = true; |
||
204 | } |
||
205 | |||
206 | // store right now as the time for the plugin's activation. |
||
207 | update_option( $this->option_prefix . 'activate_time', time() ); |
||
208 | |||
209 | // utf8mb4 conversion. |
||
210 | maybe_convert_table_to_utf8mb4( $field_map_table ); |
||
211 | maybe_convert_table_to_utf8mb4( $object_map_table ); |
||
212 | |||
213 | if ( '' === $this->user_installed_version || version_compare( $this->user_installed_version, $this->version, '<' ) ) { |
||
214 | update_option( $this->option_prefix . 'db_version', $this->version ); |
||
215 | } |
||
216 | |||
217 | if ( ! isset( $result_key ) && empty( $result_field_map ) && empty( $result_object_map ) ) { |
||
218 | // No changes, database already exists and is up-to-date. |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Add roles and capabilities |
||
226 | * This adds the configure_salesforce capability to the admin role |
||
227 | * |
||
228 | * It also allows other plugins to add the capability to other roles |
||
229 | */ |
||
230 | public function add_roles_capabilities() { |
||
231 | |||
232 | // by default, only administrators can configure the plugin. |
||
233 | $role = get_role( 'administrator' ); |
||
234 | $role->add_cap( 'configure_salesforce' ); |
||
235 | |||
236 | // hook that allows other roles to configure the plugin as well. |
||
237 | $roles = apply_filters( $this->option_prefix . 'roles_configure_salesforce', null ); |
||
238 | |||
239 | // for each role that we have, give it the configure salesforce capability. |
||
240 | if ( null !== $roles ) { |
||
241 | foreach ( $roles as $role ) { |
||
242 | $role = get_role( $role ); |
||
243 | $role->add_cap( 'configure_salesforce' ); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Check for database version |
||
251 | * When the plugin is loaded in the admin, if the database version does not match the current version, perform these methods |
||
252 | */ |
||
253 | public function wordpress_salesforce_update_db_check() { |
||
254 | |||
255 | // user is running a version less than the current one. |
||
256 | if ( version_compare( $this->user_installed_version, $this->version, '<' ) ) { |
||
257 | $this->log_trigger_settings(); |
||
258 | $this->wordpress_salesforce_tables(); |
||
259 | } else { |
||
260 | return true; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Check for log trigger settings based on plugin version |
||
266 | * When the plugin is loaded in the admin, if the previously installed version is below 2.0.0, update the values for the log trigger settings. |
||
267 | */ |
||
268 | private function log_trigger_settings() { |
||
269 | $triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() ); |
||
270 | if ( ! empty( $triggers_to_log ) ) { |
||
271 | // in version 2.0.0 of this plugin, we replaced the bit flags with strings to make them more legible. |
||
272 | // when the database version changes to 2.0.0, we should update the option value to use the new strings. |
||
273 | if ( version_compare( $this->version, '2.0.0', '==' ) && version_compare( $this->user_installed_version, '2.0.0', '<' ) ) { |
||
274 | $mappings = new Object_Sync_Sf_Mapping(); |
||
275 | $updated_triggers_to_log = array(); |
||
276 | foreach ( $triggers_to_log as $key => $value ) { |
||
277 | if ( (string) $value === (string) $mappings->sync_off_v1 ) { |
||
278 | $updated_triggers_to_log[] = $mappings->sync_off; |
||
279 | } |
||
280 | if ( (string) $value === (string) $mappings->sync_wordpress_create_v1 ) { |
||
281 | $updated_triggers_to_log[] = $mappings->sync_wordpress_create; |
||
282 | } |
||
283 | if ( (string) $value === (string) $mappings->sync_wordpress_update_v1 ) { |
||
284 | $updated_triggers_to_log[] = $mappings->sync_wordpress_update; |
||
285 | } |
||
286 | if ( (string) $value === (string) $mappings->sync_wordpress_delete_v1 ) { |
||
287 | $updated_triggers_to_log[] = $mappings->sync_wordpress_delete; |
||
288 | } |
||
289 | if ( (string) $value === (string) $mappings->sync_sf_create_v1 ) { |
||
290 | $updated_triggers_to_log[] = $mappings->sync_sf_create; |
||
291 | } |
||
292 | if ( (string) $value === (string) $mappings->sync_sf_update_v1 ) { |
||
293 | $updated_triggers_to_log[] = $mappings->sync_sf_update; |
||
294 | } |
||
295 | if ( (string) $value === (string) $mappings->sync_sf_delete_v1 ) { |
||
296 | $updated_triggers_to_log[] = $mappings->sync_sf_delete; |
||
297 | } |
||
298 | } |
||
299 | $triggers_to_log = $updated_triggers_to_log; |
||
300 | update_option( $this->option_prefix . 'triggers_to_log', $triggers_to_log ); |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Check whether the user has ActionScheduler tasks when they upgrade |
||
307 | * |
||
308 | * @param object $upgrader_object this is the WP_Upgrader object. |
||
309 | * @param array $hook_extra the array of bulk item update data. |
||
310 | * @see https://developer.wordpress.org/reference/hooks/upgrader_process_complete/ |
||
311 | * @deprecated since 2.0.0 and will be removed in version 3.0.0. |
||
312 | */ |
||
313 | public function check_for_action_scheduler( $upgrader_object, $hook_extra ) { |
||
350 | } |
||
351 | } |
||
352 | |||
353 | } |
||
354 |