WooCommerce Print Invoice & Delivery Note - Version 4.5.2

Version Description

(23.08.2019) =

  • Fixed the issue of Print buttons not working on PHP versions below 7.0.
Download this release

Release Info

Developer ashokrane
Plugin Icon 128x128 WooCommerce Print Invoice & Delivery Note
Version 4.5.2
Comparing to
See all releases

Code changes from version 4.4.8 to 4.5.2

includes/class-wcdn-print.php CHANGED
@@ -1,155 +1,242 @@
1
<?php
2
3
/**
4
* Exit if accessed directly
5
*/
6
- if ( !defined( 'ABSPATH' ) ) {
7
exit;
8
}
9
10
/**
11
* Print class
12
*/
13
- if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
14
15
- class WooCommerce_Delivery_Notes_Print {
16
17
public static $template_registrations;
18
public static $template_styles;
19
20
public $template_locations;
21
public $template;
22
23
public $api_endpoints;
24
public $query_vars;
25
26
public $order_ids;
27
public $order_email;
28
public $orders;
29
30
/**
31
* Constructor
32
*/
33
public function __construct() {
34
- // Define the templates
35
- self::$template_registrations = apply_filters( 'wcdn_template_registration', array(
36
- apply_filters( 'wcdn_template_registration_invoice', array(
37
- 'type' => 'invoice',
38
- 'labels' => array(
39
- 'name' => __( 'Invoice', 'woocommerce-delivery-notes' ),
40
- 'name_plural' => __( 'Invoices', 'woocommerce-delivery-notes' ),
41
- 'print' => __( 'Print Invoice', 'woocommerce-delivery-notes' ),
42
- 'print_plural' => __( 'Print Invoices', 'woocommerce-delivery-notes' ),
43
- 'message' => __( 'Invoice created.', 'woocommerce-delivery-notes' ),
44
- 'message_plural' => __( 'Invoices created.', 'woocommerce-delivery-notes' ),
45
- 'setting' => __( 'Show "Print Invoice" button', 'woocommerce-delivery-notes' )
46
- )
47
- ) ),
48
- apply_filters( 'wcdn_template_registration_delivery_note', array(
49
- 'type' => 'delivery-note',
50
- 'labels' => array(
51
- 'name' => __( 'Delivery Note', 'woocommerce-delivery-notes' ),
52
- 'name_plural' => __( 'Delivery Notes', 'woocommerce-delivery-notes' ),
53
- 'print' => __( 'Print Delivery Note', 'woocommerce-delivery-notes' ),
54
- 'print_plural' => __( 'Print Delivery Notes', 'woocommerce-delivery-notes' ),
55
- 'message' => __( 'Delivery Note created.', 'woocommerce-delivery-notes' ),
56
- 'message_plural' => __( 'Delivery Notes created.', 'woocommerce-delivery-notes' ),
57
- 'setting' => __( 'Show "Print Delivery Note" button', 'woocommerce-delivery-notes' )
58
- )
59
- ) ),
60
- apply_filters( 'wcdn_template_registration_receipt', array(
61
- 'type' => 'receipt',
62
'labels' => array(
63
- 'name' => __( 'Receipt', 'woocommerce-delivery-notes' ),
64
- 'name_plural' => __( 'Receipts', 'woocommerce-delivery-notes' ),
65
- 'print' => __( 'Print Receipt', 'woocommerce-delivery-notes' ),
66
- 'print_plural' => __( 'Print Receipts', 'woocommerce-delivery-notes' ),
67
- 'message' => __( 'Receipt created.', 'woocommerce-delivery-notes' ),
68
- 'message_plural' => __( 'Receipts created.', 'woocommerce-delivery-notes' ),
69
- 'setting' => __( 'Show "Print Receipt" button', 'woocommerce-delivery-notes' )
70
- )
71
- ) )
72
- ) );
73
-
74
- // Add the default template as first item after filter hooks passed
75
- array_unshift( self::$template_registrations, array(
76
- 'type' => 'order',
77
- 'labels' => array(
78
- 'name' => __( 'Order', 'woocommerce-delivery-notes' ),
79
- 'name_plural' => __( 'Orders', 'woocommerce-delivery-notes' ),
80
- 'print' => __( 'Print Order', 'woocommerce-delivery-notes' ),
81
- 'print_plural' => __( 'Print Orders', 'woocommerce-delivery-notes' ),
82
- 'message' => null,
83
- 'message_plural' => null,
84
- 'setting' => null
85
)
86
- ) );
87
88
- // Template styles
89
self::$template_styles = apply_filters( 'wcdn_template_styles', array() );
90
91
- // Add the default style as first item after filter hooks passed
92
- array_unshift( self::$template_styles, array(
93
- 'name' => __( 'Default', 'woocommerce-delivery-notes' ),
94
- 'type' => 'default',
95
- 'path' => WooCommerce_Delivery_Notes::$plugin_path . 'templates/print-order/',
96
- 'url' => WooCommerce_Delivery_Notes::$plugin_url . 'templates/print-order/'
97
- ) );
98
99
- // Default template
100
$this->template = self::$template_registrations[0];
101
102
- // Build all template locations
103
$this->template_locations = $this->build_template_locations();
104
105
- // Add the endpoint for the frontend
106
$this->api_endpoints = array(
107
- 'print-order' => get_option( 'wcdn_print_order_page_endpoint', 'print-order' )
108
);
109
110
- // Insert the query vars
111
$this->query_vars = array(
112
'print-order-type',
113
- 'print-order-email'
114
);
115
116
- // Load the hooks
117
add_action( 'init', array( $this, 'load_hooks' ) );
118
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
119
add_action( 'parse_request', array( $this, 'parse_request' ) );
120
add_action( 'template_redirect', array( $this, 'template_redirect_theme' ) );
121
add_action( 'wp_ajax_print_order', array( $this, 'template_redirect_admin' ) );
122
- add_action( 'wcdn_after_items', array( $this, 'wdn_add_extra_data_after_items' ), 10 , 1 );
123
}
124
125
- public function wdn_add_extra_data_after_items ( $order ) {
126
-
127
/**
128
* Local pickup plus plugin is active
129
*/
130
- if ( class_exists( "WC_Local_Pickup_Plus") ) {
131
-
132
$cdn_local_pickup_plugin_plugins_version = wc_local_pickup_plus()->get_version();
133
134
- if( version_compare( $cdn_local_pickup_plugin_plugins_version, '2.0.0', ">=" ) ) {
135
- $cdn_local_pickup_object = new WC_Local_Pickup_Plus_Orders();
136
- $local_pickup = wc_local_pickup_plus();
137
$cdn_local_pickup_locations = $cdn_local_pickup_object->get_order_pickup_data( $order );
138
$cdn_local_pickup__shipping_object = $local_pickup->get_shipping_method_instance();
139
- WooCommerce_Delivery_Notes_Print::cdn_print_local_pickup_address( $cdn_local_pickup_locations, $cdn_local_pickup__shipping_object );
140
}
141
}
142
}
143
144
public function cdn_print_local_pickup_address( $cdn_local_pickup_locations, $shipping_method ) {
145
146
$package_number = 1;
147
$packages_count = count( $cdn_local_pickup_locations );
148
- foreach ( $cdn_local_pickup_locations as $pickup_meta ) :
149
- ?>
150
<div>
151
<?php if ( $packages_count > 1 ) : ?>
152
- <h5><?php echo sprintf( is_rtl() ? '#%2$s %1$s': '%1$s #%2$s', esc_html( $shipping_method->get_method_title() ), $package_number ); ?></h5>
153
<?php endif; ?>
154
<ul>
155
<?php foreach ( $pickup_meta as $label => $value ) : ?>
@@ -164,14 +251,15 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
164
</ul>
165
<?php $package_number++; ?>
166
</div>
167
- <?php
168
- endforeach;
169
}
170
/**
171
* Load the init hooks
172
*/
173
public function load_hooks() {
174
- // Add the endpoints
175
$this->add_endpoints();
176
}
177
@@ -181,13 +269,13 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
181
* generate the print template and link.
182
*/
183
public function add_endpoints() {
184
- foreach( $this->api_endpoints as $var ) {
185
add_rewrite_endpoint( $var, EP_PAGES );
186
}
187
188
// Flush the rules when the transient is set.
189
// This is important to make the endpoint work.
190
- if( get_transient( 'wcdn_flush_rewrite_rules' ) == true ) {
191
delete_transient( 'wcdn_flush_rewrite_rules' );
192
flush_rewrite_rules();
193
}
@@ -195,26 +283,30 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
195
196
/**
197
* Add the query vars to the url
198
*/
199
public function add_query_vars( $vars ) {
200
- foreach( $this->query_vars as $var ) {
201
$vars[] = $var;
202
}
203
- return $vars;
204
}
205
206
/**
207
* Parse the query variables
208
*/
209
public function parse_request( $wp ) {
210
// Map endpoint keys to their query var keys, when another endpoint name was set.
211
- foreach( $this->api_endpoints as $key => $var ) {
212
- if( isset( $_GET[$var] ) ) {
213
- // changed
214
- $wdn_get_end_point_var = sanitize_text_field($_GET[$var]);
215
- $wp->query_vars[$key] = $wdn_get_end_point_var;
216
- } elseif ( isset( $wp->query_vars[$var] ) ) {
217
- $wp->query_vars[$key] = $wp->query_vars[$var];
218
}
219
}
220
}
@@ -225,41 +317,41 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
225
public function build_template_locations() {
226
$wc_template_directory = WC_TEMPLATE_PATH . 'print-order/';
227
228
- // Get the paths for custom styles
229
$settings_type = get_option( 'wcdn_template_style' );
230
$settings_path = null;
231
- $settings_url = null;
232
- if( isset( $settings_type ) && $settings_type !== 'default' ) {
233
- foreach( self::$template_styles as $template_style ) {
234
- if( $settings_type === $template_style['type'] ) {
235
$settings_path = $template_style['path'];
236
- $settings_url = $template_style['url'];
237
break;
238
}
239
}
240
}
241
242
- // Build the locations
243
$locations = array(
244
'child_theme' => array(
245
'path' => trailingslashit( get_stylesheet_directory() ) . $wc_template_directory,
246
- 'url' => trailingslashit( get_stylesheet_directory_uri() ) . $wc_template_directory
247
),
248
249
- 'theme' => array(
250
'path' => trailingslashit( get_template_directory() ) . $wc_template_directory,
251
- 'url' => trailingslashit( get_template_directory_uri() ) . $wc_template_directory
252
),
253
254
- 'settings' => array(
255
'path' => $settings_path,
256
- 'url' => $settings_url
257
),
258
259
- 'plugin' => array(
260
'path' => self::$template_styles[0]['path'],
261
- 'url' => self::$template_styles[0]['url']
262
- )
263
);
264
265
return $locations;
@@ -270,10 +362,10 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
270
*/
271
public function template_redirect_theme() {
272
global $wp;
273
- // Check the page url and display the template when on my-account page
274
- if( !empty( $wp->query_vars['print-order'] ) && is_account_page() ) {
275
- $type = !empty( $wp->query_vars['print-order-type'] ) ? $wp->query_vars['print-order-type'] : null;
276
- $email = !empty( $wp->query_vars['print-order-email'] ) ? $wp->query_vars['print-order-email'] : null;
277
$this->generate_template( $wp->query_vars['print-order'], $type, $email );
278
exit;
279
}
@@ -283,13 +375,13 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
283
* Template handling in the back-end
284
*/
285
public function template_redirect_admin() {
286
- // Let the backend only access the page
287
- // changed
288
- if( is_admin() && current_user_can( 'edit_shop_orders' ) && !empty( $_REQUEST['print-order'] ) && !empty( $_REQUEST['action'] ) ) {
289
- $type = !empty( $_REQUEST['print-order-type'] ) ? sanitize_text_field ( $_REQUEST['print-order-type'] ) : null;
290
- $email = !empty( $_REQUEST['print-order-email'] ) ? sanitize_email ( $_REQUEST['print-order-email'] ) : null;
291
- // changed
292
- $wdn_get_print_order = sanitize_text_field ( $_GET['print-order'] );
293
$this->generate_template( $wdn_get_print_order, $type, $email );
294
exit;
295
}
@@ -298,52 +390,59 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
298
299
/**
300
* Generate the template
301
*/
302
public function generate_template( $order_ids, $template_type = 'order', $order_email = null ) {
303
global $post, $wp;
304
305
- // Explode the ids when needed
306
- if( !is_array( $order_ids ) ) {
307
- $this->order_ids = array_filter( explode('-', $order_ids ) );
308
}
309
310
- // Set the current template
311
- foreach( self::$template_registrations as $template_registration ) {
312
- if( $template_type == $template_registration['type'] ) {
313
$this->template = $template_registration;
314
break;
315
}
316
}
317
318
- // Set the email
319
- if( empty( $order_email ) ) {
320
$this->order_email = null;
321
} else {
322
$this->order_email = strtolower( $order_email );
323
}
324
325
- // Create the orders and check permissions
326
$populated = $this->populate_orders();
327
328
- // Only continue if the orders are populated
329
- if( !$populated ) {
330
die();
331
}
332
333
- // Load the print template html
334
$location = $this->get_template_file_location( 'print-order.php' );
335
wc_get_template( 'print-order.php', null, $location, $location );
336
exit;
337
}
338
339
/**
340
- * Find the location of a template file
341
*/
342
public function get_template_file_location( $name, $url_mode = false ) {
343
$found = '';
344
- foreach( $this->template_locations as $template_location ) {
345
- if( isset( $template_location['path'] ) && file_exists( trailingslashit( $template_location['path'] ) . $name ) ) {
346
- if( $url_mode ) {
347
$found = $template_location['url'];
348
} else {
349
$found = $template_location['path'];
@@ -356,157 +455,164 @@ if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) {
356
357
/**
358
* Get print page url
359
*/
360
public function get_print_page_url( $order_ids, $template_type = 'order', $order_email = null, $permalink = false ) {
361
- // Explode the ids when needed
362
- if( !is_array( $order_ids ) ) {
363
$order_ids = array_filter( explode( '-', $order_ids ) );
364
}
365
366
- // Build the args
367
$args = array();
368
369
- // Set the template type arg
370
- foreach( self::$template_registrations as $template_registration ) {
371
- if( $template_type == $template_registration['type'] && $template_type != 'order' ) {
372
$args = wp_parse_args( array( 'print-order-type' => $template_type ), $args );
373
break;
374
}
375
}
376
377
- // Set the email arg
378
- if( !empty( $order_email ) ) {
379
$args = wp_parse_args( array( 'print-order-email' => $order_email ), $args );
380
}
381
382
- // Generate the url
383
$order_ids_slug = implode( '-', $order_ids );
384
385
- // Create another url depending on where the user prints. This
386
- // prevents some issues with ssl when the my-account page is
387
- // secured with ssl but the admin isn't.
388
- if( is_admin() && current_user_can( 'edit_shop_orders' ) && $permalink == false ) {
389
- // For the admin we use the ajax.php for better security
390
- $args = wp_parse_args( array( 'action' => 'print_order' ), $args );
391
$base_url = admin_url( 'admin-ajax.php' );
392
$endpoint = 'print-order';
393
394
- // Add the order ids and create the url
395
$url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
396
} else {
397
- // For the theme
398
$base_url = wc_get_page_permalink( 'myaccount' );
399
$endpoint = $this->api_endpoints['print-order'];
400
401
- // Add the order ids and create the url
402
- if( get_option( 'permalink_structure' ) ) {
403
$url = trailingslashit( trailingslashit( $base_url ) . $endpoint . '/' . $order_ids_slug );
404
} else {
405
$url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
406
}
407
}
408
409
- // Add all other args
410
$url = add_query_arg( $args, $url );
411
412
return esc_url( $url );
413
}
414
415
/**
416
- * Create the orders list and check the permissions
417
*/
418
private function populate_orders() {
419
$this->orders = array();
420
421
- // Get the orders
422
- $args = array(
423
'posts_per_page' => -1,
424
- 'post_type' => 'shop_order',
425
- 'post_status' => 'any',
426
- 'post__in' => $this->order_ids,
427
- 'orderby' => 'post__in'
428
);
429
$posts = get_posts( $args );
430
431
- // All orders should exist
432
- if( count( $posts ) !== count( $this->order_ids ) ) {
433
$this->orders = null;
434
return false;
435
}
436
437
- // Check permissons of the user to determine
438
- // if the orders should be populated.
439
- foreach( $posts as $post ) {
440
$order = new WC_Order( $post->ID );
441
442
- $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_id() : $order->id;
443
- // Logged in users
444
- if( is_user_logged_in() && ( !current_user_can( 'edit_shop_orders' ) && !current_user_can( 'view_order', $wdn_order_id ) ) ) {
445
$this->orders = null;
446
return false;
447
}
448
449
- $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_billing_email() : $order->billing_email;
450
-
451
452
- // An email is required for not logged in users
453
- if( !is_user_logged_in() && ( empty( $this->order_email ) || strtolower( $wdn_order_billing_id ) != $this->order_email ) ) {
454
$this->orders = null;
455
return false;
456
}
457
458
- // Save the order to get it without an additional database call
459
- $this->orders[$post->ID] = $order;
460
}
461
return true;
462
}
463
464
/**
465
- * Get the order
466
*/
467
public function get_order( $order_id ) {
468
- if( isset( $this->orders[$order_id] ) ) {
469
- return $this->orders[$order_id];
470
}
471
- return;
472
}
473
474
/**
475
- * Get the order invoice number
476
*/
477
public function get_order_invoice_number( $order_id ) {
478
- $invoice_count = intval( get_option( 'wcdn_invoice_number_count', 1 ) );
479
$invoice_prefix = get_option( 'wcdn_invoice_number_prefix' );
480
$invoice_suffix = get_option( 'wcdn_invoice_number_suffix' );
481
482
- // Add the invoice number to the order when it doesn't yet exist
483
- $meta_key = '_wcdn_invoice_number';
484
$meta_added = add_post_meta( $order_id, $meta_key, $invoice_prefix . $invoice_count . $invoice_suffix, true );
485
486
- // Update the total count
487
- if( $meta_added ) {
488
- update_option( 'wcdn_invoice_number_count', $invoice_count + 1 );
489
}
490
491
- // Get the invoice number
492
return apply_filters( 'wcdn_order_invoice_number', get_post_meta( $order_id, $meta_key, true ) );
493
}
494
495
/**
496
- * Get the order invoice date
497
*/
498
public function get_order_invoice_date( $order_id ) {
499
- // Add the invoice date to the order when it doesn't yet exist
500
- $meta_key = '_wcdn_invoice_date';
501
$meta_added = add_post_meta( $order_id, $meta_key, time(), true );
502
503
- // Get the invoice date
504
- $meta_date = get_post_meta( $order_id, $meta_key, true );
505
- $formatted_date = date_i18n( get_option('date_format'), $meta_date );
506
return apply_filters( 'wcdn_order_invoice_date', $formatted_date, $meta_date );
507
}
508
509
}
510
511
}
512
- ?>
1
<?php
2
+ /**
3
+ * Print class
4
+ *
5
+ * @package woocommerce-print-invoice-delivery-notes
6
+ */
7
8
/**
9
* Exit if accessed directly
10
*/
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
exit;
13
}
14
15
/**
16
* Print class
17
*/
18
+ if ( ! class_exists( 'WCDN_Print' ) ) {
19
20
+ /**
21
+ * WooCommerce Delivery Notes Print class.
22
+ */
23
+ class WCDN_Print {
24
25
+ /**
26
+ * Template registrations
27
+ *
28
+ * @var array $template_registrations
29
+ */
30
public static $template_registrations;
31
+
32
+ /**
33
+ * Template styles
34
+ *
35
+ * @var array $template_styles
36
+ */
37
public static $template_styles;
38
39
+ /**
40
+ * Template locations
41
+ *
42
+ * @var array $template_locations
43
+ */
44
public $template_locations;
45
+
46
+ /**
47
+ * Default Template
48
+ *
49
+ * @var array $template
50
+ */
51
public $template;
52
53
+ /**
54
+ * API Endpoints
55
+ *
56
+ * @var array $api_endpoints
57
+ */
58
public $api_endpoints;
59
+
60
+ /**
61
+ * Query vars
62
+ *
63
+ * @var array $query_vars
64
+ */
65
public $query_vars;
66
67
+ /**
68
+ * Order IDs
69
+ *
70
+ * @var array $order_ids
71
+ */
72
public $order_ids;
73
+
74
+ /**
75
+ * Order email
76
+ *
77
+ * @var array $order_email
78
+ */
79
public $order_email;
80
+
81
+ /**
82
+ * Orders
83
+ *
84
+ * @var array $orders
85
+ */
86
public $orders;
87
88
/**
89
* Constructor
90
*/
91
public function __construct() {
92
+ // Define the templates.
93
+ self::$template_registrations = apply_filters(
94
+ 'wcdn_template_registration',
95
+ array(
96
+ apply_filters(
97
+ 'wcdn_template_registration_invoice',
98
+ array(
99
+ 'type' => 'invoice',
100
+ 'labels' => array(
101
+ 'name' => __( 'Invoice', 'woocommerce-delivery-notes' ),
102
+ 'name_plural' => __( 'Invoices', 'woocommerce-delivery-notes' ),
103
+ 'print' => __( 'Print Invoice', 'woocommerce-delivery-notes' ),
104
+ 'print_plural' => __( 'Print Invoices', 'woocommerce-delivery-notes' ),
105
+ 'message' => __( 'Invoice created.', 'woocommerce-delivery-notes' ),
106
+ 'message_plural' => __( 'Invoices created.', 'woocommerce-delivery-notes' ),
107
+ 'setting' => __( 'Show "Print Invoice" button', 'woocommerce-delivery-notes' ),
108
+ ),
109
+ )
110
+ ),
111
+ apply_filters(
112
+ 'wcdn_template_registration_delivery_note',
113
+ array(
114
+ 'type' => 'delivery-note',
115
+ 'labels' => array(
116
+ 'name' => __( 'Delivery Note', 'woocommerce-delivery-notes' ),
117
+ 'name_plural' => __( 'Delivery Notes', 'woocommerce-delivery-notes' ),
118
+ 'print' => __( 'Print Delivery Note', 'woocommerce-delivery-notes' ),
119
+ 'print_plural' => __( 'Print Delivery Notes', 'woocommerce-delivery-notes' ),
120
+ 'message' => __( 'Delivery Note created.', 'woocommerce-delivery-notes' ),
121
+ 'message_plural' => __( 'Delivery Notes created.', 'woocommerce-delivery-notes' ),
122
+ 'setting' => __( 'Show "Print Delivery Note" button', 'woocommerce-delivery-notes' ),
123
+ ),
124
+ )
125
+ ),
126
+ apply_filters(
127
+ 'wcdn_template_registration_receipt',
128
+ array(
129
+ 'type' => 'receipt',
130
+ 'labels' => array(
131
+ 'name' => __( 'Receipt', 'woocommerce-delivery-notes' ),
132
+ 'name_plural' => __( 'Receipts', 'woocommerce-delivery-notes' ),
133
+ 'print' => __( 'Print Receipt', 'woocommerce-delivery-notes' ),
134
+ 'print_plural' => __( 'Print Receipts', 'woocommerce-delivery-notes' ),
135
+ 'message' => __( 'Receipt created.', 'woocommerce-delivery-notes' ),
136
+ 'message_plural' => __( 'Receipts created.', 'woocommerce-delivery-notes' ),
137
+ 'setting' => __( 'Show "Print Receipt" button', 'woocommerce-delivery-notes' ),
138
+ ),
139
+ )
140
+ ),
141
+ )
142
+ );
143
+
144
+ // Add the default template as first item after filter hooks passed.
145
+ array_unshift(
146
+ self::$template_registrations,
147
+ array(
148
+ 'type' => 'order',
149
'labels' => array(
150
+ 'name' => __( 'Order', 'woocommerce-delivery-notes' ),
151
+ 'name_plural' => __( 'Orders', 'woocommerce-delivery-notes' ),
152
+ 'print' => __( 'Print Order', 'woocommerce-delivery-notes' ),
153
+ 'print_plural' => __( 'Print Orders', 'woocommerce-delivery-notes' ),
154
+ 'message' => null,
155
+ 'message_plural' => null,
156
+ 'setting' => null,
157
+ ),
158
)
159
+ );
160
161
+ // Template styles.
162
self::$template_styles = apply_filters( 'wcdn_template_styles', array() );
163
164
+ // Add the default style as first item after filter hooks passed.
165
+ array_unshift(
166
+ self::$template_styles,
167
+ array(
168
+ 'name' => __( 'Default', 'woocommerce-delivery-notes' ),
169
+ 'type' => 'default',
170
+ 'path' => WooCommerce_Delivery_Notes::$plugin_path . 'templates/print-order/',
171
+ 'url' => WooCommerce_Delivery_Notes::$plugin_url . 'templates/print-order/',
172
+ )
173
+ );
174
175
+ // Default template.
176
$this->template = self::$template_registrations[0];
177
178
+ // Build all template locations.
179
$this->template_locations = $this->build_template_locations();
180
181
+ // Add the endpoint for the frontend.
182
$this->api_endpoints = array(
183
+ 'print-order' => get_option( 'wcdn_print_order_page_endpoint', 'print-order' ),
184
);
185
186
+ // Insert the query vars.
187
$this->query_vars = array(
188
'print-order-type',
189
+ 'print-order-email',
190
);
191
192
+ // Load the hooks.
193
add_action( 'init', array( $this, 'load_hooks' ) );
194
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
195
add_action( 'parse_request', array( $this, 'parse_request' ) );
196
add_action( 'template_redirect', array( $this, 'template_redirect_theme' ) );
197
add_action( 'wp_ajax_print_order', array( $this, 'template_redirect_admin' ) );
198
+ add_action( 'wcdn_after_items', array( $this, 'wdn_add_extra_data_after_items' ), 10, 1 );
199
}
200
201
+ /**
202
+ * Add extra data after items
203
+ *
204
+ * @param object $order Order.
205
+ */
206
+ public function wdn_add_extra_data_after_items( $order ) {
207
+
208
/**
209
* Local pickup plus plugin is active
210
*/
211
+ if ( class_exists( 'WC_Local_Pickup_Plus' ) ) {
212
+
213
$cdn_local_pickup_plugin_plugins_version = wc_local_pickup_plus()->get_version();
214
215
+ if ( version_compare( $cdn_local_pickup_plugin_plugins_version, '2.0.0', '>=' ) ) {
216
+ $cdn_local_pickup_object = new WC_Local_Pickup_Plus_Orders();
217
+ $local_pickup = wc_local_pickup_plus();
218
$cdn_local_pickup_locations = $cdn_local_pickup_object->get_order_pickup_data( $order );
219
$cdn_local_pickup__shipping_object = $local_pickup->get_shipping_method_instance();
220
+ self::cdn_print_local_pickup_address( $cdn_local_pickup_locations, $cdn_local_pickup__shipping_object );
221
}
222
}
223
}
224
225
+ /**
226
+ * Print Local Pickup Address
227
+ *
228
+ * @param array $cdn_local_pickup_locations Local pickup locations.
229
+ * @param object $shipping_method Shipping method.
230
+ */
231
public function cdn_print_local_pickup_address( $cdn_local_pickup_locations, $shipping_method ) {
232
233
$package_number = 1;
234
$packages_count = count( $cdn_local_pickup_locations );
235
+ foreach ( $cdn_local_pickup_locations as $pickup_meta ) :
236
+ ?>
237
<div>
238
<?php if ( $packages_count > 1 ) : ?>
239
+ <h5><?php echo wp_kses_post( sprintf( is_rtl() ? '#%2$s %1$s' : '%1$s #%2$s', esc_html( $shipping_method->get_method_title() ), $package_number ) ); ?></h5>
240
<?php endif; ?>
241
<ul>
242
<?php foreach ( $pickup_meta as $label => $value ) : ?>
251
</ul>
252
<?php $package_number++; ?>
253
</div>
254
+ <?php
255
+ endforeach;
256
}
257
+
258
/**
259
* Load the init hooks
260
*/
261
public function load_hooks() {
262
+ // Add the endpoints.
263
$this->add_endpoints();
264
}
265
269
* generate the print template and link.
270
*/
271
public function add_endpoints() {
272
+ foreach ( $this->api_endpoints as $var ) {
273
add_rewrite_endpoint( $var, EP_PAGES );
274
}
275
276
// Flush the rules when the transient is set.
277
// This is important to make the endpoint work.
278
+ if ( '1' === get_transient( 'wcdn_flush_rewrite_rules' ) ) {
279
delete_transient( 'wcdn_flush_rewrite_rules' );
280
flush_rewrite_rules();
281
}
283
284
/**
285
* Add the query vars to the url
286
+ *
287
+ * @param array $vars Query variables.
288
*/
289
public function add_query_vars( $vars ) {
290
+ foreach ( $this->query_vars as $var ) {
291
$vars[] = $var;
292
}
293
+ return $vars;
294
}
295
296
/**
297
* Parse the query variables
298
+ *
299
+ * @param object $wp WP Object.
300
*/
301
public function parse_request( $wp ) {
302
// Map endpoint keys to their query var keys, when another endpoint name was set.
303
+ foreach ( $this->api_endpoints as $key => $var ) {
304
+ if ( isset( $_GET[ $var ] ) ) {
305
+ // changed.
306
+ $wdn_get_end_point_var = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
307
+ $wp->query_vars[ $key ] = $wdn_get_end_point_var;
308
+ } elseif ( isset( $wp->query_vars[ $var ] ) ) {
309
+ $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
310
}
311
}
312
}
317
public function build_template_locations() {
318
$wc_template_directory = WC_TEMPLATE_PATH . 'print-order/';
319
320
+ // Get the paths for custom styles.
321
$settings_type = get_option( 'wcdn_template_style' );
322
$settings_path = null;
323
+ $settings_url = null;
324
+ if ( isset( $settings_type ) && 'default' !== $settings_type ) {
325
+ foreach ( self::$template_styles as $template_style ) {
326
+ if ( $settings_type === $template_style['type'] ) {
327
$settings_path = $template_style['path'];
328
+ $settings_url = $template_style['url'];
329
break;
330
}
331
}
332
}
333
334
+ // Build the locations.
335
$locations = array(
336
'child_theme' => array(
337
'path' => trailingslashit( get_stylesheet_directory() ) . $wc_template_directory,
338
+ 'url' => trailingslashit( get_stylesheet_directory_uri() ) . $wc_template_directory,
339
),
340
341
+ 'theme' => array(
342
'path' => trailingslashit( get_template_directory() ) . $wc_template_directory,
343
+ 'url' => trailingslashit( get_template_directory_uri() ) . $wc_template_directory,
344
),
345
346
+ 'settings' => array(
347
'path' => $settings_path,
348
+ 'url' => $settings_url,
349
),
350
351
+ 'plugin' => array(
352
'path' => self::$template_styles[0]['path'],
353
+ 'url' => self::$template_styles[0]['url'],
354
+ ),
355
);
356
357
return $locations;
362
*/
363
public function template_redirect_theme() {
364
global $wp;
365
+ // Check the page url and display the template when on my-account page.
366
+ if ( ! empty( $wp->query_vars['print-order'] ) && is_account_page() ) {
367
+ $type = ! empty( $wp->query_vars['print-order-type'] ) ? $wp->query_vars['print-order-type'] : null;
368
+ $email = ! empty( $wp->query_vars['print-order-email'] ) ? $wp->query_vars['print-order-email'] : null;
369
$this->generate_template( $wp->query_vars['print-order'], $type, $email );
370
exit;
371
}
375
* Template handling in the back-end
376
*/
377
public function template_redirect_admin() {
378
+ // Let the backend only access the page.
379
+ // changed.
380
+ if ( is_admin() && current_user_can( 'edit_shop_orders' ) && ! empty( $_REQUEST['print-order'] ) && ! empty( $_REQUEST['action'] ) ) {
381
+ $type = ! empty( $_REQUEST['print-order-type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['print-order-type'] ) ) : null;
382
+ $email = ! empty( $_REQUEST['print-order-email'] ) ? sanitize_email( wp_unslash( $_REQUEST['print-order-email'] ) ) : null;
383
+ // changed.
384
+ $wdn_get_print_order = isset( $_GET['print-order'] ) ? sanitize_text_field( wp_unslash( $_GET['print-order'] ) ) : '';
385
$this->generate_template( $wdn_get_print_order, $type, $email );
386
exit;
387
}
390
391
/**
392
* Generate the template
393
+ *
394
+ * @param array $order_ids Order IDs.
395
+ * @param string $template_type Template type.
396
+ * @param string $order_email Order email.
397
*/
398
public function generate_template( $order_ids, $template_type = 'order', $order_email = null ) {
399
global $post, $wp;
400
401
+ // Explode the ids when needed.
402
+ if ( ! is_array( $order_ids ) ) {
403
+ $this->order_ids = array_filter( explode( '-', $order_ids ) );
404
}
405
406
+ // Set the current template.
407
+ foreach ( self::$template_registrations as $template_registration ) {
408
+ if ( $template_type === $template_registration['type'] ) {
409
$this->template = $template_registration;
410
break;
411
}
412
}
413
414
+ // Set the email.
415
+ if ( empty( $order_email ) ) {
416
$this->order_email = null;
417
} else {
418
$this->order_email = strtolower( $order_email );
419
}
420
421
+ // Create the orders and check permissions.
422
$populated = $this->populate_orders();
423
424
+ // Only continue if the orders are populated.
425
+ if ( ! $populated ) {
426
die();
427
}
428
429
+ // Load the print template html.
430
$location = $this->get_template_file_location( 'print-order.php' );
431
wc_get_template( 'print-order.php', null, $location, $location );
432
exit;
433
}
434
435
/**
436
+ * Find the location of a template file.
437
+ *
438
+ * @param string $name Template name.
439
+ * @param boolean $url_mode URL mode.
440
*/
441
public function get_template_file_location( $name, $url_mode = false ) {
442
$found = '';
443
+ foreach ( $this->template_locations as $template_location ) {
444
+ if ( isset( $template_location['path'] ) && file_exists( trailingslashit( $template_location['path'] ) . $name ) ) {
445
+ if ( $url_mode ) {
446
$found = $template_location['url'];
447
} else {
448
$found = $template_location['path'];
455
456
/**
457
* Get print page url
458
+ *
459
+ * @param array $order_ids Order ids.
460
+ * @param string $template_type Template type.
461
+ * @param string $order_email Order email.
462
+ * @param boolean $permalink Permalink.
463
*/
464
public function get_print_page_url( $order_ids, $template_type = 'order', $order_email = null, $permalink = false ) {
465
+ // Explode the ids when needed.
466
+ if ( ! is_array( $order_ids ) ) {
467
$order_ids = array_filter( explode( '-', $order_ids ) );
468
}
469
470
+ // Build the args.
471
$args = array();
472
473
+ // Set the template type arg.
474
+ foreach ( self::$template_registrations as $template_registration ) {
475
+ if ( $template_type === $template_registration['type'] && 'order' !== $template_type ) {
476
$args = wp_parse_args( array( 'print-order-type' => $template_type ), $args );
477
break;
478
}
479
}
480
481
+ // Set the email arg.
482
+ if ( ! empty( $order_email ) ) {
483
$args = wp_parse_args( array( 'print-order-email' => $order_email ), $args );
484
}
485
486
+ // Generate the url.
487
$order_ids_slug = implode( '-', $order_ids );
488
489
+ // Create another url depending on where the user prints. This prevents some issues with ssl when the my-account page is secured with ssl but the admin isn't.
490
+ if ( is_admin() && current_user_can( 'edit_shop_orders' ) && false === $permalink ) {
491
+ // For the admin we use the ajax.php for better security.
492
+ $args = wp_parse_args( array( 'action' => 'print_order' ), $args );
493
$base_url = admin_url( 'admin-ajax.php' );
494
$endpoint = 'print-order';
495
496
+ // Add the order ids and create the url.
497
$url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
498
} else {
499
+ // For the theme.
500
$base_url = wc_get_page_permalink( 'myaccount' );
501
$endpoint = $this->api_endpoints['print-order'];
502
503
+ // Add the order ids and create the url.
504
+ if ( get_option( 'permalink_structure' ) ) {
505
$url = trailingslashit( trailingslashit( $base_url ) . $endpoint . '/' . $order_ids_slug );
506
} else {
507
$url = add_query_arg( $endpoint, $order_ids_slug, $base_url );
508
}
509
}
510
511
+ // Add all other args.
512
$url = add_query_arg( $args, $url );
513
514
return esc_url( $url );
515
}
516
517
/**
518
+ * Create the orders list and check the permissions.
519
*/
520
private function populate_orders() {
521
$this->orders = array();
522
523
+ // Get the orders.
524
+ $args = array(
525
'posts_per_page' => -1,
526
+ 'post_type' => 'shop_order',
527
+ 'post_status' => 'any',
528
+ 'post__in' => $this->order_ids,
529
+ 'orderby' => 'post__in',
530
);
531
$posts = get_posts( $args );
532
533
+ // All orders should exist.
534
+ if ( count( $posts ) !== count( $this->order_ids ) ) {
535
$this->orders = null;
536
return false;
537
}
538
539
+ // Check permissons of the user to determine if the orders should be populated.
540
+ foreach ( $posts as $post ) {
541
$order = new WC_Order( $post->ID );
542
543
+ $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_id() : $order->id;
544
+ // Logged in users.
545
+ if ( is_user_logged_in() && ( ! current_user_can( 'edit_shop_orders' ) && ! current_user_can( 'view_order', $wdn_order_id ) ) ) {
546
$this->orders = null;
547
return false;
548
}
549
550
+ $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_billing_email() : $order->billing_email;
551
552
+ // An email is required for not logged in users.
553
+ if ( ! is_user_logged_in() && ( empty( $this->order_email ) || strtolower( $wdn_order_billing_id ) !== $this->order_email ) ) {
554
$this->orders = null;
555
return false;
556
}
557
558
+ // Save the order to get it without an additional database call.
559
+ $this->orders[ $post->ID ] = $order;
560
}
561
return true;
562
}
563
564
/**
565
+ * Get the order.
566
+ *
567
+ * @param int $order_id Order id.
568
*/
569
public function get_order( $order_id ) {
570
+ if ( isset( $this->orders[ $order_id ] ) ) {
571
+ return $this->orders[ $order_id ];
572
}
573
+ return false;
574
}
575
576
/**
577
+ * Get the order invoice number.
578
+ *
579
+ * @param int $order_id Order id.
580
*/
581
public function get_order_invoice_number( $order_id ) {
582
+ $invoice_count = intval( get_option( 'wcdn_invoice_number_count', 1 ) );
583
$invoice_prefix = get_option( 'wcdn_invoice_number_prefix' );
584
$invoice_suffix = get_option( 'wcdn_invoice_number_suffix' );
585
586
+ // Add the invoice number to the order when it doesn't yet exist.
587
+ $meta_key = '_wcdn_invoice_number';
588
$meta_added = add_post_meta( $order_id, $meta_key, $invoice_prefix . $invoice_count . $invoice_suffix, true );
589
590
+ // Update the total count.
591
+ if ( $meta_added ) {
592
+ update_option( 'wcdn_invoice_number_count', $invoice_count + 1 );
593
}
594
595
+ // Get the invoice number.
596
return apply_filters( 'wcdn_order_invoice_number', get_post_meta( $order_id, $meta_key, true ) );
597
}
598
599
/**
600
+ * Get the order invoice date.
601
+ *
602
+ * @param int $order_id Order id.
603
*/
604
public function get_order_invoice_date( $order_id ) {
605
+ // Add the invoice date to the order when it doesn't yet exist.
606
+ $meta_key = '_wcdn_invoice_date';
607
$meta_added = add_post_meta( $order_id, $meta_key, time(), true );
608
609
+ // Get the invoice date.
610
+ $meta_date = get_post_meta( $order_id, $meta_key, true );
611
+ $formatted_date = date_i18n( get_option( 'date_format' ), $meta_date );
612
return apply_filters( 'wcdn_order_invoice_date', $formatted_date, $meta_date );
613
}
614
615
}
616
617
}
618
+ ?>
includes/class-wcdn-settings.php CHANGED
@@ -1,35 +1,45 @@
1
<?php
2
3
/**
4
* Exit if accessed directly
5
*/
6
- if ( !defined( 'ABSPATH' ) ) {
7
exit;
8
}
9
10
/**
11
* Settings class
12
*/
13
- if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
14
15
/**
16
* WooCommerce Print Delivery Notes
17
- *
18
* @author Tyche Softwares
19
* @package WooCommerce-Delivery-Notes/Settings
20
*/
21
- class WooCommerce_Delivery_Notes_Settings {
22
23
public $id;
24
25
/**
26
* Constructor
27
*/
28
public function __construct() {
29
- // Define default variables
30
$this->id = 'wcdn-settings';
31
32
- // Load the hooks
33
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 200 );
34
add_action( 'woocommerce_settings_start', array( $this, 'add_assets' ) );
35
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
@@ -38,16 +48,19 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
38
add_action( 'woocommerce_admin_field_wcdn_image_select', array( $this, 'output_image_select' ) );
39
add_action( 'wp_ajax_wcdn_settings_load_image', array( $this, 'load_image_ajax' ) );
40
add_filter( 'wcdn_get_settings', array( $this, 'generate_template_type_fields' ), 10, 2 );
41
- add_action( 'woocommerce_admin_field_link' , array( &$this, 'wcdn_add_admin_field_reset_button' ) );
42
}
43
44
/**
45
* It will add a reset tracking data button on the settting page.
46
* @hook woocommerce_admin_field_link
47
*/
48
- public static function wcdn_add_admin_field_reset_button ( $value ) {
49
- if ( $value [ 'id' ] == 'ts_reset_tracking' ){
50
- do_action ( 'wcdn_add_new_settings', $value );
51
}
52
}
53
@@ -55,79 +68,85 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
55
* Add the scripts
56
*/
57
public function add_assets() {
58
- // Styles
59
- wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css' );
60
61
- // Scripts
62
wp_enqueue_media();
63
- wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) );
64
- wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'custom-header', 'woocommerce-delivery-notes-print-link' ) );
65
66
- // Localize the script strings
67
$translation = array( 'resetCounter' => __( 'Do you really want to reset the counter to zero? This process can\'t be undone.', 'woocommerce-delivery-notes' ) );
68
wp_localize_script( 'woocommerce-delivery-notes-admin', 'WCDNText', $translation );
69
}
70
71
/**
72
* Create a new settings tab
73
*/
74
public function add_settings_page( $settings_tabs ) {
75
- $settings_tabs[$this->id] = __( 'Print', 'woocommerce-delivery-notes' );
76
return $settings_tabs;
77
}
78
79
/**
80
- * Output the settings fields into the tab
81
*/
82
public function output() {
83
global $current_section;
84
$settings = $this->get_settings( $current_section );
85
- woocommerce_admin_fields( $settings );
86
}
87
88
/**
89
* Save the settings
90
*/
91
- function save() {
92
- global $current_section;
93
set_transient( 'wcdn_flush_rewrite_rules', true );
94
$settings = $this->get_settings( $current_section );
95
- woocommerce_update_options( $settings );
96
}
97
98
/**
99
* Get the settings fields
100
*/
101
public function get_settings( $section = '' ) {
102
- $wcdn_faq_url = admin_url( 'index.php?page=wcdn_faq_page' ) ;
103
- $settings = apply_filters( 'wcdn_get_settings_no_section',
104
- array(
105
- array(
106
- 'title' => __( 'Template', 'woocommerce-delivery-notes' ),
107
- 'type' => 'title',
108
- 'desc' => $this->get_template_description(),
109
- 'id' => 'general_options'
110
- ),
111
112
array(
113
'title' => __( 'Style', 'woocommerce-delivery-notes' ),
114
'desc' => sprintf( __( 'The default print style. Read the <a href="%1$s">FAQ</a> to learn how to customize it.', 'woocommerce-delivery-notes' ), $wcdn_faq_url, '#' ),
115
'id' => 'wcdn_template_style',
116
'class' => 'wc-enhanced-select',
117
'default' => '',
118
'type' => 'select',
119
'options' => $this->get_options_styles(),
120
- 'desc_tip' => false,
121
),
122
123
array(
124
- 'title' => __( 'Shop Logo', 'woocommerce-delivery-notes' ),
125
- 'desc' => '',
126
- 'id' => 'wcdn_company_logo_image_id',
127
- 'css' => '',
128
- 'default' => '',
129
- 'type' => 'wcdn_image_select',
130
- 'desc_tip' => __( 'A shop logo representing your business. When the image is printed, its pixel density will automatically be eight times higher than the original. This means, 1 printed inch will correspond to about 288 pixels on the screen.', 'woocommerce-delivery-notes' )
131
),
132
133
array(
@@ -137,7 +156,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
137
'css' => 'min-width:100%;',
138
'default' => '',
139
'type' => 'text',
140
- 'desc_tip' => __( 'The shop name. Leave blank to use the default Website or Blog title defined in WordPress settings. The name will be ignored when a Logo is set.', 'woocommerce-delivery-notes' ),
141
),
142
143
array(
@@ -147,7 +166,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
147
'css' => 'min-width:100%;min-height:100px;',
148
'default' => '',
149
'type' => 'textarea',
150
- 'desc_tip' => true,
151
),
152
153
array(
@@ -157,7 +176,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
157
'css' => 'min-width:100%;min-height:100px;',
158
'default' => '',
159
'type' => 'textarea',
160
- 'desc_tip' => true,
161
),
162
163
array(
@@ -167,7 +186,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
167
'css' => 'min-width:100%;min-height:100px;',
168
'default' => '',
169
'type' => 'textarea',
170
- 'desc_tip' => true,
171
),
172
173
array(
@@ -177,22 +196,22 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
177
'css' => 'min-width:100%;min-height:100px;',
178
'default' => '',
179
'type' => 'textarea',
180
- 'desc_tip' => true,
181
),
182
183
array(
184
- 'type' => 'sectionend',
185
- 'id' => 'general_options'
186
),
187
188
array(
189
- 'title' => __( 'Pages & Buttons', 'woocommerce-delivery-notes' ),
190
- 'type' => 'title',
191
- 'desc' => '',
192
- 'id' => 'display_options'
193
- ),
194
195
- array(
196
'title' => __( 'Print Page Endpoint', 'woocommerce-delivery-notes' ),
197
'desc' => '',
198
'id' => 'wcdn_print_order_page_endpoint',
@@ -203,50 +222,50 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
203
),
204
205
array(
206
- 'title' => __( 'Email', 'woocommerce-delivery-notes' ),
207
- 'desc' => __( 'Show print link in customer emails', 'woocommerce-delivery-notes' ),
208
- 'id' => 'wcdn_email_print_link',
209
- 'default' => 'no',
210
- 'type' => 'checkbox',
211
- 'desc_tip' => __( 'This includes the emails for a new, processing and completed order. On top of that the customer invoice email also includes the link.', 'woocommerce-delivery-notes' )
212
),
213
214
array(
215
- 'title' => __( 'My Account', 'woocommerce-delivery-notes' ),
216
- 'desc' => __( 'Show print button on the "View Order" page', 'woocommerce-delivery-notes' ),
217
- 'id' => 'wcdn_print_button_on_view_order_page',
218
- 'default' => 'no',
219
- 'type' => 'checkbox',
220
- 'checkboxgroup' => 'start'
221
),
222
223
array(
224
- 'desc' => __( 'Show print buttons on the "My Account" page', 'woocommerce-delivery-notes' ),
225
- 'id' => 'wcdn_print_button_on_my_account_page',
226
- 'default' => 'no',
227
- 'type' => 'checkbox',
228
- 'checkboxgroup' => 'end'
229
),
230
231
- array(
232
- 'type' => 'sectionend',
233
- 'id' => 'display_options'
234
),
235
236
array(
237
- 'title' => __( 'Invoice', 'woocommerce-delivery-notes' ),
238
- 'type' => 'title',
239
- 'desc' => '',
240
- 'id' => 'invoice_options'
241
- ),
242
-
243
- array(
244
- 'title' => __( 'Numbering', 'woocommerce-delivery-notes' ),
245
- 'desc' => __( 'Create invoice numbers', 'woocommerce-delivery-notes' ),
246
- 'id' => 'wcdn_create_invoice_number',
247
- 'default' => 'no',
248
- 'type' => 'checkbox',
249
- 'desc_tip' => ''
250
),
251
252
array(
@@ -257,7 +276,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
257
'css' => '',
258
'default' => 1,
259
'type' => 'number',
260
- 'desc_tip' => __( 'The next invoice number.', 'woocommerce-delivery-notes' )
261
),
262
263
array(
@@ -268,7 +287,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
268
'css' => '',
269
'default' => '',
270
'type' => 'text',
271
- 'desc_tip' => __( 'This text will be prepended to the invoice number.', 'woocommerce-delivery-notes' )
272
),
273
274
array(
@@ -279,25 +298,29 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
279
'css' => '',
280
'default' => '',
281
'type' => 'text',
282
- 'desc_tip' => __( 'This text will be appended to the invoice number.', 'woocommerce-delivery-notes' )
283
),
284
- apply_filters ( 'wcdn_add_settings_field', '' ),
285
- array(
286
- 'type' => 'sectionend',
287
- 'id' => 'invoice_options'
288
),
289
- )
290
- );
291
292
- return apply_filters( 'wcdn_get_settings', $settings, $section );
293
}
294
295
/**
296
- * Get the position of a setting inside the array
297
*/
298
public function get_setting_position( $id, $settings, $type = null ) {
299
- foreach( $settings as $key => $value ) {
300
- if( isset( $value['id'] ) && $value['id'] == $id ) {
301
return $key;
302
}
303
}
@@ -307,43 +330,46 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
307
308
/**
309
* Generate the template type setting fields
310
*/
311
public function generate_template_type_fields( $settings, $section = '' ) {
312
$position = $this->get_setting_position( 'wcdn_email_print_link', $settings );
313
- if( $position !== false ) {
314
$new_settings = array();
315
316
- // Go through all registrations but remove the default 'order' type
317
- $template_registrations = WooCommerce_Delivery_Notes_Print::$template_registrations;
318
array_splice( $template_registrations, 0, 1 );
319
$end = count( $template_registrations ) - 1;
320
- foreach( $template_registrations as $index => $template_registration ) {
321
- $title = '';
322
- $desc_tip = '';
323
$checkboxgroup = '';
324
325
- // Define the group settings
326
- if( $index == 0 ) {
327
- $title = __( 'Admin', 'woocommerce-delivery-notes' );
328
$checkboxgroup = 'start';
329
- } else if( $index == $end ) {
330
- $desc_tip = __( 'The print buttons are available on the order listing and on the order detail screen.', 'woocommerce-delivery-notes' );
331
$checkboxgroup = 'end';
332
}
333
334
- // Create the setting
335
$new_settings[] = array(
336
- 'title' => $title,
337
- 'desc' => $template_registration['labels']['setting'],
338
- 'id' => 'wcdn_template_type_' . $template_registration['type'],
339
- 'default' => 'no',
340
- 'type' => 'checkbox',
341
- 'checkboxgroup' => $checkboxgroup,
342
- 'desc_tip' => $desc_tip
343
);
344
}
345
346
- // Add the settings
347
$settings = $this->array_merge_at( $settings, $new_settings, $position );
348
}
349
@@ -351,24 +377,25 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
351
}
352
353
/**
354
- * Generate the description for the template settings
355
*/
356
public function get_template_description() {
357
$description = '';
358
- $args = array(
359
- 'post_type' => 'shop_order',
360
- 'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed' ),
361
- 'posts_per_page' => 1
362
);
363
- $query = new WP_Query( $args );
364
365
- // show template preview links when an order is available
366
- if( $query->have_posts() ) {
367
- $results = $query->get_posts();
368
- $test_id = $results[0]->ID;
369
- $invoice_url = wcdn_get_print_link( $test_id, 'invoice' );
370
$delivery_note_url = wcdn_get_print_link( $test_id, 'delivery-note' );
371
- $receipt_url = wcdn_get_print_link( $test_id, 'receipt' );
372
$description = sprintf( __( 'This section lets you customise the content. You can preview the <a href="%1$s" target="%4$s" class="%5$s">invoice</a>, <a href="%2$s" target="%4$s" class="%5$s">delivery note</a> or <a href="%3$s" target="%4$s" class="%5$s">receipt</a> template.', 'woocommerce-delivery-notes' ), $invoice_url, $delivery_note_url, $receipt_url, '_blank', '' );
373
}
374
@@ -376,14 +403,14 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
376
}
377
378
/**
379
- * Generate the options for the template styles field
380
*/
381
public function get_options_styles() {
382
$options = array();
383
384
- foreach( WooCommerce_Delivery_Notes_Print::$template_styles as $template_style ) {
385
- if( is_array( $template_style ) && isset( $template_style['type'] ) && isset( $template_style['name'] ) ) {
386
- $options[$template_style['type']] = $template_style['name'];
387
}
388
}
389
@@ -391,46 +418,50 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
391
}
392
393
/**
394
- * Load image with ajax
395
*/
396
public function load_image_ajax() {
397
- // Verify the nonce
398
- if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'woocommerce-settings' ) ) {
399
die();
400
}
401
402
- // Verify the id
403
- if( empty( $_POST['attachment_id'] ) && is_numeric( $_POST['attachment_id'] ) ) {
404
die();
405
}
406
407
- $wdn_image_attachmnet_id = sanitize_text_field( $_POST['attachment_id'] );
408
- // create the image
409
$this->create_image( $wdn_image_attachmnet_id );
410
411
exit;
412
}
413
414
/**
415
- * Create image
416
*/
417
public function create_image( $attachment_id ) {
418
$attachment_src = wp_get_attachment_image_src( $attachment_id, 'medium', false );
419
- $orientation = 'landscape';
420
- if( ( $attachment_src[1] / $attachment_src[2] ) < 1 ) {
421
$orientation = 'portrait';
422
}
423
424
?>
425
- <img src="<?php echo $attachment_src[0]; ?>" class="<?php echo $orientation; ?>" alt="" />
426
<?php
427
}
428
429
/**
430
- * Output image select field
431
*/
432
public function output_image_select( $value ) {
433
- // Define the defaults
434
if ( ! isset( $value['title_select'] ) ) {
435
$value['title_select'] = __( 'Select', 'woocommerce-delivery-notes' );
436
}
@@ -439,65 +470,78 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
439
$value['title_remove'] = __( 'Remove', 'woocommerce-delivery-notes' );
440
}
441
442
- // Get additional data fields
443
- $field = WC_Admin_Settings::get_field_description( $value );
444
- $description = $field['description'];
445
$tooltip_html = $field['tooltip_html'];
446
$option_value = WC_Admin_Settings::get_option( $value['id'], $value['default'] );
447
- $class_name = 'wcdn-image-select';
448
449
- ?><tr valign="top">
450
451
452
<th scope="row" class="titledesc">
453
- <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; ?></label>
454
</th>
455
<td class="forminp image_width_settings">
456
- <input name="<?php echo esc_attr( $value['id'] ); ?>" id="<?php echo esc_attr( $value['id'] ); ?>" type="hidden" value="<?php echo esc_attr( $option_value ); ?>" class="<?php echo $class_name; ?>-image-id <?php echo esc_attr( $value['class'] ); ?>" />
457
458
- <div id="<?php echo esc_attr( $value['id'] ); ?>_field" class="<?php echo $class_name; ?>-field <?php echo esc_attr( $value['class'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>">
459
- <span id="<?php echo esc_attr( $value['id'] ); ?>_spinner" class="<?php echo $class_name; ?>-spinner spinner"></span>
460
- <div id="<?php echo esc_attr( $value['id'] ); ?>_attachment" class="<?php echo $class_name; ?>-attachment <?php echo esc_attr( $value['class'] ); ?> ">
461
<div class="thumbnail">
462
<div class="centered">
463
- <?php if( !empty( $option_value ) ) : ?>
464
<?php $this->create_image( $option_value ); ?>
465
<?php endif; ?>
466
</div>
467
</div>
468
</div>
469
470
- <div id="<?php echo esc_attr( $value['id'] ); ?>_buttons" class="<?php echo $class_name; ?>-buttons <?php echo esc_attr( $value['class'] ); ?>">
471
- <a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_remove_button" class="<?php echo $class_name; ?>-remove-button <?php if( empty( $option_value ) ) : ?>hidden<?php endif; ?> button">
472
<?php echo esc_html( $value['title_remove'] ); ?>
473
</a>
474
- <a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_add_button" class="<?php echo $class_name; ?>-add-button <?php if( !empty( $option_value ) ) : ?>hidden<?php endif; ?> button" data-uploader-title="<?php echo esc_attr( $value['title'] ); ?>" data-uploader-button-title="<?php echo esc_attr( $value['title_select'] ); ?>">
475
<?php echo esc_html( $value['title_select'] ); ?>
476
</a>
477
</div>
478
</div>
479
480
- <?php echo $description; ?>
481
</td>
482
</tr><?php
483
}
484
485
/**
486
- * Merge array at given position
487
*/
488
public function array_merge_at( $array, $insert, $position ) {
489
$new_array = array();
490
- // if pos is start, just merge them
491
- if( $position == 0 ) {
492
$new_array = array_merge( $insert, $array );
493
} else {
494
- // if pos is end just merge them
495
- if( $position >= ( count( $array ) - 1 ) ) {
496
- $new_array = array_merge($array, $insert);
497
} else {
498
- // split into head and tail, then merge head+inserted bit+tail
499
- $head = array_slice( $array, 0, $position );
500
- $tail = array_slice( $array, $position );
501
$new_array = array_merge( $head, $insert, $tail );
502
}
503
}
@@ -506,4 +550,4 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
506
}
507
508
}
509
- ?>
1
<?php
2
+ /**
3
+ * Settings class
4
+ *
5
+ * @package woocommerce-print-invoice-delivery-notes
6
+ */
7
8
/**
9
* Exit if accessed directly
10
*/
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
exit;
13
}
14
15
/**
16
* Settings class
17
*/
18
+ if ( ! class_exists( 'WCDN_Settings' ) ) {
19
20
/**
21
* WooCommerce Print Delivery Notes
22
+ *
23
* @author Tyche Softwares
24
* @package WooCommerce-Delivery-Notes/Settings
25
*/
26
+ class WCDN_Settings {
27
28
+ /**
29
+ * Id variable
30
+ *
31
+ * @var int $id ID variable
32
+ */
33
public $id;
34
35
/**
36
* Constructor
37
*/
38
public function __construct() {
39
+ // Define default variables.
40
$this->id = 'wcdn-settings';
41
42
+ // Load the hooks.
43
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 200 );
44
add_action( 'woocommerce_settings_start', array( $this, 'add_assets' ) );
45
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
48
add_action( 'woocommerce_admin_field_wcdn_image_select', array( $this, 'output_image_select' ) );
49
add_action( 'wp_ajax_wcdn_settings_load_image', array( $this, 'load_image_ajax' ) );
50
add_filter( 'wcdn_get_settings', array( $this, 'generate_template_type_fields' ), 10, 2 );
51
+ add_action( 'woocommerce_admin_field_link', array( &$this, 'wcdn_add_admin_field_reset_button' ) );
52
}
53
54
/**
55
* It will add a reset tracking data button on the settting page.
56
+ *
57
* @hook woocommerce_admin_field_link
58
+ *
59
+ * @param string $value Check if tracking is reset.
60
*/
61
+ public static function wcdn_add_admin_field_reset_button( $value ) {
62
+ if ( 'ts_reset_tracking' === $value ['id'] ) {
63
+ do_action( 'wcdn_add_new_settings', $value );
64
}
65
}
66
68
* Add the scripts
69
*/
70
public function add_assets() {
71
+ // Styles.
72
+ wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css', '', WooCommerce_Delivery_Notes::$plugin_version );
73
74
+ // Scripts.
75
wp_enqueue_media();
76
+ wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ), WooCommerce_Delivery_Notes::$plugin_version, false );
77
+ wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'custom-header', 'woocommerce-delivery-notes-print-link' ), WooCommerce_Delivery_Notes::$plugin_version, false );
78
79
+ // Localize the script strings.
80
$translation = array( 'resetCounter' => __( 'Do you really want to reset the counter to zero? This process can\'t be undone.', 'woocommerce-delivery-notes' ) );
81
wp_localize_script( 'woocommerce-delivery-notes-admin', 'WCDNText', $translation );
82
}
83
84
/**
85
* Create a new settings tab
86
+ *
87
+ * @param array $settings_tabs Add settings tab in WooCommerce->Settings page.
88
*/
89
public function add_settings_page( $settings_tabs ) {
90
+ $settings_tabs[ $this->id ] = __( 'Print', 'woocommerce-delivery-notes' );
91
return $settings_tabs;
92
}
93
94
/**
95
+ * Output the settings fields into the tab.
96
*/
97
public function output() {
98
global $current_section;
99
$settings = $this->get_settings( $current_section );
100
+ woocommerce_admin_fields( $settings );
101
}
102
103
/**
104
* Save the settings
105
*/
106
+ public function save() {
107
+ global $current_section;
108
set_transient( 'wcdn_flush_rewrite_rules', true );
109
$settings = $this->get_settings( $current_section );
110
+ woocommerce_update_options( $settings );
111
}
112
113
/**
114
* Get the settings fields
115
+ *
116
+ * @param string $section Section name.
117
*/
118
public function get_settings( $section = '' ) {
119
+ $wcdn_faq_url = admin_url( 'index.php?page=wcdn_faq_page' );
120
+ $settings = apply_filters(
121
+ 'wcdn_get_settings_no_section',
122
+ array(
123
+ array(
124
+ 'title' => __( 'Template', 'woocommerce-delivery-notes' ),
125
+ 'type' => 'title',
126
+ 'desc' => $this->get_template_description(),
127
+ 'id' => 'general_options',
128
+ ),
129
130
array(
131
'title' => __( 'Style', 'woocommerce-delivery-notes' ),
132
+ /* translators: %s: link to faq */
133
'desc' => sprintf( __( 'The default print style. Read the <a href="%1$s">FAQ</a> to learn how to customize it.', 'woocommerce-delivery-notes' ), $wcdn_faq_url, '#' ),
134
'id' => 'wcdn_template_style',
135
'class' => 'wc-enhanced-select',
136
'default' => '',
137
'type' => 'select',
138
'options' => $this->get_options_styles(),
139
+ 'desc_tip' => false,
140
),
141
142
array(
143
+ 'title' => __( 'Shop Logo', 'woocommerce-delivery-notes' ),
144
+ 'desc' => '',
145
+ 'id' => 'wcdn_company_logo_image_id',
146
+ 'css' => '',
147
+ 'default' => '',
148
+ 'type' => 'wcdn_image_select',
149
+ 'desc_tip' => __( 'A shop logo representing your business. When the image is printed, its pixel density will automatically be eight times higher than the original. This means, 1 printed inch will correspond to about 288 pixels on the screen.', 'woocommerce-delivery-notes' ),
150
),
151
152
array(
156
'css' => 'min-width:100%;',
157
'default' => '',
158
'type' => 'text',
159
+ 'desc_tip' => __( 'The shop name. Leave blank to use the default Website or Blog title defined in WordPress settings. The name will be ignored when a Logo is set.', 'woocommerce-delivery-notes' ),
160
),
161
162
array(
166
'css' => 'min-width:100%;min-height:100px;',
167
'default' => '',
168
'type' => 'textarea',
169
+ 'desc_tip' => true,
170
),
171
172
array(
176
'css' => 'min-width:100%;min-height:100px;',
177
'default' => '',
178
'type' => 'textarea',
179
+ 'desc_tip' => true,
180
),
181
182
array(
186
'css' => 'min-width:100%;min-height:100px;',
187
'default' => '',
188
'type' => 'textarea',
189
+ 'desc_tip' => true,
190
),
191
192
array(
196
'css' => 'min-width:100%;min-height:100px;',
197
'default' => '',
198
'type' => 'textarea',
199
+ 'desc_tip' => true,
200
),
201
202
array(
203
+ 'type' => 'sectionend',
204
+ 'id' => 'general_options',
205
),
206
207
array(
208
+ 'title' => __( 'Pages & Buttons', 'woocommerce-delivery-notes' ),
209
+ 'type' => 'title',
210
+ 'desc' => '',
211
+ 'id' => 'display_options',
212
+ ),
213
214
+ array(
215
'title' => __( 'Print Page Endpoint', 'woocommerce-delivery-notes' ),
216
'desc' => '',
217
'id' => 'wcdn_print_order_page_endpoint',
222
),
223
224
array(
225
+ 'title' => __( 'Email', 'woocommerce-delivery-notes' ),
226
+ 'desc' => __( 'Show print link in customer emails', 'woocommerce-delivery-notes' ),
227
+ 'id' => 'wcdn_email_print_link',
228
+ 'default' => 'no',
229
+ 'type' => 'checkbox',
230
+ 'desc_tip' => __( 'This includes the emails for a new, processing and completed order. On top of that the customer invoice email also includes the link.', 'woocommerce-delivery-notes' ),
231
+ ),
232
+
233
+ array(
234
+ 'title' => __( 'My Account', 'woocommerce-delivery-notes' ),
235
+ 'desc' => __( 'Show print button on the "View Order" page', 'woocommerce-delivery-notes' ),
236
+ 'id' => 'wcdn_print_button_on_view_order_page',
237
+ 'default' => 'no',
238
+ 'type' => 'checkbox',
239
+ 'checkboxgroup' => 'start',
240
),
241
242
array(
243
+ 'desc' => __( 'Show print buttons on the "My Account" page', 'woocommerce-delivery-notes' ),
244
+ 'id' => 'wcdn_print_button_on_my_account_page',
245
+ 'default' => 'no',
246
+ 'type' => 'checkbox',
247
+ 'checkboxgroup' => 'end',
248
),
249
250
array(
251
+ 'type' => 'sectionend',
252
+ 'id' => 'display_options',
253
),
254
255
+ array(
256
+ 'title' => __( 'Invoice', 'woocommerce-delivery-notes' ),
257
+ 'type' => 'title',
258
+ 'desc' => '',
259
+ 'id' => 'invoice_options',
260
),
261
262
array(
263
+ 'title' => __( 'Numbering', 'woocommerce-delivery-notes' ),
264
+ 'desc' => __( 'Create invoice numbers', 'woocommerce-delivery-notes' ),
265
+ 'id' => 'wcdn_create_invoice_number',
266
+ 'default' => 'no',
267
+ 'type' => 'checkbox',
268
+ 'desc_tip' => '',
269
),
270
271
array(
276
'css' => '',
277
'default' => 1,
278
'type' => 'number',
279
+ 'desc_tip' => __( 'The next invoice number.', 'woocommerce-delivery-notes' ),
280
),
281
282
array(
287
'css' => '',
288
'default' => '',
289
'type' => 'text',
290
+ 'desc_tip' => __( 'This text will be prepended to the invoice number.', 'woocommerce-delivery-notes' ),
291
),
292
293
array(
298
'css' => '',
299
'default' => '',
300
'type' => 'text',
301
+ 'desc_tip' => __( 'This text will be appended to the invoice number.', 'woocommerce-delivery-notes' ),
302
),
303
+ apply_filters( 'wcdn_add_settings_field', '' ),
304
+ array(
305
+ 'type' => 'sectionend',
306
+ 'id' => 'invoice_options',
307
),
308
+ )
309
+ );
310
311
+ return apply_filters( 'wcdn_get_settings', $settings, $section );
312
}
313
314
/**
315
+ * Get the position of a setting inside the array.
316
+ *
317
+ * @param int $id Setting ID.
318
+ * @param array $settings Setting details.
319
+ * @param string $type Type.
320
*/
321
public function get_setting_position( $id, $settings, $type = null ) {
322
+ foreach ( $settings as $key => $value ) {
323
+ if ( isset( $value['id'] ) && $value['id'] === $id ) {
324
return $key;
325
}
326
}
330
331
/**
332
* Generate the template type setting fields
333
+ *
334
+ * @param array $settings Settings fields.
335
+ * @param string $section Section name.
336
*/
337
public function generate_template_type_fields( $settings, $section = '' ) {
338
$position = $this->get_setting_position( 'wcdn_email_print_link', $settings );
339
+ if ( false !== $position ) {
340
$new_settings = array();
341
342
+ // Go through all registrations but remove the default 'order' type.
343
+ $template_registrations = WCDN_Print::$template_registrations;
344
array_splice( $template_registrations, 0, 1 );
345
$end = count( $template_registrations ) - 1;
346
+ foreach ( $template_registrations as $index => $template_registration ) {
347
+ $title = '';
348
+ $desc_tip = '';
349
$checkboxgroup = '';
350
351
+ // Define the group settings.
352
+ if ( 0 === $index ) {
353
+ $title = __( 'Admin', 'woocommerce-delivery-notes' );
354
$checkboxgroup = 'start';
355
+ } elseif ( $index === $end ) {
356
+ $desc_tip = __( 'The print buttons are available on the order listing and on the order detail screen.', 'woocommerce-delivery-notes' );
357
$checkboxgroup = 'end';
358
}
359
360
+ // Create the setting.
361
$new_settings[] = array(
362
+ 'title' => $title,
363
+ 'desc' => $template_registration['labels']['setting'],
364
+ 'id' => 'wcdn_template_type_' . $template_registration['type'],
365
+ 'default' => 'no',
366
+ 'type' => 'checkbox',
367
+ 'checkboxgroup' => $checkboxgroup,
368
+ 'desc_tip' => $desc_tip,
369
);
370
}
371
372
+ // Add the settings.
373
$settings = $this->array_merge_at( $settings, $new_settings, $position );
374
}
375
377
}
378
379
/**
380
+ * Generate the description for the template settings.
381
*/
382
public function get_template_description() {
383
$description = '';
384
+ $args = array(
385
+ 'post_type' => 'shop_order',
386
+ 'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed' ),
387
+ 'posts_per_page' => 1,
388
);
389
+ $query = new WP_Query( $args );
390
391
+ // show template preview links when an order is available.
392
+ if ( $query->have_posts() ) {
393
+ $results = $query->get_posts();
394
+ $test_id = $results[0]->ID;
395
+ $invoice_url = wcdn_get_print_link( $test_id, 'invoice' );
396
$delivery_note_url = wcdn_get_print_link( $test_id, 'delivery-note' );
397
+ $receipt_url = wcdn_get_print_link( $test_id, 'receipt' );
398
+ /* translators: %s: invoice url, delivery note url, receipt url */
399
$description = sprintf( __( 'This section lets you customise the content. You can preview the <a href="%1$s" target="%4$s" class="%5$s">invoice</a>, <a href="%2$s" target="%4$s" class="%5$s">delivery note</a> or <a href="%3$s" target="%4$s" class="%5$s">receipt</a> template.', 'woocommerce-delivery-notes' ), $invoice_url, $delivery_note_url, $receipt_url, '_blank', '' );
400
}
401
403
}
404
405
/**
406
+ * Generate the options for the template styles field.
407
*/
408
public function get_options_styles() {
409
$options = array();
410
411
+ foreach ( WCDN_Print::$template_styles as $template_style ) {
412
+ if ( is_array( $template_style ) && isset( $template_style['type'] ) && isset( $template_style['name'] ) ) {
413
+ $options[ $template_style['type'] ] = $template_style['name'];
414
}
415
}
416
418
}
419
420
/**
421
+ * Load image with ajax.
422
*/
423
public function load_image_ajax() {
424
+ // Verify the nonce.
425
+ if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'woocommerce-settings' ) ) {
426
die();
427
}
428
429
+ // Verify the id.
430
+ if ( empty( $_POST['attachment_id'] ) && is_numeric( $_POST['attachment_id'] ) ) {
431
die();
432
}
433
434
+ $wdn_image_attachmnet_id = sanitize_text_field( wp_unslash( $_POST['attachment_id'] ) );
435
+ // create the image.
436
$this->create_image( $wdn_image_attachmnet_id );
437
438
exit;
439
}
440
441
/**
442
+ * Create image.
443
+ *
444
+ * @param int $attachment_id Attachment ID.
445
*/
446
public function create_image( $attachment_id ) {
447
$attachment_src = wp_get_attachment_image_src( $attachment_id, 'medium', false );
448
+ $orientation = 'landscape';
449
+ if ( ( $attachment_src[1] / $attachment_src[2] ) < 1 ) {
450
$orientation = 'portrait';
451
}
452
453
?>
454
+ <img src="<?php echo esc_url( $attachment_src[0] ); ?>" class="<?php echo esc_attr( $orientation ); ?>" alt="" />
455
<?php
456
}
457
458
/**
459
+ * Output image select field.
460
+ *
461
+ * @param array $value Title.
462
*/
463
public function output_image_select( $value ) {
464
+ // Define the defaults.
465
if ( ! isset( $value['title_select'] ) ) {
466
$value['title_select'] = __( 'Select', 'woocommerce-delivery-notes' );
467
}
470
$value['title_remove'] = __( 'Remove', 'woocommerce-delivery-notes' );
471
}
472
473
+ // Get additional data fields.
474
+ $field = WC_Admin_Settings::get_field_description( $value );
475
+ $description = $field['description'];
476
$tooltip_html = $field['tooltip_html'];
477
$option_value = WC_Admin_Settings::get_option( $value['id'], $value['default'] );
478
+ $class_name = 'wcdn-image-select';
479
480
+ ?>
481
+ <tr valign="top">
482
483
484
<th scope="row" class="titledesc">
485
+ <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo wp_kses_post( $tooltip_html ); ?></label>
486
</th>
487
<td class="forminp image_width_settings">
488
+ <input name="<?php echo esc_attr( $value['id'] ); ?>" id="<?php echo esc_attr( $value['id'] ); ?>" type="hidden" value="<?php echo esc_attr( $option_value ); ?>" class="<?php echo esc_attr( $class_name ); ?>-image-id <?php echo esc_attr( $value['class'] ); ?>" />
489
490
+ <div id="<?php echo esc_attr( $value['id'] ); ?>_field" class="<?php echo esc_attr( $class_name ); ?>-field <?php echo esc_attr( $value['class'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>">
491
+ <span id="<?php echo esc_attr( $value['id'] ); ?>_spinner" class="<?php echo esc_attr( $class_name ); ?>-spinner spinner"></span>
492
+ <div id="<?php echo esc_attr( $value['id'] ); ?>_attachment" class="<?php echo esc_attr( $class_name ); ?>-attachment <?php echo esc_attr( $value['class'] ); ?> ">
493
<div class="thumbnail">
494
<div class="centered">
495
+ <?php if ( ! empty( $option_value ) ) : ?>
496
<?php $this->create_image( $option_value ); ?>
497
<?php endif; ?>
498
</div>
499
</div>
500
</div>
501
502
+ <div id="<?php echo esc_attr( $value['id'] ); ?>_buttons" class="<?php echo esc_attr( $class_name ); ?>-buttons <?php echo esc_attr( $value['class'] ); ?>">
503
+ <a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_remove_button" class="<?php echo esc_attr( $class_name ); ?>-remove-button
504
+ <?php
505
+ if ( empty( $option_value ) ) :
506
+ ?>
507
+ hidden<?php endif; ?> button">
508
<?php echo esc_html( $value['title_remove'] ); ?>
509
</a>
510
+ <a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_add_button" class="<?php echo esc_attr( $class_name ); ?>-add-button
511
+ <?php
512
+ if ( ! empty( $option_value ) ) :
513
+ ?>
514
+ hidden<?php endif; ?> button" data-uploader-title="<?php echo esc_attr( $value['title'] ); ?>" data-uploader-button-title="<?php echo esc_attr( $value['title_select'] ); ?>">
515
<?php echo esc_html( $value['title_select'] ); ?>
516
</a>
517
</div>
518
</div>
519
520
+ <?php echo wp_kses_post( $description ); ?>
521
</td>
522
</tr><?php
523
}
524
525
/**
526
+ * Merge array at given position.
527
+ *
528
+ * @param array $array Parent array.
529
+ * @param array $insert New array.
530
+ * @param int $position Position to merge at.
531
*/
532
public function array_merge_at( $array, $insert, $position ) {
533
$new_array = array();
534
+ // if pos is start, just merge them.
535
+ if ( 0 === $position ) {
536
$new_array = array_merge( $insert, $array );
537
} else {
538
+ // if pos is end just merge them.
539
+ if ( $position >= ( count( $array ) - 1 ) ) {
540
+ $new_array = array_merge( $array, $insert );
541
} else {
542
+ // split into head and tail, then merge head+inserted bit+tail.
543
+ $head = array_slice( $array, 0, $position );
544
+ $tail = array_slice( $array, $position );
545
$new_array = array_merge( $head, $insert, $tail );
546
}
547
}
550
}
551
552
}
553
+ ?>
includes/class-wcdn-theme.php CHANGED
@@ -1,24 +1,32 @@
1
<?php
2
3
/**
4
* Exit if accessed directly
5
*/
6
- if ( !defined( 'ABSPATH' ) ) {
7
exit;
8
}
9
10
/**
11
- * Frontend Theme class
12
*/
13
- if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
14
15
- class WooCommerce_Delivery_Notes_Theme {
16
17
/**
18
* Constructor
19
*/
20
public function __construct() {
21
- // Load the hooks
22
add_action( 'wp_loaded', array( $this, 'load_hooks' ) );
23
}
24
@@ -27,7 +35,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
27
* the theme and plugins are ready.
28
*/
29
public function load_hooks() {
30
- // hooks
31
add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'create_print_button_account_page' ), 10, 2 );
32
add_action( 'woocommerce_view_order', array( $this, 'create_print_button_order_page' ) );
33
add_action( 'woocommerce_thankyou', array( $this, 'create_print_button_order_page' ) );
@@ -40,50 +48,53 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
40
*/
41
public function add_scripts() {
42
if ( is_account_page() || is_order_received_page() || $this->is_woocommerce_tracking_page() ) {
43
- wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) );
44
- wp_enqueue_script( 'woocommerce-delivery-notes-theme', WooCommerce_Delivery_Notes::$plugin_url . 'js/theme.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ) );
45
}
46
}
47
48
/**
49
- * Create a print button for the 'My Account' page
50
*/
51
public function create_print_button_account_page( $actions, $order ) {
52
- if( get_option( 'wcdn_print_button_on_my_account_page' ) == 'yes' ) {
53
- // Add the print button
54
- $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_id() : $order->id;
55
$actions['print'] = array(
56
'url' => wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ) ),
57
- 'name' => __( 'Print', 'woocommerce-delivery-notes' )
58
);
59
}
60
return $actions;
61
}
62
63
/**
64
- * Create a print button for the 'View Order' page
65
*/
66
public function create_print_button_order_page( $order_id ) {
67
- $order = new WC_Order( $order_id );
68
- $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_billing_email() : $order->billing_email;
69
- // Output the button only when the option is enabled
70
- if( get_option( 'wcdn_print_button_on_view_order_page' ) == 'yes' ) {
71
- // Default button for all pages and logged in users
72
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ) );
73
74
- // Pass the email to the url for the tracking
75
- // and thank you page. This allows to view the
76
- // print page without logging in.
77
- if( $this->is_woocommerce_tracking_page() ) {
78
- //changed
79
- $wdn_order_email = sanitize_email( $_REQUEST['order_email'] ) ;
80
- $print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_email );
81
}
82
83
- // Thank you page
84
- if( is_order_received_page() && !is_user_logged_in() ) {
85
- // Don't output the butten when there is no email
86
- if( !$wdn_order_billing_id ) {
87
return;
88
}
89
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_billing_id );
@@ -91,7 +102,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
91
92
?>
93
<p class="order-print">
94
- <a href="<?php echo $print_url; ?>" class="button print"><?php _e( 'Print', 'woocommerce-delivery-notes' ); ?></a>
95
</p>
96
<?php
97
}
@@ -99,24 +110,28 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Theme' ) ) {
99
100
/**
101
* Add a print url to the emails that are sent to the customer
102
*/
103
public function add_email_print_url( $order, $sent_to_admin = true, $plain_text = false ) {
104
- if( get_option( 'wcdn_email_print_link' ) == 'yes' ) {
105
- $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_billing_email() : $order->billing_email;
106
- if( $wdn_order_billing_id && !$sent_to_admin ) {
107
- $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_id() : $order->id;
108
109
$url = wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ), $wdn_order_billing_id, true );
110
111
- if( $plain_text ) :
112
- echo __( 'Print your order', 'woocommerce-delivery-notes' ) . "\n\n";
113
114
- echo $url . "\n";
115
116
- echo "\n****************************************************\n\n";
117
- else : ?>
118
- <?php // changed ?>
119
- <p><strong><?php _e( 'Print:', 'woocommerce-delivery-notes' ); ?></strong> <a href="<?php echo esc_url_raw( $url ); ?>"><?php _e( 'Open print view in browser', 'woocommerce-delivery-notes' ); ?></a></p>
120
<?php endif;
121
}
122
}
@@ -124,16 +139,21 @@ echo "\n****************************************************\n\n";
124
125
/**
126
* Get the print button template type depnding on order status
127
*/
128
public function get_template_type( $order ) {
129
130
- $wdn_order_status = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_status() : $order->status;
131
132
- if( $wdn_order_status == 'completed' ) {
133
$type = apply_filters( 'wcdn_theme_print_button_template_type_complete_status', 'invoice' );
134
} else {
135
$type = apply_filters( 'wcdn_theme_print_button_template_type', 'order' );
136
}
137
return $type;
138
}
139
@@ -141,11 +161,11 @@ echo "\n****************************************************\n\n";
141
* Is WooCommerce 'Order Tracking' page
142
*/
143
public function is_woocommerce_tracking_page() {
144
- return ( is_page( wc_get_page_id( 'order_tracking' ) ) && isset( $_REQUEST['order_email'] ) ) ? true : false;
145
}
146
147
}
148
149
}
150
151
- ?>
1
<?php
2
+ /**
3
+ * Theme class
4
+ *
5
+ * @package woocommerce-print-invoice-delivery-notes
6
+ */
7
8
/**
9
* Exit if accessed directly
10
*/
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
exit;
13
}
14
15
/**
16
+ * Frontend Theme class.
17
*/
18
+ if ( ! class_exists( 'WCDN_Theme' ) ) {
19
20
+ /**
21
+ * Theme class
22
+ */
23
+ class WCDN_Theme {
24
25
/**
26
* Constructor
27
*/
28
public function __construct() {
29
+ // Load the hooks.
30
add_action( 'wp_loaded', array( $this, 'load_hooks' ) );
31
}
32
35
* the theme and plugins are ready.
36
*/
37
public function load_hooks() {
38
+ // hooks.
39
add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'create_print_button_account_page' ), 10, 2 );
40
add_action( 'woocommerce_view_order', array( $this, 'create_print_button_order_page' ) );
41
add_action( 'woocommerce_thankyou', array( $this, 'create_print_button_order_page' ) );
48
*/
49
public function add_scripts() {
50
if ( is_account_page() || is_order_received_page() || $this->is_woocommerce_tracking_page() ) {
51
+ wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ), WooCommerce_Delivery_Notes::$plugin_version, false );
52
+ wp_enqueue_script( 'woocommerce-delivery-notes-theme', WooCommerce_Delivery_Notes::$plugin_url . 'js/theme.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ), WooCommerce_Delivery_Notes::$plugin_version, false );
53
}
54
}
55
56
/**
57
+ * Create a print button for the 'My Account' page.
58
+ *
59
+ * @param array $actions My Account page actions.
60
+ * @param object $order Order object.
61
*/
62
public function create_print_button_account_page( $actions, $order ) {
63
+ if ( 'yes' === get_option( 'wcdn_print_button_on_my_account_page' ) ) {
64
+ // Add the print button.
65
+ $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_id() : $order->id;
66
$actions['print'] = array(
67
'url' => wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ) ),
68
+ 'name' => apply_filters( 'wcdn_print_button_name_on_my_account_page', __( 'Print', 'woocommerce-delivery-notes' ), $order ),
69
);
70
}
71
return $actions;
72
}
73
74
/**
75
+ * Create a print button for the 'View Order' page.
76
+ *
77
+ * @param int $order_id Order ID.
78
*/
79
public function create_print_button_order_page( $order_id ) {
80
+ $order = new WC_Order( $order_id );
81
+ $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_billing_email() : $order->billing_email;
82
+ // Output the button only when the option is enabled.
83
+ if ( 'yes' === get_option( 'wcdn_print_button_on_view_order_page' ) ) {
84
+ // Default button for all pages and logged in users.
85
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ) );
86
87
+ // Pass the email to the url for the tracking and thank you page. This allows to view the print page without logging in.
88
+ if ( $this->is_woocommerce_tracking_page() ) {
89
+ // changed.
90
+ $wdn_order_email = isset( $_REQUEST['order_email'] ) ? sanitize_email( wp_unslash( $_REQUEST['order_email'] ) ) : '';
91
+ $print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_email );
92
}
93
94
+ // Thank you page.
95
+ if ( is_order_received_page() && ! is_user_logged_in() ) {
96
+ // Don't output the butten when there is no email.
97
+ if ( ! $wdn_order_billing_id ) {
98
return;
99
}
100
$print_url = wcdn_get_print_link( $order_id, $this->get_template_type( $order ), $wdn_order_billing_id );
102
103
?>
104
<p class="order-print">
105
+ <a href="<?php echo esc_url( $print_url ); ?>" class="button print"><?php echo esc_attr( apply_filters( 'wcdn_print_button_name_order_page', __( 'Print', 'woocommerce-delivery-notes' ), $order ) ); ?></a>
106
</p>
107
<?php
108
}
110
111
/**
112
* Add a print url to the emails that are sent to the customer
113
+ *
114
+ * @param object $order Order Object.
115
+ * @param boolean $sent_to_admin Sent to admin true or false.
116
+ * @param boolean $plain_text Whether only to send plain text email or not.
117
*/
118
public function add_email_print_url( $order, $sent_to_admin = true, $plain_text = false ) {
119
+ if ( 'yes' === get_option( 'wcdn_email_print_link' ) ) {
120
+ $wdn_order_billing_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_billing_email() : $order->billing_email;
121
+ if ( $wdn_order_billing_id && ! $sent_to_admin ) {
122
+ $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_id() : $order->id;
123
124
$url = wcdn_get_print_link( $wdn_order_id, $this->get_template_type( $order ), $wdn_order_billing_id, true );
125
126
+ if ( $plain_text ) :
127
+ echo esc_html_e( 'Print your order', 'woocommerce-delivery-notes' ) . "\n\n";
128
129
+ echo esc_url( $url ) . "\n";
130
131
+ echo "\n****************************************************\n\n";
132
+ else :
133
+ ?>
134
+ <p><strong><?php esc_attr_e( 'Print:', 'woocommerce-delivery-notes' ); ?></strong> <a href="<?php echo esc_url_raw( $url ); ?>"><?php esc_attr_e( 'Open print view in browser', 'woocommerce-delivery-notes' ); ?></a></p>
135
<?php endif;
136
}
137
}
139
140
/**
141
* Get the print button template type depnding on order status
142
+ *
143
+ * @param object $order Order object.
144
*/
145
public function get_template_type( $order ) {
146
147
+ $wdn_order_status = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_status() : $order->status;
148
149
+ if ( 'completed' === $wdn_order_status ) {
150
$type = apply_filters( 'wcdn_theme_print_button_template_type_complete_status', 'invoice' );
151
} else {
152
$type = apply_filters( 'wcdn_theme_print_button_template_type', 'order' );
153
}
154
+
155
+ $type = apply_filters( 'wcdn_theme_print_button_template_type_arbitrary', $type, $order );
156
+
157
return $type;
158
}
159
161
* Is WooCommerce 'Order Tracking' page
162
*/
163
public function is_woocommerce_tracking_page() {
164
+ return ( is_page( wc_get_page_id( 'order_tracking' ) ) && isset( $_REQUEST['order_email'] ) ) ? true : false;
165
}
166
167
}
168
169
}
170
171
+ ?>
includes/class-wcdn-writepanel.php CHANGED
@@ -1,24 +1,32 @@
1
<?php
2
3
/**
4
* Exit if accessed directly
5
*/
6
- if ( !defined( 'ABSPATH' ) ) {
7
exit;
8
}
9
10
/**
11
* Writepanel class
12
*/
13
- if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
14
15
- class WooCommerce_Delivery_Notes_Writepanel {
16
17
/**
18
* Constructor
19
*/
20
public function __construct() {
21
- // Load the hooks
22
add_action( 'admin_init', array( $this, 'load_admin_hooks' ) );
23
}
24
@@ -26,7 +34,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
26
* Load the admin hooks
27
*/
28
public function load_admin_hooks() {
29
- // Hooks
30
add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ) );
31
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
32
add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );
@@ -34,7 +42,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
34
add_action( 'add_meta_boxes_shop_order', array( $this, 'add_box' ) );
35
36
add_action( 'admin_footer-edit.php', array( $this, 'add_bulk_actions' ) );
37
- add_action( 'load-edit.php', array( $this, 'load_bulk_actions' ) );
38
add_action( 'admin_notices', array( $this, 'confirm_bulk_actions' ) );
39
}
40
@@ -42,9 +50,9 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
42
* Add the styles
43
*/
44
public function add_styles() {
45
- if( $this->is_order_edit_page() || $this->is_order_post_page() ) {
46
- wp_enqueue_style('thickbox');
47
- wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css' );
48
}
49
}
50
@@ -52,10 +60,10 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
52
* Add the scripts
53
*/
54
public function add_scripts() {
55
- if( $this->is_order_edit_page() || $this->is_order_post_page() ) {
56
wp_enqueue_script( 'thickbox' );
57
- wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) );
58
- wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ) );
59
}
60
}
61
@@ -64,7 +72,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
64
*/
65
public function is_order_edit_page() {
66
global $typenow, $pagenow;
67
- if( $typenow == 'shop_order' && $pagenow == 'edit.php' ) {
68
return true;
69
} else {
70
return false;
@@ -76,7 +84,7 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
76
*/
77
public function is_order_post_page() {
78
global $typenow, $pagenow;
79
- if( $typenow == 'shop_order' && ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) ) {
80
return true;
81
} else {
82
return false;
@@ -85,18 +93,20 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
85
86
/**
87
* Add print actions to the orders listing
88
*/
89
public function add_listing_actions( $order ) {
90
91
- $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) ? $order->get_id() : $order->id;
92
?>
93
- <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
94
- <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>
95
-
96
- <a href="<?php echo wcdn_get_print_link( $wdn_order_id, $template_registration['type'] ); ?>" class="button tips print-preview-button <?php echo $template_registration['type']; ?>" target="_blank" alt="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>" data-tip="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>">
97
- <?php _e( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ); ?>
98
</a>
99
-
100
<?php endif; ?>
101
<?php endforeach; ?>
102
@@ -113,109 +123,122 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
113
* https://core.trac.wordpress.org/ticket/16031
114
*/
115
public function add_bulk_actions() {
116
- if( $this->is_order_edit_page() ) : ?>
117
<script type="text/javascript">
118
jQuery(document).ready(function($) {
119
- <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
120
- <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>
121
-
122
- $('<option>').val('wcdn_print_<?php echo $template_registration['type']; ?>').attr('title', '<?php echo $template_registration['type']; ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action"]');
123
- $('<option>').val('wcdn_print_<?php echo $template_registration['type']; ?>').attr('title', '<?php echo $template_registration['type']; ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action2"]');
124
-
125
<?php endif; ?>
126
<?php endforeach; ?>
127
});
128
</script>
129
- <?php endif;
130
}
131
132
/**
133
* Add bulk print actions to the orders listing
134
*/
135
public function load_bulk_actions() {
136
- if( $this->is_order_edit_page() ) {
137
- // get the action staht should be started
138
- $wp_list_table = _get_list_table('WP_Posts_List_Table');
139
- $action = $wp_list_table->current_action();
140
141
- // stop if there are no post ids
142
- if( !isset( $_REQUEST['post'] ) ) {
143
return;
144
}
145
146
- // only for specified actions
147
- foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) {
148
- if( $action == 'wcdn_print_' . $template_registration['type'] ) {
149
$template_type = $template_registration['type'];
150
$report_action = 'printed_' . $template_registration['type'];
151
break;
152
}
153
}
154
- if( !isset( $report_action ) ) {
155
return;
156
}
157
158
- // security check
159
- check_admin_referer('bulk-posts');
160
161
- // get referrer
162
- if( !wp_get_referer() ) {
163
return;
164
}
165
166
- // filter the referer args
167
$referer_args = array();
168
- parse_str( parse_url( wp_get_referer(), PHP_URL_QUERY ), $referer_args );
169
170
- // set the basic args for the sendback
171
$args = array(
172
- 'post_type' => $referer_args['post_type']
173
);
174
- if( isset( $referer_args['post_status'] ) ) {
175
$args = wp_parse_args( array( 'post_status' => $referer_args['post_status'] ), $args );
176
}
177
- if( isset( $referer_args['paged'] ) ) {
178
$args = wp_parse_args( array( 'paged' => $referer_args['paged'] ), $args );
179
}
180
- if( isset( $referer_args['orderby'] ) ) {
181
$args = wp_parse_args( array( 'orderby' => $referer_args['orderby'] ), $args );
182
}
183
- if( isset( $referer_args['order'] ) ) {
184
$args = wp_parse_args( array( 'orderby' => $referer_args['order'] ), $args );
185
}
186
187
- // do the action
188
$post_ids = array_map( 'absint', (array) $_REQUEST['post'] );
189
- $total = count( $post_ids );
190
- $url = wcdn_get_print_link( $post_ids , $template_type );
191
-
192
- // generate more args and the sendback string
193
- $args = wp_parse_args( array( $report_action => true, 'total' => $total, 'print_url' => urlencode( $url ) ), $args );
194
$sendback = add_query_arg( $args, '' );
195
- wp_redirect( $sendback );
196
exit;
197
}
198
- }
199
200
/**
201
* Show confirmation message that orders are printed
202
*/
203
public function confirm_bulk_actions() {
204
- if( $this->is_order_edit_page() ) {
205
- foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) {
206
- if( isset( $_REQUEST['printed_' . $template_registration['type']] ) ) {
207
- // use singular or plural form
208
$total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : 0;
209
- if( $total <= 1 ) {
210
$message = $template_registration['labels']['message'];
211
} else {
212
$message = $template_registration['labels']['message_plural'];
213
}
214
- // changed
215
?>
216
217
<div id="woocommerce-delivery-notes-bulk-print-message" class="updated">
218
- <p><?php _e( $message, 'woocommerce-delivery-notes' ); ?> <a href="<?php echo urldecode( esc_url_raw ( $_REQUEST['print_url'] ) ); ?>" target="_blank" class="print-preview-button" id="woocommerce-delivery-notes-bulk-print-button"><?php _e( 'Print now', 'woocommerce-delivery-notes' ) ?></a> <span class="print-preview-loading spinner"></span></p>
219
</div>
220
221
<?php
@@ -239,25 +262,26 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
239
global $post_id, $wcdn;
240
?>
241
<div class="print-actions">
242
- <?php foreach( WooCommerce_Delivery_Notes_Print::$template_registrations as $template_registration ) : ?>
243
- <?php if( get_option( 'wcdn_template_type_' . $template_registration['type'] ) == 'yes' && $template_registration['type'] !== 'order' ) : ?>
244
-
245
- <a href="<?php echo wcdn_get_print_link( $post_id, $template_registration['type'] ); ?>" class="button print-preview-button <?php echo $template_registration['type']; ?>" target="_blank" alt="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>"><?php _e( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ); ?></a>
246
-
247
<?php endif; ?>
248
<?php endforeach; ?>
249
<span class="print-preview-loading spinner"></span>
250
</div>
251
<?php
252
$create_invoice_number = get_option( 'wcdn_create_invoice_number' );
253
- $has_invoice_number = get_post_meta( $post_id, '_wcdn_invoice_number', true );
254
- if( !empty( $create_invoice_number ) && $create_invoice_number == 'yes' && $has_invoice_number ) :
255
$invoice_number = wcdn_get_order_invoice_number( $post_id );
256
- $invoice_date = wcdn_get_order_invoice_date( $post_id ); ?>
257
258
<ul class="print-info">
259
- <li><strong><?php _e( 'Invoice number: ', 'woocommerce-delivery-notes' ); ?></strong> <?php echo $invoice_number; ?></li>
260
- <li><strong><?php _e( 'Invoice date: ', 'woocommerce-delivery-notes' ); ?></strong> <?php echo $invoice_date; ?></li>
261
</ul>
262
263
<?php endif; ?>
@@ -268,4 +292,4 @@ if ( !class_exists( 'WooCommerce_Delivery_Notes_Writepanel' ) ) {
268
269
}
270
271
- ?>
1
<?php
2
+ /**
3
+ * Write Panel class
4
+ *
5
+ * @package woocommerce-print-invoice-delivery-notes
6
+ */
7
8
/**
9
* Exit if accessed directly
10
*/
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
exit;
13
}
14
15
/**
16
* Writepanel class
17
*/
18
+ if ( ! class_exists( 'WCDN_Writepanel' ) ) {
19
20
+ /**
21
+ * Write Panel class
22
+ */
23
+ class WCDN_Writepanel {
24
25
/**
26
* Constructor
27
*/
28
public function __construct() {
29
+ // Load the hooks.
30
add_action( 'admin_init', array( $this, 'load_admin_hooks' ) );
31
}
32
34
* Load the admin hooks
35
*/
36
public function load_admin_hooks() {
37
+ // Hooks.
38
add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ) );
39
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
40
add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );
42
add_action( 'add_meta_boxes_shop_order', array( $this, 'add_box' ) );
43
44
add_action( 'admin_footer-edit.php', array( $this, 'add_bulk_actions' ) );
45
+ add_action( 'load-edit.php', array( $this, 'load_bulk_actions' ) );
46
add_action( 'admin_notices', array( $this, 'confirm_bulk_actions' ) );
47
}
48
50
* Add the styles
51
*/
52
public function add_styles() {
53
+ if ( $this->is_order_edit_page() || $this->is_order_post_page() ) {
54
+ wp_enqueue_style( 'thickbox' );
55
+ wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css', '', WooCommerce_Delivery_Notes::$plugin_version );
56
}
57
}
58
60
* Add the scripts
61
*/
62
public function add_scripts() {
63
+ if ( $this->is_order_edit_page() || $this->is_order_post_page() ) {
64
wp_enqueue_script( 'thickbox' );
65
+ wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ), WooCommerce_Delivery_Notes::$plugin_version, false );
66
+ wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'woocommerce-delivery-notes-print-link' ), WooCommerce_Delivery_Notes::$plugin_version, false );
67
}
68
}
69
72
*/
73
public function is_order_edit_page() {
74
global $typenow, $pagenow;
75
+ if ( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
76
return true;
77
} else {
78
return false;
84
*/
85
public function is_order_post_page() {
86
global $typenow, $pagenow;
87
+ if ( 'shop_order' === $typenow && ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) ) {
88
return true;
89
} else {
90
return false;
93
94
/**
95
* Add print actions to the orders listing
96
+ *
97
+ * @param object $order Order Object.
98
*/
99
public function add_listing_actions( $order ) {
100
101
+ $wdn_order_id = ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) ? $order->get_id() : $order->id;
102
?>
103
+ <?php foreach ( WCDN_Print::$template_registrations as $template_registration ) : ?>
104
+ <?php if ( 'yes' === get_option( 'wcdn_template_type_' . $template_registration['type'] ) && 'order' !== $template_registration['type'] ) : ?>
105
+ <?php // phpcs:disable ?>
106
+ <a href="<?php echo esc_url( wcdn_get_print_link( $wdn_order_id, $template_registration['type'] ) ); ?>" class="button tips print-preview-button <?php echo esc_attr( $template_registration['type'] ); ?>" target="_blank" alt="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>" data-tip="<?php esc_attr_e( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>">
107
+ <?php esc_html_e( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ); ?>
108
</a>
109
+ <?php // phpcs:enable ?>
110
<?php endif; ?>
111
<?php endforeach; ?>
112
123
* https://core.trac.wordpress.org/ticket/16031
124
*/
125
public function add_bulk_actions() {
126
+ if ( $this->is_order_edit_page() ) :
127
+ ?>
128
<script type="text/javascript">
129
jQuery(document).ready(function($) {
130
+ <?php foreach ( WCDN_Print::$template_registrations as $template_registration ) : ?>
131
+ <?php if ( 'yes' === get_option( 'wcdn_template_type_' . $template_registration['type'] ) && 'order' !== $template_registration['type'] ) : ?>
132
+ <?php //phpcs:disable ?>
133
+ $('<option>').val('wcdn_print_<?php echo esc_attr( $template_registration['type'] ); ?>').attr('title', '<?php echo esc_attr( $template_registration['type'] ); ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action"]');
134
+ $('<option>').val('wcdn_print_<?php echo esc_attr( $template_registration['type'] ); ?>').attr('title', '<?php echo esc_attr( $template_registration['type'] ); ?>').text('<?php echo esc_js( __( $template_registration['labels']['print'], 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action2"]');
135
+ <?php //phpcs:enable ?>
136
<?php endif; ?>
137
<?php endforeach; ?>
138
});
139
</script>
140
+ <?php
141
+ endif;
142
}
143
144
/**
145
* Add bulk print actions to the orders listing
146
*/
147
public function load_bulk_actions() {
148
+ if ( $this->is_order_edit_page() ) {
149
+ // get the action that should be started.
150
+ $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
151
+ $action = $wp_list_table->current_action();
152
153
+ // stop if there are no post ids.
154
+ if ( ! isset( $_REQUEST['post'] ) ) {
155
return;
156
}
157
158
+ // only for specified actions.
159
+ foreach ( WCDN_Print::$template_registrations as $template_registration ) {
160
+ if ( 'wcdn_print_' . $template_registration['type'] === $action ) {
161
$template_type = $template_registration['type'];
162
$report_action = 'printed_' . $template_registration['type'];
163
break;
164
}
165
}
166
+ if ( ! isset( $report_action ) ) {
167
return;
168
}
169
170
+ // security check.
171
+ check_admin_referer( 'bulk-posts' );
172
173
+ // get referrer.
174
+ if ( ! wp_get_referer() ) {
175
return;
176
}
177
178
+ // filter the referer args.
179
$referer_args = array();
180
+ parse_str( wp_parse_url( wp_get_referer(), PHP_URL_QUERY ), $referer_args );
181
182
+ // set the basic args for the sendback.
183
$args = array(
184
+ 'post_type' => $referer_args['post_type'],
185
);
186
+ if ( isset( $referer_args['post_status'] ) ) {
187
$args = wp_parse_args( array( 'post_status' => $referer_args['post_status'] ), $args );
188
}
189
+ if ( isset( $referer_args['paged'] ) ) {
190
$args = wp_parse_args( array( 'paged' => $referer_args['paged'] ), $args );
191
}
192
+ if ( isset( $referer_args['orderby'] ) ) {
193
$args = wp_parse_args( array( 'orderby' => $referer_args['orderby'] ), $args );
194
}
195
+ if ( isset( $referer_args['order'] ) ) {
196
$args = wp_parse_args( array( 'orderby' => $referer_args['order'] ), $args );
197
}
198
199
+ // do the action.
200
$post_ids = array_map( 'absint', (array) $_REQUEST['post'] );
201
+ $total = count( $post_ids );
202
+ $url = wcdn_get_print_link( $post_ids, $template_type );
203
+
204
+ // generate more args and the sendback string.
205
+ $args = wp_parse_args(
206
+ array(
207
+ $report_action => true,
208
+ 'total' => $total,
209
+ 'print_url' => rawurlencode( $url ),
210
+ ),
211
+ $args
212
+ );
213
$sendback = add_query_arg( $args, '' );
214
+ wp_safe_redirect( $sendback );
215
exit;
216
}
217
+ }
218
219
/**
220
* Show confirmation message that orders are printed
221
*/
222
public function confirm_bulk_actions() {
223
+ if ( $this->is_order_edit_page() ) {
224
+ foreach ( WCDN_Print::$template_registrations as $template_registration ) {
225
+ if ( isset( $_REQUEST[ 'printed_' . $template_registration['type'] ] ) ) {
226
+ // use singular or plural form.
227
$total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : 0;
228
+ if ( $total <= 1 ) {
229
$message = $template_registration['labels']['message'];
230
} else {
231
$message = $template_registration['labels']['message_plural'];
232
}
233
?>
234
235
<div id="woocommerce-delivery-notes-bulk-print-message" class="updated">
236
+ <p><?php wp_kses_post( $message, 'woocommerce-delivery-notes' ); ?>
237
+ <a href="<?php if ( isset( $_REQUEST['print_url'] ) ) : ?>
238
+ <?php
239
+ // phpcs:ignore
240
+ echo urldecode( esc_url_raw( $_REQUEST['print_url'] ) ); ?>
241
+ <?php endif; ?>" target="_blank" class="print-preview-button" id="woocommerce-delivery-notes-bulk-print-button"><?php esc_attr_e( 'Print now', 'woocommerce-delivery-notes' ); ?></a> <span class="print-preview-loading spinner"></span></p>
242
</div>
243
244
<?php
262
global $post_id, $wcdn;
263
?>
264
<div class="print-actions">
265
+ <?php foreach ( WCDN_Print::$template_registrations as $template_registration ) : ?>
266
+ <?php if ( 'yes' === get_option( 'wcdn_template_type_' . $template_registration['type'] ) && 'order' !== $template_registration['type'] ) : ?>
267
+ <?php // phpcs:disable ?>
268
+ <a href="<?php echo esc_url( wcdn_get_print_link( $post_id, $template_registration