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