ProductsSettingsPageAdapter.php
109 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Products settings adapter for settings UI. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\Settings\SettingsUIPages; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\Settings\LegacySettingsPageAdapter; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Adapts the WooCommerce Products settings page for the settings UI renderer. |
| 16 | * |
| 17 | * @since 10.9.0 |
| 18 | */ |
| 19 | final class ProductsSettingsPageAdapter extends LegacySettingsPageAdapter { |
| 20 | |
| 21 | /** |
| 22 | * Build the canonical settings schema for a section. |
| 23 | * |
| 24 | * @param string $section Section id. Empty string means the default section. |
| 25 | * @return array |
| 26 | */ |
| 27 | public function get_schema( string $section ): array { |
| 28 | $schema = parent::get_schema( $section ); |
| 29 | |
| 30 | $schema['shell']['title'] = __( 'Product settings', 'woocommerce' ); |
| 31 | |
| 32 | if ( '' === $section ) { |
| 33 | $schema = $this->with_field_options( |
| 34 | $schema, |
| 35 | 'woocommerce_shop_page_id', |
| 36 | $this->get_page_options() |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | return $schema; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Add options to a field in a schema. |
| 45 | * |
| 46 | * @param array $schema Schema. |
| 47 | * @param string $field_id Field id. |
| 48 | * @param array $options Field options. |
| 49 | * @return array |
| 50 | */ |
| 51 | private function with_field_options( array $schema, string $field_id, array $options ): array { |
| 52 | if ( empty( $options ) || ! isset( $schema['groups'] ) || ! is_array( $schema['groups'] ) ) { |
| 53 | return $schema; |
| 54 | } |
| 55 | |
| 56 | foreach ( $schema['groups'] as $group_id => $group ) { |
| 57 | if ( ! isset( $group['fields'] ) || ! is_array( $group['fields'] ) ) { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | foreach ( $group['fields'] as $field_index => $field ) { |
| 62 | if ( ! is_array( $field ) || ( $field['id'] ?? null ) !== $field_id ) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | $schema['groups'][ $group_id ]['fields'][ $field_index ]['options'] = $options; |
| 67 | return $schema; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return $schema; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Build the page options for the shop page selector. |
| 76 | * |
| 77 | * @return array<int, array{label: string, value: string}> |
| 78 | */ |
| 79 | private function get_page_options(): array { |
| 80 | $pages = get_pages( |
| 81 | array( |
| 82 | 'sort_column' => 'menu_order', |
| 83 | 'sort_order' => 'ASC', |
| 84 | 'post_status' => array( 'publish', 'private', 'draft' ), |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | $options = array( |
| 89 | array( |
| 90 | 'label' => __( 'Select a page...', 'woocommerce' ), |
| 91 | 'value' => '', |
| 92 | ), |
| 93 | ); |
| 94 | |
| 95 | if ( ! is_array( $pages ) ) { |
| 96 | return $options; |
| 97 | } |
| 98 | |
| 99 | foreach ( $pages as $page ) { |
| 100 | $options[] = array( |
| 101 | 'label' => wp_strip_all_tags( $page->post_title ), |
| 102 | 'value' => (string) $page->ID, |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | return $options; |
| 107 | } |
| 108 | } |
| 109 |