<?php
defined( 'ABSPATH' ) || exit; // Exit if accessed directly

//Callback for deactivate in admin
function eos_dp_admin_callback(){
	if( !defined( 'FDP_EMERGENCY_LOG_ADMIN' ) && !current_user_can( 'activate_plugins' ) && function_exists( 'eos_dp_active_plugins' ) ){
	?>
		<h2><?php _e( 'Sorry, you have not the right for this page','eos-dp' ); ?></h2>
		<?php
		return;
	}
	$all_pages = !isset( $_GET['menu_group'] );
	$opts = eos_dp_get_option( 'eos_dp_general_setts' );
	$no_admin_topbar = $opts && isset( $opts['menu_in_topbar'] ) && 'false' === $opts['menu_in_topbar'];
	$no_admin_menu = $opts && isset( $opts['replace_admin_menu'] ) && 'false' === $opts['replace_admin_menu'];
	$active_plugins = eos_dp_active_plugins();
	global $menu,$submenu;
	$labels = array();
	foreach( $menu as $arr ){
		$labels[$arr[2]] = $arr[0];
	}
	$adminSetts = eos_dp_get_option( 'eos_dp_admin_setts' );
	$adminTheme = eos_dp_get_option( 'eos_dp_admin_theme' );
	$n = count( $active_plugins );
	wp_nonce_field( 'eos_dp_admin_setts','eos_dp_admin_setts' );
	wp_nonce_field( 'eos_dp_pro_auto_settings_admin','eos_dp_pro_auto_settings_admin' );
	eos_dp_navigation();
	$admin_pages = array();
	$admin_pages_key = array();
	$printedUrls = array();
	$groups = array();
	$one_item = isset( $_GET['item'] ) ? esc_attr( urlencode( $_GET['item'] ) ) : false;
	?>
	<section id="eos-dp-by-admin-section" class="eos-dp-section<?php echo $one_item ? ' fdp-backend-one-item' : ''; ?>">
		<?php if( !$one_item ){ ?>
		<div class="eos-dp-margin-top-32">
			<h2><?php _e( 'Admin Navigation.','eos-dp' ); ?></h2>
			<div>
				<input type="checkbox" id="menu_in_topbar" name="menu_in_topbar"<?php echo $no_admin_topbar ? '' : ' checked'; ?> />
				<span><?php _e( 'Check it to show a simplified admin navigation in the top bar when the main navigation misses some menu items.','eos-dp' ); ?></span>
			</div>
			<br/>
			<div>
				<input type="checkbox" id="replace_admin_menu" name="replace_admin_menu"<?php echo $no_admin_menu ? '' : ' checked'; ?> />
				<span><?php _e( 'Check it if you want FDP to rebuild the admin navigation (the performance improvement will be sligthly lower).','eos-dp' ); ?><strong style="text-decoration:underline"> BETA VERSION</strong></span>
			</div>
			<?php if( defined( 'EOS_DP_BETA_VERSION' ) && EOS_DP_BETA_VERSION && !$no_admin_topbar ) { ?>
			<div class="left">
				<span id="eos-dp-get-screen" class="dashicons dashicons-external"></span>
			</div>
			<?php } ?>
		</div>
		<?php } ?>
		<?php
		do_action( 'eos_dp_before_wrapper' );
		$row = 1;
		?>
		<table id="eos-dp-setts"  data-zoom="1">
			<?php
			if( !$all_pages ){ ?>
				<div style="margin-top:32px"><a class="fdp-all-backend-pages button" href="<?php echo add_query_arg( 'all','1',admin_url( 'admin.php?page=eos_dp_admin' ) );; ?>"><?php _e( 'All pages','eos-dp' ); ?></a></div>
			<?php } ?>
		<?php eos_dp_table_head();
		eos_dp_plugins_slider_row();
		$plugins_by_dirs = isset( $GLOBALS['eos_dp_plugins_by_dirs'] ) ? $GLOBALS['eos_dp_plugins_by_dirs'] : eos_dp_get_plugins();
		$g = 0;
		$G = isset( $_GET['menu_group'] ) ? absint( $_GET['menu_group'] ) : 0;
		foreach( $submenu as $submenu_item ){
			$submenu_item = array_values( $submenu_item );
			if( empty( $submenu_item ) ) continue;
			$keyArr = $submenu_item[0];
			$key = $keyArr[2];
			$admin_pages_key = array();
			$labels_name = isset( $labels[$key] ) ? preg_replace('/[0-9]+/', '',wp_strip_all_tags( $labels[$key] ) ) : preg_replace('/[0-9]+/', '',wp_strip_all_tags( $keyArr[0] ) );
			$menu_item_url = false !== strpos( $keyArr[2],'.php' ) ?  admin_url( $keyArr[2] ) : add_query_arg( 'page', $keyArr[2] ,admin_url( 'admin.php' ) );
			$groups[$g] = $labels_name;
			if( ( $g < 5*$G || $g > ( 5*( $G + 1 ) - 1 ) ) && !$all_pages ){
					++$g;
					continue;
			}
			if(
				false !== strpos( $menu_item_url,'eos_dp_' )
			){
				continue;
			}
			?>
			<tr style="border-style:none" class="fdp-row-separator"<?php echo $one_item && $one_item === urlencode( $labels_name ) ? ' eos-hidden' : ''; ?>>
				<td style="border-style:none;box-shadow:none">
					<a class="eos-dp-admin-main-menu-link" href="<?php echo esc_url( $menu_item_url ); ?>" target="_blank">
						<h4 class="fdp-admin-menu-title"><?php echo esc_html( $labels_name ); ?></h4>
					</a>
				</td>
				<td style="border-style:none;box-shadow:none" colspan="<?php echo count( $active_plugins ) - 1; ?>"></td>
			</tr>
			<?php
			foreach( $submenu_item as $menu_item ){
				$active = $is_edit_post = false;
				$menu_item[2] = str_replace( '&amp;','&',$menu_item[2] );
				if( false === filter_var( $menu_item[2],FILTER_VALIDATE_URL ) ){
					$menu_item_url = false !== strpos( $menu_item[2],'.php' ) ?  admin_url( $menu_item[2] ) : add_query_arg( 'page',$menu_item[2],admin_url( 'admin.php' ) );
				}
				else{
					$menu_item_url = $menu_item[2];
				}
				$values = isset( $adminSetts[$menu_item[2]] ) ? explode( ',',$adminSetts[$menu_item[2]] ) : array_fill( 0,count( $active_plugins ),',' );
				if( '' !== $labels_name && false === strpos( $menu_item[2],'eos_dp' ) ){
					$title = wp_strip_all_tags( reset( $menu_item ) );
					$title = preg_replace( '!\d+!','',$title );
					$admin_page = array( 'title' =>$title,'page' => $menu_item[2],'url' => $menu_item_url );
					if( '' === $title ) $tite = ucwords( str_replace( '-',' ',$menu_item[2] ) );
					$admin_pages_key[] = $admin_page;
					$admin_page_key = sanitize_key(
						str_replace( '.','-',
							str_replace( 'admin.php?page=','eos_dp_tlp-',
								str_replace( admin_url(),'',$menu_item_url )
							)
						)
					);
					$args = array(
						'eos_dp_debug' => 'no_errors',
						'admin_page_key' => $admin_page_key,
						'eos_dp_pro_id' => md5( EOS_DP_PRO_TESTING_UNIQUE_ID ),
						'test_id' => time()
					);

					$url = str_replace( '&amp;','&',add_query_arg( $args,wp_nonce_url( $menu_item_url,'eos_dp_preview','eos_dp_preview' ) ) );
					$rowsN = 1;
					if( false !== strpos( $menu_item_url,'edit.php' ) && ( false !== strpos( $menu_item_url,'post_type=' ) || $menu_item_url === admin_url( 'edit.php' ) ) ){
						$rowsN = 2;
					}
					for( $nrows = 0;$nrows < $rowsN;++$nrows ){
						if( 1 === $nrows ){
							$postTypeArr = explode( 'post_type=',$menu_item_url );
							if( isset( $postTypeArr[1] ) || $menu_item_url === admin_url( 'edit.php' ) ){
								if( isset( $postTypeArr[1] ) ){
									$postTypeArr = explode( '&',$postTypeArr[1] );
									$post_key = $postTypeArr[0];
									$post_type = get_post_type_object( sanitize_key( $post_key ) );
								}
								elseif( $menu_item_url === admin_url( 'edit.php' ) ){
									$post_type = get_post_type_object( 'post' );
									$post_key = 'post';
								}
								if( $post_type && isset( $post_type->labels ) && isset( $post_type->labels->singular_name ) ){
									$labels_name = $title = sprintf( __( 'Edit Single %s','eos-dp' ),$post_type->labels->singular_name );
									$menu_item[2] = $admin_page_key = 'single_'.sanitize_key( $post_key);
									$is_edit_post = true;
									$menu_item_url = false;
									$values = isset( $adminSetts[$admin_page_key] ) ? explode( ',',$adminSetts[$admin_page_key] ) : array_fill( 0,count( $active_plugins ),',' );
								}
								else{
									continue;
								}
							}
							else{
								continue;
							}
						}
						?>
						<tr class="eos-dp-admin-row eos-dp-post-row<?php echo $is_edit_post ? ' fdp-edit-single' : '';$menu_item_url && '' !== $menu_item_url && in_array( $menu_item_url,$printedUrls ) ? ' eos-dp-duplicated-url' : '';echo $one_item && $one_item !== urlencode( $labels_name ) ? ' eos-hidden' : '';echo false !== strpos( $menu_item_url,'plugins.php' ) ? ' eos-dp-not-active eos-dp-not-allowed' : ''; ?>" data-admin="<?php echo $menu_item[2]; ?>">
							<td class="eos-dp-post-name-wrp">

								<span class="fdp-row-actions-ico dashicons dashicons-plus" title="<?php __( 'Action buttons','eos-dp' ); ?>"></span>

								<span class="eos-dp-not-active-wrp"><input title="<?php printf( __( 'Activate/deactivate all plugins in %s','eos-dp' ),esc_attr( $labels_name ) ); ?>" data-row="<?php echo esc_attr( $row ); ?>" class="eos-dp-global-chk-row" type="checkbox" /></span>
								<a class="eos-dp-title" href="<?php echo esc_url( $menu_item_url ); ?>" target="_blank"><?php echo $title; ?></a>

								<div class="eos-dp-actions">
									<?php if( $menu_item_url ){ ?>
									<a class="eos-dp-view fdp-has-tooltip" href="<?php echo esc_url( $menu_item_url ); ?>" target="_blank">
										<span class="dashicons dashicons-visibility"></span>
										<div class="fdp-tooltip"><?php _e( 'View page loading plugins according the saved options','eos-dp' ); ?></div>
									</a>
									<a data-page_speed_insights="false" class="eos-dp-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo esc_url( $url ); ?>" target="_blank">
										<span class="dashicons dashicons-search"></span>
										<div class="fdp-tooltip"><?php _e( 'Preview the page loading plugins according the settings you see now on this row','eos-dp' ); ?></div>
									</a>
									<a data-page_speed_insights="false" class="eos-dp-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo add_query_arg( 'js','off',$url ); ?>" target="_blank">
										<span class="dashicons dashicons-search">
											<span class="eos-dp-no-js">JS</span>
										</span>
										<div class="fdp-tooltip"><?php _e( 'Preview the page loading plugins and the theme according the settings you see now on this row and disable JavaScript esecution','eos-dp' ); ?></div>
									</a>
									<?php do_action( 'eos_dp_action_buttons' ); ?>
									<a href="#" class="eos-dp-pro-autosettings fdp-has-tooltip">
										<span class="dashicons dashicons-plugins-checked"></span>
										<div class="fdp-tooltip"><?php _e( 'Suggest unused plugins','eos-dp' ); ?></div>
									</a>
									<?php do_action( 'eos_dp_backend_actions' ); ?>
									<?php } ?>
									<a class="eos-dp-invert-selection fdp-has-tooltip" href="#"><span class="dashicons"><span style="display:inline-block"><span class="fdp-invert-up"></span><span class="fdp-invert-down"></span></span></span>
										<div class="fdp-tooltip"><?php _e( 'Invert selection','eos-dp' ); ?></div>
									</a>
									<a class="eos-dp-copy fdp-has-tooltip" href="#"><span class="dashicons dashicons-admin-page"></span>
										<div class="fdp-tooltip"><?php _e( 'Copy this row settings','eos-dp' ); ?></div>
									</a>
									<a class="eos-dp-paste fdp-has-tooltip" href="#"><span class="dashicons dashicons-category"></span>
										<div class="fdp-tooltip"><?php _e( 'Paste last copied row settings','eos-dp' ); ?></div>
									</a>
									<?php do_action( 'eos_dp_archive_action_buttons' ); ?>
								</div>

							</td>
							<?php
							$printedUrls[] = $menu_item_url;
							$k = 0;
							foreach( $active_plugins as $plugin ){
								if( in_array( $plugin,array_keys( $plugins_by_dirs ) ) ){
									$extra_class = $plugin === EOS_DP_PLUGIN_BASE_NAME ? ' eos-hidden' : '';
									?>
									<td class="center<?php echo !in_array( $plugin,$values ) ? ' eos-dp-active' : '';echo $extra_class; ?>" data-path="<?php echo esc_attr( $plugin ); ?>">
										<div class="eos-dp-td-chk-wrp eos-dp-td-admin-chk-wrp">
											<input class="eos-dp-row-<?php echo $row; ?> eos-dp-col-<?php echo $k + 1; ?> eos-dp-col-<?php echo ( $k + 1 ).'-'.esc_attr( $menu_item[2] ); ?>" data-checked="<?php echo in_array( $plugin,$values ) ? 'checked' : 'not-checked'; ?>" type="checkbox"<?php echo in_array( $plugin,$values ) ? ' checked' : ''; ?> />
										</div>
									</td>
									<?php
									++$k;
								}
							}
							?>
							<td class="center<?php echo !isset( $adminTheme[$menu_item[2]] ) || $adminTheme[$menu_item[2]] ? ' eos-dp-active' : ''; ?>">
								<div class="eos-dp-td-chk-wrp eos-dp-td-admin-chk-wrp">
									<input class="eos-dp-row-theme eos-dp-col-<?php echo $k + 1; ?> eos-dp-col-<?php echo ( $k + 1 ).'-'.esc_attr( $menu_item[2] ); ?>" data-checked="checked" type="checkbox" checked />
								</div>
							</td>
						</tr>
						<?php
					}
					$admin_pages[$key] = array( 'title' => $labels_name,'submenu' => $admin_pages_key );
				}
				++$row;
			}
			++$g;
		}
		?>
		</table>
		<script>
		var eos_dp_admin_pages = '<?php echo str_replace( "&quot;","\"",wp_json_encode( $admin_pages ) ); ?>';
		</script>
		<?php eos_dp_save_button(); ?>
	</section>
	<?php
}

if( isset( $_GET['page'] ) && 'eos_dp_admin' === $_GET['page'] ){
	add_action( 'fdp_table_head_first_col_eos_dp_admin','eos_dp_stop_process_admin' );
}
//Add autosuggestions for all pages
function eos_dp_stop_process_admin(){
	if( defined( 'FDP_PRO_ACTIVE' ) && FDP_PRO_ACTIVE ) return;
	?>
	<div style="margin-top:64px">
		<span id="eos-dp-stop-process" class="eos-dp-not-active button" title="<?php esc_attr_e( 'Stop auto-suggestion','eos-dp' ); ?>"><?php esc_html_e( 'Stop auto-suggestion','eos-dp' ); ?></span>
	</div>
	<?php
}
