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

//Callback for deactivate by archive settings page
function eos_dp_by_term_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;
	}
	$active_plugins = eos_dp_active_plugins();
	$archiveSetts = eos_dp_get_option( 'eos_dp_archives' );
	$default_language = eos_dp_default_language();
	$n = count( $active_plugins );
	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();
	$current_tax = isset( $_GET['eos_dp_tax'] ) ? sanitize_key( $_GET['eos_dp_tax'] ) : 'category';
	$row = 1;
	$tax = get_taxonomy( $current_tax );
	$terms = get_terms( array( 'taxonomy' => $tax -> name,'hide_empty' => false,'orderby' => 'name','order' => 'ASC' ) );
	$permalink_structure = eos_dp_get_option( 'permalink_structure' );
	$plugins_by_dirs = isset( $GLOBALS['eos_dp_plugins_by_dirs'] ) ? $GLOBALS['eos_dp_plugins_by_dirs'] : eos_dp_get_plugins();
	$home_url = get_option( 'home' );
	if( defined( 'ICL_SITEPRESS_VERSION' ) || function_exists( 'pll_languages_list' ) ){
		echo eos_dp_wpml_switcher();
	}
	if( false === strpos( $permalink_structure,'%category%' ) && '.' === eos_dp_get_option( 'category_base' ) ){
	?>
	<div id="eos-dp-plain-permalink-wrg" style="line-height:1;margin:20px 0;padding:10px;color:#23282d;background:#fff;border-<?php echo is_rtl() ? 'right' : 'left'; ?>:4px solid  #dc3232">
		<div>
			<h1><?php printf( __( "Issue detected. In the %spermalinks settings%s you have a full stop as base category, and you don't have %s in your custom permalink structure.",'eos-dp' ),'<a href="'.admin_url( 'options-permalink.php' ).'" target="_blank">','</a>','%category%' ); ?></h1>
			<h1><?php __( "The plugins control will probably not work for term archives.",'eos-dp' ); ?></h1>
			<h1><?php printf( __( "If you really want these permalink settings, you need %sthe custom URLs%s for the term archives.",'eos-dp' ),'<a href="'.admin_url( 'admin.php?page=eos_dp_url' ).'" target="_blank">','</a>' ); ?></h1>
		</div>
		<div>
			<a class="button" target="_blank" href="<?php echo admin_url( 'options-permalink.php' ); ?>"><?php  _e( 'Check Permalinks Structure','eos-dp' ); ?></a>
		</div>
	</div>
	<?php
	}
	?>
	<section id="eos-dp-by-archive-section" class="eos-dp-section">
		<div id="eos-dp-wrp">
			<?php
			if( $tax && isset( $tax->labels ) ){
				$tax_labels = $tax->labels;
				?>
				<h2 id="eos-dp-terms-archives-title"><?php echo esc_html( $tax_labels->name ); ?></h2>
				<?php do_action( 'eos_dp_after_terms_archive_title' ); ?>
				<?php
			}
			if( !empty( $terms ) ){ ?>
				<table id="eos-dp-setts"  data-zoom="1">
				<?php
				eos_dp_table_head();
				eos_dp_plugins_slider_row();
				foreach( $terms as $term ){
					$active = false;
					$labels_name = $term -> name;
					$archive_url = get_term_link( $term );
					$flag = '';
					$loc = false;
					if( defined( 'ICL_SITEPRESS_VERSION' ) ){
						$loc = apply_filters( 'wpml_element_language_code',null,array('element_id' => $term->term_id, 'element_type' => $term->taxonomy ) );
						if( $loc && '' !== $loc ){
							$flag = defined( 'ICL_PLUGIN_URL' ) && defined( 'WPML_PLUGIN_PATH' ) && file_exists( WPML_PLUGIN_PATH.'/res/flags/'.$loc.'.png' ) ? ' <img src="'.esc_url( ICL_PLUGIN_URL.'/res/flags/'.$loc.'.png' ).'" />' : esc_html( strtoupper( $loc ) );
						}
					}
					if( function_exists( 'pll_get_term_language' ) ){
						$loc = pll_get_term_language( $term->term_id );
						if( $loc && '' !== $loc ){
							$flag = defined( 'POLYLANG_FILE' ) && defined( 'POLYLANG_DIR' ) && file_exists( POLYLANG_DIR.'/flags/'.str_replace( 'en.png','england.png',$loc.'.png' ) ) ? ' <img src="'.esc_url( plugins_url( '/flags/'.str_replace( 'en.png','england.png',$loc.'.png' ),POLYLANG_FILE ) ).'" />' : esc_html( strtoupper( $loc ) );
						}
					}
					if( $loc && $loc === $default_language ){
						$archive_urlA = explode( '?',$archive_url );
						$archive_url = $archive_urlA[0];
					}
					if( !is_wp_error( $archive_url ) ){
						$kArr = explode( '//',$archive_url );
						if( isset( $kArr[1] ) ){
							$key = $kArr[1];
						}
						$key = sanitize_key( str_replace( '/','__',rtrim( $key,'/' ) ) );
						$permalink_structure = eos_dp_get_option( 'permalink_structure' );
						if( false !== strpos( $permalink_structure,'%category%' ) && '%postname%' === basename( $permalink_structure )  && '.' === eos_dp_get_option( 'category_base' ) ){
							$key = str_replace( '__.__','__',$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 1 === $row ? ' fdp-row-1' : ''; ?>" data-url="<?php echo esc_attr( str_replace( $home_url,'',$archive_url ) ); ?>" data-post-type="<?php echo esc_attr( $term -> name ); ?>" data-tax="<?php echo esc_attr( $tax->name ); ?>" 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 );echo $flag; ?></span>
								<div class="eos-dp-actions">
									<?php eos_dp_debug_button( $archive_url );
									if( isset( $tax -> object_type ) ){
									$post_types = $tax -> object_type;
									$singles_url = admin_url( add_query_arg(
										array(
											'page' => 'eos_dp_menu',
											'tax_name' => esc_attr( $tax->name ),
											'term_slug' => esc_attr( $term -> slug ),
											'eos_dp_post_type' => esc_attr( $post_types[0] )
										),
										'admin.php'
									) );
									?>
									<a class="fdp-has-tooltip fdp-right-tooltip" href="<?php echo esc_url( $singles_url ); ?>">
										<span class="dashicons dashicons-admin-post"></span>
										<div class="fdp-tooltip"><?php _e( 'Go to Singles settings','eos-dp' ); ?></div>
									</a>
									<?php } ?>

									<?php eos_dp_saved_preview_button( $archive_url,$key ); ?>
									<?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( array( 'test_id'=>time(),'fdp_tax'=>esc_attr( $tax->name ) ),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( array( 'test_id'=>time(),'fdp_tax'=>esc_attr( $tax->name ),'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 the selected theme','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( 'js' => 'off','test_id'=>time(),'fdp_tax'=>esc_attr( $tax->name ) ),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>
									<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 ){
								if( in_array( $plugin,array_keys( $plugins_by_dirs ) ) ){
							?>
							<td class="center<?php echo !in_array( $plugin,$values ) ? ' 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 ).'-'.$term -> name; ?>" 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
						++$row;
					}
				}

			}
			else{
				?>
				<p><?php printf( __( 'You have no %s','eos-dp' ),esc_html( $tax -> label ) ); ?></p>
				<?php
			}
			?>
			</table>
		</div>
		<?php eos_dp_save_button(); ?>
	</section>
	<?php
}
