1 | <?php |
||||||||
2 | /** |
||||||||
3 | * What to do when the plugin is activated |
||||||||
4 | * |
||||||||
5 | * @class Object_Sync_Sf_Activate |
||||||||
6 | * @package Object_Sync_Salesforce |
||||||||
7 | */ |
||||||||
8 | |||||||||
9 | defined( 'ABSPATH' ) || exit; |
||||||||
10 | |||||||||
11 | /** |
||||||||
12 | * Object_Sync_Sf_Activate class. |
||||||||
13 | */ |
||||||||
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() { |
||||||||
83 | $this->version = object_sync_for_salesforce()->version; |
||||||||
84 | $this->file = object_sync_for_salesforce()->file; |
||||||||
85 | $this->wpdb = object_sync_for_salesforce()->wpdb; |
||||||||
86 | $this->slug = object_sync_for_salesforce()->slug; |
||||||||
87 | $this->option_prefix = object_sync_for_salesforce()->option_prefix; |
||||||||
88 | $this->action_group_suffix = object_sync_for_salesforce()->action_group_suffix; |
||||||||
89 | |||||||||
90 | $this->schedulable_classes = object_sync_for_salesforce()->schedulable_classes; |
||||||||
91 | $this->queue = object_sync_for_salesforce()->queue; |
||||||||
92 | |||||||||
93 | // database version. |
||||||||
94 | $this->user_installed_version = get_option( $this->option_prefix . 'db_version', '' ); |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
95 | |||||||||
96 | $this->add_actions(); |
||||||||
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' ) ); |
||||||||
0 ignored issues
–
show
The function
register_activation_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
![]() |
|||||||||
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 ); |
||||||||
0 ignored issues
–
show
The function
add_action 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
![]() |
|||||||||
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__ ) ); |
||||||||
0 ignored issues
–
show
The function
plugin_basename 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
![]() The function
deactivate_plugins 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
![]() |
|||||||||
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.' ); |
||||||||
0 ignored issues
–
show
The function
wp_die 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
![]() |
|||||||||
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() ) { |
||||||||
0 ignored issues
–
show
The function
is_admin 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
![]() |
|||||||||
187 | return false; |
||||||||
188 | } |
||||||||
189 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
||||||||
0 ignored issues
–
show
|
|||||||||
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() ); |
||||||||
0 ignored issues
–
show
The function
update_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
![]() |
|||||||||
208 | |||||||||
209 | // utf8mb4 conversion. |
||||||||
210 | maybe_convert_table_to_utf8mb4( $field_map_table ); |
||||||||
0 ignored issues
–
show
The function
maybe_convert_table_to_utf8mb4 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
![]() |
|||||||||
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' ); |
||||||||
0 ignored issues
–
show
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
![]() |
|||||||||
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 ); |
||||||||
0 ignored issues
–
show
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
![]() |
|||||||||
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() ); |
||||||||
0 ignored issues
–
show
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
![]() |
|||||||||
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 ); |
||||||||
0 ignored issues
–
show
The function
update_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
![]() |
|||||||||
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 ) { |
||||||||
0 ignored issues
–
show
The parameter
$upgrader_object is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||||
314 | |||||||||
315 | // skip if this action isn't this plugin being updated. |
||||||||
316 | if ( 'plugin' !== $hook_extra['type'] && 'update' !== $hook_extra['action'] && $hook_extra['plugin'] !== $this->slug ) { |
||||||||
317 | return; |
||||||||
318 | } |
||||||||
319 | |||||||||
320 | // user is running a version of this plugin that is less than 1.4.0. |
||||||||
321 | $action_scheduler_version = '1.4.0'; |
||||||||
322 | $previous_version = get_transient( $this->option_prefix . 'installed_version' ); |
||||||||
0 ignored issues
–
show
The function
get_transient 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
![]() |
|||||||||
323 | if ( version_compare( $previous_version, $action_scheduler_version, '<' ) ) { |
||||||||
324 | // delete old options. |
||||||||
325 | delete_option( $this->option_prefix . 'push_schedule_number' ); |
||||||||
0 ignored issues
–
show
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
![]() |
|||||||||
326 | delete_option( $this->option_prefix . 'push_schedule_unit' ); |
||||||||
327 | delete_option( $this->option_prefix . 'salesforce_schedule_number' ); |
||||||||
328 | delete_option( $this->option_prefix . 'salesforce_schedule_unit' ); |
||||||||
329 | if ( '' === $this->queue ) { |
||||||||
0 ignored issues
–
show
|
|||||||||
330 | delete_transient( $this->option_prefix . 'installed_version' ); |
||||||||
0 ignored issues
–
show
The function
delete_transient 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
![]() |
|||||||||
331 | return; |
||||||||
332 | } |
||||||||
333 | foreach ( $this->schedulable_classes as $key => $schedule ) { |
||||||||
334 | $schedule_name = $key; |
||||||||
335 | $action_group_name = $schedule_name . $this->action_group_suffix; |
||||||||
336 | // exit if there is no initializer property on this schedule. |
||||||||
337 | if ( ! isset( $this->schedulable_classes[ $schedule_name ]['initializer'] ) ) { |
||||||||
338 | continue; |
||||||||
339 | } |
||||||||
340 | // create new recurring task for ActionScheduler to check for data to pull from Salesforce. |
||||||||
341 | $this->queue->schedule_recurring( |
||||||||
342 | time(), // plugin seems to expect UTC. |
||||||||
343 | $this->queue->get_frequency( $schedule_name, 'seconds' ), |
||||||||
344 | $this->schedulable_classes[ $schedule_name ]['initializer'], |
||||||||
345 | array(), |
||||||||
346 | $action_group_name |
||||||||
347 | ); |
||||||||
348 | } |
||||||||
349 | delete_transient( $this->option_prefix . 'installed_version' ); |
||||||||
350 | } |
||||||||
351 | } |
||||||||
352 | |||||||||
353 | } |
||||||||
354 |