-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpmpro-pdf-invoices.php
1163 lines (962 loc) · 38.1 KB
/
pmpro-pdf-invoices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Paid Memberships Pro - PDF Invoices
* Description: Generates PDF Invoices for Paid Memberships Pro Orders.
* Plugin URI: https://yoohooplugins.com/plugins/pmpro-pdf-invoices/
* Author: Yoohoo Plugins
* Author URI: https://yoohooplugins.com
* Version: 1.23
* License: GPLv2 or later
* Tested up to: 6.7
* Requires PHP: 7.2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pmpro-pdf-invoices
* Domain Path: /languages
* Network: false
*
* Paid Memberships Pro - PDF Invoices is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Paid Memberships Pro - PDF Invoices is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Paid Memberships Pro - PDF Invoices. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
*/
defined( 'ABSPATH' ) or exit;
/**
* Include update class for automatic updates.
*/
if ( ! defined( 'YOOHOO_STORE' ) ) {
define( 'YOOHOO_STORE', 'https://yoohooplugins.com/edd-sl-api/' );
}
define( 'PMPRO_PDF_PLUGIN_ID', 2117 );
define( 'PMPRO_PDF_VERSION', '1.23' );
define( 'PMPRO_PDF_DIR', dirname( __file__ ) );
define( 'PMPRO_PDF_LOGO_URL', 'PMPRO_PDF_LOGO_URL');
define( 'PMPRO_PDF_REWRITE_TOKEN', 'PMPRO_PDF_REWRITE_TOKEN');
define( 'PMPRO_PDF_ADMIN_EMAILS', 'PMPRO_PDF_ADMIN_EMAILS');
// Include the template editor page/functions
include PMPRO_PDF_DIR . '/includes/template-editor.php';
// Include license settings page.
include PMPRO_PDF_DIR . '/includes/general-settings.php';
function pmpropdf_init() {
// Load text domain
load_plugin_textdomain( 'pmpro-pdf-invoices', false, PMPRO_PDF_DIR . '/languages' );
if ( isset( $_REQUEST['pmpropdf'] ) ) {
// Include other files.
include PMPRO_PDF_DIR . '/includes/download-pdf.php';
}
/** Check if the strict redirect is in place*/
if(pmpropdf_check_rewrite_active()){
//Silence in this case
}
pmpropdf_check_should_zip();
}
add_action( 'init', 'pmpropdf_init' );
function pmpropdf_settings_page() {
add_options_page( 'Paid Memberships Pro PDF Invoice License Settings', 'PMPro PDF Invoice', 'manage_options', 'pmpro_pdf_invoices_license_key', 'pmpro_pdf_invoice_settings_page' );
}
add_action( 'admin_menu', 'pmpropdf_settings_page' );
/**
* Class to handle automatic updates.
*/
if ( ! class_exists( 'PMPro_PDF_Invoice_Updater' ) ) {
include( PMPRO_PDF_DIR . '/includes/class.pmpro-pdf-invoice-updater.php' );
}
$license_key = trim( get_option( 'pmpro_pdf_invoice_license_key' ) );
$edd_updater = new PMPro_PDF_Invoice_Updater( YOOHOO_STORE, __FILE__, array(
'version' => PMPRO_PDF_VERSION,
'license' => $license_key,
'item_id' => PMPRO_PDF_PLUGIN_ID,
'author' => 'Yoohoo Plugins',
'url' => home_url()
)
);
use Dompdf\Dompdf;
include( PMPRO_PDF_DIR . '/includes/dompdf/autoload.inc.php' );
/**
* Hook into the PMPro Email Attachements hook
* Get the last order for the user
* Store the PDF into the uploads directory
* Return new attachments array to the PMPro email attachment hook
*/
function pmpropdf_attach_pdf_email( $attachments, $email ) {
$admin_emails = get_option(PMPRO_PDF_ADMIN_EMAILS, false);
// Let's not send it to admins
if( strpos( $email->template, "admin" ) !== false && empty($admin_emails)){
return $attachments;
}
$email_templates = apply_filters( 'pmpropdf_pdf_included_email_templates', array( 'invoice', 'billable_invoice', 'check_pending', 'check_pending_reminder' ) );
// If the email template isn't a checkout email or in the list of email templates, bail.
if ( strpos( $email->template, "checkout_" ) === false && ! in_array( $email->template, $email_templates ) ) {
return $attachments;
}
// Let developers decide if attach the pdf
if ( ! apply_filters( 'pmpropdf_can_attach_pdf_email', true, $email ) ) {
return $attachments;
}
// Make sure there is an order code available, otherwise get it from the user.
if ( empty( $email->data['order_code'] ) ) {
$user = get_user_by( "email", $email->data['user_email'] );
$last_order = pmpropdf_get_last_order( $user->ID );
} else {
$order_code = $email->data['order_code'];
$last_order = pmpropdf_get_order_by_code($order_code);
}
// Bail if order is empty / doesn't exist.
// We do this early to avoid initializing the DomPDF library if it is unneeded
if ( empty( $last_order[0] ) ) {
return $attachments;
}
$order_data = $last_order[0];
$path = pmpropdf_generate_pdf($order_data);
if($path !== false){
$attachments[] = $path;
}
return $attachments;
}
add_filter( 'pmpro_email_attachments', 'pmpropdf_attach_pdf_email', 10, 2 );
/**
* Generate PDF invoice when an order is added.
* @since 1.5
*/
function pmpropdf_added_order( $order ) {
// Let developers decide if generate the pdf
if ( apply_filters( 'pmpropdf_can_generate_pdf_on_added_order', true, $order ) ) {
$last_order = pmpropdf_get_order_by_code( $order->code );
// Bail if order is empty / doesn't exist.
// We do this early to avoid initializing the DomPDF library if it is unneeded
if ( empty( $last_order[0] ) ) {
return;
}
$order_data = $last_order[0];
pmpropdf_generate_pdf( $order_data );
}
}
add_action( 'pmpro_added_order', 'pmpropdf_added_order' );
/**
* Handles storage of PDF Invoice
* Modular design allows it to be used in the primary pmpro_email_attachments_hook
* As well as the batch processing tool
*/
function pmpropdf_generate_pdf($order_data, $return_dom_pdf = false){
// Stop PDF from generating in certain cases.
if ( ! apply_filters( 'pmpropdf_should_generate_pdf', true, $order_data ) ) {
return;
}
$user = get_user_by('ID', $order_data->user_id);
$order = new MemberOrder( $order_data->code );
$dompdf = new Dompdf( apply_filters( 'pmpropdf_dompdf_args', array( 'enable_remote' => true ) ) );
$body = pmpropdf_get_order_template_html();
// Build the string for billing data from the order object or $order_data.
if ( ! empty( $order->billing->name ) ) {
$billing_details = "<p><strong>" . __( 'Billing Details', 'pmpro-pdf-invoices' ) . "</strong></p>";
$billing_details .= "<p>" . $order->billing->name . "<br>";
$billing_details .= $order->billing->street . ", " . $order->billing->street2 . "<br>";
$billing_details .= $order->billing->zip . " " . $order->billing->city . " (" . $order->billing->state . "), " . $order->billing->country . "<br>";
$billing_details .= $order->billing->phone . "</p>";
} elseif ( ! empty( $order_data->billing->name ) ) {
$billing_details = "<p><strong>" . __( 'Billing Details', 'pmpro-pdf-invoices' ) . "</strong></p>";
$billing_details .= "<p>" . $order_data->billing->name . "<br>";
$billing_details .= $order_data->billing->street . ", " . $order_data->billing->street2 . "<br>";
$billing_details .= $order_data->billing->zip . " " . $order_data->billing->city . " (" . $order_data->billing->state . "), " . $order_data->billing->country . "<br>";
$billing_details .= $order_data->billing->phone . "</p>";
} else {
$billing_details = '';
}
if ( isset( $_GET['sub_action'] ) && $_GET['sub_action'] == 'view_sample' ) {
$date = date_i18n( get_option( 'date_format' ), current_time( 'timestamp' ) );
} else {
$date = date_i18n( get_option( 'date_format' ), $order->getTimestamp() );
}
$gateway = pmpro_gateways();
// Get the payment method for the order, adds support for "Free" as this isn't a registered payment gateway.
$payment_method = isset( $gateway[$order_data->gateway] ) ? apply_filters( 'pmpro_pdf_gateway_string', $gateway[$order_data->gateway] ) : __( 'N/A', 'pmpro-pdf-invoices' );
if ( $order_data->gateway == 'free' ) {
$payment_method = apply_filters( 'pmpro_pdf_gateway_string', __( 'Free', 'pmpro-pdf-invoices' ) );
}
$order_level_name = '';
if(function_exists('pmpro_getLevel')){
$order_level = pmpro_getLevel($order_data->membership_id);
if(!empty($order_level) && !empty($order_level->name)){
$order_level_name = $order_level->name;
}
}
$logo_url = get_option(PMPRO_PDF_LOGO_URL, '');
$logo_image = !empty($logo_url) ? "<img style='max-width:300px;' src='$logo_url' />" : '';
$member_first_name = isset( $user->data->first_name ) ? sanitize_text_field( $user->data->first_name ) : '';
$member_last_name = isset( $user->data->last_name ) ? sanitize_text_field( $user->data->last_name ) : '';
// Items to replace.
$replacements = array(
'{{invoice_code}}' => $order_data->code ?: '',
'{{user_email}}' => $user->data->user_email ?: '',
'{{membership_level}}' => $order_level_name ?: '',
'{{membership_enddate}}' => pmpro_get_membership_expiration_text( $order_level, $user->ID ) ?: '',
'{{membership_description}}' => $order_level->description ?: '',
'{{membership_level_confirmation_message}}' => $order_level->confirmation ?: '',
'{{payment_method}}' => $payment_method ?: '',
'{{total}}' => pmpro_formatPrice($order_data->total) ?: '',
'{{site}}' => get_bloginfo( 'sitename' ) ?: '',
'{{sitename}}' => get_bloginfo( 'sitename' ) ?: '',
'{{site_url}}' => esc_url( get_site_url() ) ?: '',
'{{subtotal}}' => pmpro_formatPrice( $order_data->subtotal ) ?: '',
'{{tax}}' => pmpro_formatPrice($order_data->tax) ?: '',
'{{ID}}' => $order_data->membership_id ?: '',
'{{invoice_date}}' => $date ?: '',
'{{logo_image}}' => $logo_image ?: '',
'{{admin_email}}' => get_bloginfo( 'admin_email' ),
'{{display_name}}' => $user->data->display_name ?: '',
'{{levels_url}}' => pmpro_url( 'levels' ) ?: '',
'{{billing_address}}' => wp_kses_post( $billing_details ) ?: '', // The formatted billing address.
'{{billing_name}}' => isset( $order_data->billing->name ) ? sanitize_text_field( $order_data->billing->name ) : '',
'{{billing_street}}' => isset( $order_data->billing->street ) ? sanitize_text_field( $order_data->billing->street ) : '',
'{{billing_street2}}' => isset( $order_data->billing->street2 ) ? sanitize_text_field( $order_data->billing->street2 ) : '',
'{{billing_city}}' => isset( $order_data->billing->city ) ? sanitize_text_field( $order_data->billing->city ) : '',
'{{billing_state}}' => isset( $order_data->billing->state ) ? sanitize_text_field( $order_data->billing->state ) : '',
'{{billing_zip}}' => isset( $order_data->billing->zip ) ? sanitize_text_field( $order_data->billing->zip ) : '',
'{{billing_country}}' => isset( $order_data->billing->country ) ? sanitize_text_field( $order_data->billing->country ) : '',
'{{billing_phone}}' => isset( $order_data->billing->phone ) ? sanitize_text_field( $order_data->billing->phone ) : '',
'{{order_link}}' => pmpro_login_url( pmpro_url( 'invoice', '?invoice=' . $order_data->code ) ),
'{{order_url}}' => pmpro_login_url( pmpro_url( 'invoice', '?invoice=' . $order_data->code ) ),
'{{name}}' => $user->data->display_name ?: '',
'{{first_name}}' => $member_first_name,
'{{last_name}}' => $member_last_name,
'{{full_name}}' => ! empty( $member_first_name . ' ' . $member_last_name ) ?: '',
);
//Additional replacements - Developer hook to add custom variable parse
//Should use key-value pair array (assoc)
$replacements = apply_filters('pmpro_pdf_invoice_custom_variables', $replacements, $user, $order_data );
// Setup PDF Structure
$body = str_replace(
array_keys( $replacements ),
array_values( $replacements ),
$body
);
if( preg_match_all( '/(?<={{)(.*?)(?=}})/m', $body, $matches ) ) {
foreach( $matches as $match_group ) {
foreach( $match_group as $mg ) {
$cleaned_up = strip_tags( $mg );
$meta = get_user_meta( $order_data->user_id, $cleaned_up, true );
// Check if it's an array or not and handle accordingly.
if ( is_array( $meta ) ) {
$meta = implode( ', ', $meta );
}
// escape the meta data, but using wp_kses_post to allow for HTML in the meta data.
$meta = wp_kses_post( $meta );
$body = str_replace( '{{'.$cleaned_up.'}}', $meta, $body );
}
}
}
$dompdf->loadHtml( $body );
$dompdf = apply_filters( 'pmpropdf_dompdf_before_render', $dompdf );
$dompdf->render();
// This allows calling functions to get access to the dompdf instance, instead of storing
if($return_dom_pdf){
return $dompdf;
}
$output = $dompdf->output();
// Let's write this file to a directory now.
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
$invoice_name = pmpropdf_generate_invoice_name($order_data->code);
$path = $invoice_dir . $invoice_name;
try{
file_put_contents( $path, $output );
} catch (Exception $ex){
return false;
}
do_action( 'pmpropdf_generated_pdf_invoice', $order_data->id, $path );
return $path;
}
/**
* Generate and download a sample PDF without needing to checkout.
* Uses sample data.
* @since 1.10
*/
function pmpropdf_generate_sample_pdf(){
if ( class_exists( 'MemberOrder' ) ) {
/* Create a dummy order */
$order = new MemberOrder();
$order->get_test_order();
/* Fill the missing details in the order, that are needed for PDF */
$order->code = $order->getRandomCode();
$order->total = $order->InitialPayment;
$order->subtotal = $order->total;
$order->tax = 0;
/* Cross populate the billing object */
if ( ! empty( $order->billing ) && is_object( $order->billing ) ) {
foreach( $order->billing as $key => $value ) {
$dynamiKey = "billing_{$key}";
$order->{$dynamiKey} = $value;
}
}
$dompdf = pmpropdf_generate_pdf( $order, true );
$dompdf->stream( 'invoice_sample.pdf' );
exit();
}
}
/**
* Generate the invoice name based on the invoice code.
* @since 1.22
*/
function pmpropdf_admin_column_header( $columns ) {
// Only load this for orders.
if ( ! current_user_can( 'pmpro_orders' ) ) {
return $columns;
}
$columns['pmpro_pdf'] = __( 'Invoice PDF', 'pmpro-pdf-invoices' );
return $columns;
}
add_filter( 'pmpro_manage_orderslist_columns', 'pmpropdf_admin_column_header' );
function my_pmpro_pdf_column_stuff( $column, $order_id) {
if ( $column != 'pmpro_pdf' ) {
return;
}
$order = new MemberOrder( $order_id );
if ( ! current_user_can( 'pmpro_orders' ) ) {
return;
}
if ( file_exists( pmpropdf_get_invoice_directory_or_url() . pmpropdf_generate_invoice_name($order->code) ) ){
echo '<a href="' . esc_url( admin_url( '?pmpropdf=' . $order->code ) ). '" target="_blank">' . esc_html__( 'Download PDF', 'pmpro-pdf-invoices' ) .'</a>';
} else {
echo '<a href="javascript:void(0)" id="pmpro-pdf-generate_' . esc_attr( $order->code ) . '" class="pmpro-pdf-generate" order_code="' . esc_attr( $order->code ) . '">' . esc_html__( 'Generate PDF', 'pmpro-pdf-invoices' ) . '</a>';
}
}
add_action( 'pmpro_manage_orderlist_custom_column', 'my_pmpro_pdf_column_stuff', 10, 2 );
/**
* Helper function to get member order when class not available.
* Revisit this at a later stage.
*/
function pmpropdf_get_last_order( $user_id ) {
global $wpdb;
$user_id = intval( $user_id );
$order = $wpdb->get_results("SELECT * FROM $wpdb->pmpro_membership_orders WHERE user_id = " . esc_sql( $user_id ) . " AND status NOT IN('cancelled') ORDER BY timestamp DESC LIMIT 1");
return $order;
}
/**
* Get specific order by its order code
* Proxy of: pmpropdf_get_last_order
*/
function pmpropdf_get_order_by_code( $order_code ) {
global $wpdb;
$order = $wpdb->get_results("SELECT * FROM $wpdb->pmpro_membership_orders WHERE code = '" . esc_sql( $order_code ) . "' LIMIT 1");
return $order;
}
/**
* Returns the invoice storage directory
* Creates it if it does no exist
*/
function pmpropdf_get_invoice_directory_or_url($url = false){
$upload_dir = wp_upload_dir();
$invoice_dir = ($url ? $upload_dir['baseurl'] : $upload_dir['basedir'] ) . '/pmpro-invoices/';
if($url == false){
if ( !file_exists( $invoice_dir ) ) {
mkdir( $invoice_dir, 0777, true );
}
}
return $invoice_dir;
}
/**
* Generates an invoice name from an order code
*/
function pmpropdf_generate_invoice_name($order_code){
$invoice_prefix = apply_filters( 'pmpro_pdf_invoice_prefix', 'INV' );
$invoice_name = $invoice_prefix . $order_code . ".pdf";
return apply_filters( 'pmpro_pdf_invoice_name', $invoice_name, $order_code );
}
/**
* Get batch of orders
* Return the ordders for loop processing
*/
function pmpropdf_get_order_batch($batch_size = 100, $batch_no = 0){
global $wpdb;
$offset = $batch_no * $batch_size;
$batch_sql = "SELECT * FROM $wpdb->pmpro_membership_orders ORDER BY timestamp ASC LIMIT $batch_size OFFSET $offset";
$batch = $wpdb->get_results($batch_sql);
return $batch;
}
/**
* Process a batch of orders
* Check if the current order has a PDF generated
* Generate one if this is not the case
* Skip it if we have this invoice already created
*/
function pmpropdf_process_batch($batch_size = 100, $batch_no = 0){
$output_array = array(
'skipped' => 0,
'created' => 0,
'batch_no' => $batch_no,
'batch_count' => 0
);
try{
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
$batch = pmpropdf_get_order_batch($batch_size, $batch_no);
foreach ($batch as $order_data) {
$invoice_name = pmpropdf_generate_invoice_name($order_data->code);
if(file_exists($invoice_dir . $invoice_name)){
$output_array['skipped'] += 1;
} else {
$path = pmpropdf_generate_pdf($order_data);
$output_array['created'] += 1;
}
}
$output_array['batch_count'] = count($batch);
} catch (Exception $ex){
$output_array['error'] = "An unexpected error occurred, we were not able to complete PDF generation";
} catch (Error $err){
$output_array['error'] = "An unexpected error occurred, we were not able to complete PDF generation";
}
return $output_array;
}
/**
* AJAX Loop
*/
function pmpropdf_batch_processor() {
if(defined('DOING_AJAX') && DOING_AJAX){
$batch_size = !empty($_POST['batch_size']) ? intval($_POST['batch_size']) : 100;
$batch_no = !empty($_POST['batch_no']) ? intval($_POST['batch_no']) : 0;
$batch_output = pmpropdf_process_batch($batch_size, $batch_no);
echo json_encode($batch_output);
}
die();
}
add_action( 'wp_ajax_pmpropdf_batch_processor', 'pmpropdf_batch_processor' );
/**
* Download PDF invoice.
* @since 1.2
*/
function pmpropdf_download_invoice( $order_code ) {
if( file_exists( pmpropdf_get_invoice_directory_or_url() . pmpropdf_generate_invoice_name( $order_code ) ) ) {
$invoice_name = pmpropdf_generate_invoice_name( $order_code );
$download_url = esc_url( pmpropdf_get_invoice_directory_or_url( true ) . $invoice_name );
$access_key = pmpropdf_get_rewrite_token();
$download_url .= "?access=$access_key";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$invoice_name.'"');
readfile($download_url);
/**
* This is removed to support the force htaccess redirect
* Auto download is now automatically handled in the htaccess file
*
* -------------
* header( 'Content-Description: File Transfer' );
* header( 'Content-Type: application/octet-stream' );
* header( 'Content-Disposition: attachment; filename="'.basename( $invoice_name ).'"' );
* header( 'Expires: 0' );
* header( 'Cache-Control: must-revalidate' );
* header( 'Pragma: public' );
* header( 'Content-Length: ' . filesize( $download_url ) );
* flush(); // Flush system output buffer
* readfile( $download_url );
* -------------
*/
exit;
}
}
/**
* Check if the rewrite .htaccess file is in place
* If not, create it.
*/
function pmpropdf_check_rewrite_active(){
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
$htaccess_location = $invoice_dir . ".htaccess";
if(!file_exists($htaccess_location)){
try{
$access_key = pmpropdf_get_rewrite_token();
$server_ip = $_SERVER['REMOTE_ADDR'];
$htaccess = fopen($htaccess_location, "w");
$content = "<IfModule mod_rewrite.c>\n";
$content .= "RewriteEngine On\n";
$content .= "RewriteCond %{QUERY_STRING} !access=$access_key\n";
$content .= "RewriteRule (.*) - [F]\n";
$content .= "</IfModule>\n\n";
$content .= "<FilesMatch \"\.(pdf)$\">\n";
$content .= "ForceType application/octet-stream\n";
$content .= "Header set Content-Disposition attachment\n";
$content .= "</FilesMatch>";
fwrite($htaccess, $content);
fclose($htaccess);
return true;
} catch (Exception $e){
return false;
}
}
return true;
}
/**
* Unlinks/deletes the current rewrite .htaccess files
* This will then be regenerated in the next loop
*/
function pmpropdf_remove_rewrite_for_regen(){
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
$htaccess_location = $invoice_dir . ".htaccess";
if(file_exists($htaccess_location)){
unlink($htaccess_location);
}
}
/**
* Get the rewrite access token
*/
function pmpropdf_get_rewrite_token(){
$access_token = get_option(PMPRO_PDF_REWRITE_TOKEN);
if($access_token === false){
$access_token = bin2hex(openssl_random_pseudo_bytes(16));
update_option(PMPRO_PDF_REWRITE_TOKEN, $access_token);
}
return $access_token;
}
/**
* Shortcode handler for the invoice list based on current user
*/
function pmpropdf_download_list_shortcode_handler(){
global $wpdb, $current_user;
// This is if it's shown on a page that doesn't require login.
if ( empty( $current_user ) && apply_filters( 'pmpropdf_show_logged_out_shortcode_message', false ) ) {
return esc_html__( 'Please login to view your invoices.', 'pmpro-pdf-invoices' );
}
$limit = apply_filters( 'pmpropdf_invoice_table_limit', 15 );
$invoices = $wpdb->get_results("
SELECT *, UNIX_TIMESTAMP(timestamp) as timestamp
FROM $wpdb->pmpro_membership_orders
WHERE user_id = '$current_user->ID'
AND status NOT
IN('review', 'token', 'error')
ORDER BY timestamp DESC LIMIT " . $limit
);
if(!empty($invoices)){
$content = '';
foreach ($invoices as $key => $invoice) {
$invoice_id = $invoice->id;
$invoice = new MemberOrder;
$invoice->getMemberOrderByID($invoice_id);
$invoice->getMembershipLevel();
$membership_level = $invoice->membership_level->name;
if ( file_exists( pmpropdf_get_invoice_directory_or_url() . pmpropdf_generate_invoice_name($invoice->code) ) ){
$content .= '<tr>';
$content .= '<td>' . date_i18n(get_option("date_format"), $invoice->timestamp) . '</td>';
$content .= '<td>' . $membership_level . '</td>';
$content .= '<td>' . pmpro_formatPrice($invoice->total) . '</td>';
$content .= '<td><a href="' . esc_url( admin_url( '?pmpropdf=' . $invoice->code ) ). '">' . pmpropdf_generate_invoice_name( $invoice->code ) .'</a></td>';
$content .= '</tr>';
}
}
}
if(!empty($content)){
ob_start();
?>
<div class="pmpro">
<section id="pmpro_account-pdfs" class="pmpro_section">
<h2 class="pmpro_section_title pmpro_font-x-large"><?php esc_html_e( "PDF Invoices", 'pmpro-pdf-invoices' ); ?></h3>
<div class="pmpro_card">
<div class="pmpro_card_content">
<table class="pmpro_table pmpro_table_orders">
<thead>
<tr>
<th><?php esc_html_e( "Date", 'pmpro-pdf-invoices' ); ?></th>
<th><?php esc_html_e( "Level", 'pmpro-pdf-invoices' ); ?></th>
<th><?php esc_html_e( "Amount", 'pmpro-pdf-invoices' ); ?></th>
<th><?php esc_html_e( "Download", 'pmpro-pdf-invoices' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo wp_kses_post( $content ); ?>
</tbody>
</table>
</div>
</div>
</section>
</div>
<?php
$table_content = ob_get_clean();
return $table_content;
} else {
$content = "<h3>" . esc_html__( "PDF Invoices", 'pmpro-pdf-invoices' ) . "</h3>";
$content .= "<div><em>" . esc_html__( "No PDF invoices found...", 'pmpro-pdf-invoices' ) . "</em></div>";
}
// If we make it here.
return wp_kses_post( $content );
}
add_shortcode( 'pmpropdf_download_list', 'pmpropdf_download_list_shortcode_handler' );
/**
* Shortcode handler for the download all as ZIP file
*/
function pmpropdf_download_all_zip_shortcode_handler( $atts ){
$title = __("Download all PDF's as ZIP", 'pmpro-pdf-invoices');
if(!empty($atts['title'])){
$title = sanitize_text_field($atts['title']);
}
if(class_exists('ZipArchive') && is_user_logged_in() ) {
global $wpdb, $current_user;
$invoices = $wpdb->get_results("
SELECT *, UNIX_TIMESTAMP(timestamp) as timestamp
FROM $wpdb->pmpro_membership_orders
WHERE user_id = '$current_user->ID'
AND status NOT
IN('review', 'token', 'error')
ORDER BY timestamp DESC"
);
if(!empty($invoices) && count($invoices) > 0){
return "<a href='?pmpro_pdf_invoices_action=download_zip' target='_BLANK'>$title</a>";
}
}
return '';
}
add_shortcode('pmpropdf_download_all_zip', 'pmpropdf_download_all_zip_shortcode_handler');
/**
* Checks if we received a request to perform a zip of the current users documents
*
* If so, this will go ahead and generate that plus download it
*/
function pmpropdf_check_should_zip(){
if(!empty($_REQUEST['pmpro_pdf_invoices_action']) && $_REQUEST['pmpro_pdf_invoices_action'] === 'download_zip'){
if(class_exists('ZipArchive')){
if(class_exists('ZipArchive') && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()){
global $wpdb, $current_user;
$invoices = $wpdb->get_results("
SELECT *, UNIX_TIMESTAMP(timestamp) as timestamp
FROM $wpdb->pmpro_membership_orders
WHERE user_id = '$current_user->ID'
AND status NOT
IN('review', 'token', 'error')
ORDER BY timestamp DESC"
);
if(!empty($invoices) && count($invoices) > 0){
$files = array();
foreach ($invoices as $key => $invoice) {
if ( file_exists( pmpropdf_get_invoice_directory_or_url() . pmpropdf_generate_invoice_name($invoice->code) ) ){
$files[] = pmpropdf_get_invoice_directory_or_url() . pmpropdf_generate_invoice_name($invoice->code);
}
}
$archive_name = 'invoices_' . time() . '.zip';
$archive = new ZipArchive;
if($archive->open($archive_name, ZipArchive::CREATE) === TRUE){
foreach ($files as $file) {
$archive->addFromString(basename($file), file_get_contents($file));
}
$archive->close();
/** Send the headers and file data to the browser */
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive_name);
header('Content-Length: ' . filesize($archive_name));
readfile($archive_name);
@unlink($archive_name);
}
}
}
}
} else if (!empty($_GET['page']) && !empty($_GET['sub_action'])){
if($_GET['page'] === 'pmpro_pdf_invoices_license_key' && $_GET['sub_action'] === 'download_zip_archive'){
/* This is an admin download, processes here for the sake of header output */
if(current_user_can('administrator') && class_exists('ZipArchive')){
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
if(file_exists($invoice_dir)){
$files = scandir($invoice_dir);
$pdfs = array();
foreach ($files as $file) {
if(strpos($file, '.pdf') !== FALSE){
$pdfs[] = pmpropdf_get_invoice_directory_or_url() . $file;
}
}
if(!empty($pdfs)){
$archive_name = 'invoices_archive_' . time() . '.zip';
$archive = new ZipArchive;
if($archive->open($archive_name, ZipArchive::CREATE) === TRUE){
foreach ($pdfs as $path) {
$archive->addFromString(basename($path), file_get_contents($path));
}
$archive->close();
/** Send the headers and file data to the browser */
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive_name);
header('Content-Length: ' . filesize($archive_name));
readfile($archive_name);
@unlink($archive_name);
}
}
}
}
}
}
}
function pmpropdf_footer_note ($footnote){
if(!empty($_GET['page']) && strpos($_GET['page'], 'pmpro_pdf_invoices') !== FALSE){
$footnote .= "<em> || PMPro PDF Invoices (v" . PMPRO_PDF_VERSION . ") by <a href='https://yoohooplugins.com/' target='_blank'>Yoohoo Plugins</a>.</em>";
}
return $footnote;
}
add_filter('admin_footer_text', 'pmpropdf_footer_note', 10, 1);
function pmpropdf_nginx_notice () {
if ( empty( $_REQUEST['page'] ) || strpos( $_REQUEST['page'], 'pmpro' ) === false ) {
return;
}
$user_id = get_current_user_id();
if( current_user_can( 'manage_options' ) &&
intval( get_user_meta( $user_id, 'pmpropdf_nginx_dismissed', true ) ) == false &&
( !empty( $_SERVER['SERVER_SOFTWARE'] ) && strpos( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) !== false )
){
$upload_dir = wp_upload_dir();
$baseurl = str_replace( site_url(), '', $upload_dir['baseurl'] );
$invoice_dir = $baseurl . '/pmpro-invoices/';
$access_key = pmpropdf_get_rewrite_token();
?>
<div class="updated">
<h2><?php _e('Paid Memberships Pro - PDF Invoices - Nginx Detected', 'pmpro-pdf-invoices'); ?></h2>
<p><?php _e('We detected that your installation is running on Nginx. To protect generated invoices that are stored on your web server, the following Nginx rule should be added to your Nginx WordPress installation config file.', 'pmpro-pdf-invoices' ); ?></p>
<p><code>
location <?php echo $invoice_dir; ?> {
if ($query_string !~ "access=<?php echo $access_key; ?>"){
return 403;
}
}
</code></p>
<p><a class='button button-primary' id="pmpropdf_nginx_prompt" href="<?php echo admin_url( '?pmpropdf_nginx=dismiss' ); ?>"><?php _e("I've Added The Nginx Rule", "pmpro-pdf-invoices"); ?></a></p>
</div>
<?php
}
}
add_action( 'admin_notices', 'pmpropdf_nginx_notice' );
function pmpropdf_dismiss_nginx_notice(){
if( !empty( $_REQUEST['pmpropdf_nginx'] ) && $_REQUEST['pmpropdf_nginx'] == 'dismiss' ){
if( current_user_can( 'manage_options' ) ){
$user_id = get_current_user_id();
update_user_meta( $user_id, 'pmpropdf_nginx_dismissed', 1 );
}
}
}
add_action( 'admin_init', 'pmpropdf_dismiss_nginx_notice' );
/**
* Get the template content
*
* Helper function to standardize body content retrieval, also should help with any automated migration
*
* @since 1.9
*/
function pmpropdf_get_order_template_html(){
pmpropdf_migrate_custom_template();
$path = pmpropdf_get_order_template_path();
if(!empty($path) && file_exists($path)){
return file_get_contents($path);
}
return "";
}
/**
* Get the template path
*
* Helper function to standardize path generation and testing for the template file
*
* @since 1.9
*/
function pmpropdf_get_order_template_path(){
$path = PMPRO_PDF_DIR . '/templates/order.html';
$upload_dir = wp_upload_dir();
if(!empty($upload_dir) && !empty($upload_dir['basedir'])){
$template_dir = $upload_dir['basedir'] . '/pmpro-invoice-templates/order.html';
if(file_exists($template_dir)){
$path = $template_dir;
}
}
return apply_filters("pmpro_order_template_path", $path);
}
/**
* Helper for migrating existing custom templates to the uploads directory
*
* This automated process should help prevent any template data loss
*
* @since 1.9
*/
function pmpropdf_migrate_custom_template(){
$legacy_path = get_stylesheet_directory() . "/pmpro-pdf-invoices/order.html";
if(file_exists($legacy_path)){
$legacy_content = file_get_contents($legacy_path);
$upload_dir = wp_upload_dir();
$template_dir = $upload_dir['basedir'] . '/pmpro-invoice-templates/';
if(!file_exists( $template_dir )){
mkdir( $template_dir, 0777, true );
}
if(!file_exists($template_dir . 'order.html')){
try{
file_put_contents($template_dir . 'order.html', $legacy_content);
} catch (Exception $ex){
//Silence
}
}
@unlink($legacy_path);
}
}
/**
* Regenerate PDF invoice when an order is updated
*
* @since 1.9
*/
function pmpropdf_updated_order( $order ) {
// Let developers decide if generate the pdf
if ( apply_filters( 'pmpropdf_can_regenerate_pdf_on_added_order', true, $order ) ) {
$invoice_dir = pmpropdf_get_invoice_directory_or_url();
$invoice_name = pmpropdf_generate_invoice_name($order->code);
if(file_exists($invoice_dir . $invoice_name)){
unlink($invoice_dir . $invoice_name);
}
$path = pmpropdf_generate_pdf($order);
}
}
add_action( 'pmpro_updated_order', 'pmpropdf_updated_order', 99, 1);
/**
* Function to enqueue scripts and styles on PMPro pages.
* @since 1.2
*/
function pmpropdf_enqueue_scripts_styles() {
// Only enqueue on PMPro prefixed admin pages.
if ( ! isset( $_REQUEST['page'] ) || strpos( $_REQUEST['page'], 'pmpro' ) !== 0 ) {
return;
}
// Enqueue scripts.
wp_register_script( 'pmpro-pdf-admin', plugins_url( '/includes/js/admin.js', __FILE__ ), array( 'jquery' ), PMPRO_PDF_VERSION );
wp_localize_script( 'pmpro-pdf-admin', 'pmpro_pdf_admin', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'admin_url' => esc_url( admin_url( '?pmpropdf=') ),
'download_text' => __( 'Download PDF', 'pmpro-pdf-invoices' ),
'loading_gif' => plugins_url( '/includes/images/pmpropdf-loading.gif', __FILE__ ),
'nonce' => wp_create_nonce( 'pmpro-pdf-invoices-single' ),
)
);
wp_enqueue_script( 'pmpro-pdf-admin' );