<?php

class Intense_Members extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;
		global $post;

		$this->title = __( 'Members', 'intense' );
		$this->description = __( 'Show special content for your members in WordPress. The members shortcode content will only be shown to registered users. This is useful for special call-to-actions or messages you only want to show to registered users.', 'intense' );
		$this->category = __( 'Elements', 'intense' );
		$this->preview_content = __( "Member Only Content", 'intense' );
		$this->show_preview = true;
		$this->is_container = true;		
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		if ( is_feed() ) return;

		$id = ( !empty ( $id ) ) ? ' id="' . $id . '"' : '';

		$role_allowed = true;

		if ( !empty( $roles ) ) {
			$roles = explode( ',', $roles );
			$roles = array_map( 'trim', $roles );
			$user = wp_get_current_user();	        
	        $user_role = reset($user->roles);

	        $roles = array_combine($roles, $roles);

	        if ( !empty( $user_role ) && is_array( $roles ) && !array_key_exists( $user_role, $roles ) ) {            
	            $role_allowed = 0;
	        }
		}

		if ( !is_user_logged_in() || !$role_allowed ) {
			if ( !isset( $show_message ) || $show_message ) {
				if ( empty( $login_url ) ) {
					$login_url = wp_login_url( get_permalink() );
				}

				$output = intense_run_shortcode( 'intense_promo_box', array( 
					'button_color' => $login_color,
					'background_color' => $background_color,
					'color' => $font_color,
					'size' => 'small',
					'button_link' => $login_url,
					'button_text' => $login_title,
					'button_size' => 'default'
				), $text );

				return '<div' . $id . ' class="intense members' . ( !empty( $class ) ? ' ' . $class : '' ) . '">' . $output . '</div><style>.intense.members a.promo { margin: 0 !important; }</style>';
			}
		} else {
			return do_shortcode( $content );
		}		
	}
}
