'awesome_blog_latest_posts', 'description' => esc_html__( 'A widget to display the Intro posts with thumbnail.', 'awesome-blog-lite' ) ); parent::__construct( 'Awesome_blog_intropost', esc_html__( 'Awesome: Intro Posts', 'awesome-blog-lite' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $awesome_blog_categories_dropdown = awesome_blog_categories_dropdown(); $fields = array( 'intropost_title' => array( 'awesome_blog_widgets_name' => 'intropost_title', 'awesome_blog_widgets_title' => esc_html__( 'Widget title', 'awesome-blog-lite' ), 'awesome_blog_widgets_field_type' => 'text' ), 'intropost_cat_id' => array( 'awesome_blog_widgets_name' => 'intropost_cat_id', 'awesome_blog_widgets_title' => esc_html__( 'Intro Post Category', 'awesome-blog-lite' ), 'awesome_blog_widgets_default' => 0, 'awesome_blog_widgets_field_type' => 'select', 'awesome_blog_widgets_field_options' => $awesome_blog_categories_dropdown ), 'intropost_post_count' => array( 'awesome_blog_widgets_name' => 'intropost_post_count', 'awesome_blog_widgets_title' => esc_html__( 'Post Count', 'awesome-blog-lite' ), 'awesome_blog_widgets_default' => '5', 'awesome_blog_widgets_field_type' => 'number' ), ); 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 ; } $awesome_blog_widget_title = empty( $instance['intropost_title'] ) ? '' : $instance['intropost_title']; $awesome_blog_feature_cat_id = empty( $instance['intropost_cat_id'] ) ? '' : $instance['intropost_cat_id']; $awesome_blog_post_count = empty( $instance['intropost_post_count'] ) ? '4' : $instance['intropost_post_count']; if( is_numeric( $awesome_blog_feature_cat_id ) ) { $term = get_term( $awesome_blog_feature_cat_id ); $awesome_blog_feature_cat_id = $term->slug; } $awesome_blog_widget_args = array( 'title' => $awesome_blog_widget_title, 'cat_slug' => $awesome_blog_feature_cat_id, 'count' => $awesome_blog_post_count ); echo $before_widget; do_action('awesome_blog_intro_post', $awesome_blog_widget_args ); echo $after_widget; } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @uses awesome_blog_widgets_updated_field_value() * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; $widget_fields = $this->widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { extract( $widget_field ); // Use helper function to get updated field values $instance[$awesome_blog_widgets_name] = awesome_blog_widgets_updated_field_value( $widget_field, $new_instance[$awesome_blog_widgets_name] ); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses awesome_blog_widgets_show_widget_field() */ 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 ); if( $awesome_blog_widgets_field_type === 'select' ) { if( is_numeric( $instance[$awesome_blog_widgets_name] ) ) { $term = get_term( $instance[$awesome_blog_widgets_name] ); $instance[$awesome_blog_widgets_name] = $term->slug; } } $awesome_blog_widgets_field_value = !empty( $instance[$awesome_blog_widgets_name] ) ? wp_kses_post( $instance[$awesome_blog_widgets_name] ) : ''; awesome_blog_widgets_show_widget_field( $this, $widget_field, $awesome_blog_widgets_field_value ); } } }