SHARE
TWEET

Untitled

a guest Sep 13th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. add_action( 'widgets_init',  'register_my_scheduled_posts_widget' );
  4.  
  5. function register_my_scheduled_posts_widget() {
  6.     register_widget( 'My_Scheduled_Posts_Widget' );
  7. }
  8.  
  9. class My_Scheduled_Posts_Widget extends WP_Widget {
  10.  
  11.     public function __construct() {
  12.         $widget_ops = array(
  13.             'classname' => 'scheduled_recent_entries',
  14.             'description' => __( 'Your site&#8217;s most recent (scheduled) Posts.' ),
  15.             'customize_selective_refresh' => true,
  16.         );
  17.         parent::__construct( 'scheduled-recent-posts', __( 'Scheduled Posts' ), $widget_ops );
  18.     }
  19.  
  20.     /**
  21.      * Get validated widget instance settings with back compatibility.
  22.      */
  23.     function get_instance_settings( $instance ) {
  24.         /*
  25.          * Check if the old checkbox 'status_future' setting exists.
  26.          * And if the new dropdown 'include' setting doesn't exist.
  27.          */
  28.         if ( isset( $instance['status_future'] ) && ! isset( $instance['include'] ) ) {
  29.  
  30.             // Back compatibility!
  31.             // Update the dropdown 'include' setting with the old checkbox 'status_future' setting if it was set.
  32.             $instance['include'] = $instance['status_future'] ? 'future' : 'all';
  33.         }
  34.  
  35.         // Check if the dropdown 'include' setting exists and has a value.
  36.         if ( ! ( isset( $instance['include'] ) && $instance['include'] ) ) {
  37.             // Set it to default 'all'
  38.             $instance['include'] = 'all';
  39.         }
  40.  
  41.         return $instance;
  42.     }
  43.  
  44.     /**
  45.      * Widget display.
  46.      */
  47.     public function widget( $args, $instance ) {
  48.         // Get validated instance settings with back compatibility.
  49.         $instance = $this->get_instance_settings( $instance );
  50.  
  51.         $include = (string) $instance['include'];
  52.  
  53.         /*  ***** Query for posts and display them here *****  */
  54.     }
  55.  
  56.     /**
  57.      * Update widget settings.
  58.      */
  59.     public function update( $new_instance, $old_instance ) {
  60.         $instance = $old_instance;
  61.  
  62.         $instance['include'] = $new_instance['include'];
  63.  
  64.         // Back compatibility!
  65.         // Don't update (checkbox) 'status_future' setting ever again.
  66.         unset( $instance['status_future'] );
  67.  
  68.         return $instance;
  69.     }
  70.  
  71.     /**
  72.      * Widget settings form.
  73.      */
  74.     public function form( $instance ) {
  75.         // Get validated instance settings with back compatibility.
  76.         $instance = $this->get_instance_settings( $instance );
  77.         $include  = $instance['include'];
  78.  
  79.         // Dropdown options
  80.         $include_options = array(
  81.             'all'    => __( 'all posts', 'your-plugin-textdomain' ),
  82.             'future' => __( 'posts with future dates only', 'your-plugin-textdomain' ),
  83.             'year'   => __( 'posts from the current year', 'your-plugin-textdomain' ),
  84.             'month'  => __( 'posts from the current month', 'your-plugin-textdomain' ),
  85.             'day'    => __( 'posts from today', 'your-plugin-textdomain' ),
  86.         );
  87.  
  88. ?>
  89. <p>
  90.     <label for="<?php echo $this->get_field_id( 'include' ); ?>">
  91.         <?php _e( 'Include Posts:', 'your-plugin-textdomain' ); ?>
  92.     </label>
  93.     <select name="<?php echo $this->get_field_name( 'include' ); ?>" id="<?php echo $this->get_field_id( 'include' ); ?>" class="widefat">
  94.         <?php foreach ( $include_options as $name => $label ) : ?>
  95.         <option value="<?php echo esc_attr( $name ); ?>" <?php selected( $include, $name ); ?>>
  96.             <?php echo $label; ?>
  97.         </option>
  98.         <?php endforeach; ?>
  99.     </select>
  100. </p>
  101. <?php
  102.     }
  103. }
RAW Paste Data
Top