'editorial_posts_list', 'description' => __( 'Display latest or random posts in list view.', 'editorial' ) ); parent::__construct( 'editorial_posts_list', __( 'Editorial: Posts Lists', 'editorial' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $editorial_post_list_option = array( 'latest' => __( 'Latest Posts', 'editorial' ), 'random' => __( 'Random Posts', 'editorial' ) ); $fields = array( 'editorial_block_title' => array( 'editorial_widgets_name' => 'editorial_block_title', 'editorial_widgets_title' => __( 'Widget Title', 'editorial' ), 'editorial_widgets_field_type' => 'text' ), 'editorial_block_posts_count' => array( 'editorial_widgets_name' => 'editorial_block_posts_count', 'editorial_widgets_title' => __( 'No. of Posts', 'editorial' ), 'editorial_widgets_default' => 4, 'editorial_widgets_field_type' => 'number' ), 'editorial_block_posts_type' => array( 'editorial_widgets_name' => 'editorial_block_posts_type', 'editorial_widgets_title' => __( 'Posts Type', 'editorial' ), 'editorial_widgets_default' => 'latest', 'editorial_widgets_field_options'=> $editorial_post_list_option, 'editorial_widgets_field_type' => 'radio' ), ); return $fields; } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); if( empty( $instance ) ) { return ; } $editorial_block_title = empty( $instance['editorial_block_title'] ) ? '' : $instance['editorial_block_title']; $editorial_block_posts_count = intval( empty( $instance['editorial_block_posts_count'] ) ? 4 : $instance['editorial_block_posts_count'] ); $editorial_block_posts_type = empty( $instance['editorial_block_posts_type'] ) ? '' : $instance['editorial_block_posts_type']; echo $before_widget; ?>

have_posts() ) { while( $posts_list_query->have_posts() ) { $posts_list_query->the_post(); ?>

widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { extract( $widget_field ); // Use helper function to get updated field values $instance[$editorial_widgets_name] = editorial_widgets_updated_field_value( $widget_field, $new_instance[$editorial_widgets_name] ); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses editorial_widgets_show_widget_field() defined in widget-fields.php */ public function form( $instance ) { $widget_fields = $this->widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { // Make array elements available as variables extract( $widget_field ); $editorial_widgets_field_value = !empty( $instance[$editorial_widgets_name] ) ? wp_kses_post( $instance[$editorial_widgets_name] ) : ''; editorial_widgets_show_widget_field( $this, $widget_field, $editorial_widgets_field_value ); } } }