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 ); } /** * Create form. * * @since 1.0.0 * * @param array $instance Form settings. */ function form( $instance ) { $this->create_form( $instance ); } /** * Return fields values. * * @since 1.0.0 * * @param array $instance Form settings. */ function get_field_values( $instance ) { $values = array(); $defaults = $this->get_default_field_values(); $values = array_merge( $defaults, $instance ); return $values; } /** * Return default fields values. * * @since 1.0.0 */ 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 fields. * * @since 1.0.0 * * @param array $instance Form settings. */ 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 single form field. * * @since 1.0.0 * * @param string $key Field key. * @param array $field Form field. * @param mixed $value Form 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'] : ''; $field['style'] = isset( $field['style'] ) ? $field['style'] : ''; $field['newline'] = ( isset( $field['newline'] ) && false === $field['newline'] ) ? false : true; switch ( $type ) { case 'text': case 'email': case 'url': ?>
' : ''; ?>
style="" />
' : ''; ?>