__( 'A widget that shows post/page preview', 'adsoft_language' )
)
);
}
/**
* Helper function that holds widget fields
* Array is used in update and form functions
*/
private function widget_fields() {
$fields = array(
// Title
'widget_title' => array(
'adsoft_widgets_name' => 'widget_title',
'adsoft_widgets_title' => __( 'Title', 'adsoft_language' ),
'adsoft_widgets_field_type' => 'text'
),
// Other fields
'post_id_one' => array (
'adsoft_widgets_name' => 'post_id_one',
'adsoft_widgets_title' => __( '#1 Post ID', 'adsoft_language' ),
'adsoft_widgets_field_type' => 'number'
),
'post_id_two' => array (
'adsoft_widgets_name' => 'post_id_two',
'adsoft_widgets_title' => __( '#2 Post ID', 'adsoft_language' ),
'adsoft_widgets_field_type' => 'number'
),
'post_id_three' => array (
'adsoft_widgets_name' => 'post_id_three',
'adsoft_widgets_title' => __( '#3 Post ID', 'adsoft_language' ),
'adsoft_widgets_field_type' => 'number'
),
'post_id_four' => array (
'adsoft_widgets_name' => 'post_id_four',
'adsoft_widgets_title' => __( '#4 Post ID', 'adsoft_language' ),
'adsoft_widgets_field_type' => 'number'
),
'post_id_five' => array (
'adsoft_widgets_name' => 'post_id_five',
'adsoft_widgets_title' => __( '#5 Post ID', 'adsoft_language' ),
'adsoft_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 );
$post_id_one = $instance['post_id_one'];
$post_id_two = $instance['post_id_two'];
$post_id_three = $instance['post_id_three'];
$post_id_four = $instance['post_id_four'];
$post_id_five = $instance['post_id_five'];
$widget_title = apply_filters( 'widget_title', $instance['widget_title'] );
echo $before_widget;
// Show title
if( isset( $widget_title ) ) {
echo $before_title . $widget_title . $after_title;
}
echo '
';
// No need to do anything if 'post_id_one' field is empty
if( isset( $post_id_one ) ) {
// Check if that ID exists
if( $post_object = get_post( $post_id_one ) ) {
echo '- ' . get_the_post_thumbnail( $post_id_one, 'thumb-medium' ) . '' . $post_object->post_title . '';
echo '
';
}
}
// No need to do anything if 'post_id_two' field is empty
if( isset( $post_id_two ) ) {
// Check if that ID exists
if( $post_object = get_post( $post_id_two ) ) {
echo '- ' . $post_object->post_title . '';
echo '
';
}
}
// No need to do anything if 'post_id_three' field is empty
if( isset( $post_id_three ) ) {
// Check if that ID exists
if( $post_object = get_post( $post_id_three ) ) {
echo '- ' . $post_object->post_title . '' ;
echo '
';
}
}
// No need to do anything if 'post_id_four' field is empty
if( isset( $post_id_four ) ) {
// Check if that ID exists
if( $post_object = get_post( $post_id_four ) ) {
echo '- ' . $post_object->post_title . '' ;
echo '
';
}
}
// No need to do anything if 'post_id_five' field is empty
if( isset( $post_id_five ) ) {
// Check if that ID exists
if( $post_object = get_post( $post_id_five ) ) {
echo '- ' . $post_object->post_title . '' ;
echo '
';
}
}
echo '
';
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 adsoft_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[$adsoft_widgets_name] = adsoft_widgets_updated_field_value( $widget_field, $new_instance[$adsoft_widgets_name] );
echo $instance[$adsoft_widgets_name];
}
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*
* @uses adsoft_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 );
$adsoft_widgets_field_value = isset( $instance[$adsoft_widgets_name] ) ? esc_attr( $instance[$adsoft_widgets_name] ) : '';
adsoft_widgets_show_widget_field( $this, $widget_field, $adsoft_widgets_field_value );
}
}
}