args = $args; $widget_options = isset( $args['widget'] ) ? $args['widget'] : array(); $widget_options = wp_parse_args( $widget_options, array( 'customize_selective_refresh' => true, ) ); $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 ); } /** * Outputs the settings form. * * @since 1.0.0 * * @param array $instance Widget instance. * @return void */ public function form( $instance ) { $this->create_form( $instance ); } /** * Returns field values. * * @since 1.0.0 * * @param array $instance Widget instance. * @return array Field values. */ protected function get_field_values( $instance ) { $values = array(); $defaults = $this->get_default_field_values(); $values = array_merge( $defaults, $instance ); return $values; } /** * Returns default field values. * * @since 1.0.0 * * @return array Default field 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; } /** * Create form. * * @since 1.0.0 * * @param array $instance Widget instance. * @return void */ 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 ] ); } } /** * Create form field. * * @since 1.0.0 * * @param string $key Key. * @param array $field Field detail. * @param mixed $value Field value. * @return void */ 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': ?>

show_field_description( $field ); ?>

/>
show_field_description( $field ); ?>

show_field_description( $field ); ?>

/> show_field_description( $field ); ?>

show_field_description( $field ); ?>


show_field_description( $field ); ?>

$label ) : ?> show_field_description( $field ); ?>

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 ); ?>

show_field_description( $field ); ?>

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 ); ?>

show_field_description( $field ); ?>

update_fields( $new_instance, $old_instance ); return $instance; } /** * Update fields with sanitized values. * * @since 1.0.0 * * @param array $new_instance New widget instance. * @param array $old_instance Old widget instance. * @return array Modified widget instance. */ protected function update_fields( $new_instance, $old_instance ) { $instance = $old_instance; foreach ( $this->fields as $key => $field ) { $instance[ $key ] = $this->sanitize_field( $key, $field, $new_instance[ $key ] ); } return $instance; } /** * Sanitize field. * * @since 1.0.0 * * @param string $key Key. * @param string $field Widget field. * @param mixed $new_value Value. * @return mixed Sanitized value. */ 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 = sanitize_textarea_field( $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; } }