PageRenderTime 55ms CodeModel.GetById 17ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/events-manager/admin/em-admin.php

https://github.com/90rdon/bric
PHP | 272 lines | 222 code | 16 blank | 34 comment | 86 complexity | 33c46e7277a4e92d1af85c7fb9660c8e MD5 | raw file
  1<?php
  2//Admin functions
  3function em_admin_menu(){
  4	global $menu, $submenu, $pagenow;
  5	//Count pending bookings	
  6   	if( get_option('dbem_rsvp_enabled') ){
  7		$bookings_num = '';
  8		$bookings_pending_count = apply_filters('em_bookings_pending_count',0);
  9		if( get_option('dbem_bookings_approval') == 1){ 
 10			$bookings_pending_count += count(EM_Bookings::get(array('status'=>'0', 'blog'=>get_current_blog_id()))->bookings);
 11		}
 12		if($bookings_pending_count > 0){
 13			$bookings_num = '<span class="update-plugins count-'.$bookings_pending_count.'"><span class="plugin-count">'.$bookings_pending_count.'</span></span>';
 14		}
 15   	}else{
 16   		$bookings_num = '';
 17		$bookings_pending_count = 0;
 18   	}
 19	//Count pending events
 20	$events_num = '';
 21	$events_pending_count = EM_Events::count(array('status'=>0, 'scope'=>'all', 'blog'=>get_current_blog_id()));
 22	//TODO Add flexible permissions
 23	if($events_pending_count > 0){
 24		$events_num = '<span class="update-plugins count-'.$events_pending_count.'"><span class="plugin-count">'.$events_pending_count.'</span></span>';
 25	}
 26	//Count pending recurring events
 27	$events_recurring_num = '';
 28	$events_recurring_pending_count = EM_Events::count(array('status'=>0, 'recurring'=>1, 'scope'=>'all', 'blog'=>get_current_blog_id()));
 29	//TODO Add flexible permissions
 30	if($events_recurring_pending_count > 0){
 31		$events_recurring_num = '<span class="update-plugins count-'.$events_recurring_pending_count.'"><span class="plugin-count">'.$events_recurring_pending_count.'</span></span>';
 32	}
 33	$both_pending_count = apply_filters('em_items_pending_count', $events_pending_count + $bookings_pending_count + $events_recurring_pending_count);
 34	$both_num = ($both_pending_count > 0) ? '<span class="update-plugins count-'.$both_pending_count.'"><span class="plugin-count">'.$both_pending_count.'</span></span>':'';
 35  	// Add a submenu to the custom top-level menu:
 36   	$plugin_pages = array();
 37   	if( get_option('dbem_rsvp_enabled') ){
 38		$plugin_pages['bookings'] = add_submenu_page('edit.php?post_type='.EM_POST_TYPE_EVENT, __('Bookings', 'dbem'), __('Bookings', 'dbem').$bookings_num, 'manage_bookings', 'events-manager-bookings', "em_bookings_page");
 39   	}
 40	$plugin_pages['options'] = add_submenu_page('edit.php?post_type='.EM_POST_TYPE_EVENT, __('Events Manager Settings','dbem'),__('Settings','dbem'), 'list_users', "events-manager-options", 'em_admin_options_page');
 41	$plugin_pages['help'] = add_submenu_page('edit.php?post_type='.EM_POST_TYPE_EVENT, __('Getting Help for Events Manager','dbem'),__('Help','dbem'), 'list_users', "events-manager-help", 'em_admin_help_page');
 42	//If multisite global with locations set to be saved in main blogs we can force locations to be created on the main blog only
 43	if( EM_MS_GLOBAL && !is_main_site() && get_site_option('dbem_ms_mainblog_locations') ){
 44		include( dirname(__FILE__)."/em-ms-locations.php" );
 45		$plugin_pages['locations'] = add_submenu_page('edit.php?post_type='.EM_POST_TYPE_EVENT, __('Locations','dbem'),__('Locations','dbem'), 'read_others_locations', "locations", 'em_admin_ms_locations');
 46	}
 47	$plugin_pages = apply_filters('em_create_events_submenu',$plugin_pages);
 48	//We have to modify the menus manually
 49	if( !empty($both_num) ){ //Main Event Menu
 50		//go through the menu array and modify the events menu if found
 51		foreach ( (array)$menu as $key => $parent_menu ) {
 52			if ( $parent_menu[2] == 'edit.php?post_type='.EM_POST_TYPE_EVENT ){
 53				$menu[$key][0] = $menu[$key][0]. $both_num;
 54				break;
 55			}
 56		}
 57	}
 58	if( !empty($events_num) && !empty($submenu['edit.php?post_type='.EM_POST_TYPE_EVENT]) ){ //Submenu Event Item
 59		//go through the menu array and modify the events menu if found
 60		foreach ( (array)$submenu['edit.php?post_type='.EM_POST_TYPE_EVENT] as $key => $submenu_item ) {
 61			if ( $submenu_item[2] == 'edit.php?post_type='.EM_POST_TYPE_EVENT ){
 62				$submenu['edit.php?post_type='.EM_POST_TYPE_EVENT][$key][0] = $submenu['edit.php?post_type='.EM_POST_TYPE_EVENT][$key][0]. $events_num;
 63				break;
 64			}
 65		}
 66	}
 67	if( !empty($events_recurring_num) && !empty($submenu['edit.php?post_type='.EM_POST_TYPE_EVENT]) ){ //Submenu Recurring Event Item
 68		//go through the menu array and modify the events menu if found
 69		foreach ( (array)$submenu['edit.php?post_type='.EM_POST_TYPE_EVENT] as $key => $submenu_item ) {
 70			if ( $submenu_item[2] == 'edit.php?post_type=event-recurring' ){
 71				$submenu['edit.php?post_type='.EM_POST_TYPE_EVENT][$key][0] = $submenu['edit.php?post_type='.EM_POST_TYPE_EVENT][$key][0]. $events_recurring_num;
 72				break;
 73			}
 74		}
 75	}
 76	/* Hack! Add location/recurrence isn't possible atm so this is a workaround */
 77	global $_wp_submenu_nopriv;
 78	if( $pagenow == 'post-new.php' && !empty($_REQUEST['post_type']) ){
 79		if( $_REQUEST['post_type'] == EM_POST_TYPE_LOCATION && !empty($_wp_submenu_nopriv['edit.php']['post-new.php']) && current_user_can('edit_locations') ){
 80			unset($_wp_submenu_nopriv['edit.php']['post-new.php']);
 81		}
 82		if( $_REQUEST['post_type'] == 'event-recurring' && !empty($_wp_submenu_nopriv['edit.php']['post-new.php']) && current_user_can('edit_recurring_events') ){
 83			unset($_wp_submenu_nopriv['edit.php']['post-new.php']);
 84		}
 85	}
 86}
 87add_action('admin_menu','em_admin_menu');
 88
 89function em_ms_admin_menu(){
 90	add_menu_page( __('Events Manager','dbem'), __('Events Manager','dbem'), 'activate_plugins', 'events-manager-options', 'em_ms_admin_options_page', plugins_url('includes/images/calendar-16.png', dirname(dirname(__FILE__)).'/events-manager.php') );
 91	add_submenu_page('events-manager-options', __('Update Blogs','dbem'),__('Update Blogs','dbem'), 'activate_plugins', "events-manager-update", 'em_ms_upgrade');	
 92}
 93add_action('network_admin_menu','em_ms_admin_menu');
 94
 95function em_admin_init(){
 96	//in MS global mode and locations are stored in the main blog, then a user must have at least a subscriber role
 97	if( EM_MS_GLOBAL && is_user_logged_in() && !is_main_site() && get_site_option('dbem_ms_mainblog_locations') ){
 98		EM_Object::ms_global_switch();
 99		$user = new WP_User(get_current_user_id());
100		if( count($user->roles) == 0 ){
101			$user->set_role('subscriber');
102		}
103		EM_Object::ms_global_switch_back();
104	}
105}
106add_action('admin_init','em_admin_init');
107
108/**
109 * Generate warnings and notices in the admin area
110 */
111function em_admin_warnings() {
112	global $EM_Notices;
113	//If we're editing the events page show hello to new user
114	$events_page_id = get_option ( 'dbem_events_page' );
115	$dismiss_link_joiner = ( count($_GET) > 0 ) ? '&amp;':'?';
116	
117	if( current_user_can('activate_plugins') ){
118		//New User Intro
119		if (isset ( $_GET ['disable_hello_to_user'] ) && $_GET ['disable_hello_to_user'] == 'true'){
120			// Disable Hello to new user if requested
121			update_option('dbem_hello_to_user',0);
122		}elseif ( get_option ( 'dbem_hello_to_user' ) ) {
123			//FIXME update welcome msg with good links
124			$advice = sprintf( __("<p>Events Manager is ready to go! It is highly recommended you read the <a href='%s'>Getting Started</a> guide on our site, as well as checking out the <a href='%s'>Settings Page</a>. <a href='%s' title='Don't show this advice again'>Dismiss</a></p>", 'dbem'), 'http://wp-events-plugin.com/documentation/getting-started/?utm_source=em&utm_medium=plugin&utm_content=installationlink&utm_campaign=plugin_links', EM_ADMIN_URL .'&amp;page=events-manager-options',  $_SERVER['REQUEST_URI'].$dismiss_link_joiner.'disable_hello_to_user=true');
125			?>
126			<div id="message" class="updated">
127				<?php echo $advice; ?>
128			</div>
129			<?php
130		}
131	
132		//If events page couldn't be created or is missing
133		if( !empty($_GET['em_dismiss_events_page']) ){
134			update_option('dbem_dismiss_events_page',1);
135		}else{
136			if ( !get_page($events_page_id) && !get_option('dbem_dismiss_events_page') ){
137				?>
138				<div id="em_page_error" class="updated">
139					<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>', 'dbem'), EM_ADMIN_URL .'&amp;page=events-manager-options', $_SERVER['REQUEST_URI'].$dismiss_link_joiner.'em_dismiss_events_page=1' ); ?></p>
140				</div>
141				<?php		
142			}
143		}
144		
145		if( defined('EMP_VERSION') && EMP_VERSION < EM_PRO_MIN_VERSION && !defined('EMP_DISABLE_WARNINGS')){ 
146			?>
147			<div id="em_page_error" class="updated">
148				<p><?php _e('There is a newer version of Events Manager Pro which is recommended for this current version of Events Manager as new features have been added. Please go to the plugin website and download the latest update.','dbem'); ?></p>
149			</div>
150			<?php
151		}
152	
153		if( is_multisite() && !empty($_REQUEST['page']) && $_REQUEST['page']=='events-manager-options' && is_super_admin() && get_option('dbem_ms_update_nag') ){
154			if( !empty($_GET['disable_dbem_ms_update_nag']) ){
155				delete_site_option('dbem_ms_update_nag');
156			}else{
157				?>
158				<div id="em_page_error" class="updated">
159					<p><?php echo sprintf(__('MultiSite options have moved <a href="%s">here</a>. <a href="%s">Dismiss message</a>','dbem'),admin_url().'network/admin.php?page=events-manager-options', $_SERVER['REQUEST_URI'].'&amp;disable_dbem_ms_update_nag=1'); ?></p>
160				</div>
161				<?php
162			}
163		}
164		
165		if( is_super_admin() && get_option('dbem_migrate_images_nag') ){
166			if( !empty($_GET['disable_dbem_migrate_images_nag']) ){
167				delete_site_option('dbem_migrate_images_nag');
168			}else{
169				?>
170				<div id="em_page_error" class="updated">
171					<p><?php echo sprintf(__('Whilst they will still appear using placeholders, you need to <a href="%s">migrate your location and event images</a> in order for them to appear in your edit forms and media library. <a href="%s">Dismiss message</a>','dbem'),admin_url().'edit.php?post_type=event&page=events-manager-options&em_migrate_images=1&_wpnonce='.wp_create_nonce('em_migrate_images'), em_add_get_params($_SERVER['REQUEST_URI'], array('disable_dbem_migrate_images_nag' => 1))); ?></p>
172				</div>
173				<?php
174			}
175		}
176		if( !empty($_REQUEST['page']) && 'events-manager-options' == $_REQUEST['page'] && get_option('dbem_pro_dev_updates') == 1 ){
177			?>
178			<div id="message" class="updated">
179				<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.','dbem'),'<code>define(\'EMP_DEV_UPDATES\',true);</code>'); ?></p>
180			</div>
181			<?php
182		}
183		if( class_exists('SitePress') && !class_exists('EM_WPML') && !get_site_option('disable_em_wpml_warning') ){
184			if( !empty($_REQUEST['disable_em_wpml_warning']) ){
185				update_site_option('disable_em_wpml_warning',1);
186			}else{
187				?>
188				<div id="message" class="updated">
189					<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>','dbem'),'http://wordpress.org/extend/plugins/events-manager-wpml/', add_query_arg(array('disable_em_wpml_warning'=>1))); ?></p>
190				</div>
191				<?php
192			}
193		}
194	}
195	//Warn about EM page edit
196	if ( preg_match( '/(post|page).php/', $_SERVER ['SCRIPT_NAME']) && isset ( $_GET ['action'] ) && $_GET ['action'] == 'edit' && isset ( $_GET ['post'] ) && $_GET ['post'] == "$events_page_id") {
197		$message = sprintf ( __ ( "This page corresponds to the <strong>Events Manager</strong> %s page. Its content will be overriden 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. ", 'dbem' ), __('Events','dbem'), EM_ADMIN_URL .'&amp;page=events-manager-options' );
198		$notice = "<div class='error'><p>$message</p></div>";
199		echo $notice;
200	}
201	echo $EM_Notices;		
202}
203add_action ( 'admin_notices', 'em_admin_warnings', 100 );
204
205/**
206 * Settings link in the plugins page menu
207 * @param array $links
208 * @param string $file
209 * @return array
210 */
211function em_plugin_action_links($actions, $file, $plugin_data) {
212	$new_actions = array();
213	$new_actions[] = sprintf( '<a href="'.EM_ADMIN_URL.'&amp;page=events-manager-options">%s</a>', __('Settings', 'dbem') );
214	$new_actions = array_merge($new_actions, $actions);
215	if( is_multisite() ){
216		$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');
217	}else{
218		$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');
219	}
220	$new_actions[] = '<span class="delete"><a href="'.$uninstall_url.'" class="delete">'.__('Uninstall','dbem').'</a></span>';
221	return $new_actions;
222}
223add_filter( 'plugin_action_links_events-manager/events-manager.php', 'em_plugin_action_links', 10, 3 );
224
225//Updates and Dev versions
226function em_updates_check( $transient ) {
227    // Check if the transient contains the 'checked' information
228    if( empty( $transient->checked ) )
229        return $transient;
230        
231    //only bother if we're checking for dev versions
232    if( get_option('em_check_dev_version') || get_option('dbem_pro_dev_updates') ){     
233	    //check WP repo for trunk version
234	    $request = wp_remote_get('http://plugins.svn.wordpress.org/events-manager/trunk/events-manager.php');
235	    
236	    if( !is_wp_error($request) ){
237		    preg_match('/Version: ([0-9a-z\.]+)/', $request['body'], $matches);
238		    
239		    if( !empty($matches[1]) ){
240		    	//we have a version number!
241			    if( version_compare($transient->checked[EM_SLUG], $matches[1]) < 0) {
242			    	$response = new stdClass();
243			    	$response->slug = EM_SLUG;
244					$response->new_version = $matches[1] ;
245			        $response->url = 'http://wordpress.org/extend/plugins/events-manager/';
246				    $response->package = 'http://downloads.wordpress.org/plugin/events-manager.zip';
247			       	$transient->response[EM_SLUG] = $response;
248			    }
249		    }
250		}
251		
252		delete_option('em_check_dev_version');
253    }
254    
255    return $transient;
256}
257add_filter('pre_set_site_transient_update_plugins', 'em_updates_check'); // Hook into the plugin update check and mod for dev version
258
259function em_user_action_links( $actions, $user ){
260	if ( !is_network_admin() && current_user_can( 'manage_others_bookings' ) ){
261		if( get_option('dbem_edit_bookings_page') && (!is_admin() || !empty($_REQUEST['is_public'])) ){
262			$my_bookings_page = get_permalink(get_option('dbem_edit_bookings_page'));
263			$bookings_link = em_add_get_params($my_bookings_page, array('person_id'=>$user->ID), false);
264		}else{
265			$bookings_link = EM_ADMIN_URL. "&page=events-manager-bookings&person_id=".$user->ID;
266		}
267		$actions['bookings'] = "<a href='$bookings_link'>" . __( 'Bookings','dbem' ) . "</a>";
268	}
269	return $actions;
270}
271add_filter('user_row_actions','em_user_action_links',10,2);
272?>