| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /**
- * Take settings registered for WP-Admin and hooks them up to the REST API
- *
- * @package WooCommerce/Classes
- * @version 3.0.0
- * @since 3.0.0
- */
- defined( 'ABSPATH' ) || exit;
- /**
- * Register WP admin settings class.
- */
- class WC_Register_WP_Admin_Settings {
- /**
- * Contains the current class to pull settings from.
- * Either a admin page object or WC_Email object
- *
- * @var WC_Register_WP_Admin_Settings
- */
- protected $object;
- /**
- * Hooks into the settings API and starts registering our settings.
- *
- * @since 3.0.0
- * @param WC_Email|WC_Settings_Page $object The object that contains the settings to register.
- * @param string $type Type of settings to register (email or page).
- */
- public function __construct( $object, $type ) {
- if ( ! is_object( $object ) ) {
- return;
- }
- $this->object = $object;
- if ( 'page' === $type ) {
- add_filter( 'woocommerce_settings_groups', array( $this, 'register_page_group' ) );
- add_filter( 'woocommerce_settings-' . $this->object->get_id(), array( $this, 'register_page_settings' ) );
- } elseif ( 'email' === $type ) {
- add_filter( 'woocommerce_settings_groups', array( $this, 'register_email_group' ) );
- add_filter( 'woocommerce_settings-email_' . $this->object->id, array( $this, 'register_email_settings' ) );
- }
- }
- /**
- * Register's all of our different notification emails as sub groups
- * of email settings.
- *
- * @since 3.0.0
- * @param array $groups Existing registered groups.
- * @return array
- */
- public function register_email_group( $groups ) {
- $groups[] = array(
- 'id' => 'email_' . $this->object->id,
- 'label' => $this->object->title,
- 'description' => $this->object->description,
- 'parent_id' => 'email',
- );
- return $groups;
- }
- /**
- * Registers all of the setting form fields for emails to each email type's group.
- *
- * @since 3.0.0
- * @param array $settings Existing registered settings.
- * @return array
- */
- public function register_email_settings( $settings ) {
- foreach ( $this->object->form_fields as $id => $setting ) {
- $setting['id'] = $id;
- $setting['option_key'] = array( $this->object->get_option_key(), $id );
- $new_setting = $this->register_setting( $setting );
- if ( $new_setting ) {
- $settings[] = $new_setting;
- }
- }
- return $settings;
- }
- /**
- * Registers a setting group, based on admin page ID & label as parent group.
- *
- * @since 3.0.0
- * @param array $groups Array of previously registered groups.
- * @return array
- */
- public function register_page_group( $groups ) {
- $groups[] = array(
- 'id' => $this->object->get_id(),
- 'label' => $this->object->get_label(),
- );
- return $groups;
- }
- /**
- * Registers settings to a specific group.
- *
- * @since 3.0.0
- * @param array $settings Existing registered settings.
- * @return array
- */
- public function register_page_settings( $settings ) {
- /**
- * WP admin settings can be broken down into separate sections from
- * a UI standpoint. This will grab all the sections associated with
- * a particular setting group (like 'products') and register them
- * to the REST API.
- */
- $sections = $this->object->get_sections();
- if ( empty( $sections ) ) {
- // Default section is just an empty string, per admin page classes.
- $sections = array( '' );
- }
- foreach ( $sections as $section => $section_label ) {
- $settings_from_section = $this->object->get_settings( $section );
- foreach ( $settings_from_section as $setting ) {
- if ( ! isset( $setting['id'] ) ) {
- continue;
- }
- $setting['option_key'] = $setting['id'];
- $new_setting = $this->register_setting( $setting );
- if ( $new_setting ) {
- $settings[] = $new_setting;
- }
- }
- }
- return $settings;
- }
- /**
- * Register a setting into the format expected for the Settings REST API.
- *
- * @since 3.0.0
- * @param array $setting Setting data.
- * @return array|bool
- */
- public function register_setting( $setting ) {
- if ( ! isset( $setting['id'] ) ) {
- return false;
- }
- $description = '';
- if ( ! empty( $setting['desc'] ) ) {
- $description = $setting['desc'];
- } elseif ( ! empty( $setting['description'] ) ) {
- $description = $setting['description'];
- }
- $new_setting = array(
- 'id' => $setting['id'],
- 'label' => ( ! empty( $setting['title'] ) ? $setting['title'] : '' ),
- 'description' => $description,
- 'type' => $setting['type'],
- 'option_key' => $setting['option_key'],
- );
- if ( isset( $setting['default'] ) ) {
- $new_setting['default'] = $setting['default'];
- }
- if ( isset( $setting['options'] ) ) {
- $new_setting['options'] = $setting['options'];
- }
- if ( isset( $setting['desc_tip'] ) ) {
- if ( true === $setting['desc_tip'] ) {
- $new_setting['tip'] = $description;
- } elseif ( ! empty( $setting['desc_tip'] ) ) {
- $new_setting['tip'] = $setting['desc_tip'];
- }
- }
- return $new_setting;
- }
- }
|