Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Makes a custom Widget for displaying information posts
- *
- * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
- *
- * @package WordPress
- * @subpackage Twenty_Eleven
- * @since Twenty Eleven 1.0
- */
- class Information_Widget extends WP_Widget {
- function Information_Widget() {
- $widget_ops = array( 'classname' => 'information_widget', 'description' => __( 'Use this widget to display Information Posts', 'twentyeleven' ) );
- $this->WP_Widget( 'information_widget', __( 'Information Posts', 'twentyeleven' ), $widget_ops );
- $this->alt_option_name = 'information_widget';
- add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
- add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
- add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
- }
- function widget( $args, $instance ) {
- $cache = wp_cache_get( 'information_widget', 'widget' );
- if ( !is_array( $cache ) )
- $cache = array();
- if ( ! isset( $args['widget_id'] ) )
- $args['widget_id'] = null;
- if ( isset( $cache[$args['widget_id']] ) ) {
- echo $cache[$args['widget_id']];
- return;
- }
- ob_start();
- extract( $args, EXTR_SKIP );
- $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base);
- if ( ! isset( $instance['postid'] ) )
- $postid = 0;
- if ( ! $postid = absint( $instance['postid'] ) )
- $postid = 0;
- $thumbnail = ( isset( $instance['thumbnail'] ) ) ? $instance['thumbnail'] : false;
- $excerpt = ( isset( $instance['excerpt'] ) ) ? $instance['excerpt'] : false;
- // Set the global arguments
- global $widget_args;
- $widget_args=array();
- $widget_args['postid'] = $postid;
- $widget_args['thumbnail'] = $thumbnail;
- $widget_args['excerpt'] = $excerpt;
- $widget_args['uid'] = '-' .$args['widget_id'];
- // Display the infomation post
- echo $before_widget;
- echo $before_title;
- echo $title; // Can set this with a widget option, or omit altogether
- echo $after_title;
- get_template_part('information','html');
- echo $after_widget;
- // Reset the post globals as this query will have stomped on it
- wp_reset_postdata();
- $cache[$args['widget_id']] = ob_get_flush();
- wp_cache_set( 'information_widget', $cache, 'widget' );
- }
- /**
- * Deals with the settings when they are saved by the admin. Here is
- * where any validation should be dealt with.
- **/
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['title'] = strip_tags( $new_instance['title'] );
- $instance['postid'] = (int) $new_instance['postid'];
- $instance['thumbnail'] = $new_instance['thumbnail'];
- $instance['excerpt'] = $new_instance['excerpt'];
- $this->flush_widget_cache();
- $alloptions = wp_cache_get( 'alloptions', 'options' );
- if ( isset( $alloptions['information_widget'] ) )
- delete_option( 'information_widget' );
- return $instance;
- }
- function flush_widget_cache() {
- wp_cache_delete( 'information_widget', 'widget' );
- }
- /**
- * Displays the form for this widget on the Widgets page of the WP Admin area.
- **/
- function form( $instance ) {
- $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
- $postid = isset( $instance['postid'] ) ? absint( $instance['postid'] ) : 0;
- $thumbnail = ( isset( $instance['thumbnail'] ) ) ? $instance['thumbnail'] : false;
- $excerpt = isset( $instance['excerpt'] ) ? strip_tags( $instance['excerpt'] ) : false;
- $postlist = dr_info_get_posts();
- ?>
- <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Information Title', 'twentyeleven' ); ?></label>
- <br/>
- <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
- <p><label for="<?php echo $this->get_field_id('postid'); ?>"><?php _e("Information Post Title"); ?></label>
- <br/>
- <select id="<?php echo $this->get_field_id('postid'); ?>" name="<?php echo $this->get_field_name('postid'); ?>">
- <?php
- foreach ($postlist as $output) :
- $selected = ( $output['value'] == esc_attr($postid) ) ? ' selected = "selected" ' : '';
- $option = '<option '.$selected .'value="' . $output['value'];
- $option = $option .'">';
- $option = $option .$output['label'];
- $option = $option .'</option>';
- echo $option;
- endforeach;
- ?>
- </select></p>
- <p><label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e("Hide Post Featured Image"); ?></label>
- <br/>
- <?php $checked = ( $thumbnail ) ? ' checked=\"checked\" ' : ''; ?>
- <input type="checkbox" id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" value="true" <?php echo $checked; ?> />
- </p>
- <p><label for="<?php echo $this->get_field_id('excerpt'); ?>"><?php _e("Display Post Excerpt Only"); ?></label>
- <br/>
- <?php $checked = ( $excerpt ) ? ' checked=\"checked\" ' : ''; ?>
- <input type="checkbox" id="<?php echo $this->get_field_id('excerpt'); ?>" name="<?php echo $this->get_field_name('excerpt'); ?>" value="true" <?php echo $checked; ?> />
- </p>
- <?php
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement