'bike_shop_latest_posts', 'description' => esc_html__( 'A widget to display the latest posts with thumbnail.', 'bike-shop' ) ); parent::__construct( 'bike_shop_latestpost', esc_html__( 'bikeshop: Latest Posts', 'bike-shop' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( 'latestpost_title' => array( 'bike_shop_widgets_name' => 'latestpost_title', 'bike_shop_widgets_title' => esc_html__( 'Widget title', 'bike-shop' ), 'bike_shop_widgets_field_type' => 'text' ), 'latestpost_post_order' => array( 'bike_shop_widgets_name' => 'latestpost_post_order', 'bike_shop_widgets_title' => esc_html__( 'Post Order', 'bike-shop' ), 'bike_shop_widgets_default' => 'default', 'bike_shop_widgets_field_type' => 'select', 'bike_shop_widgets_field_options' => array( 'default' => esc_html__( 'Default Order', 'bike-shop' ), 'random' => esc_html__( 'Random Order', 'bike-shop' ), ) ), 'latestpost_post_count' => array( 'bike_shop_widgets_name' => 'latestpost_post_count', 'bike_shop_widgets_title' => esc_html__( 'Post Count', 'bike-shop' ), 'bike_shop_widgets_default' => '5', 'bike_shop_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 ; } $bike_shop_widget_title = empty( $instance['latestpost_title'] ) ? '' : $instance['latestpost_title']; $bike_shop_post_order = empty( $instance['latestpost_post_order'] ) ? 'default' : $instance['latestpost_post_order']; $bike_shop_post_count = empty( $instance['latestpost_post_count'] ) ? '5' : $instance['latestpost_post_count']; echo $before_widget; ?>
    absint( $bike_shop_post_count ) ); if( 'random' === $bike_shop_post_order ) { $bike_shop_posts_args['orderby'] = 'rand'; } $bike_shop_posts_query = new WP_Query( $bike_shop_posts_args ); if( $bike_shop_posts_query->have_posts() ) { while( $bike_shop_posts_query->have_posts() ) { $bike_shop_posts_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[$bike_shop_widgets_name] = bike_shop_widgets_updated_field_value( $widget_field, $new_instance[$bike_shop_widgets_name] ); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses bike_shop_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 ); $bike_shop_widgets_field_value = !empty( $instance[$bike_shop_widgets_name] ) ? wp_kses_post( $instance[$bike_shop_widgets_name] ) : ''; bike_shop_widgets_show_widget_field( $this, $widget_field, $bike_shop_widgets_field_value ); } } }