swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Controls the UPE compatibility before we release UPE - adds some notices for the merchant if necessary. |
| 9 | * |
| 10 | * @since 5.5.0 |
| 11 | */ |
| 12 | class WC_Stripe_UPE_Compatibility_Controller { |
| 13 | public function __construct() { |
| 14 | add_action( 'admin_notices', [ $this, 'add_compatibility_notice' ] ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * I created this as a separate method, so it can be mocked in unit tests. |
| 19 | * |
| 20 | * @retun string |
| 21 | */ |
| 22 | public function get_wc_version() { |
| 23 | return WC_VERSION; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Adds a compatibility notice in the `wp-admin` area in case the version of WooCommerce or WordPress are (or will be) not supported. |
| 28 | */ |
| 29 | public function add_compatibility_notice() { |
| 30 | /* |
| 31 | * The following might be hard to read, but here's what I'm trying to do: |
| 32 | * - If WP and WC are both supported -> nothing to do |
| 33 | * - If WC is not supported -> construct message saying "Stripe requires WooCommerce 5.4 or greater to be installed and active. Your version of WooCommerce [X.X] is no longer be supported" |
| 34 | * - If WP is not supported -> construct message saying "Stripe requires WordPress 5.6 or greater. Your version of WordPress [X.X] is no longer be supported" |
| 35 | * - If WC & WP are both not supported -> construct message saying "Stripe requires WordPress 5.6 or greater and WooCommerce 5.4 or greater to be installed and active. Your versions of WordPress [X.X] and WooCommerce [X.X] are no longer be supported" |
| 36 | */ |
| 37 | $unsatisfied_requirements = array_filter( |
| 38 | [ |
| 39 | [ |
| 40 | 'name' => 'WordPress', |
| 41 | 'version' => get_bloginfo( 'version' ), |
| 42 | 'is_supported' => WC_Stripe_UPE_Compatibility::is_wp_supported(), |
| 43 | /* translators: %s. WordPress version installed. */ |
| 44 | 'message' => sprintf( __( 'WordPress %s or greater', 'woocommerce-gateway-stripe' ), WC_Stripe_UPE_Compatibility::MIN_WP_VERSION ), |
| 45 | ], |
| 46 | [ |
| 47 | 'name' => 'WooCommerce', |
| 48 | 'version' => $this->get_wc_version(), |
| 49 | 'is_supported' => version_compare( $this->get_wc_version(), WC_Stripe_UPE_Compatibility::MIN_WC_VERSION, '>=' ), |
| 50 | 'message' => sprintf( |
| 51 | /* translators: %s. WooCommerce version installed. */ |
| 52 | __( |
| 53 | 'WooCommerce %s or greater to be installed and active', |
| 54 | 'woocommerce-gateway-stripe' |
| 55 | ), |
| 56 | WC_Stripe_UPE_Compatibility::MIN_WC_VERSION |
| 57 | ), |
| 58 | ], |
| 59 | ], |
| 60 | function ( $requirement ) { |
| 61 | return ! $requirement['is_supported']; |
| 62 | } |
| 63 | ); |
| 64 | |
| 65 | if ( count( $unsatisfied_requirements ) === 0 ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $this->show_current_compatibility_notice( $unsatisfied_requirements ); |
| 70 | } |
| 71 | |
| 72 | private function get_installed_versions_message( $unsatisfied_requirements ) { |
| 73 | return join( |
| 74 | __( ' and ', 'woocommerce-gateway-stripe' ), |
| 75 | array_map( |
| 76 | function ( $requirement ) { |
| 77 | return $requirement['name'] . ' ' . $requirement['version']; |
| 78 | }, |
| 79 | $unsatisfied_requirements |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | private function get_unsatisfied_requirements_message( $unsatisfied_requirements ) { |
| 85 | return join( |
| 86 | __( ' and ', 'woocommerce-gateway-stripe' ), |
| 87 | array_map( |
| 88 | function ( $requirement ) { |
| 89 | return $requirement['message']; |
| 90 | }, |
| 91 | $unsatisfied_requirements |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | private function show_current_compatibility_notice( $unsatisfied_requirements ) { |
| 97 | /* |
| 98 | * The following might be hard to read, but here's what I'm trying to do: |
| 99 | * - If WP and WC are both supported -> nothing to do |
| 100 | * - If WC is not supported -> construct message saying "WooCommerce Stripe requires WooCommerce 5.4 or greater to be installed and active. Your version of WooCommerce [X.X] is no longer supported" |
| 101 | * - If WP is not supported -> construct message saying "WooCommerce Stripe requires WordPress 5.6 or greater. Your version of WordPress [X.X] is no longer supported" |
| 102 | * - If WC & WP are both not supported -> construct message saying "WooCommerce Stripe requires WordPress 5.6 or greater and WooCommerce 5.4 or greater to be installed and active. Your versions of WordPress [X.X] and WooCommerce [X.X] are no longer supported" |
| 103 | */ |
| 104 | $unsatisfied_requirements_message = $this->get_unsatisfied_requirements_message( $unsatisfied_requirements ); |
| 105 | |
| 106 | $unsatisfied_requirements_versions = $this->get_installed_versions_message( $unsatisfied_requirements ); |
| 107 | |
| 108 | echo '<div class="error"><p><strong>'; |
| 109 | echo wp_kses_post( |
| 110 | sprintf( |
| 111 | /* translators: $1. Minimum WooCommerce and/or WordPress versions. $2. Current WooCommerce and/or versions. $3 Learn more link. */ |
| 112 | _n( |
| 113 | 'WooCommerce Stripe requires %1$s. Your version of %2$s is no longer supported.', |
| 114 | 'WooCommerce Stripe requires %1$s. Your versions of %2$s are no longer supported.', |
| 115 | count( $unsatisfied_requirements ), |
| 116 | 'woocommerce-gateway-stripe' |
| 117 | ), |
| 118 | $unsatisfied_requirements_message, |
| 119 | $unsatisfied_requirements_versions |
| 120 | ) |
| 121 | ); |
| 122 | echo '</strong></p></div>'; |
| 123 | } |
| 124 | } |