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'; } }