esc_html__( 'A widget that shows post/page preview', 'biru' ) ) ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( // This widget has no title // Other fields 'post_id' => array ( 'biru_widgets_name' => 'post_id', 'biru_widgets_title' => esc_html__( 'Post ID', 'biru' ), 'biru_widgets_field_type' => 'number' ), 'display_title' => array ( 'biru_widgets_name' => 'display_title', 'biru_widgets_title' => esc_html__( 'Show post title', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'display_thumbnail' => array ( 'biru_widgets_name' => 'display_thumbnail', 'biru_widgets_title' => esc_html__( 'Show featured image', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'display_excerpt' => array ( 'biru_widgets_name' => 'display_excerpt', 'biru_widgets_title' => esc_html__( 'Show excerpt', 'biru' ), 'biru_widgets_field_type' => 'checkbox' ), 'read_more_text' => array ( 'biru_widgets_name' => 'read_more_text', 'biru_widgets_title' => esc_html__( 'Read more link text', 'biru' ), 'biru_widgets_description' => esc_html__( 'Leave empty for no link', 'biru' ), 'biru_widgets_field_type' => 'text' ), ); 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 ); $post_id = $instance['post_id']; $display_title = $instance['display_title']; $display_thumbnail = $instance['display_thumbnail']; $display_excerpt = $instance['display_excerpt']; $read_more_text = $instance['read_more_text']; // No need to do anything if 'post_id' field is empty if( isset( $post_id ) ) { // Check if that ID exists if( $post_object = get_post( $post_id ) ) { echo $before_widget; // Check if title needs to be shown if( $display_title && isset( $post_object->post_title ) ) echo $before_title . $post_object->post_title . $after_title; // Check if thumbnail needs to be shown and if post has thumbnail if( $display_thumbnail && has_post_thumbnail( $post_id ) ) echo '
' . get_the_post_thumbnail( $post_id, 'thumb-medium' ) . '
'; // Check if excerpt needs to be shown if( $display_excerpt ) echo '
'; echo wpautop( $post_object->post_excerpt ? $post_object->post_excerpt : biru_limit_string(strip_tags($post_object->post_content), 150) ); echo '
'; // Check if excerpt needs to be shown if( $read_more_text ) echo '
' . $read_more_text . '
'; 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 biru_widgets_updated_field_value() defined in widget-fields.php * * @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[$biru_widgets_name] = biru_widgets_updated_field_value( $widget_field, $new_instance[$biru_widgets_name] ); echo $instance[$biru_widgets_name]; } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses biru_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 ); $biru_widgets_field_value = isset( $instance[$biru_widgets_name] ) ? esc_attr( $instance[$biru_widgets_name] ) : ''; biru_widgets_show_widget_field( $this, $widget_field, $biru_widgets_field_value ); } } }