Last active
September 13, 2017 16:08
-
-
Save keesiemeijer/d5a80db6930a38f9abb3045af270bb70 to your computer and use it in GitHub Desktop.
Wiget with dropdown and back compatibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'widgets_init', 'register_my_scheduled_posts_widget' ); | |
function register_my_scheduled_posts_widget() { | |
register_widget( 'My_Scheduled_Posts_Widget' ); | |
} | |
class My_Scheduled_Posts_Widget extends WP_Widget { | |
public function __construct() { | |
$widget_ops = array( | |
'classname' => 'scheduled_recent_entries', | |
'description' => __( 'Your site’s most recent (scheduled) Posts.' ), | |
'customize_selective_refresh' => true, | |
); | |
parent::__construct( 'scheduled-recent-posts', __( 'Scheduled Posts' ), $widget_ops ); | |
} | |
/** | |
* Get validated widget instance settings with back compatibility. | |
*/ | |
function get_instance_settings( $instance ) { | |
/* | |
* Check if the old checkbox 'status_future' setting exists. | |
* And if the new dropdown 'include_posts' setting doesn't exist. | |
*/ | |
if ( isset( $instance['status_future'] ) && ! isset( $instance['include_posts'] ) ) { | |
// Back compatibility! | |
// Update the dropdown 'include_posts' setting with the old checkbox 'status_future' setting if it was set. | |
$instance['include_posts'] = $instance['status_future'] ? 'future' : 'all'; | |
} | |
// Check if the dropdown 'include_posts' setting exists and has a value. | |
if ( ! ( isset( $instance['include_posts'] ) && $instance['include_posts'] ) ) { | |
// Set it to default 'all' | |
$instance['include_posts'] = 'all'; | |
} | |
return $instance; | |
} | |
/** | |
* Widget display. | |
*/ | |
public function widget( $args, $instance ) { | |
// Get validated instance settings with back compatibility. | |
$instance = $this->get_instance_settings( $instance ); | |
$include = (string) $instance['include_posts']; | |
/* ***** Query for posts and display them here ***** */ | |
} | |
/** | |
* Update widget settings. | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['include_posts'] = $new_instance['include_posts']; | |
// Back compatibility! | |
// Don't update (checkbox) 'status_future' setting ever again. | |
unset( $instance['status_future'] ); | |
return $instance; | |
} | |
/** | |
* Widget settings form. | |
*/ | |
public function form( $instance ) { | |
// Get validated instance settings with back compatibility. | |
$instance = $this->get_instance_settings( $instance ); | |
$include = $instance['include_posts']; | |
// Dropdown options | |
$include_options = array( | |
'all' => __( 'all posts', 'your-plugin-textdomain' ), | |
'future' => __( 'posts with future dates only', 'your-plugin-textdomain' ), | |
'year' => __( 'posts from the current year', 'your-plugin-textdomain' ), | |
'month' => __( 'posts from the current month', 'your-plugin-textdomain' ), | |
'day' => __( 'posts from today', 'your-plugin-textdomain' ), | |
); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'include' ); ?>"> | |
<?php _e( 'Include Posts:', 'your-plugin-textdomain' ); ?> | |
</label> | |
<select name="<?php echo $this->get_field_name( 'include' ); ?>" id="<?php echo $this->get_field_id( 'include' ); ?>" class="widefat"> | |
<?php foreach ( $include_options as $name => $label ) : ?> | |
<option value="<?php echo esc_attr( $name ); ?>" <?php selected( $include, $name ); ?>> | |
<?php echo $label; ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</p> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment