-
Notifications
You must be signed in to change notification settings - Fork 48
/
wpo-ips-functions.php
1029 lines (880 loc) · 32 KB
/
wpo-ips-functions.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
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
|--------------------------------------------------------------------------
| Document getter functions
|--------------------------------------------------------------------------
|
| Global functions to get the document object for an order
|
*/
function wcpdf_filter_order_ids( $order_ids, $document_type ) {
$order_ids = apply_filters( 'wpo_wcpdf_process_order_ids', $order_ids, $document_type );
// filter out trashed orders.
foreach ( $order_ids as $key => $order_id ) {
$order = wc_get_order( $order_id );
if ( ! empty( $order ) && is_callable( array( $order, 'get_status' ) ) && $order->get_status() == 'trash' ) {
unset( $order_ids[ $key ] );
}
}
return $order_ids;
}
/**
* Get the document object for an order
*
* @param string $document_type
* @param mixed $order
* Passing an order object will return the document object for that order.
* Passing an array of order ids will return a BulkDocument object.
* Passing a single order ID within an array retrieves the document object for that order and refreshes the order object to ensure the data is up-to-date.
* Passing null will return a document object without an order.
*
* @param bool $init
*
* @return object|false
*/
function wcpdf_get_document( string $document_type, $order, bool $init = false ) {
if ( ! empty( $order ) ) {
if ( ! is_object( $order ) && ! is_array( $order ) && is_numeric( $order ) ) {
$order = array( absint( $order ) ); // convert single order id to array.
}
if ( is_object( $order ) ) {
// we filter order_ids for objects too:
// an order object may need to be converted to several refunds for example.
$order_ids = array( $order->get_id() );
$filtered_order_ids = wcpdf_filter_order_ids( $order_ids, $document_type );
// check if something has changed.
$order_id_diff = array_diff( $filtered_order_ids, $order_ids );
if ( empty( $order_id_diff ) && count( $order_ids ) == count( $filtered_order_ids ) ) {
// nothing changed, load document with Order object.
do_action( 'wpo_wcpdf_process_template_order', $document_type, $order->get_id() );
$document = WPO_WCPDF()->documents->get_document( $document_type, $order );
if ( ! $document || ! is_callable( array( $document, 'is_allowed' ) ) || ! $document->is_allowed() ) {
return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init );
}
if ( $init && ! $document->exists() ) {
$document->init();
$document->save();
}
return apply_filters( 'wcpdf_get_document', $document, $document_type, $order, $init );
} else {
// order ids array changed, continue processing that array.
$order_ids = $filtered_order_ids;
}
} elseif ( is_array( $order ) ) {
$order_ids = wcpdf_filter_order_ids( $order, $document_type );
} else {
return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init );
}
if ( empty( $order_ids ) ) {
// No orders to export for this document type.
return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init );
}
// if we only have one order, it's simple.
if ( count( $order_ids ) == 1 ) {
$order_id = array_pop( $order_ids );
$order = wc_get_order( $order_id );
do_action( 'wpo_wcpdf_process_template_order', $document_type, $order_id );
$document = WPO_WCPDF()->documents->get_document( $document_type, $order );
if ( ! $document || ! $document->is_allowed() ) {
return apply_filters( 'wcpdf_get_document', false, $document_type, $order, $init );
}
if ( $init && ! $document->exists() ) {
$document->init();
$document->save();
}
// otherwise we use bulk class to wrap multiple documents in one.
} else {
$document = wcpdf_get_bulk_document( $document_type, $order_ids );
}
} else {
// orderless document (used as wrapper for bulk, for example).
$document = WPO_WCPDF()->documents->get_document( $document_type, $order );
}
return apply_filters( 'wcpdf_get_document', $document, $document_type, $order, $init );
}
function wcpdf_get_bulk_document( $document_type, $order_ids ) {
return new \WPO\IPS\Documents\BulkDocument( $document_type, $order_ids );
}
function wcpdf_get_invoice( $order, $init = false ) {
return wcpdf_get_document( 'invoice', $order, $init );
}
function wcpdf_get_packing_slip( $order, $init = false ) {
return wcpdf_get_document( 'packing-slip', $order, $init );
}
function wcpdf_get_bulk_actions() {
$actions = array();
$documents = WPO_WCPDF()->documents->get_documents( 'enabled', 'any' );
foreach ( $documents as $document ) {
foreach ( $document->output_formats as $output_format ) {
$slug = $document->get_type();
if ( 'pdf' !== $output_format ) {
$slug .= "_{$output_format}";
}
if ( $document->is_enabled( $output_format ) ) {
$actions[$slug] = strtoupper( $output_format ) . ' ' . $document->get_title();
}
}
}
return apply_filters( 'wpo_wcpdf_bulk_actions', $actions );
}
/**
* Load HTML into (pluggable) PDF library, DomPDF 1.0.2 by default
* Use wpo_wcpdf_pdf_maker filter to change the PDF class (which can wrap another PDF library).
*
* @param string $html
* @param array $settings
* @param null|object $document
* @return WPO\IPS\Makers\PDFMaker
*/
function wcpdf_get_pdf_maker( $html, $settings = array(), $document = null ) {
$class = '\\WPO\\IPS\\Makers\\PDFMaker';
if ( ! class_exists( $class ) ) {
include_once( WPO_WCPDF()->plugin_path() . '/includes/Makers/PDFMaker.php' );
}
$class = apply_filters( 'wpo_wcpdf_pdf_maker', $class );
return new $class( $html, $settings, $document );
}
/**
* Get UBL Maker
* Use wpo_wcpdf_ubl_maker filter to change the UBL class (which can wrap another UBL library).
*
* @return WPO\IPS\Makers\UBLMaker
*/
function wcpdf_get_ubl_maker() {
$class = '\\WPO\\IPS\\Makers\\UBLMaker';
if ( ! class_exists( $class ) ) {
include_once( WPO_WCPDF()->plugin_path() . '/includes/Makers/UBLMaker.php' );
}
$class = apply_filters( 'wpo_wcpdf_ubl_maker', $class );
return new $class();
}
/**
* Check if UBL is available
*
* @return bool
*/
function wcpdf_is_ubl_available(): bool {
// Check `sabre/xml` library here: https://packagist.org/packages/sabre/xml
return apply_filters( 'wpo_wcpdf_ubl_available', WPO_WCPDF()->is_dependency_version_supported( 'php' ) );
}
/**
* Check if the default PDF maker is used for creating PDF
*
* @return bool whether the PDF maker is the default or not
*/
function wcpdf_pdf_maker_is_default() {
$default_pdf_maker = '\\WPO\\IPS\\Makers\\PDFMaker';
return $default_pdf_maker == apply_filters( 'wpo_wcpdf_pdf_maker', $default_pdf_maker );
}
function wcpdf_pdf_headers( $filename, $mode = 'inline', $pdf = null ) {
switch ( $mode ) {
case 'download':
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/pdf' );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Connection: Keep-Alive' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
break;
case 'inline':
default:
header( 'Content-type: application/pdf' );
header( 'Content-Disposition: inline; filename="' . $filename . '"' );
break;
}
do_action( 'wpo_wcpdf_headers', $filename, $mode, $pdf );
}
function wcpdf_ubl_headers( $filename, $size ) {
$charset = apply_filters( 'wcpdf_ubl_headers_charset', 'UTF-8' );
header( 'Content-Description: File Transfer' );
header( 'Content-Type: text/xml; charset=' . $charset );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Transfer-Encoding: binary' );
header( 'Connection: Keep-Alive' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
header( 'Content-Length: ' . $size );
do_action( 'wpo_after_ubl_headers', $filename, $size );
}
/**
* Get the document file
*
* @param object $document
* @param string $output_format
* @param string $error_handling
* @return string
*/
function wcpdf_get_document_file( object $document, string $output_format = 'pdf', $error_handling = 'exception' ): string {
$default_output_format = 'pdf';
if ( ! $document ) {
$error_message = 'No document object provided.';
return wcpdf_error_handling( $error_message, $error_handling, true, 'critical' );
}
if ( empty( $output_format ) ) {
$output_format = $default_output_format;
}
if ( ! in_array( $output_format, $document->output_formats ) ) {
$error_message = "Invalid output format: {$output_format}. Expected one of: " . implode( ', ', $document->output_formats );
return wcpdf_error_handling( $error_message, $error_handling, true, 'critical' );
}
$tmp_path = WPO_WCPDF()->main->get_tmp_path( 'attachments' );
if ( ! @is_dir( $tmp_path ) || ! wp_is_writable( $tmp_path ) ) {
$error_message = "Couldn't get the attachments temporary folder path: {$tmp_path}.";
return wcpdf_error_handling( $error_message, $error_handling, true, 'critical' );
}
$function = "get_document_{$output_format}_attachment";
if ( ! is_callable( array( WPO_WCPDF()->main, $function ) ) ) {
$error_message = "The {$function} method is not callable on WPO_WCPDF()->main.";
return wcpdf_error_handling( $error_message, $error_handling, true, 'critical' );
}
$file_path = WPO_WCPDF()->main->$function( $document, $tmp_path );
return apply_filters( 'wpo_wcpdf_get_document_file', $file_path, $document, $output_format );
}
/**
* Get the document output format extension
*
* @param string $output_format
* @return string
*/
function wcpdf_get_document_output_format_extension( string $output_format ): string {
$output_formats = array(
'pdf' => '.pdf',
'ubl' => '.xml',
);
return isset( $output_formats[ $output_format ] ) ? $output_formats[ $output_format ] : $output_formats['pdf'];
}
/**
* Wrapper for deprecated functions so we can apply some extra logic.
*
* @since 2.0
* @param string $function
* @param string $version
* @param string $replacement
*/
function wcpdf_deprecated_function( $function, $version, $replacement = null ) {
if ( apply_filters( 'wcpdf_disable_deprecation_notices', false ) ) {
return;
}
// if the deprecated function is called from one of our filters, $this should be $document.
$filter = current_filter();
$global_wcpdf_filters = array( 'wp_ajax_generate_wpo_wcpdf' );
if ( ! empty( $filter ) && ! empty( $replacement ) && ! in_array( $filter, $global_wcpdf_filters ) && false !== strpos( $filter, 'wpo_wcpdf' ) && false !== strpos( $replacement, '$this' ) ) {
$replacement = str_replace( '$this', '$document', $replacement );
$replacement = "{$replacement} - check that the \$document parameter is included in your action or filter ($filter)!";
}
$is_ajax = function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : defined( 'DOING_AJAX' ) && DOING_AJAX;
if ( $is_ajax ) {
do_action( 'deprecated_function_run', $function, $replacement, $version );
$log_string = "The {$function} function is deprecated since version {$version}.";
$log_string .= $replacement ? " Replace with {$replacement}." : '';
error_log( $log_string );
wcpdf_log_error( $log_string, 'warning' );
} else {
_deprecated_function( $function, $version, $replacement );
}
}
/**
* Logger function to capture errors thrown by this plugin, uses the WC Logger when possible (WC3.0+)
*/
function wcpdf_log_error( $message, $level = 'error', $e = null ) {
if ( function_exists( 'wc_get_logger' ) ) {
$logger = wc_get_logger();
$context = array( 'source' => 'wpo-wcpdf' );
if ( is_callable( array( $e, 'getFile' ) ) && is_callable( array( $e, 'getLine' ) ) ) {
$message = sprintf( '%s (%s:%d)', $message, $e->getFile(), $e->getLine() );
}
if ( apply_filters( 'wcpdf_log_stacktrace', false ) && is_callable( array( $e, 'getTraceAsString' ) ) ) {
$message .= "\n" . $e->getTraceAsString();
}
// The `log` method accepts any valid level as its first argument.
// debug - 'Detailed debug information'
// info - 'Interesting events'
// notice - 'Normal but significant events'
// warning - 'Exceptional occurrences that are not errors'
// error - 'Runtime errors that do not require immediate'
// critical - 'Critical conditions'
// alert - 'Action must be taken immediately'
// emergency - 'System is unusable'.
$logger->log( $level, $message, $context );
} else {
error_log( "WCPDF error ({$level}): {$message}" );
}
}
function wcpdf_output_error( $message, $level = 'error', $e = null ) {
if ( ! current_user_can( 'edit_shop_orders' ) ) {
esc_html_e( 'Error creating PDF, please contact the site owner.', 'woocommerce-pdf-invoices-packing-slips' );
return;
}
?>
<div style="border: 2px solid red; padding: 5px;">
<h3><?php echo wp_kses_post( $message ); ?></h3>
<?php if ( is_callable( array( $e, 'getFile' ) ) && is_callable( array( $e, 'getLine' ) ) ): ?>
<pre><?php echo esc_html( $e->getFile() ); ?> (<?php echo esc_html( $e->getLine() ); ?>)</pre>
<?php endif ?>
<?php if ( is_callable( array( $e, 'getTraceAsString' ) ) ) : ?>
<pre><?php echo esc_html( $e->getTraceAsString() ); ?></pre>
<?php endif ?>
</div>
<?php
}
/**
* Error handling function
*
* @param string $message
* @param string $handling_type
* @param bool $log_error
* @param string $log_level
* @return mixed
* @throws Exception
*/
function wcpdf_error_handling( string $message, string $handling_type = 'exception', bool $log_error = true, string $log_level = 'error' ) {
if ( $log_error ) {
wcpdf_log_error( $message, $log_level );
}
switch ( $handling_type ) {
case 'exception':
throw new \Exception( $message );
break;
case 'output':
wcpdf_output_error( $message, $log_level );
break;
}
return false;
}
/**
* Date formatting function
*
* @param object $document
* @param string $date_type Optional. A date type to be filtered eg. 'invoice_date', 'order_date_created', 'order_date_modified', 'order_date', 'order_date_paid', 'order_date_completed', 'current_date', 'document_date', 'packing_slip_date'.
*/
function wcpdf_date_format( $document = null, $date_type = null ) {
return apply_filters( 'wpo_wcpdf_date_format', wc_date_format(), $document, $date_type );
}
/**
* Catch MySQL errors
*
* Inspired from here: https://github.com/johnbillion/query-monitor/blob/d5b622b91f18552e7105e62fa84d3102b08975a4/collectors/db_queries.php#L125-L280
*
* With SAVEQUERIES constant defined as 'false', '$wpdb->queries' is empty and '$EZSQL_ERROR' is used instead.
* Using the Query Monitor plugin, the SAVEQUERIES constant is defined as 'true'
* More info about this constant can be found here: https://wordpress.org/support/article/debugging-in-wordpress/#savequeries
*
* @param object $wpdb
* @return array errors found
*/
function wcpdf_catch_db_object_errors( $wpdb ) {
global $EZSQL_ERROR;
$errors = array();
// using '$wpdb->queries'.
if ( ! empty( $wpdb->queries ) && is_array( $wpdb->queries ) ) {
foreach ( $wpdb->queries as $query ) {
$result = isset( $query['result'] ) ? $query['result'] : null;
if ( is_wp_error( $result ) && is_array( $result->errors ) ) {
foreach ( $result->errors as $error ) {
$errors[] = reset( $error );
}
}
}
}
// fallback to '$EZSQL_ERROR'.
if ( empty( $errors ) && ! empty( $EZSQL_ERROR ) && is_array( $EZSQL_ERROR ) ) {
foreach ( $EZSQL_ERROR as $error ) {
$errors[] = $error['error_str'];
}
}
// log errors.
if ( ! empty( $errors ) ) {
foreach ( $errors as $error_message ) {
wcpdf_log_error( $error_message, 'critical' );
}
}
return $errors;
}
/**
* String convert encoding.
*
* @param string $string
* @param string $tool
* @return string
*/
function wcpdf_convert_encoding( $string, $tool = 'mb_convert_encoding' ) {
if ( empty( $string ) ) {
return $string;
}
$tool = apply_filters( 'wpo_wcpdf_convert_encoding_tool', $tool );
$from_encoding = apply_filters( 'wpo_wcpdf_convert_from_encoding', 'UTF-8', $tool );
switch ( $tool ) {
case 'mb_convert_encoding':
$to_encoding = apply_filters( 'wpo_wcpdf_convert_to_encoding', 'HTML-ENTITIES', $tool );
// provided by composer 'symfony/polyfill-mbstring' library.
// it uses 'iconv()', must have 'libiconv' configured instead of 'glibc' library.
if ( class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) {
$string = \Symfony\Polyfill\Mbstring\Mbstring::mb_convert_encoding( $string, $to_encoding, $from_encoding );
}
break;
case 'uconverter':
$to_encoding = apply_filters( 'wpo_wcpdf_convert_to_encoding', 'HTML-ENTITIES', $tool );
// only for PHP 8.2+.
if ( version_compare( PHP_VERSION, '8.1', '>' ) && class_exists( 'UConverter' ) && extension_loaded( 'intl' ) ) {
$string = UConverter::transcode( $string, $to_encoding, $from_encoding );
}
break;
case 'iconv':
$to_encoding = apply_filters( 'wpo_wcpdf_convert_to_encoding', 'ISO-8859-1', $tool );
// provided by composer 'symfony/polyfill-iconv' library.
if ( class_exists( '\\Symfony\\Polyfill\\Iconv\\Iconv' ) ) {
$string = \Symfony\Polyfill\Iconv\Iconv::iconv( $from_encoding, $to_encoding, $string );
// default server library.
// must have 'libiconv' configured instead of 'glibc' library.
} elseif ( function_exists( 'iconv' ) ) {
$string = iconv( $from_encoding, $to_encoding, $string );
}
break;
}
return $string;
}
/**
* Sanitize HTML content, prevents XSS attacks.
*
* @param string $html
* @param string $context
* @param array $allow_tags
*
* @return string
*/
function wpo_wcpdf_sanitize_html_content( string $html, string $context = '', array $allow_tags = array() ): string {
if ( empty( $html ) ) {
return $html;
}
// default allowed tags
$allow_tags = array_merge( apply_filters( 'wpo_wcpdf_sanitize_html_default_allow_tags', array(
// tag => allowed attributes eg. array( 'href', 'title' ) in case of a <a> tag.
'br' => array(),
'em' => array(),
'strong' => array(),
'p' => array(),
), $context ), $allow_tags );
$safe_tags = array(
'b' => array(),
'blockquote' => array(),
'br' => array(),
'em' => array(),
'i' => array(),
'li' => array(),
'ol' => array(),
'p' => array(),
'strong' => array(),
'u' => array(),
'ul' => array(),
'span' => array( 'style' ),
'h1' => array(),
'h2' => array(),
'h3' => array(),
'h4' => array(),
'h5' => array(),
'h6' => array(),
'div' => array( 'style' ),
'table' => array( 'border', 'cellspacing', 'cellpadding' ),
'tr' => array(),
'td' => array( 'colspan', 'rowspan' ),
'th' => array( 'colspan', 'rowspan', 'scope' ),
'thead' => array(),
'tbody' => array(),
'tfoot' => array(),
'code' => array(),
'pre' => array(),
'dl' => array(),
'dt' => array(),
'dd' => array(),
'hr' => array(),
'sup' => array(),
'sub' => array(),
'figure' => array(),
'figcaption' => array(),
'abbr' => array( 'title' ),
);
$filtered_tags = array();
foreach ( $allow_tags as $tag => $attributes ) {
if ( array_key_exists( $tag, $safe_tags ) ) {
$safe_attributes = array_intersect( $attributes, $safe_tags[ $tag ] );
$filtered_tags[ $tag ] = ! empty( $safe_attributes ) ? $safe_attributes : array();
}
}
if ( empty( $filtered_tags ) ) {
return $html;
}
$dom = new \DOMDocument();
// clean up special chars
if ( apply_filters( 'wpo_wcpdf_convert_encoding', function_exists( 'htmlspecialchars_decode' ) ) ) {
$html = htmlspecialchars_decode( wcpdf_convert_encoding( $html ), ENT_QUOTES );
}
libxml_use_internal_errors( true ); // suppress malformed HTML errors
@$dom->loadHTML( '<div>' . $html . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
libxml_clear_errors();
$extra_wrapper = $dom->getElementsByTagName( 'div' )->item( 0 );
$content = ! empty( $extra_wrapper ) ? $extra_wrapper->parentNode->removeChild( $extra_wrapper ) : null;
if ( ! empty( $content ) ) {
// Clear DOM by removing all nodes from it.
while ( $dom->firstChild ) {
$dom->removeChild( $dom->firstChild );
}
// Append the content to the DOM to remove the extra DIV wrapper.
while ( $content->firstChild ) {
$dom->appendChild( $content->firstChild );
}
}
$xpath = new \DOMXPath( $dom );
// iterate over all nodes.
foreach ( $xpath->query( '//*' ) as $node ) {
// check if the node is allowed.
if ( array_key_exists( $node->nodeName, $filtered_tags ) ) {
// if the node is allowed, check each attribute.
foreach ( $node->attributes as $attr ) {
if ( ! in_array( $attr->nodeName, $filtered_tags[ $node->nodeName ] ) ) {
$node->removeAttribute( $attr->nodeName );
}
}
} else {
// if the node is not allowed, remove it but try to preserve text.
if ( $node->parentNode ) {
$fragment = $dom->createDocumentFragment();
while ( $node->childNodes->length > 0 ) {
$fragment->appendChild( $node->childNodes->item( 0 ) );
}
if ( $fragment->hasChildNodes() ) {
$node->parentNode->replaceChild( $fragment, $node );
} else {
$node->parentNode->removeChild( $node );
}
}
}
}
$html = $dom->saveHTML();
if ( empty( $html ) ) {
return '';
}
return trim( $html );
}
/**
* Sanitize phone number
*
* @param string $text
*
* @return string
*/
function wpo_wcpdf_sanitize_phone_number( string $text ): string {
return preg_replace( '/[^0-9\+\-\(\)\s\.x]/', '', $text );
}
/**
* Safe redirect or die.
*
* @param string $url
* @param string|WP_Error $message
* @return void
*/
function wcpdf_safe_redirect_or_die( $url = '', $message = '' ) {
if ( ! empty( $url ) ) {
wp_safe_redirect( $url );
exit;
} else {
wp_die( $message );
}
}
/**
* Parse document date for WP_Query.
*
* @param array $wp_query_args
* @param array $query_args
*
* @return array
*/
function wpo_wcpdf_parse_document_date_for_wp_query( array $wp_query_args, array $query_vars ): array {
$documents = WPO_WCPDF()->documents->get_documents();
if ( ! empty( $documents ) ) {
foreach ( $documents as $document ) {
if ( ! empty( $query_vars[ "wcpdf_{$document->slug}_date" ] ) ) {
$wp_query_args = ( new \WC_Order_Data_Store_CPT() )->parse_date_for_wp_query( $query_vars[ "wcpdf_{$document->slug}_date" ], "_wcpdf_{$document->slug}_date", $wp_query_args );
if ( isset( $wp_query_args[ "wcpdf_{$document->slug}_date" ] ) ) {
unset( $wp_query_args[ "wcpdf_{$document->slug}_date" ] );
}
}
}
}
return $wp_query_args;
}
/**
* Get multilingual languages.
*
* @return array
*/
function wpo_wcpdf_get_multilingual_languages(): array {
$languages = array();
// refers to WPML or Polylang only
if ( function_exists( 'icl_get_languages' ) ) {
// use this instead of function call for development outside of WPML
// $icl_get_languages = 'a:3:{s:2:"en";a:8:{s:2:"id";s:1:"1";s:6:"active";s:1:"1";s:11:"native_name";s:7:"English";s:7:"missing";s:1:"0";s:15:"translated_name";s:7:"English";s:13:"language_code";s:2:"en";s:16:"country_flag_url";s:43:"http://yourdomain/wpmlpath/res/flags/en.png";s:3:"url";s:23:"http://yourdomain/about";}s:2:"fr";a:8:{s:2:"id";s:1:"4";s:6:"active";s:1:"0";s:11:"native_name";s:9:"Français";s:7:"missing";s:1:"0";s:15:"translated_name";s:6:"French";s:13:"language_code";s:2:"fr";s:16:"country_flag_url";s:43:"http://yourdomain/wpmlpath/res/flags/fr.png";s:3:"url";s:29:"http://yourdomain/fr/a-propos";}s:2:"it";a:8:{s:2:"id";s:2:"27";s:6:"active";s:1:"0";s:11:"native_name";s:8:"Italiano";s:7:"missing";s:1:"0";s:15:"translated_name";s:7:"Italian";s:13:"language_code";s:2:"it";s:16:"country_flag_url";s:43:"http://yourdomain/wpmlpath/res/flags/it.png";s:3:"url";s:26:"http://yourdomain/it/circa";}}';
// $icl_get_languages = unserialize($icl_get_languages);
$icl_get_languages = icl_get_languages( 'skip_missing=0' );
foreach ( $icl_get_languages as $lang => $data ) {
$languages[ $data['language_code'] ] = $data['native_name'];
}
}
return apply_filters( 'wpo_wcpdf_multilingual_languages', $languages );
}
/**
* Get image mime type
*
* @param string $src
* @return string
*/
function wpo_wcpdf_get_image_mime_type( string $src ): string {
$mime_type = '';
if ( empty( $src ) ) {
return $mime_type;
}
// Check if 'getimagesize' function exists and try to get mime type for local files
if ( function_exists( 'getimagesize' ) && ! filter_var( $src, FILTER_VALIDATE_URL ) ) {
$image_info = @getimagesize( $src );
if ( $image_info && isset( $image_info['mime'] ) ) {
$mime_type = $image_info['mime'];
}
}
// Fallback to 'finfo_file' if mime type is empty for local files only (no remote files allowed)
if ( empty( $mime_type ) && function_exists( 'finfo_open' ) && ! filter_var( $src, FILTER_VALIDATE_URL ) ) {
$finfo = finfo_open( FILEINFO_MIME_TYPE );
if ( $finfo ) {
$mime_type = finfo_file( $finfo, $src );
finfo_close( $finfo );
}
}
// Handle remote files
if ( empty( $mime_type ) && filter_var( $src, FILTER_VALIDATE_URL ) ) {
$context = stream_context_create( array(
'http' => array(
'method' => 'HEAD',
'ignore_errors' => true,
),
'https' => array(
'method' => 'HEAD',
'ignore_errors' => true,
'verify_peer' => false,
'verify_peer_name' => false,
),
) );
$headers = @get_headers( $src, 1, $context );
if ( $headers ) {
if ( isset( $headers['Content-Type'] ) ) {
$mime_type = is_array( $headers['Content-Type'] ) ? $headers['Content-Type'][0] : $headers['Content-Type'];
}
}
}
// Fetch the actual image data if MIME type is still unknown (remote files)
if ( empty( $mime_type ) && filter_var( $src, FILTER_VALIDATE_URL ) ) {
$image_data = @file_get_contents( $src );
if ( $image_data ) {
if ( function_exists( 'finfo_open' ) ) {
$finfo = finfo_open( FILEINFO_MIME_TYPE );
if ( $finfo ) {
$mime_type = finfo_buffer( $finfo, $image_data );
finfo_close( $finfo );
}
}
}
}
// Determine using WP functions
if ( empty( $mime_type ) ) {
$path = wp_parse_url( $src, PHP_URL_PATH );
$file_info = wp_check_filetype( $path );
$mime_type = $file_info['type'] ?? '';
}
// Last chance, determine from file extension
if ( empty( $mime_type ) ) {
$path = parse_url( $src, PHP_URL_PATH );
$extension = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) );
switch ( $extension ) {
case 'jpg':
case 'jpeg':
$mime_type = 'image/jpeg';
break;
case 'png':
$mime_type = 'image/png';
break;
case 'gif':
$mime_type = 'image/gif';
break;
case 'bmp':
$mime_type = 'image/bmp';
break;
case 'webp':
$mime_type = 'image/webp';
break;
case 'svg':
$mime_type = 'image/svg+xml';
break;
}
}
return $mime_type;
}
/**
* Base64 encode file from URL or local path
*
* @param string $src
*
* @return string|bool
*/
function wpo_wcpdf_base64_encode_file( string $src ) {
if ( empty( $src ) ) {
return false;
}
$file_data = @file_get_contents( $src );
return base64_encode( $file_data ) ?? false;
}
/**
* Check if a file is readable
*
* @param string $path
* @return bool
*/
function wpo_wcpdf_is_file_readable( string $path ): bool {
if ( empty( $path ) ) {
return false;
}
// Check if the path is a URL
if ( filter_var( $path, FILTER_VALIDATE_URL ) ) {
$parsed_url = parse_url( $path );
$args = array();
// Check if the URL is localhost
if (
'localhost' === $parsed_url['host'] ||
'127.0.0.1' === $parsed_url['host'] ||
( preg_match( '/^192\.168\./', $parsed_url['host'] ) === 1 ) || // 192.168.*
( preg_match( '/^10\./', $parsed_url['host'] ) === 1 ) || // 10.*
( preg_match( '/^172\.(1[6-9]|2[0-9]|3[0-1])\./', $parsed_url['host'] ) === 1 ) || // 172.16.* to 172.31.*
getenv( 'DISABLE_SSL_VERIFY' ) === 'true'
) {
$args['sslverify'] = false;
}
$args = apply_filters( 'wpo_wcpdf_url_remote_head_args', $args, $parsed_url, $path );
$response = wp_safe_remote_head( $path, $args );
if ( is_wp_error( $response ) ) {
wcpdf_log_error( 'Failed to access file URL: ' . $path . ' Error: ' . $response->get_error_message(), 'critical' );
return false;
}
$status_code = wp_remote_retrieve_response_code( $response );
return ( $status_code === 200 );
// Local path file check
} else {
if ( @is_readable( $path ) ) {
return true;
} else {
// Fallback to fopen if first check fails
$handle = @fopen( $path, 'r' );
if ( $handle ) {
fclose( $handle );
return true;
} else {
wcpdf_log_error( 'Failed to open local file with both methods: ' . $path, 'critical' );
return false;
}
}
}
}
/**
* Get image source in base64 format
*
* @param string $src
*
* @return string
*/
function wpo_wcpdf_get_image_src_in_base64( string $src ): string {
if ( empty( $src ) ) {
return $src;
}
$mime_type = wpo_wcpdf_get_image_mime_type( $src );
if ( empty( $mime_type ) ) {
wcpdf_log_error( 'Unable to determine image mime type for file: ' . $src, 'critical' );
return $src;
}
$image_base64 = wpo_wcpdf_base64_encode_file( $src );
if ( ! $image_base64 ) {
wcpdf_log_error( 'Unable to encode image source to base64:' . $src, 'critical' );
return $src;
}
return 'data:' . $mime_type . ';base64,' . $image_base64;
}
/**
* Determine if the checkout is a block.
*
* @return bool
*/
function wpo_wcpdf_checkout_is_block(): bool {
$checkout_page_id = wc_get_page_id( 'checkout' );
$is_block = $checkout_page_id &&
function_exists( 'has_block' ) &&
has_block( 'woocommerce/checkout', $checkout_page_id );
if ( ! $is_block ) {
$is_block = class_exists( '\\WC_Blocks_Utils' ) &&
count( \WC_Blocks_Utils::get_blocks_from_page( 'woocommerce/checkout', 'checkout' ) ) > 0;
}
if ( ! $is_block ) {
$is_block = class_exists( '\\Automattic\\WooCommerce\\Blocks\\Utils\\CartCheckoutUtils' ) &&
is_callable( array( '\\Automattic\\WooCommerce\\Blocks\\Utils\\CartCheckoutUtils', 'is_checkout_block_default' ) ) &&
\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_checkout_block_default();
}
return $is_block;
}
/**
* Get the default table headers for the Simple template.
*
* @param object $document
* @return array
*/
function wpo_wcpdf_get_simple_template_default_table_headers( $document ): array {
$headers = array(
'product' => __( 'Product', 'woocommerce-pdf-invoices-packing-slips' ),
'quantity' => __( 'Quantity', 'woocommerce-pdf-invoices-packing-slips' ),
'price' => __( 'Price', 'woocommerce-pdf-invoices-packing-slips' ),
);
if ( 'packing-slip' === $document->get_type() ) {
unset( $headers['price'] );
}
return apply_filters( 'wpo_wcpdf_simple_template_default_table_headers', $headers, $document );
}
/**
* Dynamic string translation
*
* @param string $string
* @param string $textdomain
* @return string
*/
function wpo_wcpdf_dynamic_translate( string $string, string $textdomain ): string {