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

//Callback for deactivate by archive settings page
function eos_dp_by_archive_callback(){
	if( !current_user_can( 'activate_plugins' ) ){
	?>
		<h2><?php _e( 'Sorry, you have not the right for this page','eos-dp' ); ?></h2>
		<?php
		return;
	}
	$shop_url = false;
	if( function_exists( 'wc_get_page_id' ) ){
		$shop_url = get_permalink( wc_get_page_id( 'shop' ) );
	}
	$pageSpeedInsightsUrl = 'https://developers.google.com/speed/pagespeed/insights/';
	$active_plugins = eos_dp_active_plugins();
	$plugins_table = eos_dp_plugins_table();
	$archiveSetts = eos_dp_get_option( 'eos_dp_archives' );
	$n = count( $active_plugins );
	$home_url = get_option( 'home' );
	wp_nonce_field( 'eos_dp_arch_setts','eos_dp_arch_setts' );
	wp_nonce_field( 'eos_dp_key','eos_dp_key' );
	wp_nonce_field( 'eos_dp_pro_gpsi_test','eos_dp_pro_gpsi_test' );
	wp_nonce_field( 'eos_dp_pro_gt_metrix_test','eos_dp_pro_gt_metrix_test' );
	if( isset( $_GET['eos_dp_home'] ) && 'true' === $_GET['eos_dp_home'] ){
		wp_nonce_field( 'eos_dp_pro_auto_settings', 'eos_dp_pro_auto_settings' );
	}
	eos_dp_alert_plain_permalink();
	eos_dp_navigation();
	$filter_home = false;
	if( isset( $_GET['eos_dp_home'] ) && 'true' === $_GET['eos_dp_home'] ){
		$show_on_front = eos_dp_get_option( 'show_on_front' );
		if( 'posts' === $show_on_front ){
			$filter_home = true;
		}
	}
	$plugins_by_dirs = isset( $GLOBALS['eos_dp_plugins_by_dirs'] ) ? $GLOBALS['eos_dp_plugins_by_dirs'] : eos_dp_get_plugins();
	$page_for_posts = eos_dp_get_option( 'page_for_posts' );
	$show_on_front = eos_dp_get_option( 'show_on_front' );
	?>
	<section id="eos-dp-by-archive-section" class="eos-dp-section">
		<h2><?php _e( 'Archives','eos-dp' ); ?></h2>
		<?php do_action( 'eos_dp_after_archive_title' ); ?>
		<p><?php _e( "If you don’t find here what you are looking for, maybe it’s a static page that behaves like an archive. In that case, have also a look in Singles => Pages","eos-dp" ); ?></p>
		<div id="eos-dp-wrp">
			<table id="eos-dp-setts"  data-zoom="1">
			<?php eos_dp_table_head();
			eos_dp_plugins_slider_row();
			$row = 1;
			$rowN = 0;
			$key = '';
			foreach( $plugins_table as $post_type => $plugins ){
				if( !$filter_home || ( $filter_home && 'post' === $post_type ) ){
					$args = array( 'test_id'=>time(),'fdp_post_type'=>$post_type );
					$active = false;
					$postTypeObj = get_post_type_object( $post_type );
					if( !is_object( $postTypeObj ) ) continue;
					$labels = get_post_type_labels( get_post_type_object( $post_type ) );
					$labels_name = isset( $labels->name ) ? $labels->name : $post_type;
					$post_type_obj = get_post_type_object( $post_type );
					$archive_url = get_post_type_archive_link( $post_type );
					$skip = false;
					if( is_object( $post_type_obj ) && isset( $post_type_obj->rewrite ) ){
						$rewrite = $post_type_obj->rewrite;
						$slug = isset( $rewrite['slug'] ) ? $rewrite['slug'] : false;
						$page = get_page_by_path( esc_attr( $slug ) );
						if( is_object( $page ) ){
							if( get_permalink( $page->ID ) === $archive_url ){
								$skip = true;
							}
						}
					}
					if( !$skip && $shop_url !== $archive_url && $post_type !== 'page' && $archive_url && ( $post_type !== 'post' || ( !$page_for_posts && 'posts' === $show_on_front ) ) ):
						$archive_url = remove_query_arg( 'lang',$archive_url );
						$kArr = explode( '//',$archive_url );
						if( isset( $kArr[1] ) ){
							$key = $kArr[1];
						}
						$GLOBALS['fdp_action_archive_url'] = $archive_url;
						$key = sanitize_key( str_replace( '/','__',rtrim( $key,'/' ) ) );
						$values = isset( $archiveSetts[$key] ) ? explode( ',',$archiveSetts[$key] ) : array_fill( 0,count( $active_plugins ),',' );
					?>
					<tr class="eos-dp-archive-row eos-dp-post-row<?php echo 0 === $rowN ? ' fdp-row-1' : ''; ?>" data-url="<?php echo esc_attr( str_replace( $home_url,'',$archive_url ) ); ?>" data-post-type="<?php echo $post_type; ?>" data-href="<?php echo esc_url( $archive_url ); ?>" >
						<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>
							<span class="eos-dp-title"><?php printf( __( '%s Archive','eos-dp' ),$labels_name ); ?></span>
							<div class="eos-dp-actions">
								<?php eos_dp_debug_button( $archive_url ); ?>
								<?php eos_dp_saved_preview_button( $archive_url,'arch-'.$post_type ); ?>
								<?php
								$themes_list = eos_dp_active_themes_list();
								if( $themes_list ){
								?>
								<a class="eos-dp-theme-sel fdp-has-tooltip fdp-right-tooltip" style="border:1px solid #fff !important">
									<?php echo $themes_list; ?>
									<div class="fdp-tooltip"><?php _e( 'Select a different Theme and then click on the lens icon to see the preview','eos-dp' ); ?></div>
								</a>
								<?php } ?>
								<a class="eos-dp-preview eos-dp-archive-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo wp_nonce_url( add_query_arg( $args,esc_url( $archive_url ) ),'eos_dp_preview','eos_dp_preview' ); ?>" 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 and the selected theme','eos-dp' ); ?></div>
								</a>
								<a data-page_speed_insights="false" class="eos-dp-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo wp_nonce_url( add_query_arg( array_merge( $args,array( 'show_files' => 'true' ) ),esc_url( $archive_url ) ),'eos_dp_preview','eos_dp_preview' ); ?>" target="_blank">
									<span class="dashicons dashicons-search">
										<span class="dashicons dashicons-media-code"></span>
									</span>
									<div class="fdp-tooltip"><?php _e( 'Preview the page loading plugins according the settings you see now on this row and show the files that are called','eos-dp' ); ?></div>
								</a>
								<a class="eos-dp-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo wp_nonce_url( add_query_arg( array_merge( $args,array( 'js' => 'off' ) ),esc_url( $archive_url ) ),'eos_dp_preview','eos_dp_preview' ); ?>" 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
								if( !in_array( $_SERVER['REMOTE_ADDR'],array( '127.0.0.1','::1' ) ) ){
									$args['eos_dp_preview'] = 1000*absint( time()/1000 );
									$psi_url = add_query_arg(
										array(
											'url' => urlencode( add_query_arg(
												array_merge( array( 'display_usage' => 'false' ),$args ),
												esc_url( $archive_url )
											) )
										),
										$pageSpeedInsightsUrl
									);
								?>
									<a data-page_speed_insights="true" data-encode_url="true" class="eos-dp-preview eos-dp-psi-preview fdp-has-tooltip" oncontextmenu="return false;" href="<?php echo $psi_url; ?>" target="_blank" rel="noopener">
										<span class="dashicons dashicons-search">
											<img width="20" height="20" src="<?php echo EOS_DP_PLUGIN_URL.'/img/pagespeed.png'; ?>" />
										</span>
										<div class="fdp-tooltip"><?php _e( 'Check the page with Google PageSpeed Insights loading plugins and the theme according the settings you see now on this row','eos-dp' ); ?></div>
									</a>
									<?php
									if( isset( $_GET['eos_dp_home'] ) && 'true' === $_GET['eos_dp_home'] ){
										require_once EOS_DP_PLUGIN_DIR.'/admin/templates/partials/eos-dp-home-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_action_buttons' ); ?>
								<?php do_action( 'eos_dp_archive_action_buttons' ); ?>
								<a title="<?php _e( 'Close','eos-dp' ); ?>" class="eos-dp-close-actions" href="#"><span class="dashicons dashicons-no-alt"></span></a>
							</div>
						</td>
						<?php
						$k = 0;
						foreach( $active_plugins as $plugin ){
							$active = !in_array( $plugin,$values ) ? true : false;
							if( in_array( $plugin,array_keys( $plugins_by_dirs ) ) ){
							?>
							<td class="center<?php echo $active ? ' eos-dp-active' : ''; ?>" data-path="<?php echo esc_attr( $plugin ); ?>">
								<div class="eos-dp-td-chk-wrp eos-dp-td-archive-chk-wrp">
									<input class="eos-dp-row-<?php echo $row; ?> eos-dp-col-<?php echo $k + 1; ?> eos-dp-col-<?php echo ( $k + 1 ).'-'.$post_type; ?>" data-checked="<?php echo in_array( $plugin,$values ) ? 'checked' : 'not-checked'; ?>" type="checkbox"<?php echo in_array( $plugin,$values ) ? ' checked' : ''; ?> />
								</div>
							</td>
							<?php
								++$k;
							}
						}
						?>
					</tr>
					<?php
					++$rowN;
					endif;
					++$row;
				}
			}
			?>
			</table>
		</div>
		<?php eos_dp_save_button(); ?>
	</section>
	<?php
}
