-
Notifications
You must be signed in to change notification settings - Fork 27
/
class-gravity-flow.php
4244 lines (3473 loc) · 136 KB
/
class-gravity-flow.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
// Make sure Gravity Forms is active and already loaded.
if ( class_exists( 'GFForms' ) ) {
// The Add-On Framework is not loaded by default.
// Use the following function to load the appropriate files.
GFForms::include_feed_addon_framework();
/**
* Gravity Flow
*
*
* @package GravityFlow
* @subpackage Classes/Gravity_Flow
* @copyright Copyright (c) 2015, Steven Henty
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0
*/
class Gravity_Flow extends GFFeedAddOn {
private static $_instance = null;
public $_version = GRAVITY_FLOW_VERSION;
// The Framework will display an appropriate message on the plugins page if necessary
protected $_min_gravityforms_version = '1.9.10';
protected $_slug = 'gravityflow';
protected $_path = 'gravityflow/gravityflow.php';
protected $_full_path = __FILE__;
// Title of the plugin to be used on the settings page, form settings and plugins page.
protected $_title = 'Gravity Flow';
// Short version of the plugin title to be used on menus and other places where a less verbose string is useful.
protected $_short_title = 'Workflow';
protected $_capabilities = array(
'gravityflow_uninstall',
'gravityflow_settings',
'gravityflow_create_steps',
'gravityflow_submit',
'gravityflow_inbox',
'gravityflow_status',
'gravityflow_status_view_all',
'gravityflow_reports',
'gravityflow_activity',
'gravityflow_workflow_detail_admin_actions',
);
protected $_capabilities_app_settings = 'gravityflow_settings';
protected $_capabilities_form_settings = 'gravityflow_create_steps';
protected $_capabilities_app_menu = array(
'gravityflow_uninstall',
'gravityflow_settings',
'gravityflow_create_steps',
'gravityflow_submit',
'gravityflow_inbox',
'gravityflow_status',
'gravityflow_activity',
'gravityflow_reports',
);
protected $_capabilities_uninstall = 'gravityflow_uninstall';
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new Gravity_Flow();
}
return self::$_instance;
}
private $_custom_page_content = null;
private function __clone() {
} /* do nothing */
public function pre_init(){
add_filter( 'gform_export_form', array( $this, 'filter_gform_export_form' ) );
add_action( 'gform_forms_post_import', array( $this, 'action_gform_forms_post_import') );
parent::pre_init();
add_filter( 'cron_schedules', array( $this, 'filter_cron_schedule' ) );
if ( ! wp_next_scheduled( 'gravityflow_cron' ) ) {
wp_schedule_event( time(), 'fifteen_minutes', 'gravityflow_cron' );
}
add_action( 'gravityflow_cron', array( $this, 'cron' ) );
add_action( 'wp', array( $this, 'filter_wp' ) );
}
public function init() {
parent::init();
// Make sure Gravity Flow feeds are triggered before other feeds so we get a chance to intercept them.
remove_filter( 'gform_entry_post_save', array( $this, 'maybe_process_feed' ), 10 );
add_filter( 'gform_entry_post_save', array( $this, 'maybe_process_feed' ), 8, 2 );
add_action( 'gform_after_submission', array( $this, 'after_submission' ), 9, 2 );
add_action( 'gform_after_update_entry', array( $this, 'filter_after_update_entry' ), 10, 2 );
add_shortcode( 'gravityflow', array( $this, 'shortcode' ) );
add_action( 'gform_register_init_scripts', array( $this, 'filter_gform_register_init_scripts' ), 10, 3 );
add_filter( 'auto_update_plugin', array( $this, 'maybe_auto_update' ), 10, 2 );
add_filter( 'gform_enqueue_scripts', array( $this, 'filter_gform_enqueue_scripts' ), 10, 2 );
add_action( 'wp_login', array( $this, 'filter_wp_login' ), 10, 2 );
}
public function init_admin() {
parent::init_admin();
add_action( 'gform_entry_detail_sidebar_middle', array( $this, 'entry_detail_status_box' ), 10, 2 );
add_filter( 'gform_notification_events', array( $this, 'add_notification_event' ) );
add_filter( 'set-screen-option', array( $this, 'set_option' ), 10, 3 );
add_action( 'load-workflow_page_gravityflow-status', array( $this, 'load_screen_options' ) );
add_filter( 'gform_entries_field_value', array( $this, 'filter_gform_entries_field_value' ), 10, 4 );
add_action( 'gform_field_standard_settings', array( $this, 'field_settings' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
add_filter( $this->_slug . '_feed_actions', array( $this, 'filter_feed_actions' ), 10, 3 );
add_action( 'gform_post_form_duplicated', array( $this, 'action_gform_post_form_duplicated' ), 10, 2 );
add_filter( 'gform_tooltips', array( $this, 'add_tooltips' ) );
}
public function init_ajax() {
parent::init_ajax();
add_action( 'wp_ajax_gravityflow_save_feed_order', array( $this, 'ajax_save_feed_order' ) );
add_action( 'wp_ajax_gravityflow_feed_message', array( $this, 'ajax_feed_message' ) );
add_action( 'wp_ajax_gravityflow_print_entries', array( $this, 'ajax_print_entries' ) );
add_action( 'wp_ajax_nopriv_gravityflow_print_entries', array( $this, 'ajax_print_entries' ) );
add_action( 'wp_ajax_gravityflow_export_status', array( $this, 'ajax_export_status' ) );
add_action( 'wp_ajax_nopriv_gravityflow_export_status', array( $this, 'ajax_export_status' ) );
add_action( 'wp_ajax_gravityflow_download_export', array( $this, 'ajax_download_export' ) );
add_action( 'wp_ajax_rg_delete_file', array( 'RGForms', 'delete_file' ) );
}
public function init_frontend(){
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ), 10 );
add_action( 'template_redirect', array( $this, 'action_template_redirect' ), 5 );
if ( class_exists( 'GFSignature' ) ) {
add_filter( 'gform_admin_pre_render', array( $this, 'delete_signature_script' ) );
$this->maybe_save_signature();
}
}
public function get_short_title(){
return $this->translate_navigation_label( 'workflow' );
}
public function setup(){
parent::setup();
}
public function upgrade( $previous_version ) {
if ( empty( $previous_version ) ) {
$settings = $this->get_app_settings();
if ( defined( 'GRAVITY_FLOW_LICENSE_KEY' ) ) {
$settings['license_key'] = GRAVITY_FLOW_LICENSE_KEY;
} else {
update_option( 'gravityflow_pending_installation', true );
}
$settings['background_updates'] = true;
$this->update_app_settings( $settings );
}
$this->setup_db();
}
private function setup_db(){
global $wpdb;
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE $wpdb->collate";
}
$sql = "
CREATE TABLE {$wpdb->prefix}gravityflow_activity_log (
id bigint(20) unsigned not null auto_increment,
log_object varchar(50),
log_event varchar(50),
log_value varchar(255),
date_created datetime not null,
form_id mediumint(8) unsigned not null,
lead_id int(10) unsigned not null,
assignee_id varchar(255),
assignee_type varchar(50),
display_name varchar(250),
feed_id mediumint(8) unsigned not null,
duration int(10) unsigned not null,
PRIMARY KEY (id)
) $charset_collate;";
//Fixes issue with dbDelta lower-casing table names, which cause problems on case sensitive DB servers.
add_filter( 'dbdelta_create_queries', array( 'RGForms', 'dbdelta_fix_case' ) );
dbDelta( $sql );
remove_filter( 'dbdelta_create_queries', array( 'RGForms', 'dbdelta_fix_case' ) );
}
// Enqueue the JavaScript and output the root url and the nonce.
public function scripts() {
$form_id = absint( rgget( 'id' ) );
$form = GFAPI::get_form( $form_id );
$routing_fields = ! empty( $form ) ? GFCommon::get_field_filter_settings( $form ) : array();
$input_fields = array();
if ( is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
/* @var GF_Field $field */
$input_fields[] = array( 'key' => absint( $field['id'] ), 'text' => esc_html__( $field['label'] ) );
}
}
$users = is_admin() ? $this->get_users_as_choices() : array();
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gravityflow_form_editor_js',
'src' => $this->get_base_url() . "/js/form-editor{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
'strings' => array(
'user' => array(
'defaults' => array(
'label' => esc_html__( 'User', 'gravityflow' )
),
),
'role' => array(
'defaults' => array(
'label' => esc_html__( 'Role', 'gravityflow' )
),
),
),
),
array(
'handle' => 'gravityflow_multi_select',
'src' => $this->get_base_url() . "/js/multi-select{$min}.js",
'deps' => array( 'jquery' ),
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=_notempty_' ),
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=0' ),
),
),
array(
'handle' => 'gf_routing_setting',
'src' => $this->get_base_url() . "/js/routing-setting{$min}.js",
'deps' => array( 'jquery' ),
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=_notempty_' ),
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=0' ),
),
'strings' => array(
'accounts' => $users,
'fields' => $routing_fields,
'input_fields' => $input_fields,
)
),
array(
'handle' => 'gravityflow_form_settings_js',
'src' => $this->get_base_url() . "/js/form-settings{$min}.js",
'deps' => array( 'jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-datepicker', 'gform_datepicker_init', 'gf_routing_setting' ),
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=_notempty_' ),
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=0' ),
),
'strings' => array(
'feedId' => absint( rgget( 'fid' ) ),
'formId' => absint( rgget( 'id' ) ),
)
),
array(
'handle' => 'gravityflow_feed_list',
'src' => $this->get_base_url() . "/js/feed-list{$min}.js",
'deps' => array( 'jquery', 'jquery-ui-sortable' ),
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow' ),
),
),
array(
'handle' => 'gravityflow_entry_detail',
'src' => $this->get_base_url() . "/js/entry-detail{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery', 'sack' ),
'enqueue' => array(
array(
'query' => 'page=gravityflow-inbox',
),
),
),
array(
'handle' => 'gravityflow_status_list',
'src' => $this->get_base_url() . "/js/status-list{$min}.js",
'deps' => array( 'jquery', 'gform_field_filter' ),
'version' => $this->_version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-status',
),
),
'strings' => array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
),
array(
'handle' => 'google_charts',
'src' => 'https://www.google.com/jsapi',
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gravityflow-reports', ),
)
),
array(
'handle' => 'gravityflow_reports',
'src' => $this->get_base_url() . "/js/reports{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery', 'google_charts' ),
'enqueue' => array(
array( 'query' => 'page=gravityflow-reports', ),
)
),
);
return array_merge( parent::scripts(), $scripts );
}
public function enqueue_frontend_scripts(){
global $wp_query;
if ( isset( $wp_query->posts ) && is_array( $wp_query->posts ) ) {
$shortcode_found = $this->look_for_shortcode();
if ( $shortcode_found ) {
$this->enqueue_form_scripts();
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
wp_enqueue_script( 'sack', "/wp-includes/js/tw-sack$min.js", array(), '1.6.1' );
wp_enqueue_script( 'gravityflow_entry_detail', $this->get_base_url() . "/js/entry-detail{$min}.js", array( 'jquery', 'sack' ), $this->_version );
wp_enqueue_script( 'gravityflow_status_list', $this->get_base_url() . "/js/status-list{$min}.js", array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'gform_datepicker_init' ), $this->_version );
wp_enqueue_script( 'gform_field_filter', GFCommon::get_base_url() . "/js/gf_field_filter{$min}.js", array( 'jquery', 'gform_datepicker_init' ), $this->_version );
wp_enqueue_script( 'gravityflow_frontend', $this->get_base_url() . "/js/frontend{$min}.js", array(), $this->_version );
wp_enqueue_style( 'gform_admin', GFCommon::get_base_url() . "/css/admin{$min}.css", null, $this->_version );
wp_enqueue_style( 'gravityflow_entry_detail', $this->get_base_url() . "/css/entry-detail{$min}.css", null, $this->_version );
wp_enqueue_style( 'gravityflow_frontend_css', $this->get_base_url() . "/css/frontend{$min}.css", null, $this->_version );
wp_enqueue_style( 'gravityflow_status', $this->get_base_url() . "/css/status{$min}.css", null, $this->_version );
wp_localize_script( 'gravityflow_status_list', 'gravityflow_status_list_strings', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
GFCommon::maybe_output_gf_vars();
}
}
}
public function look_for_shortcode(){
global $wp_query;
$shortcode_found = false;
foreach ( $wp_query->posts as $post ) {
if ( stripos( $post->post_content, '[gravityflow' ) !== false ) {
$shortcode_found = true;
break;
}
}
return $shortcode_found;
}
public function filter_gform_enqueue_scripts( $form, $is_ajax ){
if ( $this->has_enhanced_dropdown( $form ) ) {
wp_enqueue_script( 'gform_gravityforms' );
if ( wp_script_is( 'chosen', 'registered' ) ) {
wp_enqueue_script( 'chosen' );
} else {
wp_enqueue_script( 'gform_chosen' );
}
}
}
public function filter_gform_register_init_scripts( $form, $field_values, $is_ajax ){
if ( $this->has_enhanced_dropdown( $form ) ) {
$chosen_script = $this->get_chosen_init_script( $form );
GFFormDisplay::add_init_script( $form['id'], 'workflow_assignee_chosen', GFFormDisplay::ON_PAGE_RENDER, $chosen_script );
GFFormDisplay::add_init_script( $form['id'], 'workflow_assignee_chosen', GFFormDisplay::ON_CONDITIONAL_LOGIC, $chosen_script );
}
}
public static function get_chosen_init_script( $form ) {
$chosen_fields = array();
foreach ( $form['fields'] as $field ) {
$input_type = GFFormsModel::get_input_type( $field );
if ( $field->enableEnhancedUI && in_array( $input_type, array( 'workflow_assignee_select', 'workflow_user', 'workflow_role' ) ) ) {
$chosen_fields[] = "#input_{$form['id']}_{$field->id}";
}
}
return "gformInitChosenFields('" . implode( ',', $chosen_fields ) . "','" . esc_attr( apply_filters( "gform_dropdown_no_results_text_{$form['id']}", apply_filters( 'gform_dropdown_no_results_text', __( 'No results matched', 'gravityflow' ), $form['id'] ), $form['id'] ) ) . "');";
}
public function has_enhanced_dropdown( $form ) {
if ( ! is_array( $form['fields'] ) ) {
return false;
}
foreach ( $form['fields'] as $field ) {
if ( in_array( RGFormsModel::get_input_type( $field ), array( 'workflow_assignee_select', 'workflow_user', 'workflow_role' ) ) && $field->enableEnhancedUI ) {
return true;
}
}
return false;
}
public function feed_list_title() {
$url = add_query_arg( array( 'fid' => '0' ) );
return esc_html__( 'Workflow Steps', 'gravityflow' ) . " <a class='add-new-h2' href='{$url}'>" . __( 'Add New' , 'gravityflow' ) . '</a>';
}
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_admin',
'src' => GFCommon::get_base_url() . "/css/admin{$min}.css",
'version' => GFForms::$version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-inbox',
),
array(
'query' => 'page=gravityflow-submit',
),
array(
'query' => 'page=gravityflow-status',
),
array(
'query' => 'page=gravityflow-reports',
),
array(
'query' => 'page=gravityflow-activity',
),
)
),
array(
'handle' => 'gravityflow_inbox',
'src' => $this->get_base_url() . "/css/inbox{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-inbox',
),
)
),
array(
'handle' => 'gravityflow_entry_detail',
'src' => $this->get_base_url() . "/css/entry-detail{$min}.css",
'version' => $this->_version,
'deps' => array( 'gform_admin' ),
'enqueue' => array(
array(
'query' => 'page=gravityflow-inbox&view=entry',
),
)
),
array(
'handle' => 'gravityflow_submit',
'src' => $this->get_base_url() . "/css/submit{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-submit',
),
)
),
array(
'handle' => 'gravityflow_status',
'src' => $this->get_base_url() . "/css/status{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-status',
),
)
),
array(
'handle' => 'gravityflow_activity',
'src' => $this->get_base_url() . "/css/activity{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'query' => 'page=gravityflow-activity',
),
)
),
array(
'handle' => 'gravityflow_feed_list',
'src' => $this->get_base_url() . "/css/feed-list{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow' ),
),
),
array(
'handle' => 'gravityflow_multi_select_css',
'src' => $this->get_base_url() . "/css/multi-select{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=_notempty_' ),
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=0' ),
),
),
array(
'handle' => 'gravityflow_form_settings',
'src' => $this->get_base_url() . "/css/form-settings{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=_notempty_' ),
array( 'query' => 'page=gf_edit_forms&view=settings&subview=gravityflow&fid=0' ),
),
),
array(
'handle' => 'gravityflow_settings',
'src' => $this->get_base_url() . "/css/settings{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'query' => 'page=gravityflow_settings&view=_empty_' ),
array( 'query' => 'page=gravityflow_settings&view=settings' ),
array( 'query' => 'page=gravityflow_settings&view=labels' ),
),
),
);
return array_merge( parent::styles(), $styles );
}
public function feed_settings_title() {
return esc_html__( 'Workflow Step Settings', 'gravityflow' );
}
public function set_option($status, $option, $value){
if ( 'entries_per_page' == $option ) {
return $value;
}
return $status;
}
public function get_users_as_choices() {
$editable_roles = array_reverse( get_editable_roles() );
$role_choices = array();
foreach ( $editable_roles as $role => $details ) {
$name = translate_user_role( $details['name'] );
$role_choices[] = array( 'value' => 'role|' . $role, 'label' => $name );
}
$args = apply_filters( 'gravityflow_get_users_args', array( 'number' => 1000, 'orderby' => 'display_name' ) );
$accounts = get_users( $args );
$account_choices = array();
foreach ( $accounts as $account ) {
$account_choices[] = array( 'value' => 'user_id|' . $account->ID, 'label' => $account->display_name );
}
$choices = array(
array(
'label' => __( 'Users', 'gravityflow' ),
'choices' => $account_choices,
),
array(
'label' => __( 'Roles', 'gravityflow' ),
'choices' => $role_choices,
),
);
$form_id = absint( rgget( 'id' ) );
$form = GFAPI::get_form( $form_id );
$field_choices = array();
$assignee_fields_as_choices = $this->get_assignee_fields_as_choices( $form );
if ( ! empty( $assignee_fields_as_choices ) ) {
$field_choices = $assignee_fields_as_choices;
}
$email_fields_as_choices = $this->get_email_fields_as_choices( $form );
if ( ! empty( $email_fields_as_choices ) ) {
$field_choices = array_merge( $field_choices, $email_fields_as_choices );
}
if ( rgar( $form, 'requireLogin' ) ) {
$field_choices[] = array(
'label' => __( 'User (Created by)', 'gravityflow' ),
'value' => 'entry|created_by',
);
}
if ( ! empty( $field_choices ) ) {
$choices[] = array(
'label' => __( 'Fields', 'gravityflow' ),
'choices' => $field_choices,
);
}
return $choices;
}
public function get_assignee_fields_as_choices( $form = null){
if ( empty( $form ) ){
$form_id = absint( rgget( 'id' ) );
$form = GFAPI::get_form( $form_id );
}
$assignee_fields = array();
if ( isset( $form['fields'] ) && is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
/* @var GF_Field $field */
$type = GFFormsModel::get_input_type( $field );
if ( $type == 'workflow_assignee_select' ) {
$assignee_fields[] = array( 'label' => GFFormsModel::get_label( $field ), 'value' => 'assignee_field|' . $field->id );
} elseif ( $type == 'workflow_user' ) {
$assignee_fields[] = array( 'label' => GFFormsModel::get_label( $field ), 'value' => 'assignee_user_field|' . $field->id );
} elseif ( $type == 'workflow_role' ) {
$assignee_fields[] = array( 'label' => GFFormsModel::get_label( $field ), 'value' => 'assignee_role_field|' . $field->id );
}
}
}
return $assignee_fields;
}
public function get_email_fields_as_choices( $form = null){
if ( empty( $form ) ){
$form_id = absint( rgget( 'id' ) );
$form = GFAPI::get_form( $form_id );
}
$email_fields = array();
if ( isset( $form['fields'] ) && is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
/* @var GF_Field $field */
if ( $field->get_input_type() == 'email' ) {
$email_fields[] = array( 'label' => GFFormsModel::get_label( $field ), 'value' => 'email_field|' . $field->id );
}
}
}
return $email_fields;
}
/**
* Override the feed_settings_field() function and return the configuration for the Feed Settings.
* Updating is handled by the Framework.
*
* @return array
*/
function feed_settings_fields() {
$current_step_id = $this->get_current_feed_id();
$step_type_choices = array();
$step_classes = Gravity_Flow_Steps::get_all();
foreach ( $step_classes as $key => $step_class ) {
$step_type_choice = array( 'label' => $step_class->get_label(), 'value' => $step_class->get_type() );
$step_type_choice['icon_url'] = $step_class->get_icon_url();
if ( $current_step_id > 0 ) {
$step_type_choice['disabled'] = 'disabled';
$step_type_choice['div_class'] = 'gravityflow-disabled';
}
$step_settings = $step_class->get_settings();
if ( empty( $step_settings ) ) {
unset( $step_classes[ $key ] );
} else {
$step_type_choices[] = $step_type_choice;
}
}
$settings = array();
$step_type_setting = array(
'name' => 'step_type',
'label' => esc_html__( 'Step Type', 'gravityflow' ),
'type' => 'radio_image',
'horizontal' => true,
'required' => true,
'onchange' => 'jQuery(this).parents("form").submit();',
'choices' => $step_type_choices,
);
$settings[] = array(
'title' => 'Step',
'fields' => array(
array(
'name' => 'step_name',
'label' => __( 'Name', 'gravityflow' ),
'type' => 'text',
'class' => 'medium',
'required' => true,
'tooltip' => '<h6>' . __( 'Name', 'gravityflow' ) . '</h6>' . __( 'Enter a name to uniquely identify this step.', 'gravityflow' )
),
array(
'name' => 'description',
'label' => esc_html__( 'Description', 'gravityflow' ),
'class' => 'fieldwidth-3 fieldheight-2',
'type' => 'textarea',
),
$step_type_setting,
array(
'name' => 'condition',
'tooltip' => esc_html__( "Build the conditional logic that should be applied to this step before it's allowed to be processed. If an entry does not meet the conditions of this step it will fall on to the next step in the list.", 'gravityflow' ),
'label' => 'Condition',
'type' => 'feed_condition',
'checkbox_label' => esc_html__( 'Enable Condition for this step', 'gravityflow' ),
'instructions' => esc_html__( 'Perform this step if', 'gravityflow' ),
),
array(
'name' => 'scheduled',
'label' => esc_html__( 'Schedule' ),
'type' => 'schedule',
'tooltip' => esc_html__( 'Scheduling a step will queue entries and prevent them from starting this step until the specified date or until the delay period has elapsed.', 'gravityflow' )
. ' ' . esc_html__( 'Note: the schedule setting requires the WordPress Cron which is included and enabled by default unless your host has deactivated it.', 'gravityflow' )
),
)
);
foreach ( $step_classes as $step_class ) {
$type = $step_class->get_type();
$step_settings = $step_class->get_settings();
$step_settings['id'] = 'gravityflow-step-settings-' . $type;
$step_settings['class'] = 'gravityflow-step-settings';
if ( isset( $step_settings['fields'] ) && is_array( $step_settings['fields'] ) ) {
//$step_settings['fields'] = $this->prefix_step_settings_fields( $step_settings['fields'], $type );
$final_status_options = $step_class->get_final_status_config();
foreach ( $final_status_options as $final_status_option ) {
$step_settings['fields'][] = array(
'name' => 'destination_' . $final_status_option['status'],
'label' => $final_status_option['default_destination_label'],
'type' => 'step_selector',
'default_value' => $final_status_option['default_destination'],
);
}
}
$step_settings['dependency'] = array( 'field' => 'step_type', 'values' => array( $type ) );
$settings[] = $step_settings;
}
$list_url = remove_query_arg( 'fid' );
$new_url = add_query_arg( array( 'fid' => 0 ) );
$success_feedback = sprintf( __( 'Step settings updated. %sBack to the list%s or %sAdd another step%s.', 'gravityflow' ), '<a href="' . esc_url( $list_url ) . '">', '</a>', '<a href="' . esc_url( $new_url ) . '">', '</a>' );
$settings[] = array(
'id' => 'save_button',
'fields' => array(
array(
'id' => 'save_button',
'type' => 'save',
'validation_callback' => array( $this, 'save_feed_validation_callback' ),
'name' => 'save_button',
'value' => __( 'Update Step Settings', 'gravityflow' ),
'messages' => array(
'success' => $success_feedback,
'error' => __( 'There was an error while saving the step settings', 'gravityflow' )
)
),
)
);
return $settings;
}
public function ajax_feed_message(){
$current_step_id = absint( rgget( 'fid' ) );
$entry_count = 0;
if ( $current_step_id ) {
$current_step = $this->get_step( $current_step_id );
$entry_count = $current_step->entry_count();
}
if ( $entry_count > 0 ) {
$html = '<div class="delete-alert alert_red"><i class="fa fa-exclamation-triangle gf_invalid"></i> ' . sprintf( _n( 'There is %s entry currently on this step. This entry may be affected if the settings are changed.', 'There are %s entries currently on this step. These entries may be affected if the settings are changed.', $entry_count, 'gravityflow' ), $entry_count ) . '</div>';
} else {
$html = '';
}
echo $html;
die();
}
public function save_feed_validation_callback( $field, $field_setting ){
$current_step_id = $this->get_current_feed_id();
$entry_count = 0;
$current_step = false;
if ( $current_step_id ) {
$current_step = $this->get_step( $current_step_id );
$entry_count = $current_step->entry_count();
}
if ( $entry_count > 0 && $current_step ) {
$assignee_settings['assignees'] = array();
$current_assignees = $current_step->get_assignees();
foreach ( $current_assignees as $current_assignee ) {
$assignee_settings['assignees'][] = $current_assignee->get_key();
}
if ( $current_step->get_type() == 'approval' ) {
$assignee_settings['unanimous_approval'] = $current_step->unanimous_approval;
}
$this->_assignee_settings_md5 = md5( serialize( $assignee_settings ) );
}
return true;
}
public function update_feed_meta( $id, $meta ) {
parent::update_feed_meta( $id, $meta );
$results = $this->maybe_refresh_assignees();
if ( ! empty( $results['removed'] ) || ! empty( $results['added'] ) ) {
GFCommon::add_message( 'Assignees updated' );
}
}
public function maybe_refresh_assignees(){
$results = array(
'removed' => array(),
'added' => array()
);
if ( ! ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && rgget( 'subview' ) == 'gravityflow' ) ) {
return $results;
}
$current_step_id = $this->get_current_feed_id();
$current_step = $this->get_step( $current_step_id );
if ( empty ( $current_step ) ){
return $results;
}
$assignee_settings['assignees'] = array();
$assignees = $current_step->get_assignees();
foreach ( $assignees as $assignee ) {
/* @var Gravity_Flow_Assignee $assignee */
$assignee_settings['assignees'][] = $assignee->get_key();
}
if ( $current_step->get_type() == 'approval' ) {
$assignee_settings['unanimous_approval'] = $current_step->unanimous_approval;
}
$assignee_settings_md5 = md5( serialize( $assignee_settings ) );
if ( isset( $this->_assignee_settings_md5 ) && $this->_assignee_settings_md5 !== $assignee_settings_md5 ) {
$results = $this->refresh_assignees();
}
return $results;
}
public function refresh_assignees(){
$results = array(
'removed' => array(),
'added' => array()
);
$current_step_id = $this->get_current_feed_id();
$form_id = absint( rgget( 'id' ) );
$current_step = $this->get_step( $current_step_id );
$entry_count = $current_step->entry_count();
if ( $entry_count == 0 ) {
// Nothing to do
return $results;
}
$form = $this->get_current_form();
// Avoid paging through entries from GFAPI::get_entries() by using custom query.
$assignee_status_by_entry = $this->get_asssignee_status_by_entry( $form['id'] );
foreach ( $assignee_status_by_entry as $entry_id => $assignee_status ){
$entry = GFAPI::get_entry( $entry_id );
$step_for_entry = $this->get_step( $current_step_id, $entry );
if ( $entry['workflow_step'] != $step_for_entry->get_id() ) {
continue;
}
$updated = false;
$current_assignees = $step_for_entry->get_assignees();
foreach ( $current_assignees as $assignee ) {
/* @var Gravity_Flow_Assignee $assignee */
$assignee_key = $assignee->get_key();
if ( ! isset( $assignee_status[ $assignee_key ] ) ) {
// New assignee - set & send
$step = $this->get_step( $current_step_id, $entry );
$assignee->update_status( 'pending' );
//$step->maybe_send_assignee_notification( $assignee );
$step->end_if_complete();
$results['added'][] = $assignee;
$updated = true;
}
}
foreach ( $assignee_status as $old_assignee_key => $old_status ) {
foreach ( $current_assignees as $assignee ) {
$assignee_key = $assignee->get_key();
if ( $assignee_key == $old_assignee_key ) {
continue 2;
}
}
// No longer an assignee - remove
$old_assignee = new Gravity_Flow_Assignee( $old_assignee_key, $step_for_entry );
$old_assignee->remove();
$old_assignee->log_event( 'removed' );
$results['removed'][] = $old_assignee;
$updated = true;
}
if ( $updated ) {
$this->process_workflow( $form, $entry_id );
}
}
return $results;
}
public function get_asssignee_status_by_entry( $form_id ){
global $wpdb;
$assignee_status_by_entry = array();
$table = GFFormsModel::get_lead_meta_table_name();
$lead_table = GFFormsModel::get_lead_table_name();
$sql = $wpdb->prepare( "
SELECT *
FROM $table m
INNER JOIN $lead_table l
ON l.id = m.lead_id
WHERE m.meta_key LIKE %s
AND m.meta_key NOT LIKE '%%_timestamp'
AND m.form_id=%d
AND l.status='active'", 'workflow_user_id_%', $form_id );
$rows = $wpdb->get_results( $sql );
if ( ! is_wp_error( $rows ) && count( $rows ) > 0 ) {
foreach ( $rows as $row ) {
$user_id = str_replace( 'workflow_user_id_', '', $row->meta_key );
if ( ! isset( $assignee_status_by_entry[ $row->lead_id ] ) ) {
$assignee_status_by_entry[ $row->lead_id ] = array();
}
$assignee_status_by_entry[ $row->lead_id ][ 'user_id|' . $user_id ] = $row->meta_value;
}
}
$sql = $wpdb->prepare( "
SELECT *
FROM $table m
INNER JOIN $lead_table l
ON l.id = m.lead_id
WHERE m.meta_key LIKE %s
AND m.meta_key NOT LIKE '%%_timestamp'
AND m.form_id=%d
AND l.status='active'", 'workflow_email_%', $form_id );
$rows = $wpdb->get_results( $sql );
if ( ! is_wp_error( $rows ) && count( $rows ) > 0 ) {
foreach ( $rows as $row ) {
$user_id = str_replace( 'workflow_email_', '', $row->meta_key );
if ( ! isset( $assignee_status_by_entry[ $row->lead_id ] ) ) {
$assignee_status_by_entry[ $row->lead_id ] = array();
}