From e768a4e3ddb14114313154b4d37392c1f09e111b Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Thu, 25 Feb 2016 14:56:53 +0100 Subject: [PATCH] Implement generic support for invoice plugins --- ordernumbers_woocommerce.php | 110 ++++++++++++----------------------- 1 file changed, 37 insertions(+), 73 deletions(-) diff --git a/ordernumbers_woocommerce.php b/ordernumbers_woocommerce.php index 1a56870..b392b73 100644 --- a/ordernumbers_woocommerce.php +++ b/ordernumbers_woocommerce.php @@ -41,7 +41,10 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { add_action( 'woocommerce_admin_field_ordernumber_variables', array( $this, 'admin_field_variables' ) ); add_action( 'pre_update_option_ordernumber_variables', array( $this, 'update_option_variables')); + // THIRD-PARTY PLUGIN SUPPORT // Install hooks for third-party plugin support: + $this->thirdparty_invoicenumber_init(); + // Support for specific plugins: $this->thirdparty_wpo_wcpdf_init(); } @@ -298,82 +301,43 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { } - /** ************************************************************ - * Support for automatic extension updates - ** ************************************************************/ - public function update_access_check() { - $ordernumber = $_POST['order_number']; - $orderpass = $_POST['order_pass']; - - $json = $this->helper->ajax_counter_delete($_POST['nrtype'], $_POST['counter']); - wp_send_json($json); +// THIRD-PARTY PLUGIN SUPPORT + + /** **************************************************************** + * Generic Invoice Number handling for third-party invoice plugins + ** **************************************************************** + * + * - Filter woocommerce_generate_invoice_number($default, $order) to create the invoice number + * - Filter woocommerce_invoice_number($default, $orderID) to retrieve the + * invoice number (also create the invoice number if it does not yet exist) + */ + + protected function thirdparty_invoicenumber_init() { + // The filter to actually return the order number for the given order + add_filter ('woocommerce_generate_invoice_number', array( &$this, 'thirdparty_create_invoicenumber'), 10, 2/*<= Also get the order object! */); + add_filter ('woocommerce_invoice_number', array( &$this, 'thirdparty_get_invoicenumber'), 10, 2/*<= Also get the order ID! */); } - public function checkUpdateAccess($order_number, $order_pass, $json = array()) { - // First, extract the update server URL from the manifest, then load - // the update XML from the update server, extract the download URL, - // append the order number and password and check whether access is - // possible. - $json['success'] = FALSE; - if (isset($this->_xmlFile)) { - $xmlfile = $this->_xmlFile; + + /** + * Callback function for WooThemes PDF Invoices to generate an invoice number for an order + * The hook to customize invoice numbers (requests the invoice number from the database; + * creates a new invoice number if no entry exists in the database) + */ + function thirdparty_create_invoicenumber($default, $order) { + return $this->get_or_create_number($default, $order, 'invoice'); + } + + function thirdparty_get_invoicenumber($default, $orderid) { + if (get_option('customize_invoice', 'no')!='no') { + $_of = new WC_Order_Factory(); + $order = $_of->get_order($orderid); + $number = $this->get_or_create_number($orderid, $order, 'invoice'); + return $number; } else { - // VM 2 does not set the _xmlFile property, so construct it manually - $xmlfile = JPATH_SITE . '/plugins/' . $this->_type . '/' . $this->_name . '/' . $this->_name . '.xml'; + return $default; } - $xml = simplexml_load_file($xmlfile); - if (!$xml || !isset($xml->updateservers)) { - JFactory::getApplication()->enqueueMessage(JText::sprintf('OPENTOOLS_XMLMANIFEST_ERROR', $this->_xmlFile), 'error'); - return $json; - } - $updateservers = $xml->updateservers; - foreach ($updateservers->children() as $server) { - if ($server->getName()!='server') { - JFactory::getApplication()->enqueueMessage(JText::sprintf('OPENTOOLS_XMLMANIFEST_ERROR', $this->_xmlFile), 'error'); - continue; - } - $updateurl = html_entity_decode((string)$server); - $updatescript = simplexml_load_file($updateurl); - if (!$updatescript) { - JFactory::getApplication()->enqueueMessage(JText::sprintf('OPENTOOLS_UPDATESCRIPT_ERROR', $updateurl), 'error'); - continue; - } - $urls = $updatescript->xpath('/updates/update/downloads/downloadurl'); - while (list( , $node) = each($urls)) { - $downloadurl = (string)($node); - if ($order_number) { - $downloadurl .= (parse_url($downloadurl, PHP_URL_QUERY) ? '&' : '?') . 'order_number=' . urlencode($order_number); - } - if ($order_pass) { - $downloadurl .= (parse_url($downloadurl, PHP_URL_QUERY) ? '&' : '?') . 'order_pass=' . urlencode($order_pass); - } - $downloadurl .= (parse_url($downloadurl, PHP_URL_QUERY) ? '&' : '?') . 'check_access=1'; - - $headers = get_headers($downloadurl); - list($version, $status_code, $msg) = explode(' ',$headers[0], 3); - - // Check the HTTP Status code - switch($status_code) { - case 200: - $json['success'] = TRUE; - JFactory::getApplication()->enqueueMessage($msg, 'message'); - $this->setupUpdateCredentials($order_number, $order_pass); - break; - default: - JFactory::getApplication()->enqueueMessage($msg, 'error'); - // Clear the credentials... - $this->setupUpdateCredentials("", ""); - break; - } - $this->setAndSaveParams(array( - 'update_credentials_checked'=>$json['success'], - 'order_number' => $order_number, - 'order_pass' => $order_pass, - )); - } - } - return $json; - } - + } + /** ************************************************************ * Support for WPO WooCommerce PDF Invoices and Packaging Slips -- GitLab