LLMS_Admin_Settings::save_fields( array $settings )
Save admin fields.
Description Description
Loops through a LifterLMS settings field options array and saves the values via update_option().
Parameters Parameters
- $settings
-
(array) (Required) Opens array to output
Return Return
(boolean)
Source Source
File: includes/admin/class.llms.admin.settings.php
// phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce is checked in self::save().
if ( empty( $_POST ) ) {
return false;
}
// Options to update will be stored here.
$update_options = array();
// Loop options and get values to save.
foreach ( $settings as $field ) {
if ( ! isset( $field['id'] ) ) {
continue;
}
$type = isset( $field['type'] ) ? sanitize_title( $field['type'] ) : '';
// Remove secure options from the database.
if ( isset( $field['secure_option'] ) && llms_get_secure_option( $field['secure_option'] ) ) {
delete_option( $field['id'] );
continue;
}
// Get the option name.
$option_value = null;
// Determines if the option value is an array.
$is_array_option = false !== strpos( $field['id'], '[' );
switch ( $type ) {
case 'checkbox':
if ( $is_array_option ) {
$option_value = self::get_array_field_posted_value( $field['id'] ) ? 'yes' : 'no';
} elseif ( isset( $_POST[ $field['id'] ] ) ) {
$option_value = 'yes';
} else {
$option_value = 'no';
}
break;
case 'textarea':
case 'wpeditor':
if ( isset( $_POST[ $field['id'] ] ) ) {
$option_value = wp_kses_post( trim( llms_filter_input( INPUT_POST, $field['id'], FILTER_DEFAULT ) ) );
} else {
$option_value = '';
}
break;
case 'password':
case 'text':
case 'email':
case 'number':
case 'select':
case 'single_select_page':
case 'single_select_membership':
case 'radio':
case 'hidden':
case 'image':
if ( $is_array_option ) {
$option_value = self::get_array_field_posted_value( $field['id'] );
} elseif ( isset( $_POST[ $field['id'] ] ) ) {
$option_value = llms_filter_input_sanitize_string( INPUT_POST, $field['id'] );
} else {
$option_value = '';
}
if ( isset( $field['sanitize'] ) && 'slug' === $field['sanitize'] ) {
$option_value = sanitize_title( $option_value );
}
break;
case 'multiselect':
if ( isset( $_POST[ $field['id'] ] ) ) {
$option_value = llms_filter_input_sanitize_string( INPUT_POST, $field['id'], array( FILTER_REQUIRE_ARRAY ) );
} else {
$option_value = '';
}
break;
default:
/**
* Action run for external field types.
*
* @since Unknown
* @deprecated 7.0.0 Use `llms_update_option_{$type}` filter hook instead.
*
* @param type $arg Description.
*/
do_action_deprecated( "lifterlms_update_option_{$type}", array( $field ), '7.0.0' );
}
// Special treatment for the 'maxlength' attribute.
if ( in_array( $type, array( 'text', 'textarea' ), true ) && isset( $field['custom_attributes']['maxlength'] ) ) {
$option_value = llms_trim_string( $option_value, (int) $field['custom_attributes']['maxlength'], '' );
}
/**
* Filters the value of a settings field after it has been parsed and sanitized
* and before it is saved to the database.
*
* The dynamic portion of this hook, `{$type}` refers to the setting field type:
* email, text, checkbox, etc...
*
* @since 7.0.0
*
* @param string|null $option_value The sanitized option value or `null`.
* @param array $field The settings field array.
*/
$option_value = apply_filters( "llms_update_option_{$type}", $option_value, $field );
if ( ! is_null( $option_value ) ) {
if ( $is_array_option ) {
parse_str( $field['id'], $option_array );
// Option name is first key.
$option_name = current( array_keys( $option_array ) );
// Get old option value.
if ( ! isset( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = get_option( $option_name, array() );
}
if ( ! is_array( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = array();
}
// Set keys and value.
$key = key( $option_array[ $option_name ] );
$update_options[ $option_name ][ $key ] = $option_value;
} else {
$update_options[ $field['id'] ] = $option_value;
}
}
/**
* Action run prior to the update of a LifterLMS setting field option.
*
* An update isn't guaranteed after this action if the method's logic can't
* find a valid posted valued to persist to the database.
*
* @since Unknown
*
* @param array $field The admin setting field array to be updated.
*/
do_action( 'lifterlms_update_option', $field );
}
// Now save the options.
foreach ( $update_options as $name => $value ) {
update_option( $name, $value );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
return true;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 7.0.0 | Add handling for array fields for standard input types. Account for the maxlength input text and textarea attribute. |
| 5.9.0 | Stop using deprecated FILTER_SANITIZE_STRING. |
| 3.35.2 | Don't strip tags on editor and textarea fields that allow HTML. |
| 3.35.0 | Sanitize $_POST data. |
| 3.29.0 | Unknown. |
| 1.0.0 | Introduced. |