'', 'text' => '', 'fields' => array(), 'send_to' => '', 'msg_height' => 6, 'button_size' => 'm', 'button_title' => '' ); public static $fields_list = array(); /* Widget setup */ function __construct() { /* Widget settings. */ $widget_ops = array( 'description' => _x( 'Contact form', 'widget', 'the7mk2' ) ); /* Create the widget. */ parent::__construct( 'presscore-contact-form-widget', DT_WIDGET_PREFIX . _x( 'Contact form', 'widget', 'the7mk2' ), $widget_ops ); // fill fields list self::$fields_list = array( 'name' => array( 'title' => __( 'Name', 'the7mk2' ), 'defaults' => array( 'on' => true, 'required' => true ) ), 'email' => array( 'title' => __( 'E-mail', 'the7mk2' ), 'defaults' => array( 'on' => true, 'required' => true ) ), 'telephone' => array( 'title' => __( 'Telephone', 'the7mk2' ), 'defaults' => array( 'on' => false, 'required' => false ) ), 'country' => array( 'title' => __( 'Country', 'the7mk2' ), 'defaults' => array( 'on' => false, 'required' => false ) ), 'city' => array( 'title' => __( 'City', 'the7mk2' ), 'defaults' => array( 'on' => false, 'required' => false ) ), 'company' => array( 'title' => __( 'Company', 'the7mk2' ), 'defaults' => array( 'on' => false, 'required' => false ) ), 'website' => array( 'title' => __( 'Website', 'the7mk2' ), 'defaults' => array( 'on' => false, 'required' => false ) ), 'message' => array( 'title' => __( 'Message', 'the7mk2' ), 'defaults' => array( 'on' => true, 'required' => false ) ), ); // if ( !is_admin() ) { // add_action('init', array(&$this, 'presscore_enqueue_styles')); // } add_filter('dt_core_send_mail-to', array(&$this, 'presscore_send_to_filter'), 20); } /* Display the widget */ function widget( $args, $instance ) { // enqueue script in footer $this->presscore_enqueue_scripts(); static $number = 0; $number++; extract( $args ); $instance = wp_parse_args( (array) $instance, self::$widget_defaults ); /* Our variables from the widget settings. */ $title = apply_filters( 'widget_title', $instance['title'] ); $text = $instance['text']; $send_to = $instance['send_to']; $fields = $instance['fields']; $fields_not_empty = in_array(true, wp_list_pluck($fields, 'on') ); $msg_height = $instance['msg_height']; $class_adapter = array( 'email' => 'mail' ); echo $before_widget ; // title if ( $title ) echo $before_title . $title . $after_title; // content if ( $text ) echo '
' . apply_filters('get_the_excerpt', $text) . '
'; // fields if ( $fields_not_empty ) { // form begin echo '
' . "\n"; echo ''; // some sort of bot check echo ''; $fields_str = ''; $message = ''; // fields loop foreach ( $fields as $index=>$field ) { $tmp_field = ''; // if field disabled - continue if ( empty( $field['on'] ) ) { continue; } // if field not in reference array - continue // this check may be replased with array_intersect_key before loop if ( !isset(self::$fields_list[ $index ]) ) { continue; } // get field data from reference array ( title and default values ) $field_data = self::$fields_list[ $index ]; // init some handy variables $valid_class = ''; $title = $field_data['title']; $name = $index; $required_str = 'false'; $field_class = $index; // class adapter for some of fields if ( isset($class_adapter[ $index ]) ) $field_class = $class_adapter[ $index ]; // do some stuff for required fields if ( $field['required'] ) { // add * to title ) $title .= ' *'; // some strange flag $required_str = 'true'; // construct validation class for validationEngine $valid_params = array( 'required' ); switch( $index ) { case 'email': $valid_params[] = 'custom[email]'; break; case 'telephone': $valid_params[] = 'custom[phone]'; break; case 'website': $valid_params[] = 'custom[url]'; break; } $valid_class = ' class="' . esc_attr( sprintf('validate[%s]', implode( ',', $valid_params ) ) ) . '"'; } // escape some variables for output $title = esc_attr( $title ); $name = esc_attr( $name ); $required_str = esc_attr( $required_str ); // textarea or input ? if ( 'message' != $index ) { $tmp_field = '' . "\n"; } else { $tmp_field = '' . "\n"; } // end field output $tmp_field = sprintf( '%s', esc_attr($field_class), // $name, $title, $tmp_field ); if ( 'message' != $index ) { $fields_str .= $tmp_field; } else { $message = $tmp_field; } } if ( $fields_str ) { $fields_str = '
' . $fields_str . '
'; } echo $fields_str . $message; $button_title = !empty($instance['button_title']) ? $instance['button_title'] : __( 'Submit', 'the7mk2' ); // buttons echo '

'; echo presscore_get_button_html( array( 'href' => '#', 'title' => esc_html( $button_title ), 'class' => 'dt-btn dt-btn-' . esc_attr( $instance['button_size'] ) . ' dt-btn-submit', 'atts' => ' rel="nofollow"' ) ); echo presscore_get_button_html( array( 'href' => '#', 'title' => __( 'clear', 'the7mk2' ), 'class' => 'clear-form', 'atts' => ' rel="nofollow"' ) ); echo ''; echo '

'; // form end echo '' . "\n"; } echo $after_widget; } /* Update the widget settings */ function update( $new_instance, $old_instance ) { $instance = wp_parse_args( $old_instance, self::$widget_defaults ); $instance['title'] = strip_tags($new_instance['title']); $instance['text'] = $new_instance['text']; $instance['send_to'] = $new_instance['send_to']; $instance['fields'] = self::presscore_sanitize_fields($new_instance['fields']); $instance['msg_height'] = absint( $new_instance['msg_height'] ); $instance['button_size'] = in_array( $instance['button_size'], array( 's', 'm', 'l' ) ) ? $instance['button_size'] : self::$widget_defaults['button_size']; $instance['button_title'] = esc_html( $instance['button_title'] ); return $instance; } /** * Displays the widget settings controls on the widget panel. * Make use of the get_field_id() and get_field_name() function * when creating your form elements. This handles the confusing stuff. */ function form( $instance ) { /* Set up some default widget settings. */ $instance = wp_parse_args( (array) $instance, self::$widget_defaults ); $fields = empty( $instance['fields'] ) ? array() : $instance['fields']; ?>

$field ) : $value = isset( $fields[ $index ] ) ? $fields[ $index ] : array(); $field_on = isset( $value['on'] ) ? $value['on'] : $field['defaults']['on']; $field_required = isset( $value['required'] ) ? $value['required'] : $field['defaults']['required']; ?>

   

id == $_POST['widget_id'] ) { $option = get_option($this->option_name); if ( isset($option[ $this->number ]) && !empty($option[ $this->number ]['send_to']) ) { $em = $option[ $this->number ]['send_to']; } } return $em; } public static function presscore_sanitize_fields( $fields = array() ) { // clear fields $fields = array_intersect_key($fields, self::$fields_list); // sanitize data foreach ( $fields as &$field ) { $field['on'] = (bool) absint($field['on']); $field['required'] = isset( $field['required'] ); } return $fields; } public static function presscore_register_widget() { register_widget( get_class() ); } }