wo_forms = new WOForms();
}
/**
* Get an ID either from the string provided or an array.
*
* @param string|array $id The variable to pull the ID from.
*
* @return string
*/
private function id( $id ) {
if ( is_array( $id ) ) {
return $id[ array_key_first( $id ) ];
}
return $id;
}
/**
* Create a name from an ID.
* Formats as an HTML array if an array is provided.
*
* @param string|array $id The variable to pull the name from.
*
* @return string
*/
private function name( $id ) {
if ( is_array( $id ) ) {
$key = array_key_first( $id );
return $key . '[' . $id[ $key ] . ']';
}
return $id;
}
/**
* Start the page wrapper.
*
* @return void
*/
public function start() {
?>
$tab_key,
),
$admin_url
);
}
/**
* Creates page tabs from a settings array.
*
* @param array $settings An array of settings.
* @param string $admin_url The URL this tab appears on.
*
* @return array
*/
public function create_tabs_from_settings( $settings, $admin_url ) {
$tabs = array();
foreach ( $settings as $key => $group ) {
$opt_key = $this->key( $key );
$tabs[] = array(
'key' => $opt_key,
'text' => $group['title'],
'url' => $this->get_tab_url( $opt_key, $admin_url ),
);
}
return $tabs;
}
/**
* Displays settings tabs on a page.
* Tab text should already be translated.
*
* @param array $tabs An array of tabs to display.
* @param null|string $active_tab The currently active tab.
*
* @return void
*/
public function display_tabs( $tabs = array(), $active_tab = null ) {
if ( empty( $tabs ) ) {
return;
}
?>
start();
$this->title( $admin_title );
$this->form_start();
settings_errors();
/**
* Tabs
*/
$tabs = $this->create_tabs_from_settings( $settings, $admin_url );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Ignoring because we aren't saving any data and this functiopn is called by add_submenu_page (which checks capabilities)
$active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : $tabs[0]['key'];
$this->display_tabs( $tabs, $active_tab );
$active_tab_has_fields = false;
foreach ( $tabs as $tab ) {
if ( $active_tab !== $tab['key'] ) {
continue;
}
settings_fields( $tab['key'] );
do_settings_sections( $tab['key'] );
}
foreach ( $settings as $key => $group ) {
if ( $this->key( $key ) !== $active_tab ) {
continue;
}
if ( ! empty( $group['sections'] ) ) {
foreach ( $group['sections'] as $section ) {
if ( ! empty( $section['fields'] ) ) {
$active_tab_has_fields = true;
break;
}
}
}
break;
}
/**
* End page
*/
if ( $active_tab_has_fields ) {
submit_button();
}
if ( ! empty( $footer_content ) ) {
foreach ( $footer_content as $key => $callback ) {
if ( $key !== $active_tab ) {
continue;
}
if ( is_callable( $callback ) ) {
?>
form_end();
$this->end();
}
/**
* Creates settings sections and registers fields.
*
* @param array $settings A settings array from which to build our settings.
* @param mixed $class_instance The class for our settings field callbacks.
*
* @return void
*/
public function add_sections_and_settings( $settings, $class_instance ) {
foreach ( $settings as $key => $group ) {
$defaults = isset( $group['initialize'] ) ? $group['initialize'] : array();
$this->initialize( $key, $defaults );
/**
* This is the overall group
* It's a tab, and also the option key for the DB
*/
$opt_key = $this->key( $key );
foreach ( $group['sections'] as $section_key => $section ) {
$section_key = $this->key( $section_key );
/**
* These are sub-sections within the option group
*/
add_settings_section(
$section_key . '_settings_section',
isset( $section['title'] ) ? $section['title'] : null,
array( $class_instance, 'settings_callback_' . $section_key ),
$opt_key
);
/**
* Add fields
*/
if ( isset( $section['fields'] ) && ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field_key => $value ) {
$args = array();
if ( is_array( $value ) ) {
$restricted = isset( $value['restricted'] ) ? $value['restricted'] : null;
if ( $restricted ) {
$restricted_class = $this->ns . '-mode-restrict ' . $this->ns . '-mode-restrict--' . sanitize_title( $restricted );
$restricted_val = isset( $value['restricted_val'] ) ? $value['restricted_val'] : '';
if ( $restricted_val ) {
$restricted_class .= ' ' . $this->ns . '-mode-restrict--' . sanitize_title( $restricted . '- ' . $restricted_val );
}
$args['class'] = $restricted_class;
}
$value = $value['title'];
}
add_settings_field(
$field_key,
$value,
array( $class_instance, 'field_' . $this->ns . '_' . $field_key ),
$opt_key,
$section_key . '_settings_section',
$args
);
}
}
}
}
/**
* Register settings
*/
foreach ( $settings as $key => $group ) {
$key = $this->key( $key );
register_setting(
$key,
$key,
array(
'sanitize_callback' => array( $class_instance, 'sanitize_' . $key ),
),
);
}
}
/**
* Creates a form label using our settings ID.
*
* @param string $id Form field ID.
* @param string $text Text of the label.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function label( $id, $text, $args = array() ) {
$id = $this->id( $id );
return $this->wo_forms->label( $id, $text, $args );
}
/**
* Creates a select dropdown on a settings page.
*
* @param string $id Form field ID.
* @param array $options The options in the select field.
* @param mixed $current_value The current value to select.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function select( $id, $options, $current_value = null, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
if ( ! $current_value && isset( $args['default'] ) ) {
$current_value = $args['default'];
}
return $this->wo_forms->select( $name, $options, $current_value, $args );
}
/**
* Creates a checkbox on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value.
* @param mixed $checked_value The value when checked.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function checkbox( $id, $current_value, $checked_value = 1, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
return $this->wo_forms->checkbox( $name, $current_value, $checked_value, $args );
}
/**
* Creates a textarea on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value of the textarea.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function textarea( $id, $current_value, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
return $this->wo_forms->textarea( $name, $current_value, $args );
}
/**
* Creates an input of specified $type on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value of the input.
* @param string $type The type of input (e.g., 'text' or 'number').
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function input( $id, $current_value, $type = 'text', $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
return $this->wo_forms->input( $name, $current_value, $type, $args );
}
/**
* Creates an input of specified $type on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value of the input.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function file( $id, $current_value, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
return $this->wo_forms->input( $name, $current_value, 'file', $args );
}
/**
* Creates an input of specified $type on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value of the input.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function color( $id, $current_value, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
$args['classes'] = array( 'woadmin-color-picker' );
$args['data'] = array(
'default-color' => isset( $args['default_color'] ) ? $args['default_color'] : '#ffffff',
);
return $this->wo_forms->input( $name, $current_value, 'text', $args );
}
/**
* Creates an input of specified $type on a settings page.
*
* @param string $id Form field ID.
* @param mixed $current_value The current value of the input.
* @param array $args Optional args to pass to WOForms.
*
* @return string
*/
public function media( $id, $current_value, $args = array() ) {
$name = $this->name( $id );
$args['id'] = $this->id( $id );
$args['display'] = false;
$args['classes'] = array( 'woadmin-media-input' );
$args['button_text'] = isset( $args['button_text'] ) ? $args['button_text'] : __( 'Select File', 'wo-admin-framework' );
$input = '