Exceptions
1 year ago
PaymentsProviders
3 months ago
SettingsUIPages
4 weeks ago
LegacySettingsPageAdapter.php
4 weeks ago
Payments.php
3 months ago
PaymentsController.php
11 months ago
PaymentsProviders.php
2 months ago
PaymentsRestController.php
3 months ago
RegisteredSettingsSectionAdapter.php
2 weeks ago
SettingsUIPageInterface.php
4 weeks ago
SettingsUIRequestContext.php
5 days ago
SettingsUISchema.php
2 weeks ago
Utils.php
2 months ago
LegacySettingsPageAdapter.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Legacy WC_Settings_Page adapter for settings UI. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\Settings; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\Settings\SettingsUIPageInterface as PublicSettingsUIPageInterface; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Adapts a WC_Settings_Page instance into the settings UI page contract. |
| 16 | * |
| 17 | * Internal implementation of the legacy settings adapter. Extensions should use |
| 18 | * Automattic\WooCommerce\Admin\Settings\LegacySettingsPageAdapter. |
| 19 | * |
| 20 | * @since 10.9.0 |
| 21 | */ |
| 22 | class LegacySettingsPageAdapter implements PublicSettingsUIPageInterface { |
| 23 | |
| 24 | /** |
| 25 | * Legacy settings page. |
| 26 | * |
| 27 | * @var \WC_Settings_Page |
| 28 | */ |
| 29 | protected \WC_Settings_Page $settings_page; |
| 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | * |
| 34 | * @since 10.9.0 |
| 35 | * |
| 36 | * @param \WC_Settings_Page $settings_page Legacy settings page. |
| 37 | */ |
| 38 | public function __construct( \WC_Settings_Page $settings_page ) { |
| 39 | $this->settings_page = $settings_page; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * {@inheritDoc} |
| 44 | */ |
| 45 | public function get_page_id(): string { |
| 46 | return $this->settings_page->get_id(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Build the canonical settings schema for a section. |
| 51 | * |
| 52 | * @param string $section Section id. Empty string means the default section. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function get_schema( string $section ): array { |
| 56 | $schema = SettingsUISchema::from_legacy_settings( |
| 57 | $this->settings_page->get_id(), |
| 58 | $section, |
| 59 | $this->settings_page->get_label(), |
| 60 | $this->settings_page->get_settings( $section ), |
| 61 | $this->get_save_adapter( $section ) |
| 62 | ); |
| 63 | |
| 64 | $schema['shell']['sectionNavigation'] = $this->get_section_navigation( $section ); |
| 65 | |
| 66 | return $schema; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get secondary settings section navigation for the settings UI shell. |
| 71 | * |
| 72 | * @param string $current_section Current section id. |
| 73 | * @return array<int, array{id: string, label: string, href: string, active: bool}> |
| 74 | */ |
| 75 | private function get_section_navigation( string $current_section ): array { |
| 76 | $sections = $this->settings_page->get_sections(); |
| 77 | if ( empty( $sections ) || 1 === count( $sections ) ) { |
| 78 | return array(); |
| 79 | } |
| 80 | |
| 81 | $navigation = array(); |
| 82 | foreach ( $sections as $id => $label ) { |
| 83 | $section_id = (string) $id; |
| 84 | $navigation[] = array( |
| 85 | 'id' => '' === $section_id ? 'default' : $section_id, |
| 86 | 'label' => wp_strip_all_tags( html_entity_decode( (string) $label, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ), |
| 87 | 'href' => add_query_arg( |
| 88 | array( |
| 89 | 'page' => 'wc-settings', |
| 90 | 'tab' => sanitize_title( $this->settings_page->get_id() ), |
| 91 | 'section' => sanitize_title( $section_id ), |
| 92 | ), |
| 93 | admin_url( 'admin.php' ) |
| 94 | ), |
| 95 | 'active' => $current_section === $section_id, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return $navigation; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get script handles that must be loaded before the settings UI app mounts. |
| 104 | * |
| 105 | * @param string $section Section id. Empty string means the default section. |
| 106 | * @return string[] |
| 107 | */ |
| 108 | public function get_script_handles( string $section ): array { |
| 109 | return array(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the default save adapter for fields on this page. |
| 114 | * |
| 115 | * @param string $section Section id. Empty string means the default section. |
| 116 | * @return string |
| 117 | */ |
| 118 | public function get_save_adapter( string $section ): string { |
| 119 | return 'form_post'; |
| 120 | } |
| 121 | } |
| 122 |