• File: PaymentSettingsScreen.php
  • Full Path: /home/slvgwrnz/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/Settings/PaymentSettingsScreen.php
  • Date Modified: 10/26/2026 10:38 AM
  • File size: 3.78 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
/**
 * Loads the experimental payment settings screen.
 */

declare( strict_types=1 );

namespace Automattic\WooCommerce\Internal\Admin\Settings;

use Automattic\WooCommerce\Admin\Features\Features;

defined( 'ABSPATH' ) || exit;

/**
 * Registers the wp-build admin page for payment gateway settings screens.
 *
 * This class is the only code that loads the build output in
 * `assets/client/routes`, which defines wp-build's generated functions.
 *
 * @internal
 */
final class PaymentSettingsScreen {

	/**
	 * Admin page slug. wp-build's generated code only loads its assets on this page.
	 */
	public const PAGE_SLUG = 'wc-payment-settings-wp-admin';

	/**
	 * Feature flag that controls the screen until it is released.
	 */
	private const FEATURE_FLAG = 'payment-settings-screen';

	/**
	 * Name of the render function generated by wp-build.
	 */
	private const RENDER_FUNCTION = 'woocommerce_routes_wc_payment_settings_wp_admin_render_page';

	/**
	 * Register hooks.
	 *
	 * @internal
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'handle_init' ) );
	}

	/**
	 * Load the generated page when the screen is available.
	 *
	 * @internal
	 */
	public function handle_init(): void {
		if ( ! is_admin() || is_network_admin() || ! $this->is_available() ) {
			return;
		}

		require_once $this->get_build_file();
		add_action( 'admin_menu', array( $this, 'handle_admin_menu' ) );
		add_action( 'admin_head', array( $this, 'handle_admin_head' ) );
		add_filter( 'submenu_file', array( $this, 'handle_submenu_file' ) );
	}

	/**
	 * Whether the screen can be used on this site.
	 *
	 * It needs the feature flag, WordPress's View Config and Fields APIs, and the build output.
	 *
	 * @since 11.3.0
	 *
	 * @return bool
	 */
	public function is_available(): bool {
		return Features::is_enabled( self::FEATURE_FLAG )
			&& function_exists( 'wp_get_entity_view_config' )
			&& ( class_exists( 'WP_Fields_Registry' ) || class_exists( 'Gutenberg_Fields_Registry' ) )
			&& is_readable( $this->get_build_file() );
	}

	/**
	 * Register the page under WooCommerce so the menu opens on it.
	 *
	 * @internal
	 */
	public function handle_admin_menu(): void {
		if ( ! function_exists( self::RENDER_FUNCTION ) ) {
			return;
		}

		add_submenu_page(
			'woocommerce',
			__( 'Payment settings', 'woocommerce' ),
			__( 'Payment settings', 'woocommerce' ),
			'manage_woocommerce',
			self::PAGE_SLUG,
			array( $this, 'render_page' )
		);
	}

	/**
	 * Render the page through wp-build's generated function.
	 *
	 * @internal
	 */
	public function render_page(): void {
		if ( function_exists( self::RENDER_FUNCTION ) ) {
			// @phpstan-ignore argument.type (wp-build generates this function in the build output, which PHPStan does not load.)
			call_user_func( self::RENDER_FUNCTION );
		}
	}

	/**
	 * Remove the page's own menu item; gateways link to the screen directly.
	 *
	 * Removing it after the menu is built keeps WooCommerce as the page's parent.
	 *
	 * @internal
	 */
	public function handle_admin_head(): void {
		remove_submenu_page( 'woocommerce', self::PAGE_SLUG );
	}

	/**
	 * Highlight WooCommerce > Settings on the screen.
	 *
	 * @internal
	 *
	 * @param mixed $submenu_file The submenu file.
	 * @return mixed
	 */
	public function handle_submenu_file( $submenu_file ) {
		return $this->is_current_page() ? 'wc-settings' : $submenu_file;
	}

	/**
	 * Whether the current admin request is for the screen.
	 *
	 * @return bool
	 */
	private function is_current_page(): bool {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only compares the page slug.
		return isset( $_GET['page'] ) && self::PAGE_SLUG === $_GET['page'];
	}

	/**
	 * Path to the wp-build entry point.
	 *
	 * @return string
	 */
	private function get_build_file(): string {
		return WC_ABSPATH . 'assets/client/routes/build.php';
	}
}