Skip to content

Commit a82dfcf

Browse files
Merge a9e0af2 into 7be4c64
2 parents 7be4c64 + a9e0af2 commit a82dfcf

7 files changed

+166
-48
lines changed

classes/class-object-sync-sf-activate.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,54 @@ public function wordpress_salesforce_update_db_check() {
242242

243243
// user is running a version less than the current one.
244244
if ( version_compare( $this->user_installed_version, $this->version, '<' ) ) {
245+
$this->log_trigger_settings();
245246
$this->wordpress_salesforce_tables();
246247
} else {
247248
return true;
248249
}
249250
}
250251

252+
/**
253+
* Check for log trigger settings based on plugin version
254+
* 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.
255+
*/
256+
private function log_trigger_settings() {
257+
$triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() );
258+
if ( ! empty( $triggers_to_log ) ) {
259+
// in version 2.0.0 of this plugin, we replaced the bit flags with strings to make them more legible.
260+
// when the database version changes to 2.0.0, we should update the option value to use the new strings.
261+
if ( version_compare( $this->version, '2.0.0', '==' ) && version_compare( $this->user_installed_version, '2.0.0', '<' ) ) {
262+
$mappings = new Object_Sync_Sf_Mapping();
263+
$updated_triggers_to_log = array();
264+
foreach ( $triggers_to_log as $key => $value ) {
265+
if ( (string) $value === (string) $mappings->sync_off_v1 ) {
266+
$updated_triggers_to_log[] = $mappings->sync_off;
267+
}
268+
if ( (string) $value === (string) $mappings->sync_wordpress_create_v1 ) {
269+
$updated_triggers_to_log[] = $mappings->sync_wordpress_create;
270+
}
271+
if ( (string) $value === (string) $mappings->sync_wordpress_update_v1 ) {
272+
$updated_triggers_to_log[] = $mappings->sync_wordpress_update;
273+
}
274+
if ( (string) $value === (string) $mappings->sync_wordpress_delete_v1 ) {
275+
$updated_triggers_to_log[] = $mappings->sync_wordpress_delete;
276+
}
277+
if ( (string) $value === (string) $mappings->sync_sf_create_v1 ) {
278+
$updated_triggers_to_log[] = $mappings->sync_sf_create;
279+
}
280+
if ( (string) $value === (string) $mappings->sync_sf_update_v1 ) {
281+
$updated_triggers_to_log[] = $mappings->sync_sf_update;
282+
}
283+
if ( (string) $value === (string) $mappings->sync_sf_delete_v1 ) {
284+
$updated_triggers_to_log[] = $mappings->sync_sf_delete;
285+
}
286+
}
287+
$triggers_to_log = $updated_triggers_to_log;
288+
update_option( $this->option_prefix . 'triggers_to_log', $triggers_to_log );
289+
}
290+
}
291+
}
292+
251293
/**
252294
* Check whether the user has action scheduler tasks when they upgrade
253295
*

classes/class-object-sync-sf-admin.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,19 +2363,25 @@ public function clear_sfwp_cache() {
23632363
* @return array
23642364
*/
23652365
private function clear_cache( $ajax = false ) {
2366-
$result = (bool) $this->wordpress->sfwp_transients->flush();
2367-
if ( true === $result ) {
2368-
$message = __( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
2366+
$result = $this->wordpress->sfwp_transients->flush();
2367+
$success = $result['success'];
2368+
if ( 0 < $result['count'] ) {
2369+
if ( true === $success ) {
2370+
$message = esc_html__( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
2371+
} else {
2372+
$message = esc_html__( 'There was an error clearing the plugin cache. Try refreshing this page.', 'object-sync-for-salesforce' );
2373+
}
23692374
} else {
2370-
$message = __( 'There was an error clearing the plugin cache. Try refreshing this page.', 'object-sync-for-salesforce' );
2375+
$success = true;
2376+
$message = esc_html__( 'The cache was not cleared because it is empty. You can try again later.', 'object-sync-for-salesforce' );
23712377
}
23722378
if ( false === $ajax ) {
23732379
// translators: parameter 1 is the result message.
23742380
echo sprintf( '<p>%1$s</p>', $message );
23752381
} else {
23762382
return array(
23772383
'message' => $message,
2378-
'success' => $result,
2384+
'success' => $success,
23792385
);
23802386
}
23812387
}

classes/class-object-sync-sf-logging.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function setup( $title_or_params, $message = '', $trigger = 0, $parent =
488488
$triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() );
489489
// if we force strict on this in_array, it fails because the mapping triggers are bit operators, as indicated in Object_Sync_Sf_Mapping class's method __construct().
490490
// todo: make this not use bit operators so we can use a strict in_array.
491-
if ( in_array( $trigger, maybe_unserialize( $triggers_to_log ) ) || 0 === $trigger ) {
491+
if ( in_array( $trigger, maybe_unserialize( $triggers_to_log ), true ) || 0 === $trigger ) {
492492
$this->add( $title, $message, $parent );
493493
} elseif ( is_array( $trigger ) && array_intersect( $trigger, maybe_unserialize( $triggers_to_log ) ) ) {
494494
$this->add( $title, $message, $parent );

classes/class-object-sync-sf-mapping.php

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,32 @@ public function __construct() {
248248
/*
249249
* These parameters are how we define when syncing should occur on each field map.
250250
* They get used in the admin settings, as well as the push/pull methods to see if something should happen.
251-
* It is unclear why the Drupal module used bit flags, but it seems reasonable to keep the convention.
252251
*/
253-
$this->sync_off = 0x0000;
254-
$this->sync_wordpress_create = 0x0001;
255-
$this->sync_wordpress_update = 0x0002;
256-
$this->sync_wordpress_delete = 0x0004;
257-
$this->sync_sf_create = 0x0008;
258-
$this->sync_sf_update = 0x0010;
259-
$this->sync_sf_delete = 0x0020;
252+
$this->sync_off = 'off';
253+
$this->sync_wordpress_create = 'wp_create';
254+
$this->sync_wordpress_update = 'wp_update';
255+
$this->sync_wordpress_delete = 'wp_delete';
256+
$this->sync_sf_create = 'sf_create';
257+
$this->sync_sf_update = 'sf_update';
258+
$this->sync_sf_delete = 'sf_delete';
259+
260+
// deprecated bit flags from version 1.x.
261+
$this->sync_off_v1 = 0x0000;
262+
$this->sync_wordpress_create_v1 = 0x0001;
263+
$this->sync_wordpress_update_v1 = 0x0002;
264+
$this->sync_wordpress_delete_v1 = 0x0004;
265+
$this->sync_sf_create_v1 = 0x0008;
266+
$this->sync_sf_update_v1 = 0x0010;
267+
$this->sync_sf_delete_v1 = 0x0020;
260268

261269
// Define which events are initialized by which system.
262270
$this->wordpress_events = array( $this->sync_wordpress_create, $this->sync_wordpress_update, $this->sync_wordpress_delete );
263271
$this->salesforce_events = array( $this->sync_sf_create, $this->sync_sf_update, $this->sync_sf_delete );
264272

273+
// deprecated bit flags from version 1.x.
274+
$this->wordpress_events_v1 = array( $this->sync_wordpress_create_v1, $this->sync_wordpress_update_v1, $this->sync_wordpress_delete_v1 );
275+
$this->salesforce_events_v1 = array( $this->sync_sf_create_v1, $this->sync_sf_update_v1, $this->sync_sf_delete_v1 );
276+
265277
// Constants for the directions to map things.
266278
$this->direction_wordpress_sf = 'wp_sf';
267279
$this->direction_sf_wordpress = 'sf_wp';
@@ -325,11 +337,11 @@ public function create_fieldmap( $posted = array(), $wordpress_fields = array(),
325337
public function get_fieldmaps( $id = null, $conditions = array(), $reset = false ) {
326338
$table = $this->fieldmap_table;
327339
if ( null !== $id ) { // get one fieldmap.
328-
$map = $this->wpdb->get_row( 'SELECT * FROM ' . $table . ' WHERE id = ' . $id, ARRAY_A );
329-
$map['salesforce_record_types_allowed'] = maybe_unserialize( $map['salesforce_record_types_allowed'] );
330-
331-
$map['fields'] = maybe_unserialize( $map['fields'] );
332-
$map['sync_triggers'] = maybe_unserialize( $map['sync_triggers'] );
340+
$map = $this->wpdb->get_row( 'SELECT * FROM ' . $table . ' WHERE id = ' . $id, ARRAY_A );
341+
$mappings = array();
342+
$mappings[] = $map;
343+
$mappings = $this->prepare_fieldmap_data( $mappings );
344+
$map = $mappings[0];
333345
return $map;
334346
} elseif ( ! empty( $conditions ) ) { // get multiple but with a limitation.
335347
$mappings = array();
@@ -1178,6 +1190,62 @@ private function prepare_fieldmap_data( $mappings, $record_type = '' ) {
11781190
if ( '' !== $record_type && ! in_array( $record_type, $mappings[ $id ]['salesforce_record_types_allowed'], true ) ) {
11791191
unset( $mappings[ $id ] );
11801192
}
1193+
1194+
// in v2 of this plugin, we replaced the bit flags with strings to make them more legible.
1195+
if ( version_compare( $this->version, '2.0.0', '>=' ) ) {
1196+
1197+
$sync_triggers = $mappings[ $id ]['sync_triggers'];
1198+
1199+
// check if the triggers stored in the database are up to date. if not, update them.
1200+
$intersect = array_intersect( $sync_triggers, array_merge( $this->wordpress_events, $this->salesforce_events ) );
1201+
1202+
if ( empty( $intersect ) ) {
1203+
$updated_sync_triggers = array();
1204+
1205+
foreach ( $sync_triggers as $key => $value ) {
1206+
if ( $value === (string) $this->sync_off_v1 ) {
1207+
$updated_sync_triggers[] = $this->sync_off;
1208+
}
1209+
if ( $value === (string) $this->sync_wordpress_create_v1 ) {
1210+
$updated_sync_triggers[] = $this->sync_wordpress_create;
1211+
}
1212+
if ( $value === (string) $this->sync_wordpress_update_v1 ) {
1213+
$updated_sync_triggers[] = $this->sync_wordpress_update;
1214+
}
1215+
if ( $value === (string) $this->sync_wordpress_delete_v1 ) {
1216+
$updated_sync_triggers[] = $this->sync_wordpress_delete;
1217+
}
1218+
if ( $value === (string) $this->sync_sf_create_v1 ) {
1219+
$updated_sync_triggers[] = $this->sync_sf_create;
1220+
}
1221+
if ( $value === (string) $this->sync_sf_update_v1 ) {
1222+
$updated_sync_triggers[] = $this->sync_sf_update;
1223+
}
1224+
if ( $value === (string) $this->sync_sf_delete_v1 ) {
1225+
$updated_sync_triggers[] = $this->sync_sf_delete;
1226+
}
1227+
}
1228+
$mappings[ $id ]['sync_triggers'] = maybe_unserialize( $updated_sync_triggers );
1229+
1230+
$data = array();
1231+
if ( ! empty( $mappings[ $id ]['sync_triggers'] ) ) {
1232+
$data['sync_triggers'] = array();
1233+
foreach ( $mappings[ $id ]['sync_triggers'] as $key => $value ) {
1234+
$mappings[ $id ]['sync_triggers'][ $key ] = esc_html( $mappings[ $id ]['sync_triggers'][ $key ] );
1235+
}
1236+
$data['sync_triggers'] = maybe_serialize( $mappings[ $id ]['sync_triggers'] );
1237+
// update the sync triggers field to use the new variable name.
1238+
$update = $this->wpdb->update(
1239+
$this->fieldmap_table,
1240+
$data,
1241+
array(
1242+
'id' => $mappings[ $id ]['id'],
1243+
)
1244+
);
1245+
}
1246+
continue;
1247+
}
1248+
}
11811249
}
11821250

11831251
return $mappings;

classes/class-object-sync-sf-salesforce-pull.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,8 @@ private function get_updated_records() {
418418
$sf_sync_trigger = $this->mappings->sync_sf_update;
419419
}
420420

421-
// Only queue when the record's trigger is configured for the mapping
422-
// these are bit operators, so we leave out the strict.
423-
if ( isset( $map_sync_triggers ) && isset( $sf_sync_trigger ) && in_array( $sf_sync_trigger, $map_sync_triggers ) ) { // wp or sf crud event.
421+
// Only queue when the record's trigger is configured for the mapping.
422+
if ( isset( $map_sync_triggers ) && isset( $sf_sync_trigger ) && in_array( $sf_sync_trigger, $map_sync_triggers, true ) ) { // wp or sf crud event.
424423
$data = array(
425424
'object_type' => $type,
426425
'object' => $result,
@@ -675,9 +674,8 @@ private function get_next_record_batch( $last_sync, $salesforce_mapping, $map_sy
675674
} else {
676675
$sf_sync_trigger = $this->mappings->sync_sf_update;
677676
}
678-
// Only queue when the record's trigger is configured for the mapping
679-
// these are bit operators, so we leave out the strict.
680-
if ( isset( $map_sync_triggers ) && isset( $sf_sync_trigger ) && in_array( $sf_sync_trigger, $map_sync_triggers ) ) { // wp or sf crud event.
677+
// Only queue when the record's trigger is configured for the mapping.
678+
if ( isset( $map_sync_triggers ) && isset( $sf_sync_trigger ) && in_array( $sf_sync_trigger, $map_sync_triggers, true ) ) { // wp or sf crud event.
681679
$data = array(
682680
'object_type' => $type,
683681
'object' => $result,
@@ -803,8 +801,8 @@ private function get_pull_query( $type, $salesforce_mapping = array() ) {
803801
)
804802
);
805803

806-
// these are bit operators, so we leave out the strict.
807-
if ( in_array( $this->mappings->sync_sf_create, $salesforce_mapping['sync_triggers'] ) ) {
804+
// check if this is a Salesforce create event.
805+
if ( in_array( $this->mappings->sync_sf_create, $salesforce_mapping['sync_triggers'], true ) ) {
808806
$soql->fields['CreatedDate'] = 'CreatedDate';
809807
}
810808

@@ -1144,7 +1142,7 @@ function( $x ) use ( $key ) {
11441142
$pull_allowed = true;
11451143

11461144
// if the current fieldmap does not allow delete, set pull_allowed to false.
1147-
if ( isset( $map_sync_triggers ) && ! in_array( $this->mappings->sync_sf_delete, $map_sync_triggers ) ) {
1145+
if ( isset( $map_sync_triggers ) && ! in_array( $this->mappings->sync_sf_delete, $map_sync_triggers, true ) ) {
11481146
$pull_allowed = false;
11491147
}
11501148

@@ -1244,7 +1242,7 @@ public function salesforce_pull_process_records( $object_type, $object, $sf_sync
12441242
$salesforce_id = $object;
12451243
// Load the Salesforce object data to save in WordPress. We need to make sure that this data does not get cached, which is consistent with other pull behavior as well as in other methods in this class.
12461244
// We should only do this if we're not trying to delete data in WordPress - otherwise, we'll get a 404 from Salesforce and the delete will fail.
1247-
if ( $sf_sync_trigger != $this->mappings->sync_sf_delete ) { // trigger is a bit operator.
1245+
if ( $sf_sync_trigger !== $this->mappings->sync_sf_delete ) {
12481246
$object = $sfapi->object_read(
12491247
$object_type,
12501248
$salesforce_id,
@@ -1420,7 +1418,7 @@ public function salesforce_pull_process_records( $object_type, $object, $sf_sync
14201418
$wordpress_id_field_name = $structure['id_field'];
14211419

14221420
// don't do parameters if we are deleting.
1423-
if ( ( true === $is_new && $sf_sync_trigger == $this->mappings->sync_sf_create ) || $sf_sync_trigger == $this->mappings->sync_sf_update ) { // trigger is a bit operator
1421+
if ( ( true === $is_new && $sf_sync_trigger === $this->mappings->sync_sf_create ) || $sf_sync_trigger === $this->mappings->sync_sf_update ) { // trigger is a bit operator
14241422
// map the Salesforce values to WordPress fields.
14251423
$params = $this->mappings->map_params( $salesforce_mapping, $object, $sf_sync_trigger, false, $is_new, $wordpress_id_field_name );
14261424

@@ -1452,7 +1450,7 @@ public function salesforce_pull_process_records( $object_type, $object, $sf_sync
14521450
if ( empty( $params ) ) {
14531451
return;
14541452
}
1455-
} elseif ( $sf_sync_trigger == $this->mappings->sync_sf_delete ) {
1453+
} elseif ( $sf_sync_trigger === $this->mappings->sync_sf_delete ) {
14561454
$is_new = false;
14571455
} // end checking for create/update/delete.
14581456

@@ -2088,7 +2086,7 @@ private function delete_called_from_salesforce( $sf_sync_trigger, $synced_object
20882086
$op = '';
20892087

20902088
// deleting mapped objects.
2091-
if ( $sf_sync_trigger == $this->mappings->sync_sf_delete ) { // trigger is a bit operator.
2089+
if ( $sf_sync_trigger === $this->mappings->sync_sf_delete ) { // trigger is a bit operator.
20922090
if ( isset( $mapping_object['id'] ) ) {
20932091

20942092
$op = 'Delete';
@@ -2314,7 +2312,7 @@ private function is_pull_allowed( $object_type, $object, $sf_sync_trigger, $sale
23142312
$pull_allowed = true;
23152313

23162314
// if the current fieldmap does not allow create, we need to check if there is an object map for the Salesforce object Id. if not, set pull_allowed to false.
2317-
if ( ! in_array( $this->mappings->sync_sf_create, $map_sync_triggers ) ) {
2315+
if ( ! in_array( $this->mappings->sync_sf_create, $map_sync_triggers, true ) ) {
23182316
$object_map = $this->mappings->load_all_by_salesforce( $object['Id'] );
23192317
if ( empty( $object_map ) ) {
23202318
$pull_allowed = false;

classes/class-object-sync-sf-salesforce-push.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ public function salesforce_push_sync_rest( $object_type, $object, $mapping, $sf_
724724
if ( is_int( $object ) ) {
725725
$wordpress_id = $object;
726726
// if this is NOT a deletion, try to get all of the object's data.
727-
if ( $sf_sync_trigger != $this->mappings->sync_wordpress_delete ) { // this is a bit flag.
727+
if ( $sf_sync_trigger !== $this->mappings->sync_wordpress_delete ) {
728728
$object = $this->wordpress->get_wordpress_object_data( $object_type, $wordpress_id );
729729
} else {
730730
// otherwise, flag it as a delete and limit what we try to get.
@@ -798,9 +798,8 @@ public function salesforce_push_sync_rest( $object_type, $object, $mapping, $sf_
798798
$op = '';
799799
$result = '';
800800

801-
// deleting mapped objects
802-
// these are bit operators, so we leave out the strict.
803-
if ( $sf_sync_trigger == $this->mappings->sync_wordpress_delete ) {
801+
// deleting mapped objects.
802+
if ( $sf_sync_trigger === $this->mappings->sync_wordpress_delete ) {
804803
if ( isset( $mapping_object['id'] ) ) {
805804
$op = 'Delete';
806805

@@ -1473,9 +1472,7 @@ private function is_push_allowed( $object_type, $object, $sf_sync_trigger, $mapp
14731472
$push_allowed = true;
14741473

14751474
// if the current fieldmap does not allow the wp create trigger, we need to check if there is an object map for the WordPress object ID. if not, set push_allowed to false.
1476-
// these are bit operators, so we leave out the strict.
1477-
// So don't ever put true in here even though WPCS complains.
1478-
if ( ! in_array( $this->mappings->sync_wordpress_create, $map_sync_triggers ) ) {
1475+
if ( ! in_array( $this->mappings->sync_wordpress_create, $map_sync_triggers, true ) ) {
14791476
$structure = $this->wordpress->get_wordpress_table_structure( $object_type );
14801477
$wordpress_id_field_name = $structure['id_field'];
14811478
$object_map = array();
@@ -1489,9 +1486,8 @@ private function is_push_allowed( $object_type, $object, $sf_sync_trigger, $mapp
14891486
}
14901487
}
14911488

1492-
// these are bit operators, so we leave out the strict.
1493-
// So don't ever put true in here even though WPCS complains.
1494-
if ( ! in_array( $sf_sync_trigger, $map_sync_triggers ) ) {
1489+
// check if this is a Salesforce sync trigger.
1490+
if ( ! in_array( $sf_sync_trigger, $map_sync_triggers, true ) ) {
14951491
$push_allowed = false;
14961492
}
14971493

classes/class-object-sync-sf-wordpress-transient.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,25 @@ public function delete( $cachekey ) {
9393
/**
9494
* Delete the entire cache for this plugin
9595
*
96-
* @return bool True if successful, false otherwise.
96+
* @return array $result has the success result and how many entries were cleared.
9797
*/
9898
public function flush() {
99-
$keys = $this->all_keys();
100-
$result = true;
99+
$keys = $this->all_keys();
100+
$success = true;
101+
$count = 0;
101102
if ( ! empty( $keys ) ) {
102103
foreach ( $keys as $key ) {
103-
$result = delete_transient( $key );
104+
$success = delete_transient( $key );
105+
$count++;
104106
}
105107
}
106-
$result = delete_transient( $this->name );
108+
$success = delete_transient( $this->name );
109+
if ( true === $success ) {
110+
$count++;
111+
}
112+
$result = array();
113+
$result['success'] = $success;
114+
$result['count'] = $count;
107115
return $result;
108116
}
109117

0 commit comments

Comments
 (0)