id, array( $this, 'output_sections' ) ); add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); add_action( 'woocommerce_admin_field_add_settings_slot', array( $this, 'add_settings_slot' ) ); add_filter( 'admin_body_class', array( $this, 'add_settings_ui_body_class' ) ); } /** * Get settings page ID. * * @since 3.0.0 * @return string */ public function get_id() { return $this->id; } /** * Get settings page label. * * @since 3.0.0 * @return string */ public function get_label() { return $this->label; } /** * Get the settings UI page adapter for this settings page. * * Settings pages can override this to opt in to the settings UI renderer * while retaining the classic WooCommerce settings page route and save flow. * * @since 10.9.0 * @return SettingsUIPageInterface|null */ public function get_settings_ui_page(): ?SettingsUIPageInterface { return null; } /** * Add a body class for settings pages rendered through the settings UI. * * @since 10.9.0 * * @param string $classes The existing body classes for the admin area. * @return string The modified body classes for the admin area. */ public function add_settings_ui_body_class( $classes ) { global $current_section, $current_tab; if ( ! is_string( $classes ) || $this->id !== $current_tab ) { return $classes; } $section = is_string( $current_section ) ? $current_section : ''; $context = $this->get_settings_ui_request_context( $section ); try { if ( ! $context || ! $context->is_rendering_enabled() ) { return $classes; } // Preflight and cache adapter failures before this body class can hide the classic Save button. $context->get_page_id(); // The legacy fallback renderer needs the classic styling: the settings UI // body class hides the legacy Save button via CSS. if ( $context->has_schema_failed() || $context->has_script_handle_loading_failed() ) { return $classes; } $is_rendering_drill_down = $context->is_drill_down(); } catch ( \Throwable $e ) { return $classes; } $body_classes = explode( ' ', $classes ); if ( ! in_array( 'woocommerce-settings-ui-page', $body_classes, true ) ) { $classes .= ' woocommerce-settings-ui-page'; $body_classes[] = 'woocommerce-settings-ui-page'; } if ( $is_rendering_drill_down && ! in_array( 'woocommerce-settings-ui-drill-down', $body_classes, true ) ) { $classes .= ' woocommerce-settings-ui-drill-down'; } return $classes; } /** * Log a developer-facing notice when settings UI rendering falls back to the legacy renderer. * * @since 10.9.0 * * @param string $page_id Settings UI page id. * @param string $section_id Section id. * @param string $reason Fallback reason. */ private function log_settings_ui_fallback( string $page_id, string $section_id, string $reason ): void { wc_doing_it_wrong( 'WC_Settings_Page::output', sprintf( /* translators: 1: settings page id, 2: settings section id, 3: fallback reason. */ __( 'Settings UI rendering for page "%1$s" section "%2$s" fell back to the legacy settings renderer. Reason: %3$s', 'woocommerce' ), $page_id, '' === $section_id ? 'default' : $section_id, $reason ), '10.9.0' ); } /** * Creates the React mount point for settings slot. */ public function add_settings_slot() { ?>
id ] = $this->label; return $pages; } /** * Get settings array for the default section. * * External settings classes (registered via 'woocommerce_get_settings_pages' filter) * might have redefined this method as "get_settings($section_id='')", thus we need * to use this method internally instead of 'get_settings_for_section' to register settings * and render settings pages. * * *But* we can't just redefine the method as "get_settings($section_id='')" here, since this * will break on PHP 8 if any external setting class have it as 'get_settings()'. * * Thus we leave the method signature as is and use 'func_get_arg' to get the setting id * if it's supplied, and we use this method internally; but it's deprecated and should * otherwise never be used. * * @deprecated 5.4.0 Use 'get_settings_for_section' (passing an empty string for default section) * * @return array Settings array, each item being an associative array representing a setting. */ public function get_settings() { $section_id = 0 === func_num_args() ? '' : func_get_arg( 0 ); return $this->get_settings_for_section( $section_id ); } /** * Get settings array. * * The strategy for getting the settings is as follows: * * - If a method named 'get_settings_for_{section_id}_section' exists in the class * it will be invoked (for the default '' section, the method name is 'get_settings_for_default_section'). * Derived classes can implement these methods as required. * * - Otherwise, 'get_settings_for_section_core' will be invoked. Derived classes can override it * as an alternative to implementing 'get_settings_for_{section_id}_section' methods. * * @param string $section_id The id of the section to return settings for, an empty string for the default section. * * @return array Settings array, each item being an associative array representing a setting. */ final public function get_settings_for_section( $section_id ) { if ( '' === $section_id ) { $method_name = 'get_settings_for_default_section'; } else { $method_name = "get_settings_for_{$section_id}_section"; } if ( method_exists( $this, $method_name ) ) { $settings = $this->$method_name(); } else { $settings = $this->get_settings_for_section_core( $section_id ); } return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $section_id ); } /** * Settings section registry instance, or null when the modern settings SDK is unavailable. * * The class can be missing mid-update: a 10.9 copy of this file may load before the autoloader * class map can safely resolve the new registry, so a direct call would fatal. * * @return SettingsSectionRegistry|null */ protected function get_settings_section_registry() { try { if ( ! class_exists( SettingsSectionRegistry::class ) ) { return null; } return SettingsSectionRegistry::get_instance(); } catch ( \Throwable $e ) { return null; } } /** * Settings UI request context, or null when the modern settings SDK is unavailable. * * @param string $section Section id. * @return SettingsUIRequestContext|null */ protected function get_settings_ui_request_context( $section ) { try { if ( ! class_exists( SettingsUIRequestContext::class ) ) { return null; } return SettingsUIRequestContext::for_settings_page( $this, $section ); } catch ( \Throwable $e ) { return null; } } /** * Get the settings for a given section. * This method is invoked from 'get_settings_for_section' when no 'get_settings_for_{current_section}_section' * method exists in the class. * * When overriding, note that the 'woocommerce_get_settings_' filter must NOT be triggered, * as this is already done by 'get_settings_for_section'. * * @param string $section_id The section name to get the settings for. * * @return array Settings array, each item being an associative array representing a setting. */ protected function get_settings_for_section_core( $section_id ) { $registry = $this->get_settings_section_registry(); $registered_section = $registry ? $registry->get_registered( $this->id, (string) $section_id ) : null; return $registered_section ? $registered_section->get_settings( $this ) : array(); } /** * Get all sections for this page, both the own ones and the ones defined via filters. * * @return array */ public function get_sections() { $sections = $this->get_own_sections(); $registry = $this->get_settings_section_registry(); $registered_sections = $registry ? $registry->get_sections_for_page( $this->id ) : array(); foreach ( $registered_sections as $section_id => $section_label ) { // Preserve sections declared by the settings page when a registered section uses the same id. if ( array_key_exists( $section_id, $sections ) ) { continue; } $sections[ $section_id ] = $section_label; } /** * Filters the sections for this settings page. * * @since 2.2.0 * @param array $sections The sections for this settings page. */ return (array) apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); } /** * Get own sections for this page. * Derived classes should override this method if they define sections. * There should always be one default section with an empty string as identifier. * * Example: * return array( * '' => __( 'General', 'woocommerce' ), * 'foobars' => __( 'Foos & Bars', 'woocommerce' ), * ); * * @return array An associative array where keys are section identifiers and the values are translated section names. */ protected function get_own_sections() { return array( '' => __( 'General', 'woocommerce' ) ); } /** * Output sections. */ public function output_sections() { global $current_section; $sections = $this->get_sections(); if ( empty( $sections ) || 1 === count( $sections ) ) { return; } echo '
'; } /** * Output the HTML for the settings. */ public function output() { global $current_section; $section = is_string( $current_section ) ? $current_section : ''; $context = $this->get_settings_ui_request_context( $section ); $hide_save_button_overridden = false; $hide_save_button_existed = array_key_exists( 'hide_save_button', $GLOBALS ); $previous_hide_save_button = $hide_save_button_existed ? $GLOBALS['hide_save_button'] : null; try { if ( $context && $context->is_rendering_enabled() ) { $page_id = $context->get_page_id(); if ( $context->has_schema_failed() ) { $schema_failure_reason = method_exists( $context, 'get_schema_failure_reason' ) ? $context->get_schema_failure_reason() : __( 'Settings UI schema generation failed.', 'woocommerce' ); $this->log_settings_ui_fallback( $page_id, $section, $schema_failure_reason ); } else { $mount_id = 'wc_settings_ui_' . sanitize_html_class( $this->id ) . '_' . sanitize_html_class( '' === $section ? 'default' : $section ); $context->enqueue_script_handles(); if ( $context->has_script_handle_loading_failed() ) { $this->log_settings_ui_fallback( $page_id, $section, $context->get_script_handles_failure_reason() ); } else { $GLOBALS['hide_save_button'] = true; $hide_save_button_overridden = true; printf( '
', esc_attr( $mount_id ), esc_attr( $page_id ), esc_attr( $section ) ); return; } } } } catch ( \Throwable $e ) { // A stale or unavailable Settings UI class must keep the classic renderer usable during updates. if ( $hide_save_button_overridden ) { if ( $hide_save_button_existed ) { $GLOBALS['hide_save_button'] = $previous_hide_save_button; } else { unset( $GLOBALS['hide_save_button'] ); } } wc_get_logger()->error( sprintf( 'Settings UI rendering failed for page "%1$s" section "%2$s": %3$s: %4$s', $this->id, '' === $section ? 'default' : $section, get_class( $e ), $e->getMessage() ), array( 'source' => 'settings-ui' ) ); if ( $e instanceof \Exception ) { wc_caught_exception( $e, __METHOD__ ); } } // We can't use "get_settings_for_section" here // for compatibility with derived classes overriding "get_settings". $settings = $this->get_settings( $section ); WC_Admin_Settings::output_fields( $settings ); } /** * Save settings and trigger the 'woocommerce_update_options_'.id action. */ public function save() { $this->save_settings_for_current_section(); $this->do_update_options_action(); } /** * Save settings for current section. */ protected function save_settings_for_current_section() { global $current_section; // We can't use "get_settings_for_section" here // for compatibility with derived classes overriding "get_settings". $settings = $this->get_settings( $current_section ); WC_Admin_Settings::save_fields( $settings ); } /** * Trigger the 'woocommerce_update_options_'.id action. * * @param string $section_id Section to trigger the action for, or null for current section. */ protected function do_update_options_action( $section_id = null ) { global $current_section; if ( is_null( $section_id ) ) { $section_id = $current_section; } if ( $section_id ) { do_action( 'woocommerce_update_options_' . $this->id . '_' . $section_id ); } } } endif;