-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minnpost-salesforce.php
1017 lines (924 loc) · 34.1 KB
/
minnpost-salesforce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: MinnPost Salesforce
Plugin URI:
Description:
Version: 0.0.10
Author: Jonathan Stegall
Author URI: https://code.minnpost.com
License: GPL2+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: minnpost-salesforce
*/
// Start up the plugin
class Minnpost_Salesforce {
/**
* @var string
*/
private $version;
/**
* @var object
*/
public $salesforce;
/**
* This is our constructor
*
* @return void
*/
public function __construct() {
$this->version = '0.0.10';
$this->admin_init();
$this->init();
register_activation_hook( __FILE__, array( $this, 'add_user_fields' ) );
register_activation_hook( __FILE__, array( $this, 'add_roles_capabilities' ) );
register_deactivation_hook( __FILE__, array( $this, 'remove_roles_capabilities' ) );
}
/**
* admin start
*
* @throws \Exception
*/
private function admin_init() {
add_action( 'admin_init', array( $this, 'salesforce' ) );
add_action( 'admin_init', array( $this, 'minnpost_salesforce_settings_forms' ) );
}
public function add_user_fields() {
add_user_meta( 1, 'member_level', '' );
}
/**
* start
*
* @throws \Exception
*/
private function init() {
add_filter( 'object_sync_for_salesforce_find_sf_object_match', array( $this, 'find_sf_object_match' ), 10, 4 );
add_filter( 'object_sync_for_salesforce_push_object_allowed', array( $this, 'push_not_allowed' ), 10, 5 );
add_filter( 'object_sync_for_salesforce_pull_query_modify', array( $this, 'pull_query_modify' ), 10, 4 );
add_filter( 'object_sync_for_salesforce_settings_tabs', array( $this, 'minnpost_tabs' ), 10, 1 );
add_action( 'object_sync_for_salesforce_push_success', array( $this, 'push_member_level' ), 10, 5 );
add_filter( 'object_sync_for_salesforce_push_update_params_modify', array( $this, 'set_fields_if_missing' ), 10, 5 );
add_action( 'object_sync_for_salesforce_pre_pull', array( $this, 'pull_member_level' ), 10, 5 );
add_filter( 'user_account_management_custom_error_message', array( $this, 'login_fail_check' ), 10, 3 );
add_filter( 'minnpost_membership_get_active_recurring_donations', array( $this, 'get_active_recurring_donations' ), 10, 5 );
add_filter( 'minnpost_membership_get_pledged_opportunities', array( $this, 'get_pledged_opportunities' ), 10, 7 );
add_filter( 'minnpost_membership_get_failed_opportunities', array( $this, 'get_failed_opportunities' ), 10, 10 );
add_filter( 'minnpost_membership_get_successful_opportunities', array( $this, 'get_successful_opportunities' ), 10, 4 );
add_filter( 'minnpost_membership_get_member_level', array( $this, 'get_member_level' ), 10, 3 );
}
/**
* Load the Salesforce object
* Also make it available to this whole class
*
* @return $this->salesforce
*/
public function salesforce() {
// get the base class
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
if ( is_plugin_active( 'object-sync-for-salesforce/object-sync-for-salesforce.php' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../object-sync-for-salesforce/object-sync-for-salesforce.php';
if ( function_exists( 'object_sync_for_salesforce' ) ) {
$salesforce = object_sync_for_salesforce();
$this->salesforce = $salesforce;
return $this->salesforce;
}
}
}
/**
* Create default WordPress admin settings form for MinnPost-specific salesforce things
* This is for the Settings page/tab
*/
public function minnpost_salesforce_settings_forms() {
$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
$page = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
$section = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
$input_callback_default = array( $this, 'display_input_field' );
$textarea_default = array( $this, 'display_textarea' );
$editor_default = array( $this, 'display_editor' );
$input_checkboxes_default = array( $this, 'display_checkboxes' );
$this->fields_minnpost_settings(
'minnpost',
'minnpost',
array(
'text' => $input_callback_default,
'checkboxes' => $input_checkboxes_default,
'textarea' => $textarea_default,
'editor' => $editor_default,
)
);
}
/**
* Fields for the Log Settings tab
* This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
*
* @param string $page
* @param string $section
* @param array $callbacks
*/
private function fields_minnpost_settings( $page, $section, $callbacks ) {
add_settings_section( $page, ucwords( str_replace( '_', ' ', $page ) ), null, $page );
// todo: figure out how to pick what objects to prematch against and put that here in the admin settings
$minnpost_salesforce_settings = array(
'cms_id_field_in_salesforce' => array(
'title' => __( 'Name of WordPress user id field in Salesforce', 'minnpost-wordpress-salesforce' ),
'callback' => $callbacks['text'],
'page' => $page,
'section' => $section,
'args' => array(
'type' => 'text',
'desc' => __( 'Enter the name of the Salesforce field where the WordPress user ID value is stored, if applicable.', 'minnpost-wordpress-salesforce' ),
'constant' => '',
),
),
'nonmember_level_name' => array(
'title' => __( 'Name of Non-Member Level', 'minnpost-wordpress-salesforce' ),
'callback' => $callbacks['text'],
'page' => $page,
'section' => $section,
'args' => array(
'type' => 'text',
'desc' => '',
'constant' => '',
),
),
'no_account_message' => array(
'title' => __( 'No Account Message', 'minnpost-wordpress-salesforce' ),
'callback' => $callbacks['editor'],
'page' => $page,
'section' => $section,
'args' => array(
// translators: 1) is the register URL
'desc' => sprintf( __( 'This message will show to users who have Salesforce records but no website accounts if they try to log in. $register_url will show as %1$s and will prefill the email address the user is trying to use.', 'minnpost-wordpress-salesforce' ), site_url( '/user/register/' ) ),
'constant' => '',
'type' => 'text',
'rows' => '5',
'media_buttons' => false,
),
),
);
foreach ( $minnpost_salesforce_settings as $key => $attributes ) {
$id = 'salesforce_api_' . $key;
$name = 'salesforce_api_' . $key;
$title = $attributes['title'];
$callback = $attributes['callback'];
$page = $attributes['page'];
$section = $attributes['section'];
$args = array_merge(
$attributes['args'],
array(
'title' => $title,
'id' => $id,
'label_for' => $id,
'name' => $name,
)
);
add_settings_field( $id, $title, $callback, $page, $section, $args );
register_setting( $section, $id );
}
}
/**
* Add an admin tab to the Salesforce plugin's settings
*
* @param array $tabs
* @return array $tabs
*/
public function minnpost_tabs( $tabs ) {
$tabs['minnpost'] = 'MinnPost';
return $tabs;
}
/**
* Do not add user with ID of 1 to Salesforce
*
* @param bool $push_allowed
* @param string $object_type
* @param array $object
* @param int $sf_sync_trigger
* @param array $mapping
* @return bool $push_allowed
*/
public function push_not_allowed( $push_allowed, $object_type, $object, $sf_sync_trigger, $mapping ) {
if ( 'user' === $object_type && 1 === $object['ID'] ) { // do not add user 1 to salesforce
$push_allowed = false;
}
return $push_allowed;
}
/**
* Filter the SOQL query that is used to pull records
*
* @param object $soql
* @param string $object_type is the Salesforce object type
* @param array $salesforce_mapping is the fieldmap that maps the two object types
* @param array $mapped_fields is the fields that are being mapped
* @return object $soql
*/
public function pull_query_modify( $soql, $object_type, $salesforce_mapping, $mapped_fields ) {
if ( 'Contact' === $object_type ) {
$wordpress_user_id_field_in_salesforce = get_option( 'salesforce_api_cms_id_field_in_salesforce', '' );
if ( '' === $wordpress_user_id_field_in_salesforce ) {
return $soql;
}
$soql->add_condition( $wordpress_user_id_field_in_salesforce, "''", '!=' );
}
return $soql;
}
/**
* Find an object match between a WordPress object and a Salesforce object
* This is designed to find out if there is already a map based on the available WordPress data
*
* @param string $salesforce_id
* Unique identifier for the Salesforce object
* @param array $wordpress_object
* Array of the WordPress object's data
* @param array $mapping
* Array of the fieldmap between the WordPress and Salesforce object types
* @param string $action
* Is this a push or pull action?
*
* @return array $salesforce_id
* Unique identifier for the Salesforce object
*
* todo: may need a way for this to prevent a deletion in Salesforce if multiple contacts match the email address, for example. the plugin itself will block it if there are existing map rows. we might need to expand it for this, or maybe it is sufficient as it is. mp would probably turn off the delete hooks anyway.
*/
public function find_sf_object_match( $salesforce_id, $wordpress_object, $mapping, $action ) {
if ( 'push' === $action && 'user' === $mapping['wordpress_object'] ) {
if ( is_object( $this->salesforce ) ) {
$salesforce_api = $this->salesforce->salesforce['sfapi'];
} else {
$salesforce = $this->salesforce();
$salesforce_api = $salesforce->salesforce['sfapi'];
}
if ( is_object( $salesforce_api ) ) {
// we want to see if the user's email address exists as a primary on any contact and use that contact if so
$mail = $wordpress_object['user_email'];
$query = "SELECT Id FROM Contact WHERE Consolidated_EMail__c LIKE '%$mail%'";
$result = $salesforce_api->query( $query );
if ( isset( $result['data']['totalSize'] ) && 1 === $result['data']['totalSize'] ) {
$salesforce_id = $result['data']['records'][0]['Id'];
} elseif ( isset( $result['data']['totalSize'] ) && $result['data']['totalSize'] > 1 ) {
error_log( 'Salesforce has ' . $result['data']['totalSize'] . ' matches for this email. Try to log all of them: ' . print_r( $result['data']['records'], true ) );
}
}
}
return $salesforce_id;
}
/**
* Apply the member level to the user's roles
* This runs after the user has been pushed to Salesforce and has a response from Salesforce, which may have a member level
* If the current object is a user with an ID, and it comes from Salesforce with a member level, do stuff with it
* Currently it just deals with the roles associated with the user
*
* @param string $op
* What kind of operation we were doing (create, update, delete)
* @param array $sf_response
* The full response from Salesforce
* @param array $synced_object
* The WordPress object, object map, and field map together
* @param string $object_id
* The Salesforce ID
* @param string $wordpress_id_field_name
* How to identify the ID field for the WordPress object
*/
public function push_member_level( $op, $sf_response, $synced_object, $object_id, $wordpress_id_field_name ) {
// we run it on the push_success hook because that gives us the salesforce data we need
if ( isset( $synced_object['wordpress_object'][ $wordpress_id_field_name ] ) && isset( $sf_response['data']['Membership_Level__c'] ) ) {
$wordpress_id = $synced_object['wordpress_object'][ $wordpress_id_field_name ];
$salesforce_member_level = $sf_response['data']['Membership_Level__c'];
$this->set_member_level( $wordpress_id_field_name, $wordpress_id, $salesforce_member_level );
}
}
/**
* Set contact fields if this is a new Contact being added to Salesforce
* This runs before the user has been pushed to Salesforce, but we have data for it, which may have a Salesforce ID
*
* @param array $params
* Params mapping the fields to their values
* @param string $salesforce_id
* Salesforce ID if there is a matched object
* @param array $mapping
* Mapping object.
* @param array $object
* WordPress object data.
* @param string $object_type
* WordPress object type
*/
public function set_fields_if_missing( $params, $salesforce_id, $mapping, $object, $object_type = '' ) {
// this should only run if we're mapping a user that does not have a Salesforce id.
if ( 'user' === $object_type && null === $salesforce_id ) {
$params['FirstName'] = $object['first_name'];
$params['LastName'] = $object['last_name'];
$params['Consolidated_EMail__c'] = $object['user_email'];
}
return $params;
}
/**
* Apply the member level to the user's roles
* This runs before the user has been pulled from Salesforce, but we have Salesforce data for it, which may have a member level
* If the current object is a user with an ID, and it comes from Salesforce with a member level, do stuff with it
* Currently it just deals with the roles associated with the user
*
* @param int $wordpress_id
* ID for the WordPress object
* @param array $mapping
* The fieldmap between the WordPress and Salesforce objects
* @param array $object
* The Salesforce object
* @param string $wordpress_id_field_name
* How to identify the ID field for the WordPress object
* @param array $params
* The params array that matches fields to each other for saving
*/
public function pull_member_level( $wordpress_id, $mapping, $object, $wordpress_id_field_name, $params ) {
// as per this question, if the only thing that changes is the member level formula that we reference, the updated api call does not get triggered
// https://salesforce.stackexchange.com/questions/42726/how-to-detect-changes-in-formula-field-value-via-api
// i think it should run on the pre pull hook because we don't let salesforce create users by itself
if ( null !== $wordpress_id && isset( $params['member_level']['value'] ) ) {
$this->set_member_level( $wordpress_id_field_name, $wordpress_id, $params['member_level']['value'] );
}
}
/**
* If a user fails to log in, check to see if they exist in Salesforce
*
* @param string $message
* @param string $error_code
* @param array $data
* @return string $message
*/
public function login_fail_check( $message, $error_code, $data ) {
if ( 'invalid_username' === $error_code || 'invalid_email' === $error_code || 'invalidcombo' === $error_code ) {
if ( is_object( $this->salesforce ) ) {
$salesforce_api = $this->salesforce->salesforce['sfapi'];
} else {
$salesforce = $this->salesforce();
$salesforce_api = $salesforce->salesforce['sfapi'];
}
if ( is_object( $salesforce_api ) ) {
$mail = isset( $data['user_email'] ) ? $data['user_email'] : '';
if ( '' !== $mail ) {
$query = "SELECT Id FROM Contact WHERE Membership_level_number__c > 0 AND Consolidated_EMail__c LIKE '%$mail%'";
$result = $salesforce_api->query( $query );
if ( isset( $result['data']['totalSize'] ) && 1 === $result['data']['totalSize'] ) {
$salesforce_id = $result['data']['records'][0]['Id'];
$no_account_message = get_option( 'salesforce_api_no_account_message', '' );
if ( '' !== $no_account_message ) {
$message = str_replace( '$register_url', site_url( '/user/register/' ) . '?user_email=' . rawurlencode( $mail ), $no_account_message );
}
}
}
}
}
return $message;
}
/**
* Get the user's active recurring donations
*
* @param int $user_id
* @param string $active_field_name
* @param string $active_field_value
* @param string $payment_type_field_name
* @param string $payment_type_field_value
* @return array $donations
*/
public function get_active_recurring_donations( $user_id, $active_field_name, $active_field_value, $payment_type_field_name, $payment_type_field_value ) {
$donations = array();
if ( is_object( $this->salesforce ) ) {
$salesforce = $this->salesforce;
} else {
$salesforce = $this->salesforce();
}
$mapping = $this->salesforce->mappings->load_all_by_wordpress( 'user', $user_id, true );
if ( ! empty( $mapping ) ) {
$mapping = $mapping[0];
}
if ( ! empty( $mapping ) ) {
$salesforce_id = $mapping['salesforce_id'];
$salesforce_api = $salesforce->salesforce['sfapi'];
$query = "SELECT Id, npe03__Amount__c, npe03__Installment_Period__c, npe03__Next_Payment_Date__c FROM npe03__Recurring_Donation__c WHERE npe03__Contact__c = '$salesforce_id'";
if ( '' !== $active_field_name && '' !== $active_field_value ) {
$query .= " AND $active_field_name = '$active_field_value'";
}
if ( '' !== $payment_type_field_name && '' !== $payment_type_field_value ) {
$query .= " AND $payment_type_field_name = '$payment_type_field_value'";
}
$result = $salesforce_api->query(
$query,
array(
'cache' => true,
'cache_expiration' => MINUTE_IN_SECONDS * 5,
)
);
if ( isset( $result['data']['totalSize'] ) && 0 <= $result['data']['totalSize'] ) {
$records = $result['data']['records'];
foreach ( $records as $record ) {
$donations[] = array(
'id' => $record['Id'],
'amount' => $record['npe03__Amount__c'],
'frequency' => $record['npe03__Installment_Period__c'],
'next_date' => $record['npe03__Next_Payment_Date__c'],
);
}
}
}
return $donations;
}
/**
* Get the user's pledged opportunities
*
* @param int $user_id
* @param string $recurrence_field
* @param string $recurrence_value
* @param string $contact_id_field
* @param string $payment_type_field_name
* @param string $payment_type_field_value
* @param string $opportunity_type_value
* @return array $donations
*/
public function get_pledged_opportunities( $user_id, $recurrence_field, $recurrence_value, $contact_id_field, $payment_type_field_name, $payment_type_field_value, $opportunity_type_value = '' ) {
$donations = array();
if ( is_object( $this->salesforce ) ) {
$salesforce = $this->salesforce;
} else {
$salesforce = $this->salesforce();
}
$mapping = $this->salesforce->mappings->load_all_by_wordpress( 'user', $user_id, true );
if ( ! empty( $mapping ) ) {
$mapping = $mapping[0];
}
if ( ! empty( $mapping ) ) {
$salesforce_id = $mapping['salesforce_id'];
$salesforce_api = $salesforce->salesforce['sfapi'];
$query = "SELECT Id, Amount, CloseDate FROM Opportunity WHERE StageName = 'Pledged' AND $contact_id_field = '$salesforce_id'";
if ( '' !== $recurrence_field && '' !== $recurrence_value ) {
$query .= " AND $recurrence_field = '$recurrence_value'";
}
if ( '' !== $payment_type_field_name && '' !== $payment_type_field_value ) {
$query .= " AND $payment_type_field_name = '$payment_type_field_value'";
}
if ( '' !== $opportunity_type_value ) {
$query .= " AND Type = '$opportunity_type_value'";
}
$result = $salesforce_api->query(
$query,
array(
'cache' => true,
'cache_expiration' => MINUTE_IN_SECONDS * 5,
)
);
if ( isset( $result['data']['totalSize'] ) && 0 <= $result['data']['totalSize'] ) {
$records = $result['data']['records'];
foreach ( $records as $record ) {
$donations[] = array(
'id' => $record['Id'],
'amount' => $record['Amount'],
'next_date' => $record['CloseDate'],
);
}
}
}
return $donations;
}
/**
* Get the user's failed opportunities based on the passed criteria
*
* @param int $user_id
* @param string $history_opp_contact_field
* @param string $opp_payment_type_field
* @param string $opp_payment_type_value
* @param string $history_failed_value
* @param string|int $history_days_for_failed
* @param string $recurrence_field_name
* @param string $recurrence_field_value
* @param string $failed_recurring_id_field
* @param string $opportunity_type_value
* @return array $donations
*/
public function get_failed_opportunities( $user_id, $history_opp_contact_field, $opp_payment_type_field, $opp_payment_type_value, $history_failed_value, $history_days_for_failed, $recurrence_field_name, $recurrence_field_value, $failed_recurring_id_field, $opportunity_type_value = '' ) {
$donations = array();
if ( is_object( $this->salesforce ) ) {
$salesforce = $this->salesforce;
} else {
$salesforce = $this->salesforce();
}
$mapping = $this->salesforce->mappings->load_all_by_wordpress( 'user', $user_id, true );
if ( ! empty( $mapping ) ) {
$mapping = $mapping[0];
}
if ( ! empty( $mapping ) ) {
$salesforce_id = $mapping['salesforce_id'];
$salesforce_api = $salesforce->salesforce['sfapi'];
$query = "SELECT Id, Amount, CloseDate, $failed_recurring_id_field, $recurrence_field_name FROM Opportunity WHERE StageName = '$history_failed_value' AND $history_opp_contact_field = '$salesforce_id'";
if ( '' !== $opp_payment_type_field && '' !== $opp_payment_type_value ) {
$query .= " AND $opp_payment_type_field = '$opp_payment_type_value'";
}
if ( '' !== $history_days_for_failed ) {
$thirty_days_ago = gmdate( 'Y-m-d', strtotime( '-30 days' ) );
$today = current_time( 'Y-m-d' );
$query .= " AND ( CloseDate <= $today AND CloseDate >= $thirty_days_ago )";
}
if ( '' !== $opportunity_type_value ) {
$query .= " AND Type = '$opportunity_type_value'";
}
$result = $salesforce_api->query(
$query,
array(
'cache' => true,
'cache_expiration' => MINUTE_IN_SECONDS * 5,
)
);
if ( isset( $result['data']['totalSize'] ) && 0 <= $result['data']['totalSize'] ) {
$records = $result['data']['records'];
foreach ( $records as $record ) {
if ( $record[ $recurrence_field_name ] !== $recurrence_field_value && '' !== $failed_recurring_id_field ) {
$id = $record[ $failed_recurring_id_field ];
} else {
$id = $record['Id'];
}
$donation = array(
'id' => $id,
'amount' => $record['Amount'],
'close_date' => $record['CloseDate'],
);
if ( $record[ $recurrence_field_name ] !== $recurrence_field_value && '' !== $failed_recurring_id_field ) {
$donation['frequency'] = $record[ $recurrence_field_name ];
}
$donations[] = $donation;
}
}
}
return $donations;
}
/**
* Get the user's successful opportunities based on the passed criteria
*
* @param int $user_id
* @param string $history_opp_contact_field
* @param string $history_success_value
* @param string $opportunity_type_value
* @return array $donations
*/
public function get_successful_opportunities( $user_id, $history_opp_contact_field, $history_success_value, $opportunity_type_value = '' ) {
$donations = array();
if ( is_object( $this->salesforce ) ) {
$salesforce = $this->salesforce;
} else {
$salesforce = $this->salesforce();
}
$mapping = $this->salesforce->mappings->load_all_by_wordpress( 'user', $user_id, true );
if ( ! empty( $mapping ) ) {
$mapping = $mapping[0];
}
if ( ! empty( $mapping ) ) {
$salesforce_id = $mapping['salesforce_id'];
$salesforce_api = $salesforce->salesforce['sfapi'];
$query = "SELECT Id, Amount, CloseDate FROM Opportunity WHERE StageName = '$history_success_value' AND $history_opp_contact_field = '$salesforce_id'";
if ( '' !== $opportunity_type_value ) {
$query .= " AND Type = '$opportunity_type_value'";
}
$result = $salesforce_api->query(
$query,
array(
'cache' => true,
'cache_expiration' => MINUTE_IN_SECONDS * 5,
)
);
if ( isset( $result['data']['totalSize'] ) && 0 <= $result['data']['totalSize'] ) {
$records = $result['data']['records'];
foreach ( $records as $record ) {
$donations[] = array(
'id' => $record['Id'],
'amount' => $record['Amount'],
'close_date' => $record['CloseDate'],
);
}
}
}
return $donations;
}
/**
* Get the user's member level without cache
*
* @param int $user_id
* @return string $member_level
*/
public function get_member_level( $user_id ) {
$member_level = '';
if ( is_object( $this->salesforce ) ) {
$salesforce = $this->salesforce;
} else {
$salesforce = $this->salesforce();
}
$mapping = $this->salesforce->mappings->load_all_by_wordpress( 'user', $user_id, true );
if ( ! empty( $mapping ) ) {
$mapping = $mapping[0];
}
if ( ! empty( $mapping ) ) {
$salesforce_id = $mapping['salesforce_id'];
$salesforce_api = $salesforce->salesforce['sfapi'];
$query = "SELECT Id, Membership_Level__c FROM Contact WHERE Id = '$salesforce_id'";
$result = $salesforce_api->query(
$query,
array(
'cache' => true,
'cache_expiration' => MINUTE_IN_SECONDS * 5,
)
);
if ( isset( $result['data']['totalSize'] ) && 1 === $result['data']['totalSize'] ) {
$member_level = $result['data']['records'][0]['Membership_Level__c'];
}
}
return $member_level;
}
/**
* Do the actual setting of the member level.
* This works the same for push and pull, it just requires the correct data
*
* @param string $wordpress_id_field_name
* How to identify the ID field for the WordPress object
* @param int $wordpress_id
* ID for the WordPress object
* @param string $salesforce_member_level
* The member level value from Salesforce
*/
private function set_member_level( $wordpress_id_field_name, $wordpress_id, $salesforce_member_level ) {
$user = get_user_by( $wordpress_id_field_name, $wordpress_id );
if ( false !== $user ) {
$nonmember_level_name = get_option( 'salesforce_api_nonmember_level_name', 'Non-member' );
if ( $salesforce_member_level !== $nonmember_level_name ) {
$level_from_salesforce = 'member_' . strtolower( substr( $salesforce_member_level, 9 ) );
} else {
$level_from_salesforce = $salesforce_member_level;
}
$wp_roles = new WP_Roles(); // get all the available roles in WordPress
$wp_roles = $wp_roles->get_names(); // just get the names
$this_user_roles = $user->roles; // this is roles for this user
// check all the user's current roles
if ( ! empty( $this_user_roles ) ) {
foreach ( $this_user_roles as $key => $value ) {
$level_from_wordpress = $value;
// if the user's role didn't change, get out of this function
if ( false !== strpos( $value, 'member_' ) && $level_from_wordpress === $level_from_salesforce ) {
continue;
}
// this user was a member but now they're not. remove the level and get out of this function.
if ( false !== strpos( $value, 'member_' ) && $level_from_salesforce === $nonmember_level_name ) {
// this user is no longer a member, so get rid of the level
$user->remove_role( $value );
continue;
}
// if the user has a new member level, get rid of the old one
if ( false !== strpos( $value, 'member_' ) && $level_from_wordpress !== $level_from_salesforce ) {
$user->remove_role( $value );
continue;
}
}
}
// if the salesforce level is a role, add it to the user
if ( array_key_exists( $level_from_salesforce, $wp_roles ) ) {
$user->add_role( $level_from_salesforce );
}
// if a user has no roles, give them the default WordPress role
// this is helpful for legacy accounts that could lapse in their membership and then be left with no user role, which could be problematic.
if ( empty( $this_user_roles ) ) {
$default_role = get_option( 'default_role' );
$user->add_role( $default_role );
}
// add the value from the Salesforce API to the user's meta for member level
if ( $salesforce_member_level !== $nonmember_level_name ) {
update_user_meta( $user->ID, 'member_level', $salesforce_member_level );
} else {
update_user_meta( $user->ID, 'member_level', $nonmember_level_name );
}
} // End if().
}
/**
* Add roles and capabilities
* This adds the member roles
*/
public function add_roles_capabilities() {
$bronze = add_role( 'member_bronze', 'Member - Bronze', array() );
$silver = add_role( 'member_silver', 'Member - Silver', array() );
$gold = add_role( 'member_gold', 'Member - Gold', array() );
$platinum = add_role( 'member_platinum', 'Member - Platinum', array() );
}
/**
* Remove roles and capabilities
* This removes the member roles
*/
public function remove_roles_capabilities() {
remove_role( 'member_bronze' );
remove_role( 'member_silver' );
remove_role( 'member_gold' );
remove_role( 'member_platinum' );
}
/**
* Default display for <input> fields
*
* @param array $args
*/
public function display_input_field( $args ) {
$type = $args['type'];
$id = $args['label_for'];
$name = $args['name'];
$desc = $args['desc'];
$checked = '';
$class = 'regular-text';
if ( 'checkbox' === $type ) {
$class = 'checkbox';
}
if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
$value = esc_attr( get_option( $id, '' ) );
if ( 'checkbox' === $type ) {
if ( '1' === $value ) {
$checked = 'checked ';
}
$value = 1;
}
if ( '' === $value && isset( $args['default'] ) && '' !== $args['default'] ) {
$value = $args['default'];
}
echo sprintf(
'<input type="%1$s" value="%2$s" name="%3$s" id="%4$s" class="%5$s"%6$s>',
esc_attr( $type ),
esc_attr( $value ),
esc_attr( $name ),
esc_attr( $id ),
sanitize_html_class( $class . esc_html( ' code' ) ),
esc_html( $checked )
);
if ( '' !== $desc ) {
echo sprintf(
'<p class="description">%1$s</p>',
esc_html( $desc )
);
}
} else {
echo sprintf(
'<p><code>%1$s</code></p>',
esc_html__( 'Defined in wp-config.php', 'minnpost-wordpress-salesforce' )
);
}
}
/**
* Display for multiple checkboxes
* Above method can handle a single checkbox as it is
*
* @param array $args
*/
public function display_checkboxes( $args ) {
$type = 'checkbox';
$name = $args['name'];
$options = get_option( $name, array() );
foreach ( $args['items'] as $key => $value ) {
$text = $value['text'];
$id = $value['id'];
$desc = $value['desc'];
$checked = '';
if ( is_array( $options ) && in_array( $key, $options, true ) ) {
$checked = 'checked';
} elseif ( is_array( $options ) && empty( $options ) ) {
if ( isset( $value['default'] ) && true === $value['default'] ) {
$checked = 'checked';
}
}
echo sprintf(
'<div class="checkbox"><label><input type="%1$s" value="%2$s" name="%3$s[]" id="%4$s"%5$s>%6$s</label></div>',
esc_attr( $type ),
esc_attr( $key ),
esc_attr( $name ),
esc_attr( $id ),
esc_html( $checked ),
esc_html( $text )
);
if ( '' !== $desc ) {
echo sprintf(
'<p class="description">%1$s</p>',
esc_html( $desc )
);
}
}
}
/**
* Display for a dropdown/select
*
* @param array $args
*/
public function display_textarea( $args ) {
$id = $args['label_for'];
$name = $args['name'];
$desc = $args['desc'];
$rows = $args['rows'];
$cols = $args['cols'];
$class = 'regular-text';
if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
$value = esc_attr( get_option( $id, '' ) );
if ( '' === $value && isset( $args['default'] ) && '' !== $args['default'] ) {
$value = $args['default'];
}
if ( '' !== $rows ) {
$rows_attr = ' rows="' . esc_attr( $rows ) . '"';
} else {
$rows_attr = '';
}
if ( '' !== $cols ) {
$cols_attr = ' cols="' . esc_attr( $cols ) . '"';
} else {
$cols_attr = '';
}
echo sprintf(
'<textarea name="%1$s" id="%2$s" class="%3$s"%4$s%5$s>%6$s</textarea>',
esc_attr( $name ),
esc_attr( $id ),
sanitize_html_class( $class . esc_html( ' code' ) ),
$rows_attr,
$cols_attr,
esc_attr( $value )
);
if ( '' !== $desc ) {
echo sprintf(
'<p class="description">%1$s</p>',
esc_html( $desc )
);
}
} else {
echo sprintf(
'<p><code>%1$s</code></p>',
esc_html__( 'Defined in wp-config.php', 'minnpost-wordpress-salesforce' )
);
}
}
/**
* Display for a wysiwyg editir
*
* @param array $args
*/
public function display_editor( $args ) {
$id = $args['label_for'];
$name = $args['name'];
$desc = $args['desc'];
$checked = '';
$class = 'regular-text';
if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
$value = wp_kses_post( get_option( $id, '' ) );
if ( '' === $value && isset( $args['default'] ) && '' !== $args['default'] ) {
$value = $args['default'];
}
$settings = array();
if ( isset( $args['wpautop'] ) ) {
$settings['wpautop'] = $args['wpautop'];
}
if ( isset( $args['media_buttons'] ) ) {
$settings['media_buttons'] = $args['media_buttons'];
}
if ( isset( $args['default_editor'] ) ) {
$settings['default_editor'] = $args['default_editor'];
}
if ( isset( $args['drag_drop_upload'] ) ) {
$settings['drag_drop_upload'] = $args['drag_drop_upload'];
}
if ( isset( $args['name'] ) ) {
$settings['textarea_name'] = $args['name'];
}
if ( isset( $args['rows'] ) ) {
$settings['textarea_rows'] = $args['rows']; // default is 20
}
if ( isset( $args['tabindex'] ) ) {
$settings['tabindex'] = $args['tabindex'];
}
if ( isset( $args['tabfocus_elements'] ) ) {
$settings['tabfocus_elements'] = $args['tabfocus_elements'];
}
if ( isset( $args['editor_css'] ) ) {
$settings['editor_css'] = $args['editor_css'];
}
if ( isset( $args['editor_class'] ) ) {
$settings['editor_class'] = $args['editor_class'];
}
if ( isset( $args['teeny'] ) ) {
$settings['teeny'] = $args['teeny'];
}
if ( isset( $args['dfw'] ) ) {
$settings['dfw'] = $args['dfw'];
}
if ( isset( $args['tinymce'] ) ) {
$settings['tinymce'] = $args['tinymce'];
}
if ( isset( $args['quicktags'] ) ) {
$settings['quicktags'] = $args['quicktags'];
}
wp_editor( $value, $id, $settings );