diff --git a/mxp-sepaqr/muxp-sepaqr.php b/mxp-sepaqr/muxp-sepaqr.php index e99fede..43f3d97 100644 --- a/mxp-sepaqr/muxp-sepaqr.php +++ b/mxp-sepaqr/muxp-sepaqr.php @@ -1,169 +1,94 @@ get_data(); - // do we need the user? if so: $user = $order->get_user(); - if ( !empty($order->get_total()) && (float)$order->get_total() > 0 ) { echo '

' . esc_attr(__('For a convenient payment scan this qr code!' , 'mxp-sepa-qr-code-addon-for-woocommerce')) . '
'; echo 'qr-code

'; } } - - // only executed when using bacs as payment: - add_action( 'woocommerce_thankyou_bacs', 'muxp_add_text_to_thankyoupage' ); - // use action 'woocommerce_thankyou' to execute with all payment methods - - - /** - * add text snippet after the order table in email message - * if total amount is greater than 0 - * since there is no specific hook for payment methods, we need to make sure that the code is - * only shown if bacs is used as payment method - */ function muxp_email_after_order_table( $order, $sent_to_admin, $plain_text, $email) { // TODO: check whether spam filters are triggered by embedded image data, - // if so replace image with a link to the qr minipage ( site_url() . '?' . muxp_QUERY_PARAM . '=' . md5($order->get_total(). '_' . $order->get_id()) - // and activate muxp_USE_TRANSIENTS if ($sent_to_admin) { @@ -190,168 +115,90 @@ function muxp_email_after_order_table( $order, $sent_to_admin, $plain_text, $ema query[muxp_QUERY_PARAM] ) || $wp_query->query[muxp_QUERY_PARAM] == '') { - // stop processing if our param does not exist or is empty - return $template; - } - - $md5 = $wp_query->query[muxp_QUERY_PARAM]; - - if ( false === muxp_valid_md5($md5) ) { - $html = '

' . __('This is not a valid md5 hash :-( ', 'mxp-sepa-qr-code-addon-for-woocommerce') . '
' . __('Generating uncached dummy QR for 11 Euro/OrderID 11', 'mxp-sepa-qr-code-addon-for-woocommerce') . '

'; - } elseif ( false === ( $qrcode = muxp_get_transient_by_hash($md5) ) ) { - // valid md5, but nothing found, what a pity ... - if ( $md5 == "351436ef4b279e1811a6c68a2dd58b1b" ) { - // this is the test md5 generated from 11_11 ... test for cache population - muxp_set_transient(11,11,muxp_SAD_SMILEY); - } - $html = '
QR-Code not found or expired. Doh!' . PHP_EOL . '
Sad Smiley'; - } else { - $html = 'QR Code'; - } - echo '
' . esc_attr($html) . '
'; - // bail out without returning since we are done and do not want a complete WP page - exit; - - - } // TODO : check for a better place than template_redirect to hook into - add_action('template_redirect', 'muxp_qrpage_from_hash'); - - - /** - * Introduce a custom query variable for QR retrieval and testing purposes - */ - - function muxp_add_qv($vars){ - $vars[] = muxp_QUERY_PARAM; - return $vars; - } - add_filter( 'query_vars', 'muxp_add_qv'); - - - /* Utility and Wrapper functions */ - - /** - * get/create QR code from amount and order id, using first bacs account or defaults - * first, fetch from cache, if possible - * store result into cache or transient storage for later retrieval - * create cache/transient ID from a md5 hash of amount and orderid - */ - - function muxp_get_qrcode($amount,$orderid) { if ( false === ( $qrcode = muxp_get_transient_by_data($amount,$orderid) ) ) { - // not found - so regenerate the code and save as transient - $qrcode = muxp_create_qrcode($amount,$orderid); - } - if ( empty($qrcode) ) { - return false; - } - + $store_qr_code_as_image = get_option('muxp_store_qr_code_as_image', 'off'); if ($store_qr_code_as_image !== 'on') { // Default to transient QR codes - muxp_set_transient( $amount,$orderid, $qrcode ); - return $qrcode; - } else { - $qrcode_url = muxp_set_persistent($orderid, $qrcode); - return $qrcode_url; - } - } - - - /** * create a SEPA qr code based on first bacs account iban and bic , amount and id of order * store into transient for later retrieval in email link @@ -363,105 +210,55 @@ function muxp_create_qrcode ($amount,$orderid) { $payloadtext = __('Order-ID:' , 'mxp-sepa-qr-code-addon-for-woocommerce') . ' ' . $orderid; $bacs_accounts = get_option( 'woocommerce_bacs_accounts'); - if ( ! empty( $bacs_accounts[0] ) ) { - $iban = $bacs_accounts[0]['iban']; - $bic = $bacs_accounts[0]['bic']; - $company = $bacs_accounts[0]['account_name']; - } - // fallbacks if empty: - if ( empty($iban) ) { $iban = muxp_BACS_IBAN; } - if ( empty($bic) ) { $bic = muxp_BACS_BIC; } - if ( empty($company) ) { $company = muxp_BACS_COMPANY; } - - require_once(dirname(__FILE__) . "/vendor/bezahlcode/bezahlcode.class.php"); - - $bezahlcode = new Bezahlcode($iban, $bic, $company, "phpqrcode", false); - $bezahlcode->generatePayload($payloadtext, $amount); - $base64 = $bezahlcode->generateBase64('png'); - if (empty($base64) ) { - return false; - } - return $base64; - } - - // wrapper functions for caching or transients, depending on config - - function muxp_get_transient_by_data ($amount,$id) { - $transient = muxp_TRANSIENT_PREFIX . md5($amount. '_' . $id); - if (defined('muxp_USE_TRANSIENTS')) { - return get_transient( $transient ); - } - return wp_cache_get( $transient ); - } - - function muxp_get_transient_by_hash ($md5) { - $transient = muxp_TRANSIENT_PREFIX . $md5; - if (defined('muxp_USE_TRANSIENTS')) { - return get_transient( $transient ); - } - return wp_cache_get( $transient ); - - } - - - function muxp_set_transient ($amount,$id,$qrcode) { - $transient = muxp_TRANSIENT_PREFIX . md5($amount . '_' . $id); - if (defined('muxp_USE_TRANSIENTS')) { - $store = set_transient( $transient, $qrcode, muxp_TRANSIENT_LIFETIME ); - } else { - $store = wp_cache_set( $transient, $qrcode, '', muxp_TRANSIENT_LIFETIME ); - } - return $store; - } /** @@ -481,19 +278,12 @@ function muxp_set_persistent($id, $qrcode) { return wp_upload_dir()['baseurl'] . "/muxp-sepa-qr-code-addon-for-woocommerce/$id.png"; } - - // helper to validate the string is syntactically correct md5 - - function muxp_valid_md5($md5 ='') { - return strlen($md5) == 32 && ctype_xdigit($md5); - } - add_action('admin_menu', 'muxp_add_admin_menu'); function muxp_add_admin_menu() {