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
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 | // 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. |