Automattic\WooCommerce\Internal\Admin\WCPayPromotion

Init{}WC 1.0

WooPayments Promotion engine.

Хуков нет.

Использование

$Init = new Init();
// use class methods

Методы

  1. public __construct()
  2. public add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_payment_method_promotions' ) )
  3. public if ( ! is_array( $specs ) || 0 === count( $specs ) )
  4. public isset( $_GET['page'] )
  5. ERROR: no method name found on line ``
  6. ERROR: no method name found on line `* @return array List of gateway classes.`
  7. ERROR: no method name found on line `// If the React-based Payments settings page is enabled, we don't need the old WooPayments promotion system,`
  8. ERROR: no method name found on line `*/`
  9. ERROR: no method name found on line `/* phpcs:disable WordPress.Security.NonceVerification */`
  10. public add_filter( 'woocommerce_payment_gateways', array( __CLASS__, 'possibly_register_pre_install_wc_pay_promotion_gateway' ) )
  11. public add_component_settings( $settings )
  12. ERROR: no method name found on line `// When specs are not empty but have errors, save for 3 hours.`
  13. ERROR: no method name found on line `* @return array`
  14. ERROR: no method name found on line `return $wcpay_promotion && 'woocommerce_payments:woopay' === $wcpay_promotion->id;`
  15. ERROR: no method name found on line `* Checks if promoted gateway can be registered.`
  16. public static load_payment_method_promotions()
  17. ERROR: no method name found on line `* For visibility, we want WooPayments to be at the top of the list.`
  18. ERROR: no method name found on line `*`
  19. public static can_show_promotion()
  20. ERROR: no method name found on line ``
  21. public static delete_specs_transient()
  22. ERROR: no method name found on line `}`
  23. ERROR: no method name found on line `$id = WCPaymentGatewayPreInstallWCPayPromotion::GATEWAY_ID;`
  24. ERROR: no method name found on line `return;`
  25. public static is_woopay_eligible()
  26. ERROR: no method name found on line `$specs_to_save = null;`
  27. ERROR: no method name found on line `* @param array $settings Component settings.`
  28. public get_user_locale()
  29. ERROR: no method name found on line `/**`
  30. ERROR: no method name found on line `* @return array Modified ordering.`
  31. public static get_promotions()
  32. ERROR: no method name found on line `return $gateways;`
  33. ERROR: no method name found on line `* @return array`
  34. ERROR: no method name found on line ``
  35. ERROR: no method name found on line `*`
  36. ERROR: no method name found on line ``
  37. public static get_cached_or_default_promotions()
  38. ERROR: no method name found on line ``
  39. ERROR: no method name found on line ``
  40. ERROR: no method name found on line `return;`
  41. ERROR: no method name found on line `// as we will show the WooPayments suggestion with the new system.`
  42. ERROR: no method name found on line `return $results['suggestions'];`
  43. ERROR: no method name found on line `* @return boolean If promoted gateway should be registered.`
  44. ERROR: no method name found on line ``
  45. ERROR: no method name found on line `// When specs are not empty but have errors, save for 3 hours.`
  46. ERROR: no method name found on line ``
  47. public static get_wc_pay_promotion_spec( $fetch_from_remote = true )
  48. public static get_specs()
  49. ERROR: no method name found on line ``
  50. ERROR: no method name found on line `}`
  51. ERROR: no method name found on line `}`
  52. ERROR: no method name found on line ``
  53. ERROR: no method name found on line `*`
  54. ERROR: no method name found on line `* Possibly registers the pre-install WooPayments promoted gateway.`
  55. ERROR: no method name found on line `*/`
  56. ERROR: no method name found on line `* @param array $gateways List of gateway classes.`
  57. ERROR: no method name found on line ``
  58. ERROR: no method name found on line `/**`
  59. public static possibly_register_pre_install_wc_pay_promotion_gateway( $gateways )
  60. ERROR: no method name found on line ``
  61. ERROR: no method name found on line ``
  62. ERROR: no method name found on line ``
  63. ERROR: no method name found on line ``
  64. ERROR: no method name found on line `return $gateways;`
  65. ERROR: no method name found on line ``
  66. public static set_gateway_top_of_list( $ordering )
  67. ERROR: no method name found on line ``
  68. ERROR: no method name found on line `*`
  69. ERROR: no method name found on line `*`
  70. ERROR: no method name found on line `* @return array List of promotions.`

Код Init{} WC 9.6.0

class Init extends RemoteSpecsEngine {
	/**
	 * Constructor.
	 */
	public function __construct() {
		// If the React-based Payments settings page is enabled, we don't need the old WooPayments promotion system,
		// as we will show the WooPayments suggestion with the new system.
		if ( Features::is_enabled( 'reactify-classic-payments-settings' ) ) {
			return;
		}

		/* phpcs:disable WordPress.Security.NonceVerification */
		$is_payments_setting_page = isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'];
		$is_wc_admin_page         = isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'];

		if ( $is_payments_setting_page || $is_wc_admin_page ) {
			add_filter( 'woocommerce_admin_shared_settings', array( $this, 'add_component_settings' ) );
		}

		if ( ! wp_is_json_request() && ! $is_payments_setting_page ) {
			return;
		}

		add_filter( 'woocommerce_payment_gateways', array( __CLASS__, 'possibly_register_pre_install_wc_pay_promotion_gateway' ) );
		add_filter( 'option_woocommerce_gateway_order', array( __CLASS__, 'set_gateway_top_of_list' ) );
		add_filter( 'default_option_woocommerce_gateway_order', array( __CLASS__, 'set_gateway_top_of_list' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_payment_method_promotions' ) );
		add_action( 'update_option_woocommerce_default_country', array( $this, 'delete_specs_transient' ) );
	}

	/**
	 * Possibly registers the pre-install WooPayments promoted gateway.
	 *
	 * @param array $gateways List of gateway classes.
	 *
	 * @return array List of gateway classes.
	 */
	public static function possibly_register_pre_install_wc_pay_promotion_gateway( $gateways ) {
		if ( self::can_show_promotion() && ! WCPaymentGatewayPreInstallWCPayPromotion::is_dismissed() ) {
			$gateways[] = 'Automattic\WooCommerce\Internal\Admin\WCPayPromotion\WCPaymentGatewayPreInstallWCPayPromotion';
		}
		return $gateways;
	}

	/**
	 * Checks if promoted gateway can be registered.
	 *
	 * @return boolean If promoted gateway should be registered.
	 */
	public static function can_show_promotion() {
		// Don't show if WooPayments is enabled.
		if ( class_exists( '\WC_Payments' ) ) {
			return false;
		}

		// Don't show if there is no WooPayments promotion spec.
		$wc_pay_spec = self::get_wc_pay_promotion_spec();
		if ( ! $wc_pay_spec ) {
			return false;
		}

		return true;
	}

	/**
	 * By default, new payment gateways are put at the bottom of the list on the admin "Payments" settings screen.
	 * For visibility, we want WooPayments to be at the top of the list.
	 *
	 * @param array $ordering Existing ordering of the payment gateways.
	 *
	 * @return array Modified ordering.
	 */
	public static function set_gateway_top_of_list( $ordering ) {
		$ordering = (array) $ordering;
		$id       = WCPaymentGatewayPreInstallWCPayPromotion::GATEWAY_ID;
		// Only tweak the ordering if the list hasn't been reordered with WooCommerce Payments in it already.
		if ( ! isset( $ordering[ $id ] ) || ! is_numeric( $ordering[ $id ] ) ) {
			$is_empty        = empty( $ordering ) || ( count( $ordering ) === 1 && $ordering[0] === false );
			$ordering[ $id ] = $is_empty ? 0 : ( min( $ordering ) - 1 );
		}
		return $ordering;
	}

	/**
	 * Get WooPayments promotion spec.
	 *
	 * @param boolean $fetch_from_remote Whether to fetch the spec from remote or not.
	 *
	 * @return object|false WooPayments promotion spec or false if there isn't one.
	 */
	public static function get_wc_pay_promotion_spec( $fetch_from_remote = true ) {
		$promotions            = $fetch_from_remote ? self::get_promotions() : self::get_cached_or_default_promotions();
		$wc_pay_promotion_spec = array_values(
			array_filter(
				$promotions,
				function ( $promotion ) {
					return isset( $promotion->plugins ) && in_array( 'woocommerce-payments', $promotion->plugins, true );
				}
			)
		);

		return current( $wc_pay_promotion_spec );
	}

	/**
	 * Go through the specs and run them.
	 *
	 * @return array List of promotions.
	 */
	public static function get_promotions() {
		$locale = get_user_locale();

		$specs           = self::get_specs();
		$results         = EvaluateSuggestion::evaluate_specs( $specs, array( 'source' => 'wc-wcpay-promotions' ) );
		$specs_to_return = $results['suggestions'];
		$specs_to_save   = null;

		if ( empty( $specs_to_return ) ) {
			// When specs are empty, replace it with defaults and save for 3 hours.
			$specs_to_save   = DefaultPromotions::get_all();
			$specs_to_return = EvaluateSuggestion::evaluate_specs( $specs_to_save )['suggestions'];
		} elseif ( count( $results['errors'] ) > 0 ) {
			// When specs are not empty but have errors, save for 3 hours.
			$specs_to_save = $specs;
		}

		if ( count( $results['errors'] ) > 0 ) {
			self::log_errors( $results['errors'] );
		}

		if ( $specs_to_save ) {
			WCPayPromotionDataSourcePoller::get_instance()->set_specs_transient( array( $locale => $specs_to_save ), 3 * HOUR_IN_SECONDS );
		}

		return $specs_to_return;
	}

	/**
	 * Gets either cached or default promotions.
	 *
	 * @return array
	 */
	public static function get_cached_or_default_promotions() {
		$specs = 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' )
			? DefaultPromotions::get_all()
			: WCPayPromotionDataSourcePoller::get_instance()->get_cached_specs();

		if ( ! is_array( $specs ) || 0 === count( $specs ) ) {
			$specs = DefaultPromotions::get_all();
		}
		$results = EvaluateSuggestion::evaluate_specs( $specs, array( 'source' => 'wc-wcpay-promotions' ) );
		return $results['suggestions'];
	}

	/**
	 * Get merchant WooPay eligibility.
	 *
	 * @return boolean If merchant is eligible for WooPay.
	 */
	public static function is_woopay_eligible() {
		$wcpay_promotion = self::get_wc_pay_promotion_spec( false );

		return $wcpay_promotion && 'woocommerce_payments:woopay' === $wcpay_promotion->id;
	}

	/**
	 * Delete the specs transient.
	 */
	public static function delete_specs_transient() {
		WCPayPromotionDataSourcePoller::get_instance()->delete_specs_transient();
	}

	/**
	 * Get specs or fetch remotely if they don't exist.
	 *
	 * @return array List of specs.
	 */
	public static function get_specs() {
		if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no' ) {
			return DefaultPromotions::get_all();
		}

		$specs = WCPayPromotionDataSourcePoller::get_instance()->get_specs_from_data_sources();
		// On empty remote specs, fallback to default ones.
		if ( ! is_array( $specs ) || 0 === count( $specs ) ) {
			$specs = DefaultPromotions::get_all();
		}

		return $specs;
	}

	/**
	 * Add component settings.
	 *
	 * @param array $settings Component settings.
	 *
	 * @return array
	 */
	public function add_component_settings( $settings ) {
		$settings['isWooPayEligible'] = self::is_woopay_eligible();
		return $settings;
	}

	/**
	 * Loads the payment method promotions scripts and styles.
	 */
	public static function load_payment_method_promotions() {
		WCAdminAssets::register_style( 'payment-method-promotions', 'style', array( 'wp-components' ) );
		WCAdminAssets::register_script( 'wp-admin-scripts', 'payment-method-promotions', true );
	}
}