2016-07-26 18:08:29 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take settings registered for WP-Admin and hooks them up to the REST API.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
|
|
|
* @since 3.0.0
|
2016-07-26 18:08:29 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
*/
|
|
|
|
class WC_Register_WP_Admin_Settings {
|
|
|
|
|
2016-09-07 22:05:45 +00:00
|
|
|
/** @var class Contains the current class to pull settings from. Either a admin page object or WC_Email object. */
|
|
|
|
protected $object;
|
2016-07-26 18:08:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks into the settings API and starts registering our settings.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-07 22:05:45 +00:00
|
|
|
* @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).
|
2016-07-26 18:08:29 +00:00
|
|
|
*/
|
2016-09-07 22:05:45 +00:00
|
|
|
public function __construct( $object, $type ) {
|
2017-04-05 10:10:27 +00:00
|
|
|
if ( ! is_object( $object ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 22:05:45 +00:00
|
|
|
$this->object = $object;
|
2017-04-05 10:10:27 +00:00
|
|
|
|
2016-09-07 22:05:45 +00:00
|
|
|
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' ) );
|
2016-09-09 16:39:09 +00:00
|
|
|
} elseif ( 'email' === $type ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
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.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-07 22:05:45 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-07 22:05:45 +00:00
|
|
|
* @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;
|
2016-07-26 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-02 02:40:36 +00:00
|
|
|
* Registers a setting group, based on admin page ID & label as parent group.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-02 02:40:36 +00:00
|
|
|
* @param array $groups Array of previously registered groups.
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-09-07 22:05:45 +00:00
|
|
|
public function register_page_group( $groups ) {
|
2016-07-26 18:08:29 +00:00
|
|
|
$groups[] = array(
|
2016-09-07 22:05:45 +00:00
|
|
|
'id' => $this->object->get_id(),
|
|
|
|
'label' => $this->object->get_label(),
|
2016-07-26 18:08:29 +00:00
|
|
|
);
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-02 02:40:36 +00:00
|
|
|
* Registers settings to a specific group.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-02 02:40:36 +00:00
|
|
|
* @param array $settings Existing registered settings
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-09-07 22:05:45 +00:00
|
|
|
public function register_page_settings( $settings ) {
|
2016-07-26 18:08:29 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-09-07 22:05:45 +00:00
|
|
|
$sections = $this->object->get_sections();
|
2016-07-26 18:08:29 +00:00
|
|
|
if ( empty( $sections ) ) {
|
|
|
|
// Default section is just an empty string, per admin page classes
|
|
|
|
$sections = array( '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $sections as $section => $section_label ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
$settings_from_section = $this->object->get_settings( $section );
|
2016-07-26 18:08:29 +00:00
|
|
|
foreach ( $settings_from_section as $setting ) {
|
2016-09-08 22:14:40 +00:00
|
|
|
if ( ! isset( $setting['id'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-07 22:05:45 +00:00
|
|
|
$setting['option_key'] = $setting['id'];
|
|
|
|
$new_setting = $this->register_setting( $setting );
|
2016-07-26 18:08:29 +00:00
|
|
|
if ( $new_setting ) {
|
|
|
|
$settings[] = $new_setting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 22:14:40 +00:00
|
|
|
* Register a setting into the format expected for the Settings REST API.
|
2016-07-26 18:08:29 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-09-08 22:14:40 +00:00
|
|
|
* @param array $setting
|
|
|
|
* @return array|bool
|
2016-07-26 18:08:29 +00:00
|
|
|
*/
|
|
|
|
public function register_setting( $setting ) {
|
|
|
|
if ( ! isset( $setting['id'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-07 22:05:45 +00:00
|
|
|
|
|
|
|
$description = '';
|
2016-09-09 16:39:09 +00:00
|
|
|
if ( ! empty( $setting['desc'] ) ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
$description = $setting['desc'];
|
2016-09-09 16:39:09 +00:00
|
|
|
} elseif ( ! empty( $setting['description'] ) ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
$description = $setting['description'];
|
|
|
|
}
|
|
|
|
|
2016-07-26 18:08:29 +00:00
|
|
|
$new_setting = array(
|
|
|
|
'id' => $setting['id'],
|
|
|
|
'label' => ( ! empty( $setting['title'] ) ? $setting['title'] : '' ),
|
2016-09-07 22:05:45 +00:00
|
|
|
'description' => $description,
|
2016-07-26 18:08:29 +00:00
|
|
|
'type' => $setting['type'],
|
2016-09-07 22:05:45 +00:00
|
|
|
'option_key' => $setting['option_key'],
|
2016-07-26 18:08:29 +00:00
|
|
|
);
|
2016-09-07 22:05:45 +00:00
|
|
|
|
2016-07-26 18:08:29 +00:00
|
|
|
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'] ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
$new_setting['tip'] = $description;
|
2016-09-09 16:39:09 +00:00
|
|
|
} elseif ( ! empty( $setting['desc_tip'] ) ) {
|
2016-07-26 18:08:29 +00:00
|
|
|
$new_setting['tip'] = $setting['desc_tip'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $new_setting;
|
|
|
|
}
|
2016-09-07 22:05:45 +00:00
|
|
|
|
2016-07-26 18:08:29 +00:00
|
|
|
}
|