WooCommerce_PDF_Invoices_Writepanels

The WooCommerce PDF Invoices & Packing Slips WooCommerce PDF Invoices Writepanels class.

Defined (1)

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

/includes/class-wcpdf-writepanels.php  
  1. class WooCommerce_PDF_Invoices_Writepanels { 
  2. public $bulk_actions; 
  3.  
  4. /** 
  5. * Constructor 
  6. */ 
  7. public function __construct() { 
  8. $this->general_settings = get_option('wpo_wcpdf_general_settings'); 
  9. $this->template_settings = get_option('wpo_wcpdf_template_settings'); 
  10.  
  11. add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ) ); 
  12. add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_invoice_number_column' ), 999 ); 
  13. add_action( 'manage_shop_order_posts_custom_column', array( $this, 'invoice_number_column_data' ), 2 ); 
  14. add_action( 'add_meta_boxes_shop_order', array( $this, 'add_meta_boxes' ) ); 
  15. add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'my_account_pdf_link' ), 10, 2 ); 
  16. add_action( 'admin_print_scripts', array( $this, 'add_scripts' ) ); 
  17. add_action( 'admin_print_styles', array( $this, 'add_styles' ) ); 
  18. add_action( 'admin_footer', array( $this, 'bulk_actions' ) ); 
  19.  
  20. add_action( 'save_post', array( $this, 'save_invoice_number_date' ) ); 
  21.  
  22. add_filter( 'woocommerce_shop_order_search_fields', array( $this, 'search_fields' ) ); 
  23.  
  24. $this->bulk_actions = array( 
  25. 'invoice' => __( 'PDF Invoices', 'wpo_wcpdf' ),  
  26. 'packing-slip' => __( 'PDF Packing Slips', 'wpo_wcpdf' ),  
  27. ); 
  28.  
  29. /** 
  30. * Add the styles 
  31. */ 
  32. public function add_styles() { 
  33. if( $this->is_order_edit_page() ) { 
  34. wp_enqueue_style( 'thickbox' ); 
  35.  
  36. wp_enqueue_style( 
  37. 'wpo-wcpdf',  
  38. WooCommerce_PDF_Invoices::$plugin_url . 'css/style.css',  
  39. array(),  
  40. WooCommerce_PDF_Invoices::$version 
  41. ); 
  42.  
  43. if ( version_compare( WOOCOMMERCE_VERSION, '2.1' ) >= 0 ) { 
  44. // WC 2.1 or newer (MP6) is used: bigger buttons 
  45. wp_enqueue_style( 
  46. 'wpo-wcpdf-buttons',  
  47. WooCommerce_PDF_Invoices::$plugin_url . 'css/style-buttons.css',  
  48. array(),  
  49. WooCommerce_PDF_Invoices::$version 
  50. ); 
  51. } else { 
  52. // legacy WC 2.0 styles 
  53. wp_enqueue_style( 
  54. 'wpo-wcpdf-buttons',  
  55. WooCommerce_PDF_Invoices::$plugin_url . 'css/style-buttons-wc20.css',  
  56. array(),  
  57. WooCommerce_PDF_Invoices::$version 
  58. ); 
  59.  
  60. /** 
  61. * Add the scripts 
  62. */ 
  63. public function add_scripts() { 
  64. if( $this->is_order_edit_page() ) { 
  65. wp_enqueue_script( 
  66. 'wpo-wcpdf',  
  67. WooCommerce_PDF_Invoices::$plugin_url . 'js/script.js',  
  68. array( 'jquery' ),  
  69. WooCommerce_PDF_Invoices::$version 
  70. ); 
  71. wp_localize_script(  
  72. 'wpo-wcpdf',  
  73. 'wpo_wcpdf_ajax',  
  74. array( 
  75. // 'ajaxurl' => add_query_arg( 'action', 'generate_wpo_wcpdf', admin_url( 'admin-ajax.php' ) ), // URL to WordPress ajax handling page  
  76. 'ajaxurl' => admin_url( 'admin-ajax.php' ), // URL to WordPress ajax handling page  
  77. 'nonce' => wp_create_nonce('generate_wpo_wcpdf'),  
  78. 'bulk_actions' => array_keys( apply_filters( 'wpo_wcpdf_bulk_actions', $this->bulk_actions ) ),  
  79. )  
  80. );  
  81. }  
  82.  
  83. /** 
  84. * Is order page 
  85. */ 
  86. public function is_order_edit_page() { 
  87. global $post_type; 
  88. if( $post_type == 'shop_order' ) { 
  89. return true;  
  90. } else { 
  91. return false; 
  92. }  
  93.  
  94. /** 
  95. * Add PDF actions to the orders listing 
  96. */ 
  97. public function add_listing_actions( $order ) { 
  98. // do not show buttons for trashed orders 
  99. if ( WCX_Order::get_status( $order ) == 'trash' ) { 
  100. return; 
  101.  
  102. $listing_actions = array( 
  103. 'invoice' => array ( 
  104. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=invoice&order_ids=' . WCX_Order::get_id( $order ) ), 'generate_wpo_wcpdf' ),  
  105. 'img' => WooCommerce_PDF_Invoices::$plugin_url . 'images/invoice.png',  
  106. 'alt' => __( 'PDF Invoice', 'wpo_wcpdf' ),  
  107. ),  
  108. 'packing-slip' => array ( 
  109. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=packing-slip&order_ids=' . WCX_Order::get_id( $order ) ), 'generate_wpo_wcpdf' ),  
  110. 'img' => WooCommerce_PDF_Invoices::$plugin_url . 'images/packing-slip.png',  
  111. 'alt' => __( 'PDF Packing Slip', 'wpo_wcpdf' ),  
  112. ),  
  113. ); 
  114.  
  115. $listing_actions = apply_filters( 'wpo_wcpdf_listing_actions', $listing_actions, $order );  
  116.  
  117. foreach ($listing_actions as $action => $data) { 
  118. ?> 
  119. <a href="<?php echo $data['url']; ?>" class="button tips wpo_wcpdf <?php echo $action; ?>" target="_blank" alt="<?php echo $data['alt']; ?>" data-tip="<?php echo $data['alt']; ?>"> 
  120. <img src="<?php echo $data['img']; ?>" alt="<?php echo $data['alt']; ?>" width="16"> 
  121. </a> 
  122. <?php 
  123.  
  124. /** 
  125. * Create additional Shop Order column for Invoice Numbers 
  126. * @param array $columns shop order columns 
  127. */ 
  128. public function add_invoice_number_column( $columns ) { 
  129. // Check user setting 
  130. if ( !isset($this->general_settings['invoice_number_column'] ) ) { 
  131. return $columns; 
  132.  
  133. // put the column after the Status column 
  134. $new_columns = array_slice($columns, 0, 2, true) + 
  135. array( 'pdf_invoice_number' => __( 'Invoice Number', 'wpo_wcpdf' ) ) + 
  136. array_slice($columns, 2, count($columns) - 1, true) ; 
  137. return $new_columns; 
  138.  
  139. /** 
  140. * Display Invoice Number in Shop Order column (if available) 
  141. * @param string $column column slug 
  142. */ 
  143. public function invoice_number_column_data( $column ) { 
  144. global $post, $the_order, $wpo_wcpdf; 
  145.  
  146. if ( $column == 'pdf_invoice_number' ) { 
  147. if ( empty( $the_order ) || WCX_Order::get_id( $the_order ) != $post->ID ) { 
  148. $order = WCX::get_order( $post->ID ); 
  149. echo $wpo_wcpdf->export->get_invoice_number( WCX_Order::get_id( $order ) ); 
  150. do_action( 'wcpdf_invoice_number_column_end', $order ); 
  151. } else { 
  152. echo $wpo_wcpdf->export->get_invoice_number( WCX_Order::get_id( $the_order ) ); 
  153. do_action( 'wcpdf_invoice_number_column_end', $the_order ); 
  154.  
  155. /** 
  156. * Display download link on My Account page 
  157. */ 
  158. public function my_account_pdf_link( $actions, $order ) { 
  159. $pdf_url = wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=invoice&order_ids=' . WCX_Order::get_id( $order ) . '&my-account'), 'generate_wpo_wcpdf' ); 
  160.  
  161. // check my account button settings 
  162. if (isset($this->general_settings['my_account_buttons'])) { 
  163. switch ($this->general_settings['my_account_buttons']) { 
  164. case 'available': 
  165. $invoice_allowed = WCX_Order::get_meta( $order, '_wcpdf_invoice_exists', true ); 
  166. break; 
  167. case 'always': 
  168. $invoice_allowed = true; 
  169. break; 
  170. case 'never': 
  171. $invoice_allowed = false; 
  172. break; 
  173. case 'custom': 
  174. if ( isset( $this->general_settings['my_account_restrict'] ) && in_array( WCX_Order::get_status( $order ), array_keys( $this->general_settings['my_account_restrict'] ) ) ) { 
  175. $invoice_allowed = true; 
  176. } else { 
  177. $invoice_allowed = false;  
  178. break; 
  179. } else { 
  180. // backwards compatibility 
  181. $invoice_allowed = WCX_Order::get_meta( $order, '_wcpdf_invoice_exists', true ); 
  182.  
  183. // Check if invoice has been created already or if status allows download (filter your own array of allowed statuses) 
  184. if ( $invoice_allowed || in_array(WCX_Order::get_status( $order ), apply_filters( 'wpo_wcpdf_myaccount_allowed_order_statuses', array() ) ) ) { 
  185. $actions['invoice'] = array( 
  186. 'url' => $pdf_url,  
  187. 'name' => apply_filters( 'wpo_wcpdf_myaccount_button_text', __( 'Download invoice (PDF)', 'wpo_wcpdf' ) ) 
  188. );  
  189.  
  190. return apply_filters( 'wpo_wcpdf_myaccount_actions', $actions, $order ); 
  191.  
  192. /** 
  193. * Add the meta box on the single order page 
  194. */ 
  195. public function add_meta_boxes() { 
  196. // create PDF buttons 
  197. add_meta_box( 
  198. 'wpo_wcpdf-box',  
  199. __( 'Create PDF', 'wpo_wcpdf' ),  
  200. array( $this, 'sidebar_box_content' ),  
  201. 'shop_order',  
  202. 'side',  
  203. 'default' 
  204. ); 
  205.  
  206. // Invoice number & date 
  207. add_meta_box( 
  208. 'wpo_wcpdf-data-input-box',  
  209. __( 'PDF Invoice data', 'wpo_wcpdf' ),  
  210. array( $this, 'data_input_box_content' ),  
  211. 'shop_order',  
  212. 'normal',  
  213. 'default' 
  214. ); 
  215.  
  216. /** 
  217. * Create the meta box content on the single order page 
  218. */ 
  219. public function sidebar_box_content( $post ) { 
  220. global $post_id; 
  221.  
  222. $meta_actions = array( 
  223. 'invoice' => array ( 
  224. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=invoice&order_ids=' . $post_id ), 'generate_wpo_wcpdf' ),  
  225. 'alt' => esc_attr__( 'PDF Invoice', 'wpo_wcpdf' ),  
  226. 'title' => __( 'PDF Invoice', 'wpo_wcpdf' ),  
  227. ),  
  228. 'packing-slip' => array ( 
  229. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=packing-slip&order_ids=' . $post_id ), 'generate_wpo_wcpdf' ),  
  230. 'alt' => esc_attr__( 'PDF Packing Slip', 'wpo_wcpdf' ),  
  231. 'title' => __( 'PDF Packing Slip', 'wpo_wcpdf' ),  
  232. ),  
  233. ); 
  234.  
  235. $meta_actions = apply_filters( 'wpo_wcpdf_meta_box_actions', $meta_actions, $post_id ); 
  236.  
  237. ?> 
  238. <ul class="wpo_wcpdf-actions"> 
  239. <?php 
  240. foreach ($meta_actions as $action => $data) { 
  241. printf('<li><a href="%1$s" class="button" target="_blank" alt="%2$s">%3$s</a></li>', $data['url'], $data['alt'], $data['title']); 
  242. ?> 
  243. </ul> 
  244. <?php 
  245.  
  246. /** 
  247. * Add metabox for invoice number & date 
  248. */ 
  249. public function data_input_box_content ( $post ) { 
  250. $order = WCX::get_order( $post->ID ); 
  251. $invoice_exists = WCX_Order::get_meta( $order, '_wcpdf_invoice_exists', true ); 
  252. $invoice_number = WCX_Order::get_meta( $order, '_wcpdf_invoice_number', true ); 
  253. $invoice_date = WCX_Order::get_meta( $order, '_wcpdf_invoice_date', true ); 
  254.  
  255. do_action( 'wpo_wcpdf_meta_box_start', $post->ID ); 
  256.  
  257. ?> 
  258. <h4><?php _e( 'Invoice', 'wpo_wcpdf' ) ?></h4> 
  259. <p class="form-field _wcpdf_invoice_number_field "> 
  260. <label for="_wcpdf_invoice_number"><?php _e( 'Invoice Number (unformatted!)', 'wpo_wcpdf' ); ?>:</label> 
  261. <?php if (!empty($invoice_exists)) : ?> 
  262. <input type="text" class="short" style="" name="_wcpdf_invoice_number" id="_wcpdf_invoice_number" value="<?php echo $invoice_number ?>"> 
  263. <?php else : ?> 
  264. <input type="text" class="short" style="" name="_wcpdf_invoice_number" id="_wcpdf_invoice_number" value="<?php echo $invoice_number ?>" disabled="disabled" > 
  265. <?php endif; ?> 
  266. </p> 
  267. <p class="form-field form-field-wide"> 
  268. <label for="wcpdf_invoice_date"><?php _e( 'Invoice Date:', 'wpo_wcpdf' ); ?></label> 
  269. <?php if (!empty($invoice_exists)) : ?> 
  270. <input type="text" class="date-picker-field" name="wcpdf_invoice_date" id="wcpdf_invoice_date" maxlength="10" value="<?php echo date_i18n( 'Y-m-d', strtotime( $invoice_date ) ); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />@<input type="number" class="hour" placeholder="<?php _e( 'h', 'woocommerce' ) ?>" name="wcpdf_invoice_date_hour" id="wcpdf_invoice_date_hour" min="0" max="23" size="2" value="<?php echo date_i18n( 'H', strtotime( $invoice_date ) ); ?>" pattern="([01]?[0-9]{1}|2[0-3]{1})" />:<input type="number" class="minute" placeholder="<?php _e( 'm', 'woocommerce' ) ?>" name="wcpdf_invoice_date_minute" id="wcpdf_invoice_date_minute" min="0" max="59" size="2" value="<?php echo date_i18n( 'i', strtotime( $invoice_date ) ); ?>" pattern="[0-5]{1}[0-9]{1}" /> 
  271. <?php else : ?> 
  272. <input type="text" class="date-picker-field" name="wcpdf_invoice_date" id="wcpdf_invoice_date" maxlength="10" disabled="disabled" value="" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />@<input type="number" class="hour" disabled="disabled" placeholder="<?php _e( 'h', 'woocommerce' ) ?>" name="wcpdf_invoice_date_hour" id="wcpdf_invoice_date_hour" min="0" max="23" size="2" value="" pattern="([01]?[0-9]{1}|2[0-3]{1})" />:<input type="number" class="minute" placeholder="<?php _e( 'm', 'woocommerce' ) ?>" name="wcpdf_invoice_date_minute" id="wcpdf_invoice_date_minute" min="0" max="59" size="2" value="" pattern="[0-5]{1}[0-9]{1}" disabled="disabled" /> 
  273. <?php endif; ?> 
  274. </p> 
  275. <?php 
  276.  
  277. do_action( 'wpo_wcpdf_meta_box_end', $post->ID ); 
  278.  
  279. /** 
  280. * Add actions to menu 
  281. */ 
  282. public function bulk_actions() { 
  283. global $post_type; 
  284. $bulk_actions = apply_filters( 'wpo_wcpdf_bulk_actions', $this->bulk_actions ); 
  285.  
  286. if ( 'shop_order' == $post_type ) { 
  287. ?> 
  288. <script type="text/javascript"> 
  289. jQuery(document).ready(function() { 
  290. <?php foreach ($bulk_actions as $action => $title) { ?> 
  291. jQuery('<option>').val('<?php echo $action; ?>').html('<?php echo esc_attr( $title ); ?>').appendTo("select[name='action'], select[name='action2']"); 
  292. <?php } ?> 
  293. }); 
  294. </script> 
  295. <?php 
  296.  
  297. /** 
  298. * Save invoice number 
  299. */ 
  300. public function save_invoice_number_date($post_id) { 
  301. global $wpo_wcpdf; 
  302. $post_type = get_post_type( $post_id ); 
  303. if( $post_type == 'shop_order' ) { 
  304. // bail if this is not an actual 'Save order' action 
  305. if (!isset($_POST['action']) || $_POST['action'] != 'editpost') { 
  306. return; 
  307.  
  308. $order = WCX::get_order( $post_id ); 
  309. if ( isset($_POST['_wcpdf_invoice_number']) ) { 
  310. WCX_Order::update_meta_data( $order, '_wcpdf_invoice_number', stripslashes( $_POST['_wcpdf_invoice_number'] ) ); 
  311. WCX_Order::update_meta_data( $order, '_wcpdf_formatted_invoice_number', $wpo_wcpdf->export->get_invoice_number( $post_id ) ); 
  312. WCX_Order::update_meta_data( $order, '_wcpdf_invoice_exists', 1 ); 
  313.  
  314. if ( isset($_POST['wcpdf_invoice_date']) ) { 
  315. if ( empty($_POST['wcpdf_invoice_date']) ) { 
  316. WCX_Order::delete_meta_data( $order, '_wcpdf_invoice_date' ); 
  317. } else { 
  318. $invoice_date = strtotime( $_POST['wcpdf_invoice_date'] . ' ' . (int) $_POST['wcpdf_invoice_date_hour'] . ':' . (int) $_POST['wcpdf_invoice_date_minute'] . ':00' ); 
  319. $invoice_date = date_i18n( 'Y-m-d H:i:s', $invoice_date ); 
  320. WCX_Order::update_meta_data( $order, '_wcpdf_invoice_date', $invoice_date ); 
  321. WCX_Order::update_meta_data( $order, '_wcpdf_invoice_exists', 1 ); 
  322.  
  323. if (empty($_POST['wcpdf_invoice_date']) && isset($_POST['_wcpdf_invoice_number'])) { 
  324. $invoice_date = date_i18n( 'Y-m-d H:i:s', time() ); 
  325. WCX_Order::update_meta_data( $order, '_wcpdf_invoice_date', $invoice_date ); 
  326.  
  327. if ( empty($_POST['wcpdf_invoice_date']) && empty($_POST['_wcpdf_invoice_number'])) { 
  328. WCX_Order::delete_meta_data( $order, '_wcpdf_invoice_exists' ); 
  329.  
  330. /** 
  331. * Add invoice number to order search scope 
  332. */ 
  333. public function search_fields ( $custom_fields ) { 
  334. $custom_fields[] = '_wcpdf_invoice_number'; 
  335. $custom_fields[] = '_wcpdf_formatted_invoice_number'; 
  336. return $custom_fields;