LLMS_Admin_Settings::save_fields( array $settings )

Save admin fields.


Description Description

Loops though the lifterlms options array and outputs each field.


Top ↑

Parameters Parameters

$settings

(array) (Required) Opens array to output


Top ↑

Return Return

(boolean)


Top ↑

Source Source

File: includes/admin/class.llms.admin.settings.php

	public static function save_fields( $settings ) {

		// 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 $value ) {

			if ( ! isset( $value['id'] ) ) {
				continue; }

			$type = isset( $value['type'] ) ? sanitize_title( $value['type'] ) : '';

			// Remove secure options from the database.
			if ( isset( $value['secure_option'] ) && llms_get_secure_option( $value['secure_option'] ) ) {
				delete_option( $value['id'] );
				continue;
			}

			// Get the option name.
			$option_value = null;

			switch ( $type ) {

				// Standard types.
				case 'checkbox':
					// Ooboi this is gross.
					if ( strstr( $value['id'], '[' ) ) {
						parse_str( $value['id'], $option_data );
						$main_option_names = array_keys( $option_data );
						$main_option_vals  = array_keys( $option_data[ $main_option_names[0] ] );
						if ( isset( $_POST[ $main_option_names[0] ] ) && in_array( $main_option_vals[0], array_keys( $_POST[ $main_option_names[0] ] ) ) ) {
							$option_value = 'yes';
						} else {
							$option_value = 'no';
						}
					} elseif ( isset( $_POST[ $value['id'] ] ) ) {
						$option_value = 'yes';
					} else {
						$option_value = 'no';
					}

					break;

				case 'textarea':
				case 'wpeditor':
					if ( isset( $_POST[ $value['id'] ] ) ) {
						$option_value = wp_kses_post( trim( llms_filter_input( INPUT_POST, $value['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 ( isset( $_POST[ $value['id'] ] ) ) {
						$option_value = llms_filter_input_sanitize_string( INPUT_POST, $value['id'] );
					} else {
						$option_value = '';
					}

					if ( isset( $value['sanitize'] ) && 'slug' === $value['sanitize'] ) {
						$option_value = sanitize_title( $option_value );
					}

					break;

				case 'multiselect':
					if ( isset( $_POST[ $value['id'] ] ) ) {
						$option_value = llms_filter_input_sanitize_string( INPUT_POST, $value['id'], array( FILTER_REQUIRE_ARRAY ) );
					} else {
						$option_value = '';
					}
					break;

				// Custom handling.
				default:
					do_action( 'lifterlms_update_option_' . $type, $value );

					break;

			}

			if ( ! is_null( $option_value ) ) {
				// Check if option is an array.
				if ( strstr( $value['id'], '[' ) ) {

					parse_str( $value['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;

					// Single value.
				} else {
					$update_options[ $value['id'] ] = $option_value;
				}
			}

			// Custom handling.
			do_action( 'lifterlms_update_option', $value );
		}
		// Now save the options.
		foreach ( $update_options as $name => $value ) {

			update_option( $name, $value );

		}

		// phpcs:enable WordPress.Security.NonceVerification.Missing

		return true;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
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.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.