legacy
7 years ago
v1
7 years ago
v2
6 years ago
wc-blocks
7 years ago
class-wc-rest-authentication.php
7 years ago
class-wc-rest-coupons-controller.php
7 years ago
class-wc-rest-customer-downloads-controller.php
7 years ago
class-wc-rest-customers-controller.php
7 years ago
class-wc-rest-data-continents-controller.php
7 years ago
class-wc-rest-data-controller.php
7 years ago
class-wc-rest-data-countries-controller.php
7 years ago
class-wc-rest-data-currencies-controller.php
7 years ago
class-wc-rest-exception.php
8 years ago
class-wc-rest-network-orders-controller.php
7 years ago
class-wc-rest-order-notes-controller.php
7 years ago
class-wc-rest-order-refunds-controller.php
7 years ago
class-wc-rest-orders-controller.php
7 years ago
class-wc-rest-payment-gateways-controller.php
7 years ago
class-wc-rest-product-attribute-terms-controller.php
7 years ago
class-wc-rest-product-attributes-controller.php
7 years ago
class-wc-rest-product-categories-controller.php
7 years ago
class-wc-rest-product-reviews-controller.php
7 years ago
class-wc-rest-product-shipping-classes-controller.php
7 years ago
class-wc-rest-product-tags-controller.php
7 years ago
class-wc-rest-product-variations-controller.php
7 years ago
class-wc-rest-products-controller.php
7 years ago
class-wc-rest-report-coupons-totals-controller.php
7 years ago
class-wc-rest-report-customers-totals-controller.php
7 years ago
class-wc-rest-report-orders-totals-controller.php
7 years ago
class-wc-rest-report-products-totals-controller.php
7 years ago
class-wc-rest-report-reviews-totals-controller.php
7 years ago
class-wc-rest-report-sales-controller.php
7 years ago
class-wc-rest-report-top-sellers-controller.php
7 years ago
class-wc-rest-reports-controller.php
7 years ago
class-wc-rest-setting-options-controller.php
7 years ago
class-wc-rest-settings-controller.php
7 years ago
class-wc-rest-shipping-methods-controller.php
7 years ago
class-wc-rest-shipping-zone-locations-controller.php
7 years ago
class-wc-rest-shipping-zone-methods-controller.php
7 years ago
class-wc-rest-shipping-zones-controller.php
7 years ago
class-wc-rest-system-status-controller.php
7 years ago
class-wc-rest-system-status-tools-controller.php
7 years ago
class-wc-rest-tax-classes-controller.php
7 years ago
class-wc-rest-taxes-controller.php
7 years ago
class-wc-rest-webhooks-controller.php
7 years ago
class-wc-rest-setting-options-controller.php
251 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Setting Options controller |
| 4 | * |
| 5 | * Handles requests to the /settings/$group/$setting endpoints. |
| 6 | * |
| 7 | * @package WooCommerce/API |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * REST API Setting Options controller class. |
| 15 | * |
| 16 | * @package WooCommerce/API |
| 17 | * @extends WC_REST_Setting_Options_V2_Controller |
| 18 | */ |
| 19 | class WC_REST_Setting_Options_Controller extends WC_REST_Setting_Options_V2_Controller { |
| 20 | |
| 21 | /** |
| 22 | * Endpoint namespace. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $namespace = 'wc/v3'; |
| 27 | |
| 28 | /** |
| 29 | * Get setting data. |
| 30 | * |
| 31 | * @param string $group_id Group ID. |
| 32 | * @param string $setting_id Setting ID. |
| 33 | * @return stdClass|WP_Error |
| 34 | */ |
| 35 | public function get_setting( $group_id, $setting_id ) { |
| 36 | $setting = parent::get_setting( $group_id, $setting_id ); |
| 37 | if ( is_wp_error( $setting ) ) { |
| 38 | return $setting; |
| 39 | } |
| 40 | $setting['group_id'] = $group_id; |
| 41 | return $setting; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Callback for allowed keys for each setting response. |
| 46 | * |
| 47 | * @param string $key Key to check. |
| 48 | * @return boolean |
| 49 | */ |
| 50 | public function allowed_setting_keys( $key ) { |
| 51 | return in_array( |
| 52 | $key, array( |
| 53 | 'id', |
| 54 | 'group_id', |
| 55 | 'label', |
| 56 | 'description', |
| 57 | 'default', |
| 58 | 'tip', |
| 59 | 'placeholder', |
| 60 | 'type', |
| 61 | 'options', |
| 62 | 'value', |
| 63 | 'option_key', |
| 64 | ), true |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get all settings in a group. |
| 70 | * |
| 71 | * @param string $group_id Group ID. |
| 72 | * @return array|WP_Error |
| 73 | */ |
| 74 | public function get_group_settings( $group_id ) { |
| 75 | if ( empty( $group_id ) ) { |
| 76 | return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'woocommerce' ), array( 'status' => 404 ) ); |
| 77 | } |
| 78 | |
| 79 | $settings = apply_filters( 'woocommerce_settings-' . $group_id, array() ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 80 | |
| 81 | if ( empty( $settings ) ) { |
| 82 | return new WP_Error( 'rest_setting_setting_group_invalid', __( 'Invalid setting group.', 'woocommerce' ), array( 'status' => 404 ) ); |
| 83 | } |
| 84 | |
| 85 | $filtered_settings = array(); |
| 86 | foreach ( $settings as $setting ) { |
| 87 | $option_key = $setting['option_key']; |
| 88 | $setting = $this->filter_setting( $setting ); |
| 89 | $default = isset( $setting['default'] ) ? $setting['default'] : ''; |
| 90 | // Get the option value. |
| 91 | if ( is_array( $option_key ) ) { |
| 92 | $option = get_option( $option_key[0] ); |
| 93 | $setting['value'] = isset( $option[ $option_key[1] ] ) ? $option[ $option_key[1] ] : $default; |
| 94 | } else { |
| 95 | $admin_setting_value = WC_Admin_Settings::get_option( $option_key, $default ); |
| 96 | $setting['value'] = $admin_setting_value; |
| 97 | } |
| 98 | |
| 99 | if ( 'multi_select_countries' === $setting['type'] ) { |
| 100 | $setting['options'] = WC()->countries->get_countries(); |
| 101 | $setting['type'] = 'multiselect'; |
| 102 | } elseif ( 'single_select_country' === $setting['type'] ) { |
| 103 | $setting['type'] = 'select'; |
| 104 | $setting['options'] = $this->get_countries_and_states(); |
| 105 | } elseif ( 'single_select_page' === $setting['type'] ) { |
| 106 | $pages = get_pages( |
| 107 | array( |
| 108 | 'sort_column' => 'menu_order', |
| 109 | 'sort_order' => 'ASC', |
| 110 | 'hierarchical' => 0, |
| 111 | ) |
| 112 | ); |
| 113 | $options = array(); |
| 114 | foreach ( $pages as $page ) { |
| 115 | $options[ $page->ID ] = ! empty( $page->post_title ) ? $page->post_title : '#' . $page->ID; |
| 116 | } |
| 117 | $setting['type'] = 'select'; |
| 118 | $setting['options'] = $options; |
| 119 | } |
| 120 | |
| 121 | $filtered_settings[] = $setting; |
| 122 | } |
| 123 | |
| 124 | return $filtered_settings; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns a list of countries and states for use in the base location setting. |
| 129 | * |
| 130 | * @since 3.0.7 |
| 131 | * @return array Array of states and countries. |
| 132 | */ |
| 133 | private function get_countries_and_states() { |
| 134 | $countries = WC()->countries->get_countries(); |
| 135 | if ( ! $countries ) { |
| 136 | return array(); |
| 137 | } |
| 138 | $output = array(); |
| 139 | foreach ( $countries as $key => $value ) { |
| 140 | $states = WC()->countries->get_states( $key ); |
| 141 | |
| 142 | if ( $states ) { |
| 143 | foreach ( $states as $state_key => $state_value ) { |
| 144 | $output[ $key . ':' . $state_key ] = $value . ' - ' . $state_value; |
| 145 | } |
| 146 | } else { |
| 147 | $output[ $key ] = $value; |
| 148 | } |
| 149 | } |
| 150 | return $output; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the settings schema, conforming to JSON Schema. |
| 155 | * |
| 156 | * @return array |
| 157 | */ |
| 158 | public function get_item_schema() { |
| 159 | $schema = array( |
| 160 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 161 | 'title' => 'setting', |
| 162 | 'type' => 'object', |
| 163 | 'properties' => array( |
| 164 | 'id' => array( |
| 165 | 'description' => __( 'A unique identifier for the setting.', 'woocommerce' ), |
| 166 | 'type' => 'string', |
| 167 | 'arg_options' => array( |
| 168 | 'sanitize_callback' => 'sanitize_title', |
| 169 | ), |
| 170 | 'context' => array( 'view', 'edit' ), |
| 171 | 'readonly' => true, |
| 172 | ), |
| 173 | 'group_id' => array( |
| 174 | 'description' => __( 'An identifier for the group this setting belongs to.', 'woocommerce' ), |
| 175 | 'type' => 'string', |
| 176 | 'arg_options' => array( |
| 177 | 'sanitize_callback' => 'sanitize_title', |
| 178 | ), |
| 179 | 'context' => array( 'view', 'edit' ), |
| 180 | 'readonly' => true, |
| 181 | ), |
| 182 | 'label' => array( |
| 183 | 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ), |
| 184 | 'type' => 'string', |
| 185 | 'arg_options' => array( |
| 186 | 'sanitize_callback' => 'sanitize_text_field', |
| 187 | ), |
| 188 | 'context' => array( 'view', 'edit' ), |
| 189 | 'readonly' => true, |
| 190 | ), |
| 191 | 'description' => array( |
| 192 | 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ), |
| 193 | 'type' => 'string', |
| 194 | 'arg_options' => array( |
| 195 | 'sanitize_callback' => 'sanitize_text_field', |
| 196 | ), |
| 197 | 'context' => array( 'view', 'edit' ), |
| 198 | 'readonly' => true, |
| 199 | ), |
| 200 | 'value' => array( |
| 201 | 'description' => __( 'Setting value.', 'woocommerce' ), |
| 202 | 'type' => 'mixed', |
| 203 | 'context' => array( 'view', 'edit' ), |
| 204 | ), |
| 205 | 'default' => array( |
| 206 | 'description' => __( 'Default value for the setting.', 'woocommerce' ), |
| 207 | 'type' => 'mixed', |
| 208 | 'context' => array( 'view', 'edit' ), |
| 209 | 'readonly' => true, |
| 210 | ), |
| 211 | 'tip' => array( |
| 212 | 'description' => __( 'Additional help text shown to the user about the setting.', 'woocommerce' ), |
| 213 | 'type' => 'string', |
| 214 | 'arg_options' => array( |
| 215 | 'sanitize_callback' => 'sanitize_text_field', |
| 216 | ), |
| 217 | 'context' => array( 'view', 'edit' ), |
| 218 | 'readonly' => true, |
| 219 | ), |
| 220 | 'placeholder' => array( |
| 221 | 'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ), |
| 222 | 'type' => 'string', |
| 223 | 'arg_options' => array( |
| 224 | 'sanitize_callback' => 'sanitize_text_field', |
| 225 | ), |
| 226 | 'context' => array( 'view', 'edit' ), |
| 227 | 'readonly' => true, |
| 228 | ), |
| 229 | 'type' => array( |
| 230 | 'description' => __( 'Type of setting.', 'woocommerce' ), |
| 231 | 'type' => 'string', |
| 232 | 'arg_options' => array( |
| 233 | 'sanitize_callback' => 'sanitize_text_field', |
| 234 | ), |
| 235 | 'context' => array( 'view', 'edit' ), |
| 236 | 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ), |
| 237 | 'readonly' => true, |
| 238 | ), |
| 239 | 'options' => array( |
| 240 | 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'woocommerce' ), |
| 241 | 'type' => 'object', |
| 242 | 'context' => array( 'view', 'edit' ), |
| 243 | 'readonly' => true, |
| 244 | ), |
| 245 | ), |
| 246 | ); |
| 247 | |
| 248 | return $this->add_additional_fields_schema( $schema ); |
| 249 | } |
| 250 | } |
| 251 |