'', 'text' => '', 'fields' => array(), ); /* Widget setup */ function __construct() { /* Widget settings. */ $widget_ops = array( 'description' => _x( 'Progress bars', 'widget', 'the7mk2' ) ); /* Widget control settings. */ $control_ops = array( 'width' => 260 ); /* Create the widget. */ parent::__construct( 'presscore-progress-bars-widget', DT_WIDGET_PREFIX . _x( 'Progress bars', 'widget', 'the7mk2' ), $widget_ops, $control_ops ); } /* Display the widget */ function widget( $args, $instance ) { 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']; $fields = $instance['fields']; /* translators: progress bar percents */ $percent_text = __( '%d%', 'the7mk2' ); echo $before_widget ; // title if ( $title ) { echo $before_title . $title . $after_title; } // content if ( $text ) { echo '
' . apply_filters('get_the_excerpt', $text) . '
'; } // fields if ( !empty($fields) ) { echo '
'; foreach ( $fields as $field ) { $percent_field = sprintf( $percent_text, $field['percent'] ); if ( !empty($field['title']) || !empty($field['show_percent']) ) { printf( '
%s%s
', $field['title'], empty($field['show_percent']) ? '' : '' . $percent_field . '' ); } $field['percent'] = absint($field['percent']); if ( $field['percent'] > 100 ) $field['percent'] = 100; $style = ''; if ( $field['color'] ) { $style = ' style="background-color: ' . esc_attr($field['color']) . '"'; } printf( '
', $field['percent'], $style ); } echo '
'; } echo $after_widget; } /* Update the widget settings */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['text'] = $new_instance['text']; $instance['fields'] = $this->presscore_validate_fields( $new_instance['fields'] ); 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']; ?>

'', 'percent' => 100, 'show_percent' => true, 'color' => '' ); unset( $field_defaults['show_percent'] ); foreach ( $fields as &$field ) { $field = array_intersect_key($field, $allowed_fields); $field = wp_parse_args($field, $field_defaults); $field['percent'] = absint($field['percent']); if ( $field['percent'] > 100 ) $field['percent'] = 100; $field['show_percent'] = isset($field['show_percent']); $field['title'] = esc_html($field['title']); $field['color'] = esc_attr($field['color']); } unset($field); return $fields; } public static function presscore_register_widget() { register_widget( get_class() ); add_action( 'admin_footer', array(__CLASS__, 'presscore_admin_add_widget_templates') ); } /** * Add template for widget. */ public static function presscore_admin_add_widget_templates() { if ( 'widgets.php' != $GLOBALS['hook_suffix'] ) { return; } ?>