From 7dd39f852aa1fc42d6d3b1f70bfce1605fa0e90e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 19 Oct 2020 19:25:44 +0200 Subject: clean up snippets to follow WC standards --- snippets/kudos-currency.php | 48 +++++++--- snippets/oidc-reclaim.php | 22 +++-- snippets/oidc-woocommerce.php | 206 +++++++++++++++++++++++------------------- 3 files changed, 160 insertions(+), 116 deletions(-) diff --git a/snippets/kudos-currency.php b/snippets/kudos-currency.php index ac1cac9..c84936d 100644 --- a/snippets/kudos-currency.php +++ b/snippets/kudos-currency.php @@ -1,23 +1,47 @@ + */ + +// See also https://gist.github.com/woogists/72947b71e48a62f15f5548283d4ed00f#file-wc-add-currency-symbol-php. add_filter( 'woocommerce_currencies', 'add_my_currency' ); + +/** + * Extend the list of currencies. + * + * @param array $currencies array with strings with the names of supported currencies. + * + * @return array with extended list of currencies. + */ function add_my_currency( $currencies ) { - $currencies['KUDOS'] = __( 'KUDOS', 'woocommerce' ); - $currencies['TESTKUDOS'] = __( 'TESTKUDOS', 'woocommerce' ); - return $currencies; + $currencies['KUDOS'] = __( 'KUDOS', 'woocommerce' ); + $currencies['TESTKUDOS'] = __( 'TESTKUDOS', 'woocommerce' ); + return $currencies; } -add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); +add_filter( 'woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2 ); + +/** + * Lookup a currency symbol by the given currency. + * + * @param string $currency_symbol Symbol the currency was mapped to so far. + * @param string $currency Currency to map. + * + * @return desired currency symbol, usually the input, except for the two currencies we know. + */ function add_my_currency_symbol( $currency_symbol, $currency ) { - switch( $currency ) { - case 'KUDOS': $currency_symbol = 'ク'; break; - case 'TESTKUDOS': $currency_symbol = 'テ'; break; - } - return $currency_symbol; + switch ( $currency ) { + case 'KUDOS': + $currency_symbol = 'ク'; + break; + case 'TESTKUDOS': + $currency_symbol = 'テ'; + break; + } + return $currency_symbol; } diff --git a/snippets/oidc-reclaim.php b/snippets/oidc-reclaim.php index 5ef4022..ad8e4d8 100644 --- a/snippets/oidc-reclaim.php +++ b/snippets/oidc-reclaim.php @@ -1,12 +1,16 @@ + */ -// Modify OIDC connect button text for Reclaim:ID -add_filter('openid-connect-generic-login-button-text', function( $text ) { - $text = __('Login with Re:ClaimID', 'gnutaler' ); - return $text; -}); +// Modify OIDC connect button text for Reclaim:ID. +add_filter( + 'openid-connect-generic-login-button-text', + function( $text ) { + $text = __( 'Login with Re:ClaimID', 'gnutaler' ); + return $text; + } +); diff --git a/snippets/oidc-woocommerce.php b/snippets/oidc-woocommerce.php index 815b749..ba9a310 100644 --- a/snippets/oidc-woocommerce.php +++ b/snippets/oidc-woocommerce.php @@ -1,46 +1,58 @@ + */ -/** - * Add OIDC login form to checkout page. - */ -add_action ('openid-connect-generic-register-login-form', - function ( $login_form ) { +// Add OIDC login form to checkout page. +add_action( + 'openid-connect-generic-register-login-form', + function ( $login_form ) { - // if WooCommerce is inactive, do nothing - if ( ! in_array( 'woocommerce/woocommerce.php', - apply_filters( 'active_plugins', - get_option( 'active_plugins' ) ) ) ) { - return; - } + // If WooCommerce is inactive, do nothing. + if ( ! in_array( + 'woocommerce/woocommerce.php', + apply_filters( + 'active_plugins', + get_option( 'active_plugins' ) + ), + true + ) ) { + return; + } - // show login form at the shopping cart (if not logged in) - add_action( 'woocommerce_before_checkout_billing_form', - function () use ( $login_form ) { - $user = wp_get_current_user (); - if (0 == $user->ID) { - // ID 0 is used to indicate user is not logged in. - // Re-use filter logic to generate login page - print ( $login_form->handle_login_page ('') ); - } - }); + // Show login form at the shopping cart (if not logged in). + add_action( + 'woocommerce_before_checkout_billing_form', + function () use ( $login_form ) { + $user = wp_get_current_user(); + if ( 0 === $user->ID ) { + // ID 0 is used to indicate user is not logged in. + // Re-use filter logic to generate login page. + print ( esc_html( $login_form->handle_login_page( '' ) ) ); + } + } + ); - // Add action to set cookie to redirect back to current - // (checkout) page after OIDC provided the data - add_action( 'woocommerce_before_checkout_billing_form', - array( $login_form, 'handle_redirect_cookie' ) ); + // Add action to set cookie to redirect back to current + // (checkout) page after OIDC provided the data. + add_action( + 'woocommerce_before_checkout_billing_form', + array( $login_form, 'handle_redirect_cookie' ) + ); - } + } ); -// hook to convert OIDC fields to WC checkout fields if logged in -add_filter( 'woocommerce_checkout_fields', - 'filter_checkout_get_value' ); +// Hook to convert OIDC fields to WC checkout fields if logged in. +add_filter( + 'woocommerce_checkout_fields', + 'filter_checkout_get_value' +); /** @@ -48,72 +60,76 @@ add_filter( 'woocommerce_checkout_fields', * the information obtained from OpenID Connect (OIDC). WooCommerce * already grabs many of the values properly, but there are some fields * which we have to convert manually. + * + * @param array $in Data structure with shipping details to extend and return. + * + * @return array Enhanced shipping details. */ -function filter_checkout_get_value ($in=array()){ - $user = wp_get_current_user (); - if (0 != $user->ID) { - $token_response = $user->get ('openid-connect-generic-last-user-claim'); - if ( (isset( $token_response)) && - (array_key_exists ('given_name', $token_response)) ){ - $given_name = $token_response [ 'given_name' ]; - $in['billing']['billing_first_name']['default'] = $given_name; - $in['shipping']['shipping_first_name']['default'] = $given_name; - } - if ( (isset( $token_response)) && - (array_key_exists ('family_name', $token_response)) ){ - $family_name = $token_response [ 'family_name' ]; - $in['billing']['billing_last_name']['default'] = $family_name; - $in['shipping']['shipping_last_name']['default'] = $family_name; - } - if ( (isset( $token_response)) && - (array_key_exists ('phone_number', $token_response)) ){ - $phone_number = $token_response [ 'phone_number' ]; - $in['billing']['billing_phone']['default'] = $phone_number; - $in['shipping']['shipping_phone']['default'] = $phone_number; - } - if ( (isset( $token_response)) && - (array_key_exists ('address', $token_response)) ){ - $address = $token_response [ 'address' ]; - if ( array_key_exists( 'postal_code', $address ) ){ - $postal_code = $address [ 'postal_code' ]; - $in['billing']['billing_postcode']['default'] = $postal_code; - $in['shipping']['shipping_postcode']['default'] = $postal_code; - } - if ( array_key_exists( 'country', $address ) ){ - $country = $address [ 'country' ]; - $in['billing']['billing_country']['default'] = $country; - $in['shipping']['shipping_country']['default'] = $country; - } - if ( array_key_exists( 'locality', $address ) ){ - $locality = $address [ 'locality' ]; - $in['billing']['billing_city']['default'] = $locality; - $in['shipping']['shipping_city']['default'] = $locality; - } - if ( array_key_exists ( 'region', $address ) ){ - $region = $address [ 'region' ]; - $in['billing']['billing_state']['default'] = $region; - $in['shipping']['shipping_state']['default'] = $region; - } - if ( array_key_exists ( 'formatted', $address ) ){ - $formatted = $address [ 'formatted' ]; - $lines = explode ( '\r\n', $formatted ); - if ( count($lines) > 0 ){ - $in['billing']['billing_address_1']['default'] = $lines[0]; - $in['shipping']['shipping_address_1']['default'] = $lines[0]; +function filter_checkout_get_value( $in = array() ) { + $user = wp_get_current_user(); + if ( 0 !== $user->ID ) { + $token_response = $user->get( 'openid-connect-generic-last-user-claim' ); + if ( ( isset( $token_response ) ) && + ( array_key_exists( 'given_name', $token_response ) ) ) { + $given_name = $token_response ['given_name']; + $in['billing']['billing_first_name']['default'] = $given_name; + $in['shipping']['shipping_first_name']['default'] = $given_name; + } + if ( ( isset( $token_response ) ) && + ( array_key_exists( 'family_name', $token_response ) ) ) { + $family_name = $token_response ['family_name']; + $in['billing']['billing_last_name']['default'] = $family_name; + $in['shipping']['shipping_last_name']['default'] = $family_name; + } + if ( ( isset( $token_response ) ) && + ( array_key_exists( 'phone_number', $token_response ) ) ) { + $phone_number = $token_response ['phone_number']; + $in['billing']['billing_phone']['default'] = $phone_number; + $in['shipping']['shipping_phone']['default'] = $phone_number; + } + if ( ( isset( $token_response ) ) && + ( array_key_exists( 'address', $token_response ) ) ) { + $address = $token_response ['address']; + if ( array_key_exists( 'postal_code', $address ) ) { + $postal_code = $address ['postal_code']; + $in['billing']['billing_postcode']['default'] = $postal_code; + $in['shipping']['shipping_postcode']['default'] = $postal_code; + } + if ( array_key_exists( 'country', $address ) ) { + $country = $address ['country']; + $in['billing']['billing_country']['default'] = $country; + $in['shipping']['shipping_country']['default'] = $country; + } + if ( array_key_exists( 'locality', $address ) ) { + $locality = $address ['locality']; + $in['billing']['billing_city']['default'] = $locality; + $in['shipping']['shipping_city']['default'] = $locality; + } + if ( array_key_exists( 'region', $address ) ) { + $region = $address ['region']; + $in['billing']['billing_state']['default'] = $region; + $in['shipping']['shipping_state']['default'] = $region; + } + if ( array_key_exists( 'formatted', $address ) ) { + $formatted = $address ['formatted']; + $lines = explode( '\r\n', $formatted ); + if ( count( $lines ) > 0 ) { + $in['billing']['billing_address_1']['default'] = $lines[0]; + $in['shipping']['shipping_address_1']['default'] = $lines[0]; + } + if ( count( $lines ) > 1 ) { + $in['billing']['billing_address_2']['default'] = $lines[1]; + $in['shipping']['shipping_address_2']['default'] = $lines[1]; + } } - if ( count($lines) > 1 ){ - $in['billing']['billing_address_2']['default'] = $lines[1]; - $in['shipping']['shipping_address_2']['default'] = $lines[1]; + if ( array_key_exists( 'street_address', $address ) ) { + $street_address = $address ['street_address']; + if ( ! array_key_exists( 'formatted', $address ) ) { + $in['billing']['billing_address_1']['default'] = $street_address; + $in['shipping']['shipping_address_1']['default'] = $street_address; + } } - } - if ( array_key_exists ( 'street_address', $address ) ){ - $street_address = $address [ 'street_address' ]; - if ( ! array_key_exists ( 'formatted', $address ) ){ - $in['billing']['billing_address_1']['default'] = $street_address; - $in['shipping']['shipping_address_1']['default'] = $street_address; - } } } - } - return $in; + return $in; } -- cgit v1.2.3