WooCommerce_Delivery_Notes_Writepanel

The WooCommerce Print Invoice & Delivery Note WooCommerce Delivery Notes Writepanel class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wcdn-writepanel.php  
  1. class WooCommerce_Delivery_Notes_Writepanel { 
  2.  
  3. /** 
  4. * Constructor 
  5. */ 
  6. public function __construct() { 
  7. // Load the hooks 
  8. add_action( 'admin_init', array( $this, 'load_admin_hooks' ) ); 
  9.  
  10. /** 
  11. * Load the admin hooks 
  12. */ 
  13. public function load_admin_hooks() {  
  14. add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ) ); 
  15. add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) ); 
  16. add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) ); 
  17.  
  18. add_action( 'add_meta_boxes_shop_order', array( $this, 'add_box' ) ); 
  19.  
  20. add_action( 'admin_footer-edit.php', array( $this, 'add_bulk_actions' ) ); 
  21. add_action( 'load-edit.php', array( $this, 'load_bulk_actions' ) ); 
  22. add_action( 'admin_notices', array( $this, 'confirm_bulk_actions' ) ); 
  23.  
  24. /** 
  25. * Add the styles 
  26. */ 
  27. public function add_styles() { 
  28. if( $this->is_order_edit_page() || $this->is_order_post_page() ) { 
  29. wp_enqueue_style('thickbox'); 
  30. wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css' ); 
  31.  
  32. /** 
  33. * Add the scripts 
  34. */ 
  35. public function add_scripts() { 
  36. if( $this->is_order_edit_page() || $this->is_order_post_page() ) { 
  37. wp_enqueue_script( 'thickbox' );  
  38. wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) ); 
  39. wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'woocommerce-delivery-notes-print-link', 'thickbox' ) ); 
  40. }  
  41.  
  42. /** 
  43. * Is order edit page 
  44. */ 
  45. public function is_order_edit_page() { 
  46. global $typenow, $pagenow; 
  47. if( $typenow == 'shop_order' && $pagenow == 'edit.php' ) { 
  48. return true;  
  49. } else { 
  50. return false; 
  51. }  
  52.  
  53. /** 
  54. * Is order edit page 
  55. */ 
  56. public function is_order_post_page() { 
  57. global $typenow, $pagenow; 
  58. if( $typenow == 'shop_order' && ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) ) { 
  59. return true;  
  60. } else { 
  61. return false; 
  62. }  
  63.  
  64. /** 
  65. * Add print actions to the orders listing 
  66. */ 
  67. public function add_listing_actions( $order ) { 
  68. ?>  
  69. <a href="<?php echo wcdn_get_print_link( $order->id, 'invoice' ); ?>" class="button tips print-preview-button invoice" target="_blank" alt="<?php esc_attr_e( 'Print Invoice', 'woocommerce-delivery-notes' ); ?>" data-tip="<?php esc_attr_e( 'Print Invoice', 'woocommerce-delivery-notes' ); ?>"> 
  70. <?php _e( 'Print Invoice', 'woocommerce-delivery-notes' ); ?> 
  71. </a> 
  72. <a href="<?php echo wcdn_get_print_link( $order->id, 'delivery-note' ); ?>" class="button tips print-preview-button delivery-note" target="_blank" alt="<?php esc_attr_e( 'Print Delivery Note', 'woocommerce-delivery-notes' ); ?>" data-tip="<?php esc_attr_e( 'Print Delivery Note', 'woocommerce-delivery-notes' ); ?>"> 
  73. <?php _e( 'Print Delivery Note', 'woocommerce-delivery-notes' ); ?> 
  74. </a> 
  75. <span class="print-preview-loading spinner"></span> 
  76. <?php 
  77.  
  78. /** 
  79. * Add bulk actions with javascript to the dropdown. 
  80. * This is not so pretty but WordPress does not yet 
  81. * offer any better solution. The JS code is inline  
  82. * because we can't determine the page without  
  83. * checking the post_type. 
  84. * https://core.trac.wordpress.org/ticket/16031 
  85. */ 
  86. public function add_bulk_actions() { 
  87. if( $this->is_order_edit_page() ) : ?> 
  88. <script type="text/javascript"> 
  89. jQuery(document).ready(function($) {  
  90. $('<option>').val('wcdn_print_invoice').attr('title', 'invoice').text('<?php echo esc_js( __( 'Print Invoice', 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action"]'); 
  91. $('<option>').val('wcdn_print_invoice').attr('title', 'invoice').text('<?php echo esc_js( __( 'Print Invoice', 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action2"]'); 
  92.  
  93. $('<option>').val('wcdn_print_delivery_note').attr('title', 'delivery-note').text('<?php echo esc_js( __( 'Print Delivery Note', 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action"]'); 
  94. $('<option>').val('wcdn_print_delivery_note').attr('title', 'delivery-note').text('<?php echo esc_js( __( 'Print Delivery Note', 'woocommerce-delivery-notes' ) ); ?>').appendTo('select[name="action2"]'); 
  95. }); 
  96. </script> 
  97. <?php endif; 
  98.  
  99. /** 
  100. * Add bulk print actions to the orders listing 
  101. */ 
  102. public function load_bulk_actions() { 
  103. if( $this->is_order_edit_page() ) { 
  104. // get the action staht should be started 
  105. $wp_list_table = _get_list_table('WP_Posts_List_Table'); 
  106. $action = $wp_list_table->current_action(); 
  107.  
  108. // stop if there are no post ids 
  109. if( !isset( $_REQUEST['post'] ) ) { 
  110. return; 
  111.  
  112. // only for specified actions 
  113. switch ( $action ) { 
  114. case 'wcdn_print_invoice': 
  115. $template_type = 'invoice'; 
  116. $report_action = 'printed_invoice'; 
  117. break; 
  118. case 'wcdn_print_delivery_note': 
  119. $template_type = 'delivery-note'; 
  120. $report_action = 'printed_delivery_note'; 
  121. break; 
  122. default: 
  123. return; 
  124.  
  125. // do the action 
  126. $post_ids = array_map( 'absint', (array) $_REQUEST['post'] ); 
  127. $total = count( $post_ids ); 
  128. $url = wcdn_get_print_link( $post_ids , $template_type ); 
  129. $sendback = add_query_arg( array( 'post_type' => 'shop_order', $report_action => true, 'total' => $total, 'print_url' => urlencode( $url ) ), '' ); 
  130.  
  131. wp_redirect( $sendback ); 
  132. exit; 
  133.  
  134. /** 
  135. * Show confirmation message that orders are printed 
  136. */ 
  137. public function confirm_bulk_actions() { 
  138. if( $this->is_order_edit_page() ) { 
  139. if ( isset( $_REQUEST['printed_delivery_note'] ) || isset( $_REQUEST['printed_invoice'] ) ) { 
  140. $total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : 0; 
  141.  
  142. // Confirmation message 
  143. if( isset( $_REQUEST['printed_invoice'] ) ) { 
  144. $message = sprintf( _n( 'Invoice created.', '%s invoices created.', $total, 'woocommerce-delivery-notes' ), number_format_i18n( $total ) ); 
  145. } elseif( isset( $_REQUEST['printed_delivery_note'] ) ) { 
  146. $message = sprintf( _n( 'Delivery note created.', '%s delivery notes created.', $total, 'woocommerce-delivery-notes' ), number_format_i18n( $total ) ); 
  147. ?> 
  148. <div id="woocommerce-delivery-notes-bulk-print-message" class="updated"> 
  149. <p><?php echo $message; ?> <a href="<?php echo urldecode( $_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> 
  150. </div> 
  151. <?php 
  152.  
  153. /** 
  154. * Add the meta box on the single order page 
  155. */ 
  156. public function add_box() { 
  157. add_meta_box( 'woocommerce-delivery-notes-box', __( 'Order Printing', 'woocommerce-delivery-notes' ), array( $this, 'create_box_content' ), 'shop_order', 'side', 'low' ); 
  158.  
  159. /** 
  160. * Create the meta box content on the single order page 
  161. */ 
  162. public function create_box_content() { 
  163. global $post_id; 
  164. $create_invoice_number = get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'create_invoice_number' ); 
  165. ?> 
  166. <div class="print-actions"> 
  167. <a href="<?php echo wcdn_get_print_link( $post_id, 'invoice' ); ?>" class="button print-preview-button" target="_blank" alt="<?php esc_attr_e( 'Print Invoice', 'woocommerce-delivery-notes' ); ?>"><?php _e( 'Print Invoice', 'woocommerce-delivery-notes' ); ?></a> 
  168. <a href="<?php echo wcdn_get_print_link( $post_id, 'delivery-note' ); ?>" class="button print-preview-button" target="_blank" alt="<?php esc_attr_e( 'Print Delivery Note', 'woocommerce-delivery-notes' ); ?>"><?php _e( 'Print Delivery Note', 'woocommerce-delivery-notes' ); ?></a> 
  169. <span class="print-preview-loading spinner"></span> 
  170. </div> 
  171. <?php  
  172. if( !empty( $create_invoice_number ) ) :  
  173. $invoice_number = wcdn_get_order_invoice_number( $post_id ); 
  174. ?> 
  175. <div class="print-info"> 
  176. <strong><?php _e( 'Invoice number: ', 'woocommerce-delivery-notes' ); ?></strong> <?php echo $invoice_number; ?> 
  177. </div> 
  178. <?php endif; ?> 
  179. <?php 
  180.