<?php

/**
 * BuddyPress Site Wide Notice widget
 *
 * Adds a widget only if BP Theme Compat is used.
 *
 * @package    BuddyPress
 * @subpackage BP Messages Widget
 */

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;


/**
 * Adds a widget to display site wide notices
 *
 * @uses WP_Widget
 */
class BP_Messages_Widget extends WP_Widget {
	
	function __construct() {

		$widget_ops = array( 'description' => __( 'Displays site wide notices if any', 'buddypress' ) );
		parent::__construct( false, $name = __( '(BuddyPress) Site Wide Notices', 'buddypress' ), $widget_ops );
		
	}

	/**
	 * Register the widget
	 *
	 * @uses   register_widget()
	 */
	function register_widget() {
		register_widget( 'BP_Messages_Widget' );
	}
	
	/**
	 * Displays the output, the notices if any
	 * 
	 * @param  mixed $args Arguments
	 * @param  array $instance Instance
	 * @uses   bp_message_get_notices() to display the notices if any
	 */
	function widget( $args, $instance ) {


		extract( $args );
		
		//we dont need $title, $before_title and $after_title.
		
		//displays the notices if any.
		if( is_user_logged_in() )
			bp_message_get_notices( $before_widget, $after_widget );

	}

	/**
	 * Updates the widget options
	 *
	 * @param  array $new_instance The new instance options
	 * @param  array $old_instance The old instance options
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
		// no option to set..
		
		return $instance;
	}


	/**
	 * Output the widget options form
	 *
	 * @param  $instance Instance
	 */
	function form( $instance ) {
	    ?>

	    <p>
	    	<?php _e( 'Displays site wide notices if any', 'buddypress' );?>
		</p>
	    <?php
	}

}

add_action( 'bp_widgets_init', array( 'BP_Messages_Widget', 'register_widget' ), 10 );
?>