class-wc-register-wp-admin-settings.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Take settings registered for WP-Admin and hooks them up to the REST API
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.0.0
  7. * @since 3.0.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Register WP admin settings class.
  12. */
  13. class WC_Register_WP_Admin_Settings {
  14. /**
  15. * Contains the current class to pull settings from.
  16. * Either a admin page object or WC_Email object
  17. *
  18. * @var WC_Register_WP_Admin_Settings
  19. */
  20. protected $object;
  21. /**
  22. * Hooks into the settings API and starts registering our settings.
  23. *
  24. * @since 3.0.0
  25. * @param WC_Email|WC_Settings_Page $object The object that contains the settings to register.
  26. * @param string $type Type of settings to register (email or page).
  27. */
  28. public function __construct( $object, $type ) {
  29. if ( ! is_object( $object ) ) {
  30. return;
  31. }
  32. $this->object = $object;
  33. if ( 'page' === $type ) {
  34. add_filter( 'woocommerce_settings_groups', array( $this, 'register_page_group' ) );
  35. add_filter( 'woocommerce_settings-' . $this->object->get_id(), array( $this, 'register_page_settings' ) );
  36. } elseif ( 'email' === $type ) {
  37. add_filter( 'woocommerce_settings_groups', array( $this, 'register_email_group' ) );
  38. add_filter( 'woocommerce_settings-email_' . $this->object->id, array( $this, 'register_email_settings' ) );
  39. }
  40. }
  41. /**
  42. * Register's all of our different notification emails as sub groups
  43. * of email settings.
  44. *
  45. * @since 3.0.0
  46. * @param array $groups Existing registered groups.
  47. * @return array
  48. */
  49. public function register_email_group( $groups ) {
  50. $groups[] = array(
  51. 'id' => 'email_' . $this->object->id,
  52. 'label' => $this->object->title,
  53. 'description' => $this->object->description,
  54. 'parent_id' => 'email',
  55. );
  56. return $groups;
  57. }
  58. /**
  59. * Registers all of the setting form fields for emails to each email type's group.
  60. *
  61. * @since 3.0.0
  62. * @param array $settings Existing registered settings.
  63. * @return array
  64. */
  65. public function register_email_settings( $settings ) {
  66. foreach ( $this->object->form_fields as $id => $setting ) {
  67. $setting['id'] = $id;
  68. $setting['option_key'] = array( $this->object->get_option_key(), $id );
  69. $new_setting = $this->register_setting( $setting );
  70. if ( $new_setting ) {
  71. $settings[] = $new_setting;
  72. }
  73. }
  74. return $settings;
  75. }
  76. /**
  77. * Registers a setting group, based on admin page ID & label as parent group.
  78. *
  79. * @since 3.0.0
  80. * @param array $groups Array of previously registered groups.
  81. * @return array
  82. */
  83. public function register_page_group( $groups ) {
  84. $groups[] = array(
  85. 'id' => $this->object->get_id(),
  86. 'label' => $this->object->get_label(),
  87. );
  88. return $groups;
  89. }
  90. /**
  91. * Registers settings to a specific group.
  92. *
  93. * @since 3.0.0
  94. * @param array $settings Existing registered settings.
  95. * @return array
  96. */
  97. public function register_page_settings( $settings ) {
  98. /**
  99. * WP admin settings can be broken down into separate sections from
  100. * a UI standpoint. This will grab all the sections associated with
  101. * a particular setting group (like 'products') and register them
  102. * to the REST API.
  103. */
  104. $sections = $this->object->get_sections();
  105. if ( empty( $sections ) ) {
  106. // Default section is just an empty string, per admin page classes.
  107. $sections = array( '' );
  108. }
  109. foreach ( $sections as $section => $section_label ) {
  110. $settings_from_section = $this->object->get_settings( $section );
  111. foreach ( $settings_from_section as $setting ) {
  112. if ( ! isset( $setting['id'] ) ) {
  113. continue;
  114. }
  115. $setting['option_key'] = $setting['id'];
  116. $new_setting = $this->register_setting( $setting );
  117. if ( $new_setting ) {
  118. $settings[] = $new_setting;
  119. }
  120. }
  121. }
  122. return $settings;
  123. }
  124. /**
  125. * Register a setting into the format expected for the Settings REST API.
  126. *
  127. * @since 3.0.0
  128. * @param array $setting Setting data.
  129. * @return array|bool
  130. */
  131. public function register_setting( $setting ) {
  132. if ( ! isset( $setting['id'] ) ) {
  133. return false;
  134. }
  135. $description = '';
  136. if ( ! empty( $setting['desc'] ) ) {
  137. $description = $setting['desc'];
  138. } elseif ( ! empty( $setting['description'] ) ) {
  139. $description = $setting['description'];
  140. }
  141. $new_setting = array(
  142. 'id' => $setting['id'],
  143. 'label' => ( ! empty( $setting['title'] ) ? $setting['title'] : '' ),
  144. 'description' => $description,
  145. 'type' => $setting['type'],
  146. 'option_key' => $setting['option_key'],
  147. );
  148. if ( isset( $setting['default'] ) ) {
  149. $new_setting['default'] = $setting['default'];
  150. }
  151. if ( isset( $setting['options'] ) ) {
  152. $new_setting['options'] = $setting['options'];
  153. }
  154. if ( isset( $setting['desc_tip'] ) ) {
  155. if ( true === $setting['desc_tip'] ) {
  156. $new_setting['tip'] = $description;
  157. } elseif ( ! empty( $setting['desc_tip'] ) ) {
  158. $new_setting['tip'] = $setting['desc_tip'];
  159. }
  160. }
  161. return $new_setting;
  162. }
  163. }