-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclass-gf-cli-entry.php
More file actions
1019 lines (903 loc) · 29.3 KB
/
class-gf-cli-entry.php
File metadata and controls
1019 lines (903 loc) · 29.3 KB
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
defined( 'ABSPATH' ) || defined( 'WP_CLI' ) || die();
/**
* Manage Gravity Forms Entries.
*
* @since 1.0
* @package GravityForms/CLI
* @category CLI
* @author Rockegenius
* @copyright Copyright (c) 2016-2018, Rocketgenius
*/
class GF_CLI_Entry extends WP_CLI_Command {
/**
* Returns a JSON representation of an entry.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <entry-id>
* : The Entry ID
*
* [--format=<format>]
* : Accepted values: table, json
*
* [--raw]
* : Specifying raw will display the raw field IDs and values. Best used for passing input to the create command.
*
* ## EXAMPLES
*
* wp gf entry get 1
*
* @synopsis <entry-id> [--format=<format>] [--raw]
*/
public function get( $args, $assoc_args ) {
// Ensure an entry ID is set
$entry_id = isset( $args[0] ) ? $args[0] : 0;
// Gets the entry, based on the entry ID
$entry = GFAPI::get_entry( $entry_id );
// If the entry is not found, throw an error
if ( is_wp_error( $entry ) ) {
WP_CLI::error( $entry->get_error_message() );
}
// Check if format is defined and set it. If not, default to 'table'
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';
// Check if raw flag is defined. If not, default to false.
$raw = WP_CLI\Utils\get_flag_value( $assoc_args, 'raw', false );
// Gets the form that the entry is associated with.
$form = GFAPI::get_form( $entry['form_id'] );
// If raw flag is set
if ( $raw ) {
// If the format is set to 'json'
if ( $format == 'json' ) {
// Encode the entry data into JSON for output, and bail.
WP_CLI::line( json_encode( $entry ) );
return;
}
$rows = array();
// Run through the entry object returned, and add entry data to the $rows[] array.
foreach ( $entry as $field_id => $value ) {
$rows[] = array( 'ID' => $field_id, 'Field' => $field_id, 'Value' => (string) $value );
}
// Format the rows into a table format for output, then bail.
WP_CLI\Utils\format_items( 'table', $rows, array( 'ID', 'Field', 'Value' ) );
return;
}
// Check if the format flag is set.
if ( in_array( $format, array( 'table', 'json' ) ) ) {
$rows = array();
// Run through each of the fields within the form that contains the entry.
foreach ( $form['fields'] as $field ) {
$field_id = $field->id;
// If the raw flag is set, just set the ID and value.
if ( $raw ) {
$label = $field_id;
$value = rgar( $entry, $field_id );
} else {
// If the raw flag isn't set, make it pretty.
$field = GFFormsModel::get_field( $form, $field_id );
$label = GFFormsModel::get_label( $field );
$value = $this->get_entry_value( $entry, $field_id, $form );
}
// Add the data gathered to the $rows array.
$rows[] = array( 'ID' => $field_id, 'Field' => $label, 'Value' => (string) $value );
}
// Format the items as defined for output.
WP_CLI\Utils\format_items( $format, $rows, array( 'ID', 'Field', 'Value' ) );
} else {
// Throw an error if the format is set incorrectly.
WP_CLI::error( 'Invalid format' );
}
}
/**
* Deletes an entry.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <entry-id>...
* : One or more IDs of entries to delete
*
* [--force]
* : Skip the trash
*
* ## EXAMPLES
*
* wp gf entry delete 1
* wp gf entry delete 1 3 7 10
* wp gf entry delete 1 --force
* wp gf entry delete $(wp gf entry list 1 --status=trash --format=ids ) --force
*
* @synopsis <entry-id>... [--force]
*/
public function delete( $args, $assoc_args ) {
// Parse the array containing the entry IDs
foreach ( $args as $entry_id ) {
// Get the entry object
$entry = GFAPI::get_entry( $entry_id );
// If the entry can't be found, throw an error but keep going.
if ( is_wp_error( $entry ) ) {
WP_CLI::error( $entry->get_error_message(), false );
continue;
}
// Get the status of the entry
$current_status = $entry['status'];
// If the force flag is set, set $force to true. Otherwise, false.
$force = $current_status === 'trash' ? true : WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
// Check if force is set.
if ( $force ) {
// Delete the entry
$result = GFAPI::delete_entry( $entry_id );
// Check for errors in deletion
if ( is_wp_error( $result ) ) {
// If deletion throws an error, throw another one
WP_CLI::error( 'Error deleting entry ' . $entry_id, false );
} else {
// Hooray! It worked!
WP_CLI::success( 'Deleted entry ' . $entry_id );
}
} else {
// Move the entry to trash
$result = GFAPI::update_entry_property( $entry_id, 'status', 'trash' );
if ( is_wp_error( $result ) ) {
// If trashing throws an error, throw another one.
WP_CLI::error( 'Error trashing entry ' . $entry_id, false );
} else {
// Hooray! It worked!
WP_CLI::success( 'Trashed entry ' . $entry_id );
}
}
}
}
/**
* Creates a new entry from either a JSON string with the raw entry or from field-value pairs.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* [<entry-json>]
* : A JSON representation of the complete entry.
*
* [<form-id>]
* : The Form ID of the new entry.
*
* [--<field>=<value>]
* : Associative args for the new entry.
*
* ## EXAMPLES
*
* wp gf entry create 1 --field_1=ABC --field_2=test@test.com --field_3=1234 --field_4=Hello --field_5.3=John --field_5.6=Doe
* wp gf entry create $(wp gf entry get 3 --raw --format=json)
* @synopsis [<entry-json>] [<form-id>] [--<field>=<value>]
*/
public function create( $args, $assoc_args ) {
// Check if first parameter is JSON
if ( isset( $args[0] ) && ! is_numeric( $args[0] ) ) {
// Decode the JSON
$entry = json_decode( $args[0], ARRAY_A );
// If the JSON can't be decoded, throw an error.
if ( empty( $entry ) ) {
WP_CLI::error( 'Invalid entry JSON' );
}
// Create an entry using the entry object
$result = GFAPI::add_entry( $entry );
if ( is_wp_error( $result ) ) {
// If entry creation throws an error, throw another one.
WP_CLI::error( $result->get_error_message() );
} else {
// Hooray! It worked!
WP_CLI::success( 'Entry created successfully. New Entry ID: ' . $result );
}
// That's all we need for JSON entries. Bail.
return;
}
// Assign the first parameter as the form ID
$form_id = $args[0];
// See if the form exists
$form = GFAPI::get_form( $form_id );
// If getting the form by ID fails, throw an error.
if ( empty( $form ) ) {
WP_CLI::error( 'The Form ID must be specified' );
}
// Start creating the entry object
$entry = array();
$entry['form_id'] = $form_id;
// If there isn't a value, throw an error.
if ( empty( $assoc_args ) ) {
WP_CLI::error( 'Please specify at least one value --<field>=<value>' );
}
// The the columns for this entry.
$entry_db_columns = GFFormsModel::get_lead_db_columns();
// Set up the parameters to be inserted
foreach ( $assoc_args as $field_id => $value ) {
// If the parameter is just the field ID, fix it.
if ( strpos( $field_id, 'field_' ) === 0 ) {
$field_id = str_replace( 'field_', '', $field_id );
} else {
continue;
}
// If the entry exists, ignore the entry ID parameter
if ( in_array( $field_id, $entry_db_columns ) ) {
if ( $field_id == 'id' ) {
WP_CLI::line( 'The Entry ID value will be ignored.' );
continue;
}
$entry[ $field_id ] = $value;
} else {
// Check the fields set in the parameters
$field = GFFormsModel::get_field( $form, $field_id );
if ( empty( $field ) ) {
// Throw an error if the field doesn't exist
WP_CLI::error( 'Field not found: ' . $field_id, false );
continue;
}
$entry[ $field_id ] = $value;
}
}
// If source URL isn't set, make it this site URL
if ( ! isset( $entry['source_url'] ) ) {
$entry['source_url'] = site_url();
}
// Create the entry using GFAPI.
$result = GFAPI::add_entry( $entry );
if ( is_wp_error( $result ) ) {
// If entry creation had an error, throw another error.
WP_CLI::error( $result->get_error_message() );
} else {
// Hooray! It worked!
WP_CLI::success( 'Entry created successfully. Entry ID: ' . $result );
}
}
/**
* Updates an entry.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* [<entry-id>]
* : The ID of the entry to update.
*
* [--entry-json=<entry-json>]
* : A JSON representation of the complete Entry.
*
* [--<field>=<value>]
* : Field ID-Value pairs to be updated. The field id must start with --field_
* Example: --field_1="My Value"
*
* ## EXAMPLES
*
* wp gf entry update 1 --entry-json='{snip}'
* wp gf entry update 2 --field_1="My Value" --field_2="Another value"
* wp gf entry update 3 --field_3.3="Harry" --field_3.6="Potter"
*
* @synopsis [<entry-id>] [--entry-json=<entry-json>] [--<field>=<value>]
*/
public function update( $args, $assoc_args ) {
// Check if the entry-json flag is set
if ( isset( $assoc_args['entry-json'] ) ) {
// Decode the JSON string
$entry = json_decode( $assoc_args['entry-json'], ARRAY_A );
// If the JSON isn't valid, throw an error
if ( empty( $entry ) ) {
WP_CLI::error( 'Invalid entry JSON' );
}
// If the entry ID is in the arguments, set it.
if ( isset( $args[0] ) ) {
$entry['id'] = $args[0];
}
// Update the entry
$result = GFAPI::update_entry( $entry );
if ( is_wp_error( $result ) ) {
// If there was an error updating the entry, throw another one.
WP_CLI::error( $result->get_error_message() );
} else {
// Hooray! It worked!
WP_CLI::success( 'Entry updated successfully' );
}
// Bail. We don't need anything else.
return;
}
// If an entry ID isn't set, throw an error.
if ( ! isset( $args[0] ) ) {
WP_CLI::error( 'Please specify an entry ID.' );
}
// Set the entry ID to update
$entry_id = $args[0];
// Get the current entry with the defined entry ID
$entry = GFAPI::get_entry( $entry_id );
// If the entry doesn't exist, throw an error.
if ( is_wp_error( $entry ) ) {
WP_CLI::error( $entry->get_error_message() );
}
// Get the form that matches the entry
$form = GFAPI::get_form( $entry['form_id'] );
// Set the base value for the number of fields updated
$fields_updated = 0;
// Get the entry columns
$entry_db_columns = GFFormsModel::get_lead_db_columns();
// Start setting the entry information based on the arguments passed.
foreach ( $assoc_args as $field_id => $value ) {
// If the parameter is just the field ID, fix it.
if ( strpos( $field_id, 'field_' ) === 0 ) {
$field_id = str_replace( 'field_', '', $field_id );
} else {
continue;
}
// If the value is already the same, skip it.
if ( $entry[ $field_id ] == $value ) {
WP_CLI::line( 'The value of field ' . $field_id . ' is already ' . $value . '. Skipping.' );
continue;
}
// If the entry ID was passed, skip it
if ( in_array( $field_id, $entry_db_columns ) ) {
if ( $field_id == 'id' ) {
WP_CLI::error( "Can't change the Entry ID, sorry.", false );
continue;
}
// Update the entry property.
$result = GFAPI::update_entry_property( $entry_id, $field_id, $value );
} else {
// Get the field passed in the argument
$field = GFFormsModel::get_field( $form, $field_id );
// If the field doesn't exist, throw an error.
if ( empty( $field ) ) {
WP_CLI::error( 'Field not found: ' . $field_id, false );
continue;
}
// Update the field value
$result = GFAPI::update_entry_field( $entry_id, $field_id, $value );
}
// If everything failed, throw an error.
if ( empty( $result ) ) {
WP_CLI::error( 'Field ' . $field_id, false );
} else {
// Increase the number of fields updated
$fields_updated ++;
// Get the previous entry value for this field
$previous_value = rgblank( $entry[ $field_id ]) ? '[empty]' : $entry[ $field_id ];
// Display the field that was updated.
WP_CLI::line( 'Updated field ' . $field_id . ' from ' . $previous_value . ' to ' . $value );
}
}
if ( $fields_updated > 0 ) {
// Hooray! It worked!
WP_CLI::success( 'Field values updated: ' . $fields_updated );
} else {
// Nothing was updated.
WP_CLI::line( 'No fields updated' );
}
}
/**
* Exports entries.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <form-id>
* : The ID of the form.
*
* [<filename>]
* : The filename. Defaults to <form title>-<date>.<format>
*
* [--format=<format>]
* : Acceptable values: csv, json. Default: csv.
* The JSON format contains the raw values and can be imported using the import command.
*
* [--start_date=<date>]
* : Acceptable values: Date format yy-mm-dd. Default: Empty.
* Return only entries submitted on or after the start date.
*
* [--end_date=<date>]
* : Acceptable values: Date format yy-mm-dd. Default: Current system date.
* Return only entries submitted until and including the end date.
*
* [--dir=<dir>]
* : The directory to write the file. Defaults to the current working directory.
*
* Examples
* --filename=entries.json
* --filename=entries.csv
*
* ## EXAMPLES
*
* wp gf entry export 1
* wp gf entry export 1 --format=json
* wp gf entry export 1 --format=csv --start_date="2018-01-01" --end_date="2018-03-25"
*
* @synopsis <form-id> [<filename>] [--dir=<dir>] [--format=<format>] [--start_date=<yyyy-mm-dd>] [--end_date=<yyyy-mm-dd>]
*/
public function export( $args, $assoc_args ) {
// Check is the form ID was defined.
if ( ! isset( $args[0] ) ) {
WP_CLI::error( 'Please specify a form ID.' );
}
// Set the form ID from the first argument.
$form_id = $args[0];
// Get the form based on the form ID
$form = GFAPI::get_form( $form_id );
// If the form ID was not found, throw an error.
if ( empty( $form ) ) {
WP_CLI::error( 'The form ID does not exist' );
}
// Check the format of export file
if ( isset( $args[1] ) ) {
$filename = $args[1];
// If the filename ends in .json, et the format as json. Else, csv.
$format = substr( $filename, -4 ) === 'json' ? 'json' : 'csv';
} else {
// Set the format as csv
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'csv';
// Generate the filename
$filename = sanitize_title_with_dashes( $form['title'] ) . '-' . gmdate( 'Y-m-d', GFCommon::get_local_timestamp( time() ) ) . '.' . $format;
}
// If the dir flag is set
if ( isset( $assoc_args['dir'] ) ) {
// Check if target directory is writable
if ( ! is_writable( $assoc_args['dir'] ) ) {
// If not writable, throw an error
WP_CLI::error( 'Not writable: ' . $assoc_args['dir'] );
}
// Build the full file path
$filename = $assoc_args['dir'] . DIRECTORY_SEPARATOR . $filename;
} else {
// Check if working directory is writable
if ( ! is_writable( '.' ) ) {
// Throw an error if working directory is not writable
WP_CLI::error( 'The current working directory is not writable' );
}
}
$search_criteria = array();
// Check to see if start date and end date are set to add to search_criteria
if ( isset ( $assoc_args['start_date'] ) ) {
$search_criteria['start_date'] = $assoc_args['start_date'];
}
if ( isset ( $assoc_args['end_date'] ) ) {
$search_criteria['end_date'] = $assoc_args['end_date'];
}
// Export the entries in the defined format
if ( $format == 'json' ) {
$this->export_entries_json( $form_id, $filename, $search_criteria );
} else {
$this->export_entries_csv( $form, $filename, $search_criteria );
}
}
/**
* Imports entries.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <form-id>
* : The Form ID
*
* <file>
* : The full path to the file.
*
* ## EXAMPLES
*
* wp gf entry import 1 path/to/file.json
*
* @synopsis <form-id> <file>
*/
public function import( $args, $assoc_args ) {
// Set the form ID
$form_id = $args[0];
// Set the filename
$filename = $args[1];
// Get the contents of the file
$file_contents = file_get_contents( $filename );
// Decode the JSON entry data to an array
$entries = json_decode( $file_contents, ARRAY_A );
// If the entries do not exist, bail.
if ( empty( $entries ) ) {
WP_CLI::error( 'Invalid collection of entries' );
return;
}
// Add the entries
$result = GFAPI::add_entries( $entries, $form_id );
if ( is_wp_error( $result ) ) {
// If the entries can't be added, throw an error.
WP_CLI::error( $result->get_error_message() );
} else {
// Hooray! It worked!
WP_CLI::success( 'Entries added successfully: ' . count( $result ) );
}
}
/**
* Displays a list of entries.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <form-id>
* : The Form ID
*
* [--status=<status>]
* : The status of the entries. Default: active
* Accepted values: trash, active
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, ids. Default: table.
*
* [--page_size=<page_size>]
* : The size of the page. Default: 20
*
* [--offset=<offset>]
* : Number of entries to offset when printing the entry list. Default: 0
*
* ## EXAMPLES
*
* wp gf entry list 1
*
* @synopsis <form-id> [--status=<status>] [--format=<format>] [--page_size=<page_size>] [--offset=<offset>]
* @subcommand list
*/
public function entry_list( $args, $assoc_args ) {
// Get the form ID
$form_id = $args[0];
// Get the form, based on the form ID
$form = GFAPI::get_form( $form_id );
// If not found, throw an error
if ( empty( $form ) ) {
WP_CLI::error( 'Form not found' );
}
// Check the status flag, and default to active
$status = isset( $assoc_args['status'] ) ? $assoc_args['status'] : 'active';
// Check the page size flag, and default to 20
$page_size = isset( $assoc_args['page_size'] ) ? $assoc_args['page_size'] : 20;
// Set the offset, if defined.
$offset = isset( $assoc_args['offset'] ) ? $assoc_args['offset'] : 0;
// Set up paging, based on the offset and page size.
$paging = array( 'offset' => $offset, 'page_size' => $page_size );
// Default the count to zero, to count up later
$total_count = 0;
// Set the search criteria, based on the status
$search_criteria = array(
'status' => $status,
);
// Get the format, if defined and default to table
$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';
// If the fomate is set to 'ids', get the entry IDs
if ( $format == 'ids' ) {
$entry_ids = GFAPI::get_entry_ids( $form_id, $search_criteria, array(), $paging, $total_count );
echo implode( ' ', $entry_ids );
return;
}
// Get the entries
$entries = GFAPI::get_entries( $form_id, $search_criteria, array(), $paging, $total_count );
// If the format is set to 'count', display the number of entries.
if ( $format == 'count' ) {
echo $total_count;
return;
}
// Get the columns based on form ID
$columns = RGFormsModel::get_grid_columns( $form_id, true );
// If the ID column exists, create an entry ID column
if ( ! isset( $columns['id'] ) ) {
$id_column = array( 'label' => esc_html__( 'Entry Id', 'gravityforms' ), 'type' => 'id' );
$columns = array( 'id' => $id_column ) + $columns;
}
// Get field IDs from the columns
$field_ids = array_keys( $columns );
// Get the form based on the form ID
$form = GFAPI::get_form( $form_id );
// Prep the rows array
$rows = array();
// Get the field types
$field_types = array_keys( GF_Fields::get_all() );
// Run through the entries
foreach ( $entries as $entry ) {
$row = array();
// Run through the fields
foreach ( $field_ids as $field_id ) {
// Get the entry value for the field
$value = $this->get_entry_value( $entry, $field_id, $form);
// Get the field label
$label = $columns[ $field_id ]['label'];
// Get the field type
if ( in_array( $columns[ $field_id ]['type'], $field_types ) ) {
$label = $field_id . ': ' . $label;
}
$row[ $label ] = $value;
}
// Assign the entries to a main array
$rows[] = $row;
}
// Run through the fields
$fields = array();
foreach ( $columns as $key => $column ) {
$label = $column['label'];
// Get the field type
if ( in_array( $column['type'], $field_types ) ) {
$label = $key . ': ' . $label;
}
$fields[] = $label;
}
// Output the data
WP_CLI\Utils\format_items( $format, $rows, $fields );
}
/**
* Launch system editor to edit the JSON representation of the Entry.
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <entry-id>
* : The ID of the entry to edit.
*
* ## EXAMPLES
*
* wp gf entry edit 123
*
* @synopsis <entry-id>
*/
public function edit( $args, $assoc_args ) {
// Get the entry-id argument
$entry_id = $args[0];
// Get the entry object based on the entry ID
$entry = GFAPI::get_entry( $entry_id );
// If there's an error in getting the entry, throw an error
if ( is_wp_error( $entry ) ) {
WP_CLI::error( $entry->get_error_message() );
}
// Encode the entry data as JSON
$form_json = json_encode( $entry, JSON_PRETTY_PRINT );
// Open the editor
$r = $this->_edit( $form_json, "WP-CLI gf entry {$entry_id}" );
// If no changes were made, throw a warning
if ( $r === false ) {
WP_CLI::warning( 'No change made to entry.', 'Aborted' );
} else {
// If changes were made, update
$this->update( $args, array( 'entry-json' => $r ) );
}
}
/**
* Duplicates an entry
*
* @since 1.0-beta-1
* @access public
*
* ## OPTIONS
*
* <entry-id>
* : The ID of the entry to edit.
*
* [--count=<count>]
* : The number of times to duplicate. Default: 1.
*
* ## EXAMPLES
*
* wp gf entry edit 123
*
* @synopsis <entry-id> [--count=<count>]
*/
public function duplicate( $args, $assoc_args ) {
// Get the entry ID from the arguments
$entry_id = $args[0];
// Get the entry object based on the entry ID
$entry = GFAPI::get_entry( $entry_id );
// If there's an issue getting the entry object, throw an error
if ( is_wp_error( $entry ) ) {
WP_CLI::error( $entry->get_error_message() );
}
// If the count flag is set, use it
$count = isset( $assoc_args['count'] ) ? $assoc_args['count'] : 1;
// Display the progress bar
$progress = WP_CLI\Utils\make_progress_bar( 'Duplicating entry', $count );
// For each entry added, tick up the progress bar
for ( $i = $count; $i > 0; $i -- ) {
// Add the entry
GFAPI::add_entry( $entry );
// Move the progress bar
$progress->tick();
}
// Finish up the prigress bar
$progress->finish();
// Display the success message
WP_CLI::success( 'Entries created: ' . $count );
}
/**
* Launches the editor
*
* @since 1.0-beta-1
* @access protected
*
* @param string $content The content of the editor
* @param string $title The title of the editor
*
* @return mixed An instance of the editor
*/
protected function _edit( $content, $title ) {
$output = \WP_CLI\Utils\launch_editor_for_input( $content, $title );
return $output;
}
/**
* Gets the field value from an entry
*
* @since 1.0-beta-1
* @access private
*
* @param array $entry The entry object
* @param int $field_id The field ID
* @param array $form The form object
*
* @return string The field value
*/
private function get_entry_value( $entry, $field_id, $form ) {
// Get the form ID from the form object
$form_id = $form['id'];
// Gets the field object
$field = RGFormsModel::get_field( $form, $field_id );
// Gets the value of the entry for a particular field
$value = rgar( $entry, $field_id );
// If the field type is a post category
if ( ! empty( $field ) && $field->type == 'post_category' ) {
// Get the category value
$value = GFCommon::prepare_post_category_value( $value, $field, 'entry_list' );
}
// Filtering lead value
$value = apply_filters( 'gform_get_field_value', $value, $entry, $field );
// Get the input type of the field
$input_type = $field instanceof GF_Field ? $field->get_input_type() : $field_id;
// Check the input type
switch ( $input_type ) {
// If the input type is source_url, set it.
case 'source_url' :
$value = $entry['source_url'];
break;
// If the input type is a date, format it as so.
case 'date_created' :
case 'payment_date' :
$value = GFCommon::format_date( $value, false );
break;
// If the input type is a payment amount, format it as currency.
case 'payment_amount' :
$value = GFCommon::to_money( $value, $entry['currency'] );
break;
// If the input type is created_by, get the user login name
case 'created_by' :
if ( ! empty( $value ) ) {
$userdata = get_userdata( $value );
if ( ! empty( $userdata ) ) {
$value = $userdata->user_login;
}
}
break;
// If the input type doesn't match any of these, get the value.
default:
if ( $field !== null ) {
$value = $field->get_value_export( $entry, $field_id, true, true );
}
}
// Filter the field value before returning it
$value = apply_filters( 'gform_entries_field_value', $value, $form_id, $field_id, $entry );
return $value;
}
/**
* Exports the form entries as a CSV
*
* @since 1.0.4 Added the $search_criteria parameter. Current only supports start date and end date.
* @since 1.0-beta-1
*
* @param array $form The form object to export entries from
* @param string $filename The name of the file to export to
* @param array $search_criteria The search criteria
*/
private function export_entries_csv( $form, $filename, $search_criteria = array() ) {
// Require export.php for the GFExport class
require_once( GFCommon::get_base_path() . '/export.php' );
// Add the default export fields to the form object
$form = GFExport::add_default_export_fields( $form );
// Create an empty array to add fields to
$fields = array();
if ( is_array( $form['fields'] ) ) {
/* @var GF_Field $field */
foreach ( $form['fields'] as $field ) {
// Get all field inputs
$inputs = $field->get_entry_inputs();
// If there are multiple inputs for the field
if ( is_array( $inputs ) ) {
// Add each of the input IDs to the $fields array
foreach ( $inputs as $input ) {
$fields[] = $input['id'];
}
// Add the field ID to the $fields array
} elseif ( ! $field->displayOnly ) {
$fields[] = $field->id;
}
}
}
$_POST['export_field'] = $fields;
$search_criteria['status'] = 'active';
if ( isset( $search_criteria['start_date'] ) ) {
$_POST['export_date_start'] = $search_criteria['start_date'];
}
if ( isset( $search_criteria['end_date'] ) ) {
$_POST['export_date_end'] = $search_criteria['end_date'];
}
$export_id = wp_hash( uniqid( 'export', true ) );
$export_id = sanitize_key( $export_id );
$offset = 0;
$entry_count = GFAPI::count_entries( $form['id'], $search_criteria );
$progress = WP_CLI\Utils\make_progress_bar( sprintf( 'Exporting %d entries', $entry_count ), $entry_count );
do {
$status = GFExport::start_export( $form , $offset, $export_id );
$offset = $status['offset'];
$progress_limit = $offset == 0 ? $entry_count : $offset;
for ( $i = 0; $i < $progress_limit; $i++ ) {
$progress->tick();
}
} while ( $status['status'] == 'in_progress' );
$progress->finish();
// Move the file to $filename
$export_folder = GFFormsModel::get_upload_root() . 'export/';
$source_path = $export_folder . sanitize_file_name( 'export-' . $export_id . '.csv' );
rename( $source_path, $filename );
// Display the success message
WP_CLI::success( 'Entries exported to ' . $filename );
}
/**
* Exports the form entries as JSON
*
* @since 1.0-beta-1
* @access private
*
* @param int $form_id The form ID
* @param string $filename The file to export the entries to
* @param array $search_criteria The criteria to search through entries for
* @param array $sorting The entry data to sort by
*/
private function export_entries_json( $form_id, $filename, $search_criteria = array(), $sorting = array() ) {
// Process 20 entries at a time
$page_size = 20;
// Don't offset
$offset = 0;
// Get the number of entries
$entry_count = GFAPI::count_entries( $form_id, $search_criteria );
// Create the progress bar
$progress = WP_CLI\Utils\make_progress_bar( 'Exporting entries', (int) $entry_count / $page_size );
// Set the number of remaining entries
$entries_left = $entry_count;
$all_entries = array();
// Start getting the entries
while ( $entries_left > 0 ) {
$paging = array(
'offset' => $offset,
'page_size' => $page_size,
);