args = $args; $widget_options = isset( $args['widget'] ) ? $args['widget'] : array(); $control_options = isset( $args['control'] ) ? $args['control'] : array(); $this->fields = isset( $args['fields'] ) ? $args['fields'] : array(); parent::__construct( $args['id'], $args['label'], $widget_options, $control_options ); } function form( $instance ) { $this->create_form( $instance ); } function get_field_values( $instance ) { $values = array(); $defaults = $this->get_default_field_values(); $values = array_merge( $defaults, $instance ); return $values; } private function get_default_field_values() { $default_values = array(); if ( ! empty( $this->fields ) ) { foreach ( $this->fields as $key => $field ) { $default_values[ $key ] = null; if ( ! isset( $instance[ $key ] ) && isset( $field['default'] ) ) { $default_values[ $key ] = $field['default']; } } } return $default_values; } protected function create_form( $instance ) { if ( ! $this->fields ) { return; } $values = $this->get_field_values( $instance ); foreach ( $this->fields as $key => $field ) { $this->create_form_field( $key, $field, $values[ $key ] ); } } protected function create_form_field( $key, $field, $value ) { $type = isset( $field['type'] ) ? $field['type'] : 'text'; $class = isset( $field['class'] ) ? sanitize_html_class( $field['class'] ) : ''; $field['rows'] = ( isset( $field['rows'] ) && absint( $field['rows'] ) > 0 ) ? absint( $field['rows'] ) : 5; $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; switch ( $type ) { case 'text': case 'email': case 'url': ?>

/>


$label ) : ?>

get_field_name( $key ); $page_args['id'] = $this->get_field_id( $key ); $page_args['show_option_none'] = ( isset( $field['show_option_none'] ) ) ? esc_html( $field['show_option_none'] ) : ''; $page_args['class'] = esc_attr( $class ) ; ?>

get_field_name( $key ); $tax_args['id'] = $this->get_field_id( $key ); $tax_args['show_option_all'] = ( isset( $field['show_option_all'] ) ) ? esc_html( $field['show_option_all'] ) : '' ; $tax_args['class'] = esc_attr( $class ); ?>

update_form( $new_instance, $old_instance ); return $instance; } protected function update_form( $new_instance, $old_instance ) { $instance = $old_instance; foreach ( $this->fields as $key => $field ) { if( is_array( $new_instance[ $key ] ) ) : $instance[ $key ] = $this->sanitize_field( $key, $field, serialize( $new_instance[ $key ] ) ); else: $instance[ $key ] = $this->sanitize_field( $key, $field, $new_instance[ $key ] ); endif; } return $instance; } protected function sanitize_field( $key, $field, $new_value ) { $ret = null; $field_type = isset( $field['type'] ) ? $field['type'] : 'text'; switch ( $field_type ) { case 'text': $ret = sanitize_text_field( $new_value ); break; case 'number': $ret = absint( $new_value ); break; case 'color': $ret = sanitize_text_field( $new_value ); break; case 'image': $ret = esc_url_raw( $new_value ); break; case 'select': $ret = sanitize_text_field( $new_value ); break; case 'radio': $ret = sanitize_text_field( $new_value ); break; case 'textarea': $ret = wp_kses_post( $new_value ); break; case 'url': $ret = esc_url_raw( $new_value ); break; case 'email': $ret = sanitize_email( $new_value ); break; case 'dropdown-pages': $ret = absint( $new_value ); break; case 'dropdown-taxonomies': $ret = absint( $new_value ); break; case 'checkbox': $ret = (bool)$new_value; break; default: $ret = sanitize_text_field( $new_value ); break; } return $ret; } } }