ermissions', 'manage_woocommerce', $menu_slug);
}
/**
* Renders all settings sections added to a particular settings page. This
* method is an almost exact clone of global do_settings_sections(), the main
* difference is that each section is wrapped in its own
.
*
* Part of the Settings API. Use this in a settings page callback function
* to output all the sections and fields that were added to that $page with
* add_settings_section() and add_settings_field().
*
* @global $wp_settings_sections Storage array of all settings sections added to admin pages
* @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
* @since 2.7.0
*
* @param string $page The slug name of the page whos settings sections you want to output
*/
protected function render_settings_sections($page) {
global $wp_settings_sections, $wp_settings_fields;
$settings_sections = get_value($page, $wp_settings_sections);
if(empty($settings_sections)) {
return;
}
//foreach((array)$wp_settings_sections[$page] as $section) {
$settings_tabs = get_value($page, $this->_settings_tabs);
$output_tabs = count($settings_tabs) > 1;
if($output_tabs) {
echo '
';
echo "
\n";
foreach($settings_tabs as $tab_id => $tab_info) {
echo "
';
}
/**
* Renders the buttons at the bottom of the settings page.
*/
protected function render_buttons() {
submit_button(__('Save Changes', $this->_textdomain),
'primary',
'submit',
false);
}
/**
* Renders the Options page for the plugin.
*/
public function render_options_page() {
// Prepare settings page for rendering
$this->init_settings_page();
echo '
';
echo '';
echo '
' . $this->page_title() . '
';
echo '
' . $this->page_description() . '
';
settings_errors();
echo '';
echo '
'; // Closing
}
/**
* Adds a link to Settings Page in WC Admin menu.
*/
public function add_settings_page() {
$settings_page = add_submenu_page(
self::WC_MENU_ITEM_ID,
$this->page_title(),
$this->menu_title(),
$this->get_plugin_settings_menu_permission($this->menu_slug()),
$this->menu_slug(),
array($this, 'render_options_page')
);
add_action('load-' . $settings_page, array($this, 'options_page_load'));
}
/**
* Takes an associative array of attributes and returns them as a string of
* param="value" sets to be placed in an input, select, textarea, etc tag.
*
* @param array attributes An associative array of attribute key => value
* pairs to be converted to a string. A number of "reserved" keys will be
* ignored.
* @return string
*/
protected function attributes_to_string(array $attributes) {
$reserved_attributes = array(
'id',
'name',
'value',
'method',
'action',
'type',
'for',
'multiline',
'default',
'textfield',
'valuefield',
'includenull',
'yearrange',
'fields',
'inlineerrors',
'description');
$result = array();
// Build string from array
if(is_array($attributes)) {
foreach($attributes as $attribute => $value) {
// Ignore reserved attributes
if(!in_array(strtolower($attribute), $reserved_attributes)) {
$result[] = $attribute . '="' . $value . '"';
}
}
}
return implode(' ', $result);
}
/**
* Extracts the Field ID and Field Name for an input element from the arguments
* originally passed to a rendering function.
*
* @param array field_args The arguments passed to the rendering function which
* is going to render the input field.
* @param string field_id Output argument. It will contain the ID of the field.
* @param string field_name Output argument. It will contain the name of the field.
*/
protected function get_field_ids(array $field_args, &$field_id, &$field_name) {
// Determine field ID and Name
$field_id = $field_args['id'];
if(empty($field_id)) {
throw new InvalidArgumentException(__('Field ID must be specified.', $this->_textdomain));
}
$field_name = $field_args['name'] ?? $field_args['attributes']['name'] ?? $field_id;
// If a Settings Key has been passed, modify field ID and Name to make them
// part of the Settings Key array
$settings_key = $field_args['settings_key'] ?? null;
$field_id = $this->group_field($field_id, $settings_key);
$field_name = $this->group_field($field_name, $settings_key);
}
/**
* Takes a field ID and transforms it so that it becomes part of a field group.
* Example
* - Field ID: MyField
* - Group: SomeGroup
*
* Result: SomeGroup[MyField]
* This allows to group fields together and access them as an array.
*
* @param string id The Field ID.
* @param string group The group to which the field should be added.
* @return string The new field name.
*/
protected function group_field($id, $group) {
return empty($group) ? $id : $group . '[' . $id . ']';
}
/*** Rendering methods ***/
/**
* Renders a select box.
*
* @param array args An array of arguments passed by add_settings_field().
* @param bool display Indicates if the HTML for the field should be printed
* out directly (true) or just returned (false).
* @see add_settings_field().
*/
public function render_dropdown($args, $display = true) {
$this->get_field_ids($args, $field_id, $field_name);
// Retrieve the options that will populate the dropdown
$dropdown_options = $args['options'];
if(!is_array($dropdown_options)) {
throw new InvalidArgumentException(__('Argument "options" must be an array.', $this->_textdomain));
}
// Retrieve the selected Option elements
$selected_options = get_value('selected', $args, array());
if(!is_array($selected_options)) {
$selected_options = array($selected_options);
}
// Retrieve the HTML attributes
$attributes = get_value('attributes', $args, array());
// If we are about to render a multi-select dropdown, add two square brackets
// so that all selected values will be returned as an array
// TODO Make search in array case-insensitive
if(in_array('multiple', $attributes)) {
$field_name .= '[]';
}
$html = '';
if(!empty($attributes['description'])) {
$html .= '
' . $attributes['description'] . '
';
}
if($display) {
echo $html;
}
return $html;
}
/**
* Build the HTML to represent an element.
*
* @param string type The type of input. It can be text, password, hidden,
* checkbox or radio.
* @param string field_id The ID of the field.
* @param string value The field value.
* @param array attribues Additional field attributes.
* @param string field_name The name of the field. If unspecified, the field
* ID will be taken.
* @return string The HTML representation of the field.
*/
protected function get_input_html($type, $field_id, $value, $attributes, $field_name = null) {
$field_name = !empty($field_name) ? $field_name : $field_id;
$html =
'attributes_to_string($attributes) .
' />';
if(!empty($attributes['description'])) {
if($type == 'checkbox') {
$element = 'span';
}
else {
$element = 'p';
}
$html .= '<' . $element . ' class="description">' . $attributes['description'] . '' . $element . '>';
}
return $html;
}
/**
* Build the HTML to represent a