SettingOptions.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Admin\Features\Blueprint; |
| 6 | |
| 7 | /** |
| 8 | * Handles getting options from WooCommerce settings pages. |
| 9 | * |
| 10 | * Class SettingOptions |
| 11 | */ |
| 12 | class SettingOptions { |
| 13 | /** |
| 14 | * Setting option controller. |
| 15 | * |
| 16 | * @var \WC_REST_Setting_Options_Controller |
| 17 | */ |
| 18 | private $setting_option_controller; |
| 19 | |
| 20 | /** |
| 21 | * Ignore setting types. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | private $ignore_setting_types = array( 'title', 'sectionend', 'slotfill_placeholder', 'hidden' ); |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->setting_option_controller = new \WC_REST_Setting_Options_Controller(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get options for a specific settings page. |
| 37 | * |
| 38 | * @param string $page_id The page ID. |
| 39 | * @return array |
| 40 | * |
| 41 | * @throws \Exception If the settings page is not found. |
| 42 | */ |
| 43 | public function get_page_options( $page_id ) { |
| 44 | $settings = $this->setting_option_controller->get_group_settings( $page_id ); |
| 45 | |
| 46 | if ( is_wp_error( $settings ) ) { |
| 47 | throw new \Exception( esc_html( $settings->get_error_message() ) ); |
| 48 | } |
| 49 | |
| 50 | $page_options = array(); |
| 51 | |
| 52 | foreach ( $settings as $setting ) { |
| 53 | // Skip if the setting type is not valid. |
| 54 | if ( in_array( $setting['type'], $this->ignore_setting_types, true ) || ! isset( $setting['id'] ) ) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | $key = is_array( $setting['option_key'] ) ? $setting['option_key'][0] : $setting['option_key']; |
| 59 | |
| 60 | // Skip if the option key is already in the page options. |
| 61 | if ( in_array( $key, $page_options, true ) ) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | $default_value = $setting['default'] ?? null; |
| 66 | $page_options[ $key ] = get_option( $key, $default_value ); |
| 67 | } |
| 68 | |
| 69 | return $page_options; |
| 70 | } |
| 71 | } |
| 72 |