mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Refactor style retrieval
This commit is contained in:
parent
a3fec63153
commit
ba4f46f683
1 changed files with 62 additions and 39 deletions
|
@ -986,30 +986,36 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
'mini_cart_wrapper' => '#ppc-button-minicart',
|
'mini_cart_wrapper' => '#ppc-button-minicart',
|
||||||
'is_mini_cart_disabled' => $this->is_button_disabled( 'mini-cart' ),
|
'is_mini_cart_disabled' => $this->is_button_disabled( 'mini-cart' ),
|
||||||
'cancel_wrapper' => '#ppcp-cancel',
|
'cancel_wrapper' => '#ppcp-cancel',
|
||||||
'mini_cart_style' => array(
|
'mini_cart_style' => $this->normalize_style(
|
||||||
|
array(
|
||||||
'layout' => $this->style_for_context( 'layout', 'mini-cart' ),
|
'layout' => $this->style_for_context( 'layout', 'mini-cart' ),
|
||||||
'color' => $this->style_for_context( 'color', 'mini-cart' ),
|
'color' => $this->style_for_context( 'color', 'mini-cart' ),
|
||||||
'shape' => $this->style_for_context( 'shape', 'mini-cart' ),
|
'shape' => $this->style_for_context( 'shape', 'mini-cart' ),
|
||||||
'label' => $this->style_for_context( 'label', 'mini-cart' ),
|
'label' => $this->style_for_context( 'label', 'mini-cart' ),
|
||||||
'tagline' => $this->style_for_context( 'tagline', 'mini-cart' ),
|
'tagline' => $this->style_for_context( 'tagline', 'mini-cart' ),
|
||||||
'height' => $this->settings->has( 'button_mini-cart_height' ) && $this->settings->get( 'button_mini-cart_height' ) ? $this->normalize_height( (int) $this->settings->get( 'button_mini-cart_height' ) ) : 35,
|
'height' => $this->normalize_height( $this->style_for_context( 'height', 'mini-cart', 35 ), 25, 55 ),
|
||||||
|
)
|
||||||
),
|
),
|
||||||
'style' => array(
|
'style' => $this->normalize_style(
|
||||||
|
array(
|
||||||
'layout' => $this->style_for_context( 'layout', $this->context() ),
|
'layout' => $this->style_for_context( 'layout', $this->context() ),
|
||||||
'color' => $this->style_for_context( 'color', $this->context() ),
|
'color' => $this->style_for_context( 'color', $this->context() ),
|
||||||
'shape' => $this->style_for_context( 'shape', $this->context() ),
|
'shape' => $this->style_for_context( 'shape', $this->context() ),
|
||||||
'label' => $this->style_for_context( 'label', $this->context() ),
|
'label' => $this->style_for_context( 'label', $this->context() ),
|
||||||
'tagline' => $this->style_for_context( 'tagline', $this->context() ),
|
'tagline' => $this->style_for_context( 'tagline', $this->context() ),
|
||||||
|
)
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
'separate_buttons' => array(
|
'separate_buttons' => array(
|
||||||
'card' => array(
|
'card' => array(
|
||||||
'id' => CardButtonGateway::ID,
|
'id' => CardButtonGateway::ID,
|
||||||
'wrapper' => '#ppc-button-' . CardButtonGateway::ID,
|
'wrapper' => '#ppc-button-' . CardButtonGateway::ID,
|
||||||
'style' => array(
|
'style' => $this->normalize_style(
|
||||||
|
array(
|
||||||
'shape' => $this->style_for_apm( 'shape', 'card' ),
|
'shape' => $this->style_for_apm( 'shape', 'card' ),
|
||||||
'color' => $this->style_for_apm( 'color', 'card', 'black' ),
|
'color' => $this->style_for_apm( 'color', 'card', 'black' ),
|
||||||
'layout' => $this->style_for_apm( 'poweredby_tagline', 'card', false ) === $this->normalize_style_value( true ) ? 'vertical' : 'horizontal',
|
'layout' => $this->style_for_apm( 'poweredby_tagline', 'card', false ) === $this->normalize_style_value( true ) ? 'vertical' : 'horizontal',
|
||||||
|
)
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1080,13 +1086,6 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
$localize['pay_now'] = $this->pay_now_script_data();
|
$localize['pay_now'] = $this->pay_now_script_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
|
|
||||||
$localize['button']['mini_cart_style']['tagline'] = false;
|
|
||||||
}
|
|
||||||
if ( $this->style_for_context( 'layout', $this->context() ) !== 'horizontal' ) {
|
|
||||||
$localize['button']['style']['tagline'] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $this->is_paypal_continuation() ) {
|
if ( $this->is_paypal_continuation() ) {
|
||||||
$order = $this->session_handler->order();
|
$order = $this->session_handler->order();
|
||||||
assert( $order !== null );
|
assert( $order !== null );
|
||||||
|
@ -1347,10 +1346,11 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
*
|
*
|
||||||
* @param string $style The name of the style property.
|
* @param string $style The name of the style property.
|
||||||
* @param string $context The context.
|
* @param string $context The context.
|
||||||
|
* @param ?mixed $default The default value.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string|int
|
||||||
*/
|
*/
|
||||||
private function style_for_context( string $style, string $context ): string {
|
private function style_for_context( string $style, string $context, $default = null ) {
|
||||||
// Use the cart/checkout styles for blocks.
|
// Use the cart/checkout styles for blocks.
|
||||||
$context = str_replace( '-block', '', $context );
|
$context = str_replace( '-block', '', $context );
|
||||||
|
|
||||||
|
@ -1370,6 +1370,7 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
|
|
||||||
return $this->get_style_value( "button_{$context}_${style}" )
|
return $this->get_style_value( "button_{$context}_${style}" )
|
||||||
?? $this->get_style_value( "button_${style}" )
|
?? $this->get_style_value( "button_${style}" )
|
||||||
|
?? ( $default ? $this->normalize_style_value( $default ) : null )
|
||||||
?? $this->normalize_style_value( $defaults[ $style ] ?? '' );
|
?? $this->normalize_style_value( $defaults[ $style ] ?? '' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1380,9 +1381,9 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
* @param string $apm The APM name, such as 'card'.
|
* @param string $apm The APM name, such as 'card'.
|
||||||
* @param ?mixed $default The default value.
|
* @param ?mixed $default The default value.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string|int
|
||||||
*/
|
*/
|
||||||
private function style_for_apm( string $style, string $apm, $default = null ): string {
|
private function style_for_apm( string $style, string $apm, $default = null ) {
|
||||||
return $this->get_style_value( "${apm}_button_${style}" )
|
return $this->get_style_value( "${apm}_button_${style}" )
|
||||||
?? ( $default ? $this->normalize_style_value( $default ) : null )
|
?? ( $default ? $this->normalize_style_value( $default ) : null )
|
||||||
?? $this->style_for_context( $style, 'checkout' );
|
?? $this->style_for_context( $style, 'checkout' );
|
||||||
|
@ -1392,9 +1393,9 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
* Returns the style property value or null.
|
* Returns the style property value or null.
|
||||||
*
|
*
|
||||||
* @param string $key The style property key in the settings.
|
* @param string $key The style property key in the settings.
|
||||||
* @return string|null
|
* @return string|int|null
|
||||||
*/
|
*/
|
||||||
private function get_style_value( string $key ): ?string {
|
private function get_style_value( string $key ) {
|
||||||
if ( ! $this->settings->has( $key ) ) {
|
if ( ! $this->settings->has( $key ) ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -1405,27 +1406,49 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
|
||||||
* Converts the style property value to string.
|
* Converts the style property value to string.
|
||||||
*
|
*
|
||||||
* @param mixed $value The style property value.
|
* @param mixed $value The style property value.
|
||||||
* @return string
|
* @return string|int
|
||||||
*/
|
*/
|
||||||
private function normalize_style_value( $value ): string {
|
private function normalize_style_value( $value ) {
|
||||||
if ( is_bool( $value ) ) {
|
if ( is_bool( $value ) ) {
|
||||||
$value = $value ? 'true' : 'false';
|
$value = $value ? 'true' : 'false';
|
||||||
}
|
}
|
||||||
|
if ( is_int( $value ) ) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
return (string) $value;
|
return (string) $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a value between 25 and 55.
|
* Fixes the style.
|
||||||
*
|
*
|
||||||
* @param int $height The input value.
|
* @param array $style The style properties.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function normalize_style( array $style ): array {
|
||||||
|
if ( array_key_exists( 'tagline', $style ) && ( ! array_key_exists( 'layout', $style ) || $style['layout'] !== 'horizontal' ) ) {
|
||||||
|
$style['tagline'] = false;
|
||||||
|
}
|
||||||
|
if ( array_key_exists( 'height', $style ) && ! $style['height'] ) {
|
||||||
|
unset( $style['height'] );
|
||||||
|
}
|
||||||
|
return $style;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a number between min and max.
|
||||||
|
*
|
||||||
|
* @param mixed $height The input value.
|
||||||
|
* @param int $min The minimum value.
|
||||||
|
* @param int $max The maximum value.
|
||||||
* @return int The normalized output value.
|
* @return int The normalized output value.
|
||||||
*/
|
*/
|
||||||
private function normalize_height( int $height ): int {
|
private function normalize_height( $height, int $min, int $max ): int {
|
||||||
if ( $height < 25 ) {
|
$height = (int) $height;
|
||||||
return 25;
|
if ( $height < $min ) {
|
||||||
|
return $min;
|
||||||
}
|
}
|
||||||
if ( $height > 55 ) {
|
if ( $height > $max ) {
|
||||||
return 55;
|
return $max;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $height;
|
return $height;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue