AeliaWCEU_VAT_AssistantOrders_Integration

Implements an integration with WooCommerce Orders pages.

Defined (1)

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

/src/lib/classes/integration/admin/orders_integration.php  
  1. class Orders_Integration extends \Aelia\WC\Base_Class { 
  2. /** 
  3. * Returns the URL for Ajax operations related to orders. 
  4. * @param int order_id The targer order ID. If empty, the ID of the order being 
  5. * edited, if any, is taken. 
  6. * @return string 
  7. */ 
  8. protected static function get_ajax_url($order_id = null) { 
  9. $ajax_args = array( 
  10. 'action' => Definitions::ARG_COLLECT_ORDER_VAT_INFO,  
  11. '_ajax_nonce' => wp_create_nonce('aelia-euva-' . Definitions::ARG_COLLECT_ORDER_VAT_INFO),  
  12. ); 
  13.  
  14. if(empty($order_id)) { 
  15. $order_id = self::editing_order(); 
  16. $ajax_args['order_id'] = $order_id; 
  17. // Convert the array of arguments into an HTTP query string, ready for use 
  18. // in an Ajax call 
  19. $ajax_args = http_build_query($ajax_args); 
  20.  
  21. return admin_url('admin-ajax.php', 'absolute') . '?' . $ajax_args; 
  22.  
  23. /** 
  24. * Adds localisation data for the JavaScript that extend the Tax Settings pages. 
  25. * @param array $admin_scripts_params The array of parameters to extend. 
  26. * @return array The array of parameters with additional data. 
  27. */ 
  28. public static function localize_admin_scripts(array $admin_scripts_params) { 
  29. $euva = WC_Aelia_EU_VAT_Assistant::instance(); 
  30. $text_domain = WC_Aelia_EU_VAT_Assistant::$text_domain; 
  31.  
  32. $admin_scripts_params['orders_settings'] = array( 
  33. 'collect_vat_data_for_manual_orders' => WC_Aelia_EU_VAT_Assistant::settings()->get(Settings::FIELD_COLLECT_VAT_DATA_FOR_MANUAL_ORDERS, false),  
  34. 'collect_order_ajax_url' => self::get_ajax_url(),  
  35. 'user_interface' => array( 
  36. 'collect_order_vat_prompt' => __('This operation will analyse the order, collect ' . 
  37. 'the VAT information associated to it and make ' . 
  38. 'it available to the EU VAT Assistant.', $text_domain) . 
  39. ' ' . 
  40. __('If you already performed this operation before, ' . 
  41. 'on this order, the VAT information collected ' . 
  42. 'will replace the existing one.', $text_domain) . 
  43. "\n\n" . 
  44. __('Would you like to continue?', $text_domain),  
  45. ),  
  46. ); 
  47. return $admin_scripts_params; 
  48.  
  49. /** 
  50. * Validates an Ajax request. 
  51. * @return bool 
  52. */ 
  53. public static function validate_ajax_request($user_permission = 'manage_woocommerce') { 
  54. $result = true; 
  55. if(!current_user_can($user_permission)) { 
  56. $message = 'HTTP/1.1 403 Forbidden'; 
  57. header($message, true, 403); 
  58. $result = false; 
  59.  
  60. // Handle invalid requests (e.g. a request missing required arguments) 
  61. if($result && (empty($_REQUEST[Definitions::ARG_COLLECT_ORDER_ID]) || !is_numeric($_REQUEST[Definitions::ARG_COLLECT_ORDER_ID]))) { 
  62. $message = 'HTTP/1.1 400 Bad Request'; 
  63. header($message, true, 400); 
  64. $result = false; 
  65.  
  66. if($result && !check_ajax_referer('aelia-euva-' . Definitions::ARG_COLLECT_ORDER_VAT_INFO, '_ajax_nonce', false)) { 
  67. header('HTTP/1.1 400 Bad Request', true, 400); 
  68. $message = 'Ajax referer check failed'; 
  69. $result = false; 
  70. }; 
  71.  
  72. if($result == false) { 
  73. wp_send_json(array( 
  74. 'result' => $result,  
  75. 'messages' => array($message),  
  76. )); 
  77. return $result; 
  78.  
  79. /** 
  80. * Indicates if we are on an "Edit Order" page. 
  81. * @return int|false The ID of the order being edited, or false if we are not 
  82. * on the "Edit Order" page. 
  83. */ 
  84. public static function editing_order() { 
  85. global $post; 
  86. if(is_admin() && !WC_Aelia_EU_VAT_Assistant::doing_ajax() && 
  87. is_object($post) && ($post->post_type == 'shop_order')) { 
  88. return $post->ID; 
  89. return false; 
  90.  
  91. /** 
  92. * Adds the "VAT number" field to the billing fields on the order edit page. 
  93. * This will allow admins to enter a VAT number more easily when creating 
  94. * orders manually. 
  95. * @param array billing_fields An array of billing fields. 
  96. * @retur array The billing fields, with the VAT number field added to it. 
  97. * @since 1.6.2.160315 
  98. */ 
  99. public static function woocommerce_admin_billing_fields($billing_fields) { 
  100. $text_domain = WC_Aelia_EU_VAT_Assistant::$text_domain; 
  101. $billing_fields['vat_number'] = array( 
  102. 'id' => 'vat_number',  
  103. 'label' => __('VAT number', $text_domain),  
  104. 'class' => 'tips',  
  105. 'placeholder' => __("Customer's VAT number, if any", $text_domain),  
  106. 'custom_attributes' => array( 
  107. 'data-tip' => __('This number will be stored as is, without validation.', $text_domain) . 
  108. '<br><br>'. 
  109. '<strong>' . __('Important', $text_domain) . '</strong>: ' . 
  110. __('Changing this number will not apply a tax exemption, and ' . 
  111. 'it will not affect the VAT data that has already been ' . 
  112. 'collected by the EU VAT Assistant (see <strong>VAT ' . 
  113. 'Information</strong> box).', $text_domain),  
  114. ),  
  115. 'show' => false 
  116. ); 
  117. return $billing_fields;