DefaultPaymentGateways.php
1 year ago
EvaluateSuggestion.php
1 year ago
Init.php
1 year ago
PaymentGatewaySuggestionsDataSourcePoller.php
1 year ago
PaymentGatewaysController.php
4 years ago
Init.php
137 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles running payment gateway suggestion specs |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\RemoteSpecs\RemoteSpecsEngine; |
| 11 | |
| 12 | /** |
| 13 | * Remote Payment Methods engine. |
| 14 | * This goes through the specs and gets eligible payment gateways. |
| 15 | */ |
| 16 | class Init extends RemoteSpecsEngine { |
| 17 | /** |
| 18 | * Option name for dismissed payment method suggestions. |
| 19 | */ |
| 20 | const RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION = 'woocommerce_setting_payments_recommendations_hidden'; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | PaymentGatewaysController::init(); |
| 27 | add_action( 'update_option_woocommerce_default_country', array( $this, 'delete_specs_transient' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Go through the specs and run them. |
| 32 | * |
| 33 | * @param array|null $specs payment suggestion spec array. |
| 34 | * @return array |
| 35 | */ |
| 36 | public static function get_suggestions( ?array $specs = null ) { |
| 37 | $locale = get_user_locale(); |
| 38 | |
| 39 | $specs = is_array( $specs ) ? $specs : self::get_specs(); |
| 40 | $results = EvaluateSuggestion::evaluate_specs( $specs ); |
| 41 | $specs_to_return = $results['suggestions']; |
| 42 | $specs_to_save = null; |
| 43 | |
| 44 | if ( empty( $specs_to_return ) ) { |
| 45 | // When suggestions is empty, replace it with defaults and save for 3 hours. |
| 46 | $specs_to_save = DefaultPaymentGateways::get_all(); |
| 47 | $specs_to_return = EvaluateSuggestion::evaluate_specs( $specs_to_save )['suggestions']; |
| 48 | } elseif ( count( $results['errors'] ) > 0 ) { |
| 49 | // When suggestions is not empty but has errors, save it for 3 hours. |
| 50 | $specs_to_save = $specs; |
| 51 | } |
| 52 | |
| 53 | if ( count( $results['errors'] ) > 0 ) { |
| 54 | self::log_errors( $results['errors'] ); |
| 55 | } |
| 56 | |
| 57 | if ( $specs_to_save ) { |
| 58 | PaymentGatewaySuggestionsDataSourcePoller::get_instance()->set_specs_transient( array( $locale => $specs_to_save ), 3 * HOUR_IN_SECONDS ); |
| 59 | } |
| 60 | |
| 61 | return $specs_to_return; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets either cached or default suggestions. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | public static function get_cached_or_default_suggestions() { |
| 70 | $specs = 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) |
| 71 | ? DefaultPaymentGateways::get_all() |
| 72 | : PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_cached_specs(); |
| 73 | |
| 74 | if ( ! is_array( $specs ) || 0 === count( $specs ) ) { |
| 75 | $specs = DefaultPaymentGateways::get_all(); |
| 76 | } |
| 77 | /** |
| 78 | * Allows filtering of payment gateway suggestion specs |
| 79 | * |
| 80 | * @since 6.4.0 |
| 81 | * |
| 82 | * @param array Gateway specs. |
| 83 | */ |
| 84 | $specs = apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', $specs ); |
| 85 | $results = EvaluateSuggestion::evaluate_specs( $specs ); |
| 86 | return $results['suggestions']; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Delete the specs transient. |
| 91 | */ |
| 92 | public static function delete_specs_transient() { |
| 93 | PaymentGatewaySuggestionsDataSourcePoller::get_instance()->delete_specs_transient(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get specs or fetch remotely if they don't exist. |
| 98 | */ |
| 99 | public static function get_specs() { |
| 100 | if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) { |
| 101 | return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', DefaultPaymentGateways::get_all() ); |
| 102 | } |
| 103 | $specs = PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 104 | |
| 105 | // Fetch specs if they don't yet exist. |
| 106 | if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) { |
| 107 | return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', DefaultPaymentGateways::get_all() ); |
| 108 | } |
| 109 | |
| 110 | return apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', $specs ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check if suggestions should be shown in the settings screen. |
| 115 | * |
| 116 | * @return bool |
| 117 | */ |
| 118 | public static function should_display() { |
| 119 | if ( 'yes' === get_option( self::RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION, 'no' ) ) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return apply_filters( 'woocommerce_allow_payment_recommendations', true ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Dismiss the suggestions. |
| 132 | */ |
| 133 | public static function dismiss() { |
| 134 | return update_option( self::RECOMMENDED_PAYMENT_PLUGINS_DISMISS_OPTION, 'yes' ); |
| 135 | } |
| 136 | } |
| 137 |