-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathHelper_Abstract_Options.php
More file actions
2545 lines (2123 loc) · 72.7 KB
/
Copy pathHelper_Abstract_Options.php
File metadata and controls
2545 lines (2123 loc) · 72.7 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
namespace GFPDF\Helper;
use GFPDF\Controller\Controller_Custom_Fonts;
use GFPDF\Model\Model_Custom_Fonts;
use GFPDF\Statics\Kses;
use GFPDF_Vendor\Psr\Log\LoggerInterface;
use WP_Error;
/**
* @package Gravity PDF
* @copyright Copyright (c) 2026, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class to set up the settings api callbacks
*
* Pulled straight from the Easy Digital Download register-settings.php file (props to Pippin and team)
* and modified to suit our requirements
*
* @since 4.0
*/
abstract class Helper_Abstract_Options implements Helper_Interface_Filters {
/**
* Holds the abstracted Gravity Forms API specific to Gravity PDF
*
* @var Helper_Form
*
* @since 4.0
*/
protected $gform;
/**
* Holds our log class
*
* @var LoggerInterface
*
* @since 4.0
*/
protected $log;
/**
* Holds our Helper_Data object
* which we can autoload with any data needed
*
* @var Helper_Data
*
* @since 4.0
*/
protected $data;
/**
* Holds our Helper_Templates object
* used to ease access to our PDF templates
*
* @var Helper_Templates
*
* @since 4.0
*/
protected $templates;
/**
* Holds our Helper_Misc object
* Makes it easy to access common methods throughout the plugin
*
* @var Helper_Misc
*
* @since 4.0
*/
protected $misc;
/**
* Holds our Helper_Notices object
* which we can use to queue up admin messages for the user
*
* @var Helper_Notices
*
* @since 4.0
*/
protected $notices;
/**
* Holds the current global user settings
*
* @var array
*
* @since 4.0
*/
private $settings = [];
/**
* Holds the Gravity Form PDF Settings
*
* @var array
*
* @since 4.0
*/
private $form_settings = [];
/**
* Helper_Abstract_Options constructor.
*
* @param LoggerInterface $log
* @param Helper_Abstract_Form $gform
* @param Helper_Data $data
* @param Helper_Misc $misc
* @param Helper_Notices $notices
* @param Helper_Templates $templates
*
* @since 4.0
*/
public function __construct( LoggerInterface $log, Helper_Abstract_Form $gform, Helper_Data $data, Helper_Misc $misc, Helper_Notices $notices, Helper_Templates $templates ) {
/* Assign our internal variables */
$this->log = $log;
$this->gform = $gform;
$this->data = $data;
$this->misc = $misc;
$this->notices = $notices;
$this->templates = $templates;
}
/**
* Returns an array of registered fields
*
* @return array
*
* @since 4.0
*/
abstract public function get_registered_fields();
/**
* Initialise the options API
*
* @return void
*
* @since 4.0
*/
public function init() {
$this->set_plugin_settings();
$this->add_filters();
}
/**
* Add our filters
*
* @return void
*
* @since 4.0
*/
public function add_filters() {
/* Register our core sanitize functions */
add_filter( 'gfpdf_settings_sanitize', [ $this, 'sanitize_required_field' ], 10, 4 );
add_filter( 'gfpdf_settings_sanitize', [ $this, 'sanitize_all_fields' ], 10, 4 );
add_filter( 'gfpdf_settings_sanitize_text', [ $this, 'sanitize_trim_field' ] );
add_filter( 'gfpdf_settings_sanitize_textarea', [ $this, 'sanitize_trim_field' ] );
add_filter( 'gfpdf_settings_sanitize_number', [ $this, 'sanitize_number_field' ], 10, 4 );
add_filter( 'gfpdf_settings_sanitize_paper_size', [ $this, 'sanitize_paper_size' ] );
}
/**
* Get the plugin's settings from the database
*
* @return void
* @since 4.0
*
*/
public function set_plugin_settings() {
/* assign our settings */
$this->settings = $this->get_settings();
}
/**
* @param array $new_settings
*
* @Internal This option key is managed by WordPress Settings API. You cannot store info here that isn't already registered.
* through $this->register_settings()
*
* @since 4.2
*/
public function update_settings( $new_settings ) {
update_option( 'gfpdf_settings', $new_settings );
$this->set_plugin_settings();
}
/**
* Add all settings sections and fields
*
* @param array $fields Fields that should be registered
*
* @return void
* @since 4.0
*
*/
public function register_settings( $fields = [] ) {
global $wp_settings_fields;
foreach ( $fields as $tab => $settings ) {
/* Clear all previously set types */
$group = 'gfpdf_settings_' . $tab;
if ( isset( $wp_settings_fields[ $group ] ) ) {
unset( $wp_settings_fields[ $group ] );
}
foreach ( $settings as $option ) {
$name = isset( $option['name'] ) ? $option['name'] : '';
add_settings_field(
'gfpdf_settings[' . $option['id'] . ']',
$name,
method_exists( $this, $option['type'] . '_callback' ) ? [
$this,
$option['type'] . '_callback',
] : [ $this, 'missing_callback' ],
'gfpdf_settings_' . $tab,
'gfpdf_settings_' . $tab,
[
'id' => isset( $option['id'] ) ? $option['id'] : null,
'desc' => ! empty( $option['desc'] ) ? $option['desc'] : '',
'desc2' => ! empty( $option['desc2'] ) ? $option['desc2'] : '',
'type' => isset( $option['type'] ) ? $option['type'] : null,
'name' => isset( $option['name'] ) ? $option['name'] : null,
'size' => isset( $option['size'] ) ? $option['size'] : null,
'options' => isset( $option['options'] ) ? $option['options'] : '',
'std' => isset( $option['std'] ) ? $option['std'] : '',
'min' => isset( $option['min'] ) ? $option['min'] : null,
'max' => isset( $option['max'] ) ? $option['max'] : null,
'step' => isset( $option['step'] ) ? $option['step'] : null,
'chosen' => isset( $option['chosen'] ) ? $option['chosen'] : null,
'class' => isset( $option['class'] ) ? $option['class'] : null,
'inputClass' => isset( $option['inputClass'] ) ? $option['inputClass'] : null,
'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null,
'tooltip' => isset( $option['tooltip'] ) ? $option['tooltip'] : null,
'multiple' => isset( $option['multiple'] ) ? $option['multiple'] : null,
'required' => isset( $option['required'] ) ? $option['required'] : null,
'uploaderTitle' => isset( $option['uploaderTitle'] ) ? $option['uploaderTitle'] : null,
'uploaderButtonText' => isset( $option['uploaderButtonText'] ) ? $option['uploaderButtonText'] : null,
'toggle' => isset( $option['toggle'] ) ? $option['toggle'] : null,
'data' => isset( $option['data'] ) ? $option['data'] : null,
]
);
}
}
/* Creates our settings in the options table */
register_setting( 'gfpdf_settings', 'gfpdf_settings', [ $this, 'settings_sanitize' ] );
}
/**
* Update a current registered settings
*
* @param string $group_id The top-level group we're updating
* @param string $setting_id The section group we're updating
* @param string $option_id The option we are updating
* @param mixed $option_value The new option value
*
* @return boolean True on success, false on failure
*
* @since 4.0
*/
public function update_registered_field( $group_id, $setting_id, $option_id, $option_value ) {
global $wp_settings_fields;
$group = 'gfpdf_settings_' . $group_id;
$setting = "gfpdf_settings[$setting_id]";
/* Check if our setting exists */
if ( isset( $wp_settings_fields[ $group ][ $group ][ $setting ]['args'][ $option_id ] ) ) {
/* phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited */
$wp_settings_fields[ $group ][ $group ][ $setting ]['args'][ $option_id ] = $option_value;
return true;
}
return false;
}
/**
* Get Settings
*
* Retrieves all plugin settings
*
* @return array GFPDF settings
* @since 4.0
*
*/
public function get_settings() {
$is_temp = false;
if ( $this->misc->is_gfpdf_page() ) {
/*
* We are storing temporary settings in a transient when validation fails.
* This allows us to keep track of the updated fields without updating main settings in the DB
*
* We'll check if the transient exists and use it, otherwise get the main plugin settings from the options table
*/
$tmp_settings = get_transient( 'gfpdf_settings_user_data' );
$is_temp = $tmp_settings !== false;
if ( $is_temp ) {
delete_transient( 'gfpdf_settings_user_data' );
}
}
$settings = $is_temp ? (array) $tmp_settings : get_option( 'gfpdf_settings', [] );
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_get_settings/ for more details about this filter */
$settings = apply_filters( 'gfpdf_get_settings', $settings, $is_temp );
/* Ensure $settings is an array and has not been corrupted somehow */
$settings = is_array( $settings ) ? $settings : [];
return $settings;
}
/**
* Get form settings if on that page in the admin area (by having ID and PID set in the $_GET or $_POST variables)
* Use get_pdf( $form_id, $pdf_id ) if you want to get a particular PDF setting
*
* @return array The stored form settings
*
* @since 4.0
*/
public function get_form_settings() {
/* phpcs:disable WordPress.Security.NonceVerification.Recommended */
$form_id = ! empty( $_GET['id'] ) ? (int) rgget( 'id' ) : (int) rgpost( 'id' );
$pid = ! empty( $_GET['pid'] ) ? sanitize_html_class( rgget( 'pid' ) ) : sanitize_html_class( rgpost( 'gform_pdf_id' ) );
/* phpcs:enable */
/* return early if no ID set */
if ( ! $form_id || ! $pid ) {
return [];
}
$settings = $this->get_pdf( $form_id, $pid );
if ( ! is_wp_error( $settings ) ) {
/* get the selected form settings */
return $settings;
}
$this->log->error(
'Settings Retrieval Error',
[
'form_id' => $form_id,
'pid' => $pid,
'WP_Error_Message' => $settings->get_error_message(),
'WP_Error_Code' => $settings->get_error_code(),
]
);
/* there was an error */
return [];
}
/**
* Get Form Settings
*
* Retrieves all form PDF settings
*
* @param integer $form_id The Gravity Form ID
*
* @return array|WP_Error An array of GFPDF settings, or WP_Error
* @since 4.0
*
*/
public function get_form_pdfs( $form_id ) {
if ( ! isset( $this->data->form_settings ) ) {
$this->data->form_settings = [];
}
$form_id = (int) $form_id;
if ( 0 === $form_id ) {
$error = new WP_Error( 'invalid_id', esc_html__( 'You must pass in a valid form ID', 'gravity-pdf' ) );
$this->log->error(
'Error Getting Settings.',
[
'WP_Error_Message' => $error->get_error_message(),
'WP_Error_Code' => $error->get_error_code(),
]
);
return $error;
}
/* If we haven't pulled the form meta data from the database do so now */
if ( ! isset( $this->data->form_settings[ $form_id ] ) ) {
$form = $this->gform->get_form( $form_id );
if ( empty( $form ) ) {
$error = new WP_Error( 'invalid_id', esc_html__( 'You must pass in a valid form ID', 'gravity-pdf' ) );
$this->log->error(
'Error Getting Settings.',
[
'WP_Error_Message' => $error->get_error_message(),
'WP_Error_Code' => $error->get_error_code(),
]
);
return $error;
}
/* Pull the settings from the $form object, if they exist */
$settings = ( isset( $form['gfpdf_form_settings'] ) ) ? $form['gfpdf_form_settings'] : [];
$this->data->form_settings[ $form_id ] = $settings;
}
/* return the form meta data */
return $this->data->form_settings[ $form_id ];
}
/**
* Get pdf config
*
* Looks to see if the specified setting exists, returns default if not
*
* @param integer $form_id The Gravity Form ID
*
* @param string $pdf_id The Gravity Form PDF ID
*
* @return array|WP_Error
* @since 4.0
*
*/
public function get_pdf( $form_id, $pdf_id ) {
$gfpdf_options = $this->get_form_pdfs( $form_id );
if ( ! is_wp_error( $gfpdf_options ) ) {
/* Get our PDF array if it exists */
$pdf = ! empty( $gfpdf_options[ $pdf_id ] ) ? $gfpdf_options[ $pdf_id ] : new WP_Error( 'invalid_pdf_id', esc_html__( 'You must pass in a valid PDF ID', 'gravity-pdf' ) );
if ( ! is_wp_error( $pdf ) ) {
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_pdf_config/ for more details about these filters */
$pdf = apply_filters( 'gfpdf_pdf_config', $pdf, $form_id );
$pdf = apply_filters( 'gfpdf_pdf_config_' . $form_id, $pdf, $form_id );
return $pdf;
}
/* return WP_Error */
return $pdf;
}
/* return WP_Error */
return $gfpdf_options;
}
/**
* Create a new PDF configuration option for that form
*
* @param integer $form_id The form ID
* @param array $pdf The settings array
*
* @return mixed
*
* @since 4.0
*/
public function add_pdf( $form_id, $pdf = [] ) {
$options = $this->get_form_pdfs( $form_id );
if ( ! is_wp_error( $options ) ) {
/* check the ID, if any */
$pdf['id'] = ( isset( $pdf['id'] ) ) ? $pdf['id'] : uniqid();
$pdf['active'] = ( isset( $pdf['active'] ) ) ? $pdf['active'] : true;
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_form_add_pdf/ for more details about these filters */
$pdf = apply_filters( 'gfpdf_form_add_pdf', $pdf, $form_id );
$pdf = apply_filters( 'gfpdf_form_add_pdf_' . $form_id, $pdf, $form_id );
$results = $this->update_pdf( $form_id, $pdf['id'], $pdf, true, false );
if ( $results ) {
/* return the ID if successful */
$this->log->notice(
'Successfully Added New PDF',
[
'pdf' => $pdf,
]
);
return $pdf['id'];
}
$this->log->error(
'Error Saving New PDF',
[
'error' => $results,
'pdf' => $pdf,
]
);
}
return false;
}
/**
* Update an pdf config
*
* Updates a Gravity PDF setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the gfpdf_options array.
*
* @param integer $form_id The Gravity Form ID
* @param string $pdf_id The PDF Setting ID
* @param bool|int|string $pdf The PDF settings array
* @param bool $update_db Whether we should just update the local PDF settings array, or update the DB as well
* @param bool $filters Whether we should apply the update filters
*
* @return bool True if updated, false if not.
* @since 4.0
*
*/
public function update_pdf( $form_id, $pdf_id, $pdf = '', $update_db = true, $filters = true ) {
$this->log->notice(
'Begin Updating PDF Settings',
[
'form_id' => $form_id,
'pdf_id' => $pdf_id,
'new_settings' => $pdf,
]
);
if ( empty( $pdf ) || ! is_array( $pdf ) || count( $pdf ) === 0 ) {
/* No value was passed in so we will delete the PDF */
return $this->delete_pdf( $form_id, $pdf_id );
}
/* First let's grab the current settings */
$options = $this->get_form_pdfs( $form_id );
if ( ! is_wp_error( $options ) ) {
/* Don't run when adding a new PDF */
if ( $filters ) {
$this->log->notice( 'Run PDF Update Filters' );
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_form_update_pdf/ for more details about these filters */
$pdf = apply_filters( 'gfpdf_form_update_pdf', $pdf, $form_id, $pdf_id );
$pdf = apply_filters( 'gfpdf_form_update_pdf_' . $form_id, $pdf, $form_id, $pdf_id );
}
/* Next let's try to update the value */
$options[ $pdf_id ] = $pdf;
/* get the up-to-date form object and merge in the results */
$form = $this->gform->get_form( $form_id );
/* Update our GFPDF settings */
$form['gfpdf_form_settings'] = $options;
$did_update = false;
if ( $update_db ) {
$this->log->notice(
'Updating PDF Settings in Form Object',
[
'form_id' => $form['id'],
]
);
/* Update the database, if able */
$did_update = $this->gform->update_form( $form );
}
if ( ! $update_db || $did_update !== false ) {
$this->data->form_settings[ $form_id ] = $options;
}
return $did_update;
}
$this->log->notice( 'Completed Updating PDF Settings' );
return false;
}
/**
* Remove an option
*
* Removes an Gravity PDF setting value in both the db and the global variable.
*
* @param integer $form_id The Gravity Form ID
* @param string $pdf_id The Gravity Form PDF ID
*
* @return bool|WP_Error True if updated, false if not.
* @since 4.0
*
*/
public function delete_pdf( $form_id, $pdf_id ) {
$this->log->notice(
'Begin Deleting PDF Setting',
[
'form_id' => $form_id,
'pdf_id' => $pdf_id,
]
);
/* First let's grab the current settings */
$options = $this->get_form_pdfs( $form_id );
if ( ! is_wp_error( $options ) ) {
/* Next let's try to update the value */
if ( isset( $options[ $pdf_id ] ) ) {
unset( $options[ $pdf_id ] );
}
/* get the form and merge in the results */
$form = $this->gform->get_form( $form_id );
/* Update our GFPDF settings */
$form['gfpdf_form_settings'] = $options;
/* update the database, if able */
$did_update = $this->gform->update_form( $form );
/* If it updated, let's update the global variable */
if ( $did_update !== false ) {
$this->log->notice(
'Completed Deleting PDF Setting',
[
'form_id' => $form_id,
'pdf_id' => $pdf_id,
]
);
$this->data->form_settings[ $form_id ] = $options;
}
return $did_update;
}
$this->log->error(
'Failed Deleting PDF Setting',
[
'form_id' => $form_id,
'pdf_id' => $pdf_id,
]
);
return false;
}
/**
* Get a global setting option
*
* Looks to see if the specified setting exists, returns default if not
*
* @param string $key The options key to get
* @param bool $fallback The default option value if the key isn't found
*
* @return mixed
* @since 4.0
*
*/
public function get_option( $key = '', $fallback = false ) {
$gfpdf_options = $this->settings;
$value = ( ! empty( $gfpdf_options[ $key ] ) ) ? $gfpdf_options[ $key ] : $fallback;
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_get_option/ for more details about these filters */
$value = apply_filters( 'gfpdf_get_option', $value, $key, $fallback );
$value = apply_filters( 'gfpdf_get_option_' . $key, $value, $key, $fallback );
return $value;
}
/**
* Update a global setting option
*
* Updates a Gravity PDF setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the gfpdf_options array.
*
* @param string $key The Key to update
* @param string|bool|int $value The value to set the key to
*
* @return boolean True if updated, false if not.
* @since 4.0
*
*/
public function update_option( $key = '', $value = false ) {
if ( empty( $key ) ) {
$this->log->error(
'Empty Option Key',
[
'value' => $value,
]
);
return false;
}
if ( empty( $value ) ) {
return $this->delete_option( $key );
}
/* First let's grab the current settings */
$options = get_option( 'gfpdf_settings', [] );
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_update_option/ for more details about these filters */
$value = apply_filters( 'gfpdf_update_option', $value, $key );
$value = apply_filters( 'gfpdf_update_option_' . $key, $value, $key );
/* Disable default sanitization (it shouldn't be triggered through this method) */
remove_filter( 'sanitize_option_gfpdf_settings', [ $this, 'settings_sanitize' ] );
/* Next let's try to update the value */
$options[ $key ] = $value;
$did_update = update_option( 'gfpdf_settings', $options );
/* Re-enable sanitization */
add_filter( 'sanitize_option_gfpdf_settings', [ $this, 'settings_sanitize' ] );
/* If it updated, let's update the global variable */
if ( $did_update ) {
$this->settings[ $key ] = $value;
}
return $did_update;
}
/**
* Remove a global setting option
*
* Removes an Gravity PDF setting value in both the db and the global variable.
*
* @param string $key The Key to delete
*
* @return boolean True if updated, false if not.
* @since 4.0
*
*/
public function delete_option( $key = '' ) {
if ( empty( $key ) ) {
$this->log->error( 'Option Delete Error' );
return false;
}
// First let's grab the current settings
$options = get_option( 'gfpdf_settings', [] );
// Next let's try to update the value
if ( isset( $options[ $key ] ) ) {
unset( $options[ $key ] );
}
$did_update = update_option( 'gfpdf_settings', $options );
if ( $did_update ) {
$this->settings = $options;
}
return $did_update;
}
/**
* Get a list of user capabilities
*
* @return array The array of roles available
*
* @since 4.0
*/
public function get_capabilities() {
/* function only exists on admin requests */
$roles = function_exists( 'get_editable_roles' ) ? get_editable_roles() : [];
$capabilities = [];
/* Add Gravity Forms Capabilities */
$gf_caps = $this->gform->get_capabilities();
foreach ( $gf_caps as $gf_cap ) {
$capabilities[ $gf_cap ] = $gf_cap;
}
foreach ( $roles as $role ) {
if ( isset( $role['capabilities'] ) && is_array( $role['capabilities'] ) ) {
foreach ( $role['capabilities'] as $cap => $val ) {
if ( ! isset( $capabilities[ $cap ] ) && ! in_array( $cap, $gf_caps, true ) ) {
$capabilities[ $cap ] = $cap;
}
}
}
}
/* See https://docs.gravitypdf.com/developers/filters/gfpdf_capabilities/ for more details about this filter */
return apply_filters( 'gfpdf_capabilities', $capabilities );
}
/**
* Return our paper size
*
* @return array The array of paper sizes available
*
* @since 4.0
*/
public function get_paper_size() {
return apply_filters(
'gfpdf_get_paper_size',
[
esc_html__( 'Common Sizes', 'gravity-pdf' ) => [
'A4' => esc_html__( 'A4 (210 x 297mm)', 'gravity-pdf' ),
'LETTER' => esc_html__( 'Letter (8.5 x 11in)', 'gravity-pdf' ),
'LEGAL' => esc_html__( 'Legal (8.5 x 14in)', 'gravity-pdf' ),
'LEDGER' => esc_html__( 'Ledger / Tabloid (11 x 17in)', 'gravity-pdf' ),
'EXECUTIVE' => esc_html__( 'Executive (7 x 10in)', 'gravity-pdf' ),
'CUSTOM' => esc_html__( 'Custom Paper Size', 'gravity-pdf' ),
],
esc_html__( '"A" Sizes', 'gravity-pdf' ) => [
'A0' => esc_html__( 'A0 (841 x 1189mm)', 'gravity-pdf' ),
'A1' => esc_html__( 'A1 (594 x 841mm)', 'gravity-pdf' ),
'A2' => esc_html__( 'A2 (420 x 594mm)', 'gravity-pdf' ),
'A3' => esc_html__( 'A3 (297 x 420mm)', 'gravity-pdf' ),
'A5' => esc_html__( 'A5 (148 x 210mm)', 'gravity-pdf' ),
'A6' => esc_html__( 'A6 (105 x 148mm)', 'gravity-pdf' ),
'A7' => esc_html__( 'A7 (74 x 105mm)', 'gravity-pdf' ),
'A8' => esc_html__( 'A8 (52 x 74mm)', 'gravity-pdf' ),
'A9' => esc_html__( 'A9 (37 x 52mm)', 'gravity-pdf' ),
'A10' => esc_html__( 'A10 (26 x 37mm)', 'gravity-pdf' ),
],
esc_html__( '"B" Sizes', 'gravity-pdf' ) => [
'B0' => esc_html__( 'B0 (1414 x 1000mm)', 'gravity-pdf' ),
'B1' => esc_html__( 'B1 (1000 x 707mm)', 'gravity-pdf' ),
'B2' => esc_html__( 'B2 (707 x 500mm)', 'gravity-pdf' ),
'B3' => esc_html__( 'B3 (500 x 353mm)', 'gravity-pdf' ),
'B4' => esc_html__( 'B4 (353 x 250mm)', 'gravity-pdf' ),
'B5' => esc_html__( 'B5 (250 x 176mm)', 'gravity-pdf' ),
'B6' => esc_html__( 'B6 (176 x 125mm)', 'gravity-pdf' ),
'B7' => esc_html__( 'B7 (125 x 88mm)', 'gravity-pdf' ),
'B8' => esc_html__( 'B8 (88 x 62mm)', 'gravity-pdf' ),
'B9' => esc_html__( 'B9 (62 x 44mm)', 'gravity-pdf' ),
'B10' => esc_html__( 'B10 (44 x 31mm)', 'gravity-pdf' ),
],
esc_html__( '"C" Sizes', 'gravity-pdf' ) => [
'C0' => esc_html__( 'C0 (1297 x 917mm)', 'gravity-pdf' ),
'C1' => esc_html__( 'C1 (917 x 648mm)', 'gravity-pdf' ),
'C2' => esc_html__( 'C2 (648 x 458mm)', 'gravity-pdf' ),
'C3' => esc_html__( 'C3 (458 x 324mm)', 'gravity-pdf' ),
'C4' => esc_html__( 'C4 (324 x 229mm)', 'gravity-pdf' ),
'C5' => esc_html__( 'C5 (229 x 162mm)', 'gravity-pdf' ),
'C6' => esc_html__( 'C6 (162 x 114mm)', 'gravity-pdf' ),
'C7' => esc_html__( 'C7 (114 x 81mm)', 'gravity-pdf' ),
'C8' => esc_html__( 'C8 (81 x 57mm)', 'gravity-pdf' ),
'C9' => esc_html__( 'C9 (57 x 40mm)', 'gravity-pdf' ),
'C10' => esc_html__( 'C10 (40 x 28mm)', 'gravity-pdf' ),
],
esc_html__( '"RA" and "SRA" Sizes', 'gravity-pdf' ) => [
'RA0' => esc_html__( 'RA0 (860 x 1220mm)', 'gravity-pdf' ),
'RA1' => esc_html__( 'RA1 (610 x 860mm)', 'gravity-pdf' ),
'RA2' => esc_html__( 'RA2 (430 x 610mm)', 'gravity-pdf' ),
'RA3' => esc_html__( 'RA3 (305 x 430mm)', 'gravity-pdf' ),
'RA4' => esc_html__( 'RA4 (215 x 305mm)', 'gravity-pdf' ),
'SRA0' => esc_html__( 'SRA0 (900 x 1280mm)', 'gravity-pdf' ),
'SRA1' => esc_html__( 'SRA1 (640 x 900mm)', 'gravity-pdf' ),
'SRA2' => esc_html__( 'SRA2 (450 x 640mm)', 'gravity-pdf' ),
'SRA3' => esc_html__( 'SRA3 (320 x 450mm)', 'gravity-pdf' ),
'SRA4' => esc_html__( 'SRA4 (225 x 320mm)', 'gravity-pdf' ),
],
]
);
}
/**
* Parse our installed font files
*
* @return array The array of fonts
*
* @since 4.0
*/
public function get_installed_fonts() {
$fonts = [
esc_html__( 'Unicode', 'gravity-pdf' ) => [
'dejavusanscondensed' => 'Dejavu Sans Condensed',
'dejavusans' => 'Dejavu Sans',
'dejavuserifcondensed' => 'Dejavu Serif Condensed',
'dejavuserif' => 'Dejavu Serif',
'dejavusansmono' => 'Dejavu Sans Mono',
'freesans' => 'Free Sans',
'freeserif' => 'Free Serif',
'freemono' => 'Free Mono',
'mph2bdamase' => 'MPH 2B Damase',
],
esc_html__( 'Indic', 'gravity-pdf' ) => [
'lohitkannada' => 'Lohit Kannada',
'pothana2000' => 'Pothana2000',
],
esc_html__( 'Arabic', 'gravity-pdf' ) => [
'xbriyaz' => 'XB Riyaz',
'lateef' => 'Lateef',
'kfgqpcuthmantahanaskh' => 'Bahif Uthman Taha',
],
esc_html__( 'Chinese, Japanese, Korean', 'gravity-pdf' ) => [
'sun-exta' => 'Sun Ext',
'unbatang' => 'Un Batang (Korean)',
],
esc_html__( 'Other', 'gravity-pdf' ) => [
'estrangeloedessa' => 'Estrangelo Edessa (Syriac)',
'kaputaunicode' => 'Kaputa (Sinhala)',
'abyssinicasil' => 'Abyssinica SIL (Ethiopic)',
'aboriginalsans' => 'Aboriginal Sans (Cherokee / Canadian)',
'jomolhari' => 'Jomolhari (Tibetan)',
'sundaneseunicode' => 'Sundanese (Sundanese)',
'taiheritagepro' => 'Tai Heritage Pro (Tai Viet)',
'aegyptus' => 'Aegyptus (Egyptian Hieroglyphs)',
'akkadian' => 'Akkadian (Cuneiform)',
'aegean' => 'Aegean (Greek)',
'quivira' => 'Quivira (Greek)',
'eeyekunicode' => 'Eeyek (Meetei Mayek)',
'lannaalif' => 'Lanna Alif (Tai Tham)',
'daibannasilbook' => 'Dai Banna SIL (New Tai Lue)',
'garuda' => 'Garuda (Thai)',
'khmeros' => 'Khmer OS (Khmer)',
'dhyana' => 'Dhyana (Lao)',
'tharlon' => 'TharLon (Myanmar / Burmese)',
'padaukbook' => 'Padauk Book (Myanmar / Burmese)',
'zawgyi-one' => 'Zawgyi One (Myanmar / Burmese)',
'ayar' => 'Ayar Myanmar (Myanmar / Burmese)',
'taameydavidclm' => 'Taamey David CLM (Hebrew)',
],
];
$fonts = $this->add_custom_fonts( $fonts );
return apply_filters( 'gfpdf_font_list', $fonts );
}
/**
* If any custom fonts add them to our font list
*
* @param array $fonts Current font list
*
* @return array The list of custom fonts installed in a preformatted array
*
* @since 4.0
*/
public function add_custom_fonts( $fonts = [] ) {
$custom_fonts = $this->get_custom_fonts();
if ( count( $custom_fonts ) > 0 ) {
$user_defined_fonts = [];
/* Loop through our fonts and assign them to a new array in the appropriate format */
foreach ( $custom_fonts as $font ) {
$user_defined_fonts[ $font['id'] ] = $font['font_name'];
}