<?php
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
// delete_site_option( 'eos_dp_general_setts' );
// delete_site_option( 'eos_dp_archives' );
// delete_site_option( 'eos_dp_search' );
// delete_site_option( 'eos_dp_mobile' );
// delete_site_option( 'eos_dp_by_url' );
// delete_site_option( 'eos_post_types_plugins' );
// delete_site_option( 'eos_dp_by_admin_url' );
// delete_site_option( 'eos_dp_admin_setts' );
// delete_site_option( 'eos_dp_user_options' );
// delete_site_option( 'eos_dp_integration_actions' );

//Callback for deactivate by post type settings page
function eos_dp_by_post_type_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();
	$plugins_table = apply_filters( 'eos_dp_plugins_table',eos_dp_plugins_table() );
	global $old_method;
	$old_method = apply_filters( 'fdp_old_method_padlock',false );
  foreach( $plugins_table as $pt => $arr ){
    if( !isset( $arr[0] ) || 1 !== $arr[0] ){
      $obj = get_post_type_object( $pt );
      if( 'page' === $pt || ( 'attachment' !== $pt && isset( $obj->labels ) && isset( $obj->labels->name  ) && isset( $obj->publicly_queryable ) && $obj->publicly_queryable ) ){
        $old_method = true;
        break;
      }
    }
  }
	add_action( 'fdp_table_head_first_col_eos_dp_by_post_type',function(){
		global $old_method;
		require_once EOS_DP_PLUGIN_DIR.'/admin/templates/partials/eos-dp-post-types-legend.php';
	},10 );
	$n = count( $active_plugins );
	$values_string = '';
	$values = explode( ',',$values_string );
	wp_nonce_field( 'eos_dp_pt_setts', 'eos_dp_pt_setts' );
	eos_dp_alert_plain_permalink();
	eos_dp_navigation();
	?>
	<section id="eos-dp-by-post-type-section" class="eos-dp-section">
		<div id="eos-dp-wrp">
			<table id="eos-dp-setts"  data-zoom="1">
			<?php
			eos_dp_table_head();
			$plugins_by_dirs = isset( $GLOBALS['eos_dp_plugins_by_dirs'] ) ? $GLOBALS['eos_dp_plugins_by_dirs'] : eos_dp_get_plugins();
			$row = 0;
			eos_dp_plugins_slider_row();
			foreach( $plugins_table as $post_type => $plugins ){
				if( !in_array( $post_type,array( 'attachment' ) ) ){
					$active = false;
					$labsObj = get_post_type_object( $post_type );
					if( 'page' === $post_type || ( isset( $labsObj->labels ) && isset( $labsObj->publicly_queryable ) && $labsObj->publicly_queryable ) ){
						$labs = $labsObj->labels;
						$labs_name = isset( $labs->name ) ? $labs->name : false;
						$singles = add_query_arg( 'eos_dp_post_type',$post_type,admin_url( 'admin.php?page=eos_dp_menu' ) );
					?>
					<tr class="eos-dp-post-type eos-dp-post-row<?php echo isset( $_GET['eos_dp_post_type'] ) && $post_type === $_GET['eos_dp_post_type'] ? ' fdp-from-single-page' : '';echo 0 === $row ? ' fdp-row-1' : ''; ?>" data-post-type="<?php echo esc_attr( $post_type ); ?>" data-row_id="<?php echo esc_attr( $post_type ); ?>">
						<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( $labs_name ) ); ?>" class="eos-dp-global-chk-row" type="checkbox" /></span>
							<span class="eos-dp-not-active-wrp<?php echo !isset( $plugins[0] ) || $plugins[0] === 1 || false === $old_method ? ' eos-dp-priority-active' : ''; ?> eos-dp-priority-post-type-wrp"<?php echo !$old_method ? 'style="display:none !important"' : ''; ?>><input title="<?php printf( __( 'If activated the Single %s Settings will be ignored.','eos-dp' ),esc_attr( $labs_name ) ); ?>" class="eos-dp-priority-post-type" type="checkbox" /></span>
							<span class="eos-dp-title"><a style="color:inherit;text-decoration:none" href="<?php echo $singles; ?>"><?php echo esc_html( $labs_name ); ?></a></span>
							<span class="<?php echo isset( $plugins[2] ) && $plugins[2] == '1' ? 'eos-dp-default-active' : ''; ?> eos-dp-default-post-type-wrp">
								<span class="eos-dp-default-chk-wrp">
									<input title="<?php printf( __( 'If activated the Single %s Settings will have this row settings as default.','eos-dp' ),esc_attr( $labs_name ) ); ?>" class="eos-dp-default-post-type" type="checkbox"<?php echo isset( $plugins[2] ) && $plugins[2] == '1' ? ' checked' : ''; ?>/>
									<span></span>
								</span>
							</span>
							<span class="eos-dp-x-space"></span>
							<div class="eos-dp-actions">
								<a class="eos-dp-to-singles fdp-has-tooltip" style="padding:0 10px;border:1px solid #fff !important" href="<?php echo add_query_arg( 'eos_dp_post_type',$post_type,admin_url( 'admin.php?page=eos_dp_menu' ) ); ?>"><span class="dashicons dashicons-admin-generic"></span><span style="position:relative;top:6px"><?php _e( 'Singles','eos-dp'); ?></span>
									<div class="fdp-tooltip"><?php printf( __( 'Go to singles settings of %s','eos-dp' ),$labs_name ); ?></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>
									<div class="fdp-action-msg fdp-msg-success" style="opacity:0;position:absolute;top:34px;background:#fff;padding:10px;transition:opacity 0.5s linear"><?php _e( 'Row settings copied','eos-dp' ); ?></div>
									<div class="fdp-action-msg fdp-msg-error" style="opacity:0;display:none;position:absolute;top:34px;background:#fff;padding:10px;transition:opacity 0.5s linear"><?php _e( 'It was not possible to copy the 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' ); ?>
							</div>
						</td>
					<?php
					$k = 0;
					foreach( $active_plugins as $plugin ){
						$active = !isset( $plugins[1] ) || !in_array( $plugin,explode( ',',$plugins[1] ) ) ? 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-post-type-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; ?>" type="checkbox"<?php echo $active ? ' checked' : ''; ?> />
							</div>
						</td>
					<?php
							++$k;
						}
					}
					?>
					</tr>
					<?php
					}
				}
				++$row;
			}
			?>
			</table>
		</div>
		<?php eos_dp_save_button(); ?>
	</section>
	<?php
}
