<?php
/**
 * Class that adds a misc notice
 *
 * @since v.2.0
 *
 * @return void
 */
class WPA_Add_Notices{
	public $notificationId = '';
	public $notificationMessage = '';
	public $notificationClass = '';
	public $startDate = '';
	public $endDate = '';

	function __construct( $notificationId, $notificationMessage, $notificationClass = 'updated' , $startDate = '', $endDate = '' ){
		$this->notificationId = $notificationId;
		$this->notificationMessage = $notificationMessage;
		$this->notificationClass = $notificationClass;

		if( !empty( $startDate ) && time() < strtotime( $startDate ) )
			return;

		if( !empty( $endDate ) && time() > strtotime( $endDate ) )
			return;

		add_action( 'admin_notices', array( $this, 'add_admin_notice' ) );
		add_action( 'admin_init', array( $this, 'dismiss_notification' ) );
	}


	// Display a notice that can be dismissed in case the serial number is inactive
	function add_admin_notice() {
		global $current_user ;
		global $pagenow;

		$user_id = $current_user->ID;
		do_action( $this->notificationId.'_before_notification_displayed', $current_user, $pagenow );

		if ( current_user_can( 'manage_options' ) ){
			// Check that the user hasn't already clicked to ignore the message
			if ( ! get_user_meta($user_id, $this->notificationId.'_dismiss_notification' ) ) {
				echo $finalMessage = apply_filters($this->notificationId.'_notification_message','<div class="'. $this->notificationClass .'" >'.$this->notificationMessage.'</div>', $this->notificationMessage);
			}
			do_action( $this->notificationId.'_notification_displayed', $current_user, $pagenow );
		}
		do_action( $this->notificationId.'_after_notification_displayed', $current_user, $pagenow );
	}

	function dismiss_notification() {
		global $current_user;

		$user_id = $current_user->ID;

		do_action( $this->notificationId.'_before_notification_dismissed', $current_user );

		// If user clicks to ignore the notice, add that to their user meta
		if ( isset( $_GET[$this->notificationId.'_dismiss_notification']) && '0' == $_GET[$this->notificationId.'_dismiss_notification'] )
			add_user_meta( $user_id, $this->notificationId.'_dismiss_notification', 'true', true );

		do_action( $this->notificationId.'_after_notification_dismissed', $current_user );
	}
}

//new WPA_Add_Notices( 'wck_halloween',	sprintf( __( '<p>Your <strong>Profile Builder</strong> licence has expired. <br/>Please %1$sRenew Your Licence%2$s to receive access to automatic updates and support. %3$sPurchase one now%4$s %5$sDismiss%6$s</p>', 'profilebuilder'), "<a href='http://www.cozmoslabs.com/downloads/profile-builder-pro-1-year/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Renewal' target='_blank'>", "</a>", "<a href='http://www.cozmoslabs.com/downloads/profile-builder-pro-1-year/?utm_source=PB&utm_medium=dashboard&utm_campaign=PB-Renewal' target='_blank' class='button-primary'>", "</a>", "<a href='". add_query_arg( 'wck_holloween_dismiss_notification', '0' ) ."' class='wppb-dismiss-notification'>", "</a>" ),	'updated halloween',	'27 October 2014',	'30 October 2014' );