web/wp-content/plugins/slideshow-jquery-image-gallery/classes/SlideshowPluginWidget.php
changeset 204 09a1c134465b
parent 203 f507feede89a
child 205 a4f7897e21a9
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 <?php
       
     2 /**
       
     3  * Class SlideshowPluginWidget allows showing one of your slideshows in your widget area.
       
     4  *
       
     5  * @author: Stefan Boonstra
       
     6  * @version: 04-10-12
       
     7  */
       
     8 class SlideshowPluginWidget extends WP_Widget {
       
     9 
       
    10 	/** Variables */
       
    11 	static $widgetName = 'Slideshow Widget';
       
    12 
       
    13 	/**
       
    14 	 * Initializes the widget
       
    15 	 */
       
    16 	function SlideshowPluginWidget(){
       
    17 		// Settings
       
    18 		$options = array(
       
    19 			'classname' => 'SlideshowWidget',
       
    20 			'description' => __('Enables you to show your slideshows in the widget area of your website.', 'slideshow-plugin')
       
    21 		);
       
    22 
       
    23 		// Create the widget.
       
    24 		$this->WP_Widget(
       
    25 			'slideshowWidget',
       
    26 			__('Slideshow Widget', 'slideshow-plugin'),
       
    27 			$options
       
    28 		);
       
    29 	}
       
    30 
       
    31 	/**
       
    32 	 * The widget as shown to the user.
       
    33 	 *
       
    34 	 * @param mixed array $args
       
    35 	 * @param mixed array $instance
       
    36 	 */
       
    37 	function widget($args, $instance){
       
    38 		// Get slideshowId
       
    39 		$slideshowId = '';
       
    40 		if(isset($instance['slideshowId']))
       
    41 			$slideshowId = $instance['slideshowId'];
       
    42 
       
    43 		// Get title
       
    44 		$title = '';
       
    45 		if(isset($instance['title']))
       
    46 			$title = $instance['title'];
       
    47 
       
    48 		// Prepare slideshow for output to website.
       
    49 		$output = SlideshowPlugin::prepare($slideshowId);
       
    50 
       
    51 		$beforeWidget = $afterWidget = $beforeTitle = $afterTitle = '';
       
    52 		if(isset($args['before_widget']))
       
    53 			$beforeWidget = $args['before_widget'];
       
    54 		if(isset($args['after_widget']))
       
    55 			$afterWidget = $args['after_widget'];
       
    56 		if(isset($args['before_title']))
       
    57 			$beforeTitle = $args['before_title'];
       
    58 		if(isset($args['after_title']))
       
    59 			$afterTitle = $args['after_title'];
       
    60 
       
    61 		// Output widget
       
    62 		echo $beforeWidget .
       
    63 			$beforeTitle . $title . $afterTitle .
       
    64 			$output .
       
    65 		$afterWidget;
       
    66 	}
       
    67 
       
    68 	/**
       
    69 	 * The form shown on the admins widget page. Here settings can be changed.
       
    70 	 *
       
    71 	 * @param mixed array $instance
       
    72 	 * @return string
       
    73 	 */
       
    74 	function form($instance){
       
    75 		// Defaults
       
    76 		$defaults = array(
       
    77 			'title' => __(self::$widgetName, 'slideshow-plugin'),
       
    78 			'slideshowId' => -1
       
    79 		);
       
    80 
       
    81 		// Merge database settings with defaults
       
    82 		$instance = wp_parse_args((array) $instance, $defaults);
       
    83 
       
    84 		// Get slideshows
       
    85 		$slideshows = get_posts(array(
       
    86 			'numberposts' => -1,
       
    87 			'offset' => 0,
       
    88 			'post_type' => SlideshowPluginPostType::$postType
       
    89 		));
       
    90 
       
    91 		// Include form
       
    92 		include(SlideshowPluginMain::getPluginPath() . '/views/' . __CLASS__ . '/form.php');
       
    93 	}
       
    94 
       
    95 	/**
       
    96 	 * Updates widget's settings.
       
    97 	 *
       
    98 	 * @param mixed array $newInstance
       
    99 	 * @param mixed array $instance
       
   100 	 * @return mixed array $instance
       
   101 	 */
       
   102 	function update($newInstance, $instance){
       
   103 		// Update title
       
   104 		if(isset($newInstance['title']))
       
   105 			$instance['title'] = $newInstance['title'];
       
   106 
       
   107 		// Update slideshowId
       
   108 		if(isset($newInstance['slideshowId']) && !empty($newInstance['slideshowId']))
       
   109 			$instance['slideshowId'] = $newInstance['slideshowId'];
       
   110 
       
   111 		// Save
       
   112 		return $instance;
       
   113 	}
       
   114 
       
   115 	/**
       
   116 	 * Registers this widget (should be called upon widget_init action hook)
       
   117 	 */
       
   118 	static function registerWidget(){
       
   119 		register_widget(__CLASS__);
       
   120 	}
       
   121 }