WooCommerce_Delivery_Notes_Print

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

Defined (1)

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

/includes/class-wcdn-print.php  
  1. class WooCommerce_Delivery_Notes_Print { 
  2.  
  3. public $template_directory_name; 
  4. public $template_path_theme; 
  5. public $template_path_plugin; 
  6. public $template_url_plugin; 
  7.  
  8. public $api_endpoints; 
  9. public $query_vars; 
  10. public $template_types; 
  11. public $template_type; 
  12.  
  13. public $order_ids; 
  14. public $order_email; 
  15. public $orders; 
  16.  
  17. /** 
  18. * Constructor 
  19. */ 
  20. public function __construct() {  
  21. // Set the default variables 
  22. $this->template_types = array( 
  23. 'invoice',  
  24. 'delivery-note',  
  25. 'order' 
  26. ); 
  27.  
  28. $this->api_endpoints = array(  
  29. 'print-order' => get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'print_order_page_endpoint', 'print-order' ) 
  30.  
  31. ); 
  32.  
  33. $this->query_vars = array( 
  34. 'print-order-type',  
  35. 'print-order-email' 
  36. ); 
  37.  
  38. // Load the hooks 
  39. add_action( 'init', array( $this, 'load_hooks' ) ); 
  40. add_filter( 'query_vars', array( $this, 'add_query_vars' ) ); 
  41. add_action( 'parse_request', array( $this, 'parse_request' ) ); 
  42. add_action( 'template_redirect', array( $this, 'template_redirect_theme' ) ); 
  43. add_action( 'wp_ajax_print_order', array( $this, 'template_redirect_admin' ) ); 
  44.  
  45. /** 
  46. * Load the init hooks 
  47. */ 
  48. public function load_hooks() {  
  49. // Define default variables 
  50. $this->template_directory_name = 'print-order'; 
  51. $this->template_path_theme = WC_TEMPLATE_PATH . $this->template_directory_name . '/'; 
  52. $this->template_path_plugin = WooCommerce_Delivery_Notes::$plugin_path . 'templates/' . $this->template_directory_name . '/'; 
  53. $this->template_url_plugin = WooCommerce_Delivery_Notes::$plugin_url . 'templates/' . $this->template_directory_name . '/'; 
  54.  
  55. // Add the endpoints 
  56. $this->add_endpoints(); 
  57.  
  58. /** 
  59. * Add endpoints for query vars. 
  60. * the endpoint is used in the front-end to 
  61. * generate the print template and link. 
  62. */ 
  63. public function add_endpoints() { 
  64. foreach( $this->api_endpoints as $var ) { 
  65. add_rewrite_endpoint( $var, EP_PAGES ); 
  66.  
  67. // Flush the rules when the transient is set. 
  68. // This is important to make the endpoint work. 
  69. if( get_transient( WooCommerce_Delivery_Notes::$plugin_prefix . 'flush_rewrite_rules' ) == true ) { 
  70. delete_transient( WooCommerce_Delivery_Notes::$plugin_prefix . 'flush_rewrite_rules' ); 
  71. flush_rewrite_rules(); 
  72.  
  73. /** 
  74. * Add the query vars to the url 
  75. */ 
  76. public function add_query_vars( $vars ) { 
  77. foreach( $this->query_vars as $var ) { 
  78. $vars[] = $var; 
  79. return $vars; 
  80.  
  81. /** 
  82. * Parse the query variables 
  83. */ 
  84. public function parse_request( $wp ) { 
  85. // Map endpoint keys to their query var keys, when another endpoint name was set. 
  86. foreach( $this->api_endpoints as $key => $var ) { 
  87. if( isset( $_GET[$var] ) ) { 
  88. $wp->query_vars[$key] = $_GET[$var]; 
  89. } elseif ( isset( $wp->query_vars[$var] ) ) { 
  90. $wp->query_vars[$key] = $wp->query_vars[$var]; 
  91.  
  92. /** 
  93. * Template handling in the front-end 
  94. */ 
  95. public function template_redirect_theme() { 
  96. global $wp; 
  97. // Check the page url and display the template when on my-account page 
  98. if( !empty( $wp->query_vars['print-order'] ) && is_account_page() ) { 
  99. $type = !empty( $wp->query_vars['print-order-type'] ) ? $wp->query_vars['print-order-type'] : null; 
  100. $email = !empty( $wp->query_vars['print-order-email'] ) ? $wp->query_vars['print-order-email'] : null; 
  101. $this->generate_template( $wp->query_vars['print-order'], $type, $email ); 
  102. exit; 
  103.  
  104. /** 
  105. * Template handling in the back-end 
  106. */ 
  107. public function template_redirect_admin() {  
  108. // Let the backend only access the page 
  109. if( is_admin() && !empty( $_REQUEST['print-order'] ) && !empty( $_REQUEST['action'] ) ) { 
  110. $type = !empty( $_REQUEST['print-order-type'] ) ? $_REQUEST['print-order-type'] : null; 
  111. $email = !empty( $_REQUEST['print-order-email'] ) ? $_REQUEST['print-order-email'] : null; 
  112. $this->generate_template( $_GET['print-order'], $type, $email ); 
  113. exit; 
  114. exit; 
  115.  
  116. /** 
  117. * Generate the template  
  118. */ 
  119. public function generate_template( $order_ids, $template_type = 'order', $order_email = null ) { 
  120. global $post, $wp; 
  121.  
  122. // Explode the ids when needed 
  123. if( !is_array( $order_ids ) ) { 
  124. $this->order_ids = array_filter( explode('-', $order_ids ) ); 
  125.  
  126. // Default type  
  127. if( empty( $template_type ) || !in_array( $template_type, $this->template_types ) ) { 
  128. $this->template_type = 'order'; 
  129. } else { 
  130. $this->template_type = $template_type; 
  131.  
  132. // Default email  
  133. if( empty( $order_email ) ) { 
  134. $this->order_email = null; 
  135. } else { 
  136. $this->order_email = strtolower( $order_email ); 
  137.  
  138. // Create the orders and check permissions 
  139. $populated = $this->populate_orders(); 
  140.  
  141. // Only continue if the orders are populated 
  142. if( !$populated ) { 
  143. die(); 
  144. }  
  145.  
  146. // Load the print template html 
  147. wc_get_template( 'print-order.php', null, $this->template_path_theme, $this->template_path_plugin ); 
  148. exit; 
  149.  
  150. /** 
  151. * Get print page url 
  152. */ 
  153. public function get_print_page_url( $order_ids, $template_type = 'order', $order_email = null ) { 
  154. // Explode the ids when needed 
  155. if( !is_array( $order_ids ) ) { 
  156. $order_ids = array_filter( explode( '-', $order_ids ) ); 
  157.  
  158. // Default args 
  159. $args = array(); 
  160.  
  161. if( in_array( $template_type, $this->template_types ) && $template_type != 'order' ) { 
  162. $args = wp_parse_args( array( 'print-order-type' => $template_type ), $args ); 
  163.  
  164. if( !empty( $order_email ) ) { 
  165. $args = wp_parse_args( array( 'print-order-email' => $order_email ), $args ); 
  166.  
  167. // Generate the url  
  168. $order_ids_slug = implode( '-', $order_ids ); 
  169.  
  170. // Create another url depending on where the user prints. This 
  171. // prevents some issues with ssl when the my-account page is  
  172. // secured with ssl but the admin isn't. 
  173. if( is_admin() ) { 
  174. // For the admin we use the ajax.php for better security 
  175. $args = wp_parse_args( array( 'action' => 'print_order' ), $args ); 
  176. $base_url = admin_url( 'admin-ajax.php' ); 
  177. $endpoint = 'print-order'; 
  178.  
  179. // Add the order ids and create the url 
  180. $url = add_query_arg( $endpoint, $order_ids_slug, $base_url ); 
  181. } else { 
  182. // For the theme 
  183. $base_url = get_permalink( wc_get_page_id( 'myaccount' ) ); 
  184. $endpoint = $this->api_endpoints['print-order']; 
  185.  
  186. // Add the order ids and create the url 
  187. if( get_option( 'permalink_structure' ) ) { 
  188. $url = trailingslashit( trailingslashit( $base_url ) . $endpoint . '/' . $order_ids_slug ); 
  189. } else { 
  190. $url = add_query_arg( $endpoint, $order_ids_slug, $base_url ); 
  191.  
  192. // Add all other args  
  193. $url = add_query_arg( $args, $url ); 
  194.  
  195. return $url; 
  196.  
  197. /** 
  198. * Get the template url for a file. locate by file existence 
  199. * and then return the corresponding url. 
  200. */ 
  201. public function get_template_url( $name ) {  
  202. $child_theme_path = get_stylesheet_directory() . '/' . $this->template_path_theme; 
  203. $child_theme_uri = get_stylesheet_directory_uri() . '/' . $this->template_path_theme; 
  204. $theme_path = get_template_directory() . '/' . $this->template_path_theme; 
  205. $theme_uri = get_template_directory_uri() . '/' . $this->template_path_theme; 
  206.  
  207. // buld the url depenind on where the file is 
  208. if( file_exists( $child_theme_path . $name ) ) { 
  209. $uri = $child_theme_uri . $name; 
  210. } elseif( file_exists( $theme_path . $name ) ) { 
  211. $uri = $theme_uri . $name; 
  212. } else { 
  213. $uri = $this->template_url_plugin . $name; 
  214.  
  215. return $uri; 
  216.  
  217. /** 
  218. * Create the orders list and check the permissions 
  219. */ 
  220. private function populate_orders() {  
  221. $this->orders = array(); 
  222.  
  223. // Get the orders 
  224. $args = array( 
  225. 'posts_per_page' => -1,  
  226. 'post_type' => 'shop_order',  
  227. 'post_status' => 'publish',  
  228. 'post__in' => $this->order_ids,  
  229. 'orderby' => 'post__in' 
  230. ); 
  231. $posts = get_posts( $args ); 
  232.  
  233. // All orders should exist 
  234. if( count( $posts ) !== count( $this->order_ids ) ) { 
  235. $this->orders = null; 
  236. return false; 
  237.  
  238. // Check permissons of the user to determine  
  239. // if the orders should be populated. 
  240. foreach( $posts as $post ) { 
  241. $order = new WC_Order( $post->ID ); 
  242.  
  243. // Logged in users  
  244. if( is_user_logged_in() && ( !current_user_can( 'edit_shop_orders' ) && !current_user_can( 'view_order', $order->id ) ) ) { 
  245. $this->orders = null; 
  246. return false; 
  247. }  
  248.  
  249. // An email is required for not logged in users  
  250. if( !is_user_logged_in() && ( empty( $this->order_email ) || strtolower( $order->billing_email ) != $this->order_email ) ) { 
  251. $this->orders = null; 
  252. return false; 
  253.  
  254. // Save the order to get it without an additional database call 
  255. $this->orders[$post->ID] = $order; 
  256. return true; 
  257.  
  258. /** 
  259. * Get the order 
  260. */ 
  261. public function get_order( $order_id ) {  
  262. if( isset( $this->orders[$order_id] ) ) { 
  263. return $this->orders[$order_id]; 
  264. return;  
  265. }  
  266.  
  267. /** 
  268. * Get the order invoice number 
  269. */ 
  270. public function get_order_invoice_number( $order_id ) {  
  271. $invoice_start = intval( get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number_start', 1 ) ); 
  272. $invoice_counter = intval( get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number_counter', 0 ) ); 
  273. $invoice_prefix = get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number_prefix' ); 
  274. $invoice_suffix = get_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number_suffix' ); 
  275.  
  276. // Add the invoice number to the order when it doesn't yet exist 
  277. $meta_key = '_' . WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number'; 
  278. $meta_added = add_post_meta( $order_id, $meta_key, $invoice_prefix . ( $invoice_start + $invoice_counter ) . $invoice_suffix, true ); 
  279.  
  280. // Update the total count 
  281. if( $meta_added ) { 
  282. update_option( WooCommerce_Delivery_Notes::$plugin_prefix . 'invoice_number_counter', $invoice_counter + 1 ); 
  283.  
  284. // Get the invoice number 
  285. return apply_filters( 'wcdn_order_invoice_number', get_post_meta( $order_id, $meta_key, true ) ); 
  286. }  
  287.