swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * Class Allowed_Payment_Request_Button_Types_Update |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * Class Allowed_Payment_Request_Button_Types_Update |
| 10 | * |
| 11 | * Remaps deprecated payment request button types to fallback values. |
| 12 | * |
| 13 | * @since 5.6.0 |
| 14 | */ |
| 15 | class Allowed_Payment_Request_Button_Types_Update { |
| 16 | /** |
| 17 | * Allowed_Payment_Request_Button_Types_Update constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'woocommerce_stripe_updated', [ $this, 'maybe_migrate' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Only execute the migration if not applied yet. |
| 25 | */ |
| 26 | public function maybe_migrate() { |
| 27 | // not compatible with older WC versions, due to the missing `update_option` method on the gateway class |
| 28 | if ( version_compare( WC_VERSION, '3.4.0', '<' ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $stripe_gateway = $this->get_gateway(); |
| 33 | |
| 34 | // "custom" or "branded" are no longer valid values for the button type - map them to new ones |
| 35 | $button_type = $stripe_gateway->get_option( 'payment_request_button_type' ); |
| 36 | if ( in_array( $button_type, [ 'branded', 'custom' ], true ) ) { |
| 37 | $branded_type = $stripe_gateway->get_option( 'payment_request_button_branded_type' ); |
| 38 | $stripe_gateway->update_option( |
| 39 | 'payment_request_button_type', |
| 40 | $this->map_button_type( $button_type, $branded_type ) |
| 41 | ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Maps deprecated button types to fallback values. |
| 47 | * |
| 48 | * @param mixed $button_type "payment_request_button_type" value. |
| 49 | * @param mixed $branded_type "payment_request_button_branded_type" value. |
| 50 | * |
| 51 | * @return mixed |
| 52 | */ |
| 53 | private function map_button_type( $button_type, $branded_type ) { |
| 54 | // "branded" with "logo only" => "default" (same result) |
| 55 | if ( 'branded' === $button_type && 'short' === $branded_type ) { |
| 56 | return 'default'; |
| 57 | } |
| 58 | |
| 59 | // "branded" with anything else (which would be "long"/"Text and logo") => "buy" |
| 60 | if ( 'branded' === $button_type ) { |
| 61 | return 'buy'; |
| 62 | } |
| 63 | |
| 64 | // "custom" is no longer valid => "default" |
| 65 | if ( 'custom' === $button_type ) { |
| 66 | return 'buy'; |
| 67 | } |
| 68 | |
| 69 | // anything else is good |
| 70 | return $button_type; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the main Stripe payment gateways. |
| 75 | * |
| 76 | * @return WC_Stripe_Payment_Gateway |
| 77 | */ |
| 78 | public function get_gateway() { |
| 79 | return woocommerce_gateway_stripe()->get_main_stripe_gateway(); |
| 80 | } |
| 81 | } |