'awesome_blog_latest_posts', 'description' => esc_html__( 'A widget to display the latest posts with thumbnail.', 'awesome-blog-lite' ) ); parent::__construct( 'Awesome_blog_postgrid', esc_html__( 'Awesome: Post Grid Layout', 'awesome-blog-lite' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( 'postgrid_title' => array( 'awesome_blog_widgets_name' => 'postgrid_title', 'awesome_blog_widgets_title' => esc_html__( 'Widget title', 'awesome-blog-lite' ), 'awesome_blog_widgets_field_type' => 'text' ), 'postgrid_category_id' => array( 'awesome_blog_widgets_name' => 'postgrid_category_id', 'awesome_blog_widgets_title' => esc_html__( 'Feature Category', 'awesome-blog-lite' ), 'awesome_blog_widgets_field_type' => 'multicheckboxes', 'awesome_blog_widgets_field_options' => awesome_blog_categories_checklist() ), 'postgrid_post_count' => array( 'awesome_blog_widgets_name' => 'postgrid_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['postgrid_title'] ) ? '' : $instance['postgrid_title']; $awesome_blog_feature_cat_id = empty( $instance['postgrid_category_id'] ) ? '' : $instance['postgrid_category_id']; $awesome_blog_post_count = empty( $instance['postgrid_post_count'] ) ? '5' : $instance['postgrid_post_count']; if ( !empty( $awesome_blog_feature_cat_id ) ) { foreach( $awesome_blog_feature_cat_id as $singlekey => $single ) { if( is_numeric( $singlekey ) ) { $term = get_term( $singlekey ); $term_slug = $term->slug; unset( $awesome_blog_feature_cat_id[$singlekey] ); $awesome_blog_feature_cat_id[$term_slug] = $single; } } $checked_cats = array(); foreach( $awesome_blog_feature_cat_id as $cat_key => $cat_value ) { $checked_cats[] = $cat_key; } } else { return; } $awesome_blog_feature_cat_id = implode( ",", $checked_cats ); $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_post_module_1', $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 === 'multicheckboxes' ) { foreach( $instance[$awesome_blog_widgets_name] as $singlekey => $single ) { if( is_numeric( $singlekey ) ) { $term = get_term( $singlekey ); $term_slug = $term->slug; unset( $instance[$awesome_blog_widgets_name][$singlekey] ); $instance[$awesome_blog_widgets_name][$term_slug] = $single; } } } $awesome_blog_widgets_field_value = !empty( $instance[$awesome_blog_widgets_name] ) ? $instance[$awesome_blog_widgets_name]: ''; awesome_blog_widgets_show_widget_field( $this, $widget_field, $awesome_blog_widgets_field_value ); } } }