Нет описания

class-wc-rest-setting-options-controller.php 7.3KB

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