WC_Register_WP_Admin_Settings

Take settings registered for WP-Admin and hooks them up to the REST API.

Defined (1)

The class is defined in the following location(s).

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