<?php
//Admin functions
use EM\Archetypes;

function em_admin_menu(){
	global $menu, $submenu;
	//Count pending bookings
	foreach ( Archetypes::get_cpts( [], ['event', 'types'] ) as $post_type ) {
		Archetypes::set_current( $post_type );
		$archetype = Archetypes::get( $post_type );
	    if( em_get_option('dbem_rsvp_enabled') ){
			$bookings_num = '';
			$bookings_pending_count = apply_filters('em_bookings_pending_count',0);
			if( em_get_option('dbem_bookings_approval') == 1){
				$bookings_pending_count += EM_Bookings::count(array('status'=>'0', 'blog'=>get_current_blog_id()));
			}
			if($bookings_pending_count > 0){
				$bookings_num = '<span class="update-plugins count-'.$bookings_pending_count.'"><span class="plugin-count">'.$bookings_pending_count.'</span></span>';
			}
	    }else{
	        $bookings_num = '';
			$bookings_pending_count = 0;
	    }
		//Count pending events
		$events_num = '';
		$events_pending_count = EM_Events::count(array('status'=>0, 'scope'=>'all', 'blog'=>get_current_blog_id(), 'event_type' => ['single', 'recurring']));
		//TODO Add flexible permissions
		if($events_pending_count > 0){
			$events_num = '<span class="update-plugins count-'.$events_pending_count.'"><span class="plugin-count">'.$events_pending_count.'</span></span>';
		}
		//Count pending recurring events
		$events_recurring_num = '';
		$events_recurring_pending_count = EM_Events::count(array('status'=>0, 'recurring'=>1, 'scope'=>'all', 'blog'=>get_current_blog_id(), 'event_type' => ['recurring']));
		//TODO Add flexible permissions
		if($events_recurring_pending_count > 0){
			$events_recurring_num = '<span class="update-plugins count-'.$events_recurring_pending_count.'"><span class="plugin-count">'.$events_recurring_pending_count.'</span></span>';
		}
		$both_pending_count = apply_filters('em_items_pending_count', $events_pending_count + $bookings_pending_count + $events_recurring_pending_count);
		$both_num = ($both_pending_count > 0) ? '<span class="update-plugins count-'.$both_pending_count.'"><span class="plugin-count">'.$both_pending_count.'</span></span>':'';
	    // Add a submenu to the custom top-level menu:
	    $plugin_pages = array();
	    if( em_get_option('dbem_rsvp_enabled') ){
			$plugin_pages['bookings'] = add_submenu_page('edit.php?post_type='.$post_type, __('Bookings', 'events-manager'), __('Bookings', 'events-manager').$bookings_num, 'manage_bookings', 'events-manager-bookings', "em_bookings_page");
	    }
		$plugin_pages['options'] = add_submenu_page('edit.php?post_type='.$post_type, __('Events Manager Settings','events-manager'),__('Settings','events-manager'), 'manage_options', "events-manager-options", 'em_admin_options_page');
		$plugin_pages['help'] = add_submenu_page('edit.php?post_type='.$post_type, __('Getting Help for Events Manager','events-manager'),__('Help','events-manager'), 'manage_options', "events-manager-help", 'em_admin_help_page');
		//If multisite global with locations set to be saved in main blogs we can force locations to be created on the main blog only
		if( EM_MS_GLOBAL && !is_main_site() && get_site_option('dbem_ms_mainblog_locations') ){
			// include_once: em_admin_menu() builds menus inside a foreach over event archetypes, so a plain include would redeclare em_admin_ms_locations() on the second CPT (fatal on MS-global subsites). The function only needs defining once.
			include_once( dirname(__FILE__)."/em-ms-locations.php" );
			$plugin_pages['locations'] = add_submenu_page('edit.php?post_type='.$post_type, __('Locations','events-manager'),__('Locations','events-manager'), 'read_others_locations', "locations", 'em_admin_ms_locations');
		}
		if ( $post_type === Archetypes::$event['cpt'] ) {
			$plugin_pages = apply_filters( 'em_create_events_submenu', $plugin_pages );
		} else {
			$plugin_pages = apply_filters( 'em_create_archetype_submenu_' . $post_type, $plugin_pages );
		}
		//We have to modify the menus manually to add pending counts
		if( !empty($both_num) ){ //Main Event Menu
			//go through the menu array and modify the events menu if found
			foreach ( (array)$menu as $key => $parent_menu ) {
				if ( $parent_menu[2] == 'edit.php?post_type='.$post_type ){
					$menu[$key][0] = $menu[$key][0]. $both_num;
					break;
				}
			}
		}
		// Pending Events # Pill - go through the menu array and modify the events menu if found
		if( !empty($events_num) && !empty($submenu['edit.php?post_type='.$post_type]) ){ //Submenu Event Item
			foreach ( (array)$submenu['edit.php?post_type='.$post_type] as $key => $submenu_item ) {
				if ( $submenu_item[2] == 'edit.php?post_type='.$post_type ){
					$submenu['edit.php?post_type='.$post_type][$key][0] = $submenu['edit.php?post_type='.$post_type][$key][0]. $events_num;
					break;
				}
			}
		}
		// Recurring Events # Pill - go through the menu array and modify the recurring events menu if found
		if( !empty($events_recurring_num) && !empty($submenu['edit.php?post_type='.$post_type]) ){ //Submenu Recurring Event Item
			if ( em_get_option('dbem_repeating_enabled') ) {
				$repeating_post_type = empty( $archetype['repeating']['cpt'] ) ? $archetype['cpt'] . '-recurring' : $archetype['repeating']['cpt'];
				foreach ( (array)$submenu['edit.php?post_type='.$post_type] as $key => $submenu_item ) {
					if ( $submenu_item[2] == 'edit.php?post_type='. $repeating_post_type ){
						$submenu['edit.php?post_type='.$post_type][$key][0] = $submenu['edit.php?post_type='.$post_type][$key][0]. $events_recurring_num;
						break;
					}
				}
			}
		}
	}
	Archetypes::revert_current();
}
add_action('admin_menu','em_admin_menu');

function em_admin_dashicon(){
	?>
	<style type="text/css">
		@font-face {
		  font-family: 'em_dashicons';
		  src: url('<?php echo EM_DIR_URI; ?>includes/fonts/em-dashicons.eot'); // this is for IE
		}
		@font-face {
		  font-family: 'em_dashicons';
		  src: url(data:application/font-woff;charset=utf-8;base64,d09GRk9UVE8AAAVIAAoAAAAABQAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABDRkYgAAAA9AAAAb8AAAG/+vk+uU9TLzIAAAK0AAAAYAAAAGAIIvy2Y21hcAAAAxQAAABMAAAATBpVzFdnYXNwAAADYAAAAAgAAAAIAAAAEGhlYWQAAANoAAAANgAAADYBZ6hBaGhlYQAAA6AAAAAkAAAAJAPIAeZobXR4AAADxAAAABQAAAAUAwAAM21heHAAAAPYAAAABgAAAAYABVAAbmFtZQAAA+AAAAFFAAABRVcZpu5wb3N0AAAFKAAAACAAAAAgAAMAAAEABAQAAQEBCGljb21vb24AAQIAAQA6+BwC+BsD+BgEHgoAGVP/i4seCgAZU/+LiwwHi2v4lPh0BR0AAAB2Dx0AAAB7ER0AAAAJHQAAAbYSAAYBAQgPERMWG2ljb21vb25pY29tb29udTB1MXUyMHVFNjAwAAACAYkAAwAFAgABAAQABwAKAA0BLfyUDvyUDvyUDvuUD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		.dashicons-before.dashicons-em-calendar:before {
		  font-family: 'em_dashicons';
		  content: '\e600';
		}
	</style>
	<?php
}
add_action('admin_head','em_admin_dashicon');

function em_ms_admin_menu(){
	add_menu_page( __('Events Manager','events-manager'), __('Events Manager','events-manager'), 'activate_plugins', 'events-manager-options', 'em_ms_admin_options_page', 'dashicons-calendar' );
	add_submenu_page('events-manager-options', __('Update Blogs','events-manager'),__('Update Blogs','events-manager'), 'activate_plugins', "events-manager-update", 'em_ms_upgrade');
}
add_action('network_admin_menu','em_ms_admin_menu');

function em_admin_init(){
	//in MS global mode and locations are stored in the main blog, then a user must have at least a subscriber role
	if( EM_MS_GLOBAL && is_user_logged_in() && !is_main_site() && get_site_option('dbem_ms_mainblog_locations') ){
		EM_Object::ms_global_switch();
		$user = new WP_User(get_current_user_id());
		if( count($user->roles) == 0 ){
			$user->set_role('subscriber');
		}
		EM_Object::ms_global_switch_back();
	}
}
add_action('admin_init','em_admin_init');

/**
 * Generate warnings and notices in the admin area
 */
function em_admin_warnings() {
	global $EM_Notices;
	//If we're editing the events page show hello to new user
	$events_page_id = em_get_option ( 'dbem_events_page' );
	$dismiss_link_joiner = ( count($_GET) > 0 ) ? '&amp;':'?';
	
	if( current_user_can('activate_plugins') ){
		
		//If events page couldn't be created or is missing
		if( !empty($_GET['em_dismiss_events_page']) && wp_verify_nonce($_GET['em_dismiss_events_page'], 'em_dismiss_notice') ){
			update_option('dbem_dismiss_events_page',1);
		}else{
			if ( !get_page($events_page_id) && !em_get_option('dbem_dismiss_events_page') ){
				?>
				<div id="em_page_error" class="updated">
					<p><?php echo sprintf ( __( 'Uh Oh! For some reason WordPress could not create an events page for you (or you just deleted it). Not to worry though, all you have to do is create an empty page, name it whatever you want, and select it as your events page in your <a href="%s">settings page</a>. Sorry for the extra step! If you know what you are doing, you may have done this on purpose, if so <a href="%s">ignore this message</a>', 'events-manager'), EM_ADMIN_URL .'&amp;page=events-manager-options', esc_url($_SERVER['REQUEST_URI'].$dismiss_link_joiner.'em_dismiss_events_page='.wp_create_nonce('em_dismiss_notice')) ); ?></p>
				</div>
				<?php
			}
		}
	
		if( is_multisite() && !empty($_REQUEST['page']) && $_REQUEST['page']=='events-manager-options' && em_wp_is_super_admin() && em_get_option('dbem_ms_update_nag') ){
			if( !empty($_GET['disable_dbem_ms_update_nag'])  && wp_verify_nonce($_GET['disable_dbem_ms_update_nag'], 'em_dismiss_notice') ){
				delete_site_option('dbem_ms_update_nag');
			}else{
				?>
				<div id="em_page_error" class="updated">
					<p><?php echo sprintf(__('MultiSite options have moved <a href="%s">here</a>. <a href="%s">Dismiss message</a>','events-manager'),admin_url().'network/admin.php?page=events-manager-options', esc_url($_SERVER['REQUEST_URI'].'&amp;disable_dbem_ms_update_nag='.wp_create_nonce('em_dismiss_notice'))); ?></p>
				</div>
				<?php
			}
		}
		if( !empty($_REQUEST['page']) && 'events-manager-options' == $_REQUEST['page'] && em_get_option('dbem_pro_dev_updates') == 1 ){
			?>
			<div id="message" class="updated">
				<p><?php echo sprintf(__('Dev Mode active: Just a friendly reminder that you are updating to development versions. Only admins see this message, and it will go away when you disable this <a href="#pro-api">here</a> in your settings.','events-manager'),'<code>define(\'EMP_DEV_UPDATES\',true);</code>'); ?></p>
			</div>
			<?php
		}
		if( class_exists('SitePress') && !class_exists('EM_WPML') && !get_site_option('disable_em_wpml_warning') ){
			if( !empty($_REQUEST['disable_em_wpml_warning']) && wp_verify_nonce($_GET['disable_em_wpml_warning'], 'em_dismiss_notice') ){
				update_site_option('disable_em_wpml_warning',1);
			}else{
				?>
				<div id="message" class="updated">
					<p><?php echo sprintf(__('It looks like you have WPML enabled on your site. We advise you also install our extra <a href="%s">Events Manager WPML Connector</a> plugin which helps the two work better together. <a href="%s">Dismiss message</a>','events-manager'),'http://wordpress.org/extend/plugins/events-manager-wpml/', esc_url(add_query_arg(array('disable_em_wpml_warning'=>wp_create_nonce('em_dismiss_notice'))))); ?></p>
				</div>
				<?php
			}
		}
	}
	//Warn about EM page edit
	if ( preg_match( '/(post|page).php/', $_SERVER ['SCRIPT_NAME']) && isset ( $_GET ['action'] ) && $_GET ['action'] == 'edit' && isset ( $_GET ['post'] ) && $_GET ['post'] == "$events_page_id") {
		$message = sprintf ( __ ( "This page corresponds to the <strong>Events Manager</strong> %s page. Its content will be overridden by Events Manager, although if you include the word CONTENTS (exactly in capitals) and surround it with other text, only CONTENTS will be overwritten. If you want to change the way your events look, go to the <a href='%s'>settings</a> page. ", 'events-manager'), __('Events','events-manager'), EM_ADMIN_URL .'&amp;page=events-manager-options' );
		$notice = "<div class='error'><p>$message</p></div>";
		echo $notice;
	}
	echo $EM_Notices;		
}
add_action ( 'admin_notices', 'em_admin_warnings', 100 );

/**
 * Settings link in the plugins page menu
 * @param array $links
 * @param string $file
 * @return array
 */
function em_plugin_action_links($actions, $file, $plugin_data) {
	$new_actions = array();
	$new_actions[] = sprintf( '<a href="'.EM_ADMIN_URL.'&amp;page=events-manager-options">%s</a>', __('Settings', 'events-manager') );
	$new_actions = array_merge($new_actions, $actions);
	if( is_multisite() ){
		$uninstall_url = admin_url().'network/admin.php?page=events-manager-options&amp;action=uninstall&amp;_wpnonce='.wp_create_nonce('em_uninstall_'.get_current_user_id().'_wpnonce');
	}else{
		$uninstall_url = EM_ADMIN_URL.'&amp;page=events-manager-options&amp;action=uninstall&amp;_wpnonce='.wp_create_nonce('em_uninstall_'.get_current_user_id().'_wpnonce');
	}
	$new_actions[] = '<span class="delete"><a href="'.$uninstall_url.'" class="delete">'.__('Uninstall','events-manager').'</a></span>';
	return $new_actions;
}
add_filter( 'plugin_action_links_events-manager/events-manager.php', 'em_plugin_action_links', 10, 3 );

function em_user_action_links( $actions, $user ){
	if ( !is_network_admin() && current_user_can( 'manage_others_bookings' ) ){
		if( em_get_option('dbem_edit_bookings_page') && (!is_admin() || !empty($_REQUEST['is_public'])) ){
			$my_bookings_page = get_permalink(em_get_option('dbem_edit_bookings_page'));
			$bookings_link = em_add_get_params($my_bookings_page, array('person_id'=>$user->ID), false);
		}else{
			$bookings_link = EM_ADMIN_URL. "&page=events-manager-bookings&person_id=".$user->ID;
		}
		$actions['bookings'] = "<a href='$bookings_link'>" . __( 'Bookings','events-manager') . "</a>";
	}
	return $actions;
}
add_filter('user_row_actions','em_user_action_links',10,2);

add_filter('em_admin_notice_v7-reconvert-recurrences_message', function(){
	ob_start();
	?>
	It looks like you may have previously upgraded from Events Manager 6 and converted your repeating events into our new recurring events format in Events Manager 7
	<p>We recommend running the conversion one more time due to some issues that may have prevented the full conversion in versions 7.0 - 7.0.2.</p>
	<?php
	$warning = esc_html__('This will delete all event recurrence posts/pages, and unify them into one URL. Any 404 pages resulting from these will automatically 302-redirect to the recurring event (which will be a new URL).', 'events-manager');
	$convert_url = esc_url( add_query_arg( ['action' => 'convert_repeating_to_recurrence', 'nonce' => 'x'], EM_ADMIN_URL .'&page=events-manager-options' ) );
	$convert_nonce = wp_create_nonce('convert_repeating_to_recurrence_'. get_current_user_id());
	EM\Scripts_and_Styles::add_js_var('convert_recurring_warning', __('Are you sure you want to convert all repeating events into recurring events?', 'events-manager') . "\n\n" . __('WARNING: This action cannot be undone.', 'events-manager') . "\n\n"  . $warning);
	?>
	<p><a href="<?php echo esc_url($convert_url); ?>" class="button-secondary em-convert-recurrence-link" data-nonce="<?php echo esc_attr($convert_nonce); ?>"><?php esc_html_e('Convert ALL Repeating Events', 'events-manager'); ?></a></p>
	<?php
	return ob_get_clean();
});
?>