_sanitize_data( $instance, $new_instance ); return $instance; } public function _sanitize_data($instance, $new_instance) { if ( is_array( $this->text_fields ) ) { // update the text fields values foreach ( $this->text_fields as $field ) { $instance = array_merge( $instance, $this->_update_text( $field, $new_instance ) ); } } if ( is_array( $this->text_areas ) ) { //update the textarea_values foreach ( $this->text_areas as $field ) { $instance = array_merge( $instance, $this->_update_textarea( $field, $new_instance ) ); } } if ( is_array( $this->checkboxes ) ) { // update the checkbox fields values foreach ( $this->checkboxes as $field ) { $instance = array_merge( $instance, $this->_update_checkbox( $field, $new_instance ) ); } } if ( is_array( $this->select_fields ) ) { // update the select fields values foreach ( $this->select_fields as $field ) { $instance = array_merge( $instance, $this->_update_select( $field, $new_instance ) ); } } return $instance; } /** * Update and sanitize backend value of the text field * * @param string $name * @param object $new_instance * @return object validate new instance */ public function _update_text($name, $new_instance) { $instance = array(); $instance[$name] = (!empty( $new_instance[$name] )) ? sanitize_text_field( $new_instance[$name] ) : ''; return $instance; } /** * Update and sanitize backend value of the textarea * * @param string $name * @param object $new_instance * @return object validate new instance */ public function _update_textarea($name, $new_instance) { $instance = array(); $instance[$name] = (!empty( $new_instance[$name] )) ? esc_textarea( htmlspecialchars_decode( $new_instance[$name] ) ) : ''; return $instance; } /** * Update and sanitize backend value of the checkbox field * * @param string $name * @param object $new_instance * @return object validate new instance */ public function _update_checkbox($name, $new_instance) { $instance = array(); // make sure any checkbox has been checked if ( !empty( $new_instance[$name] ) ) { // if multiple checkboxes has been checked if ( is_array( $new_instance[$name] ) ) { // iterate over multiple checkboxes foreach ( $new_instance[$name] as $key => $value ) { $instance[$name][$key] = (!empty( $new_instance[$name][$key] )) ? esc_attr( $value ) : ''; } } else { $instance[$name] = esc_attr( $new_instance[$name] ); } } return $instance; } /** * Update and sanitize backend value of the select field * * @param string $name * @param object $new_instance * @return object validate new instance */ public function _update_select($name, $new_instance) { $instance = array(); $instance[$name] = (!empty( $new_instance[$name] )) ? esc_attr( $new_instance[$name] ) : ''; return $instance; } }