<?php
/**
 * This si the markup for the custom post type plugin-list
 *
 * @package Instalist
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
?>
<?php

$allowed_html = array(
	'input' => array(
		'id'       => array(),
		'name'     => array(),
		'value'    => array(),
		'class'    => array(),
		'type'     => array(),
		'disabled' => array(),
		'required' => array(),
	),
	'div'   => array(
		'id'    => array(),
		'class' => array(),
	),
	'label' => array(
		'for'   => array(),
		'class' => array(),
	),
);

$plugin_names     = get_post_meta( $post->ID, '_plgnr_plugin_names', true ) ? get_post_meta( $post->ID, '_plgnr_plugin_names', true ) : array();
$plugin_slugs     = get_post_meta( $post->ID, '_plgnr_plugin_slugs', true ) ? get_post_meta( $post->ID, '_plgnr_plugin_slugs', true ) : array();
$plugin_icons     = get_post_meta( $post->ID, '_plgnr_plugin_icons', true ) ? get_post_meta( $post->ID, '_plgnr_plugin_icons', true ) : array();
$is_primary       = get_post_meta( $post->ID, '_is_primary_list', true ) ? get_post_meta( $post->ID, '_is_primary_list', true ) : 0;
$required_plugins = get_post_meta( $post->ID, '_plgnr_plugin_required_plugins', true ) ? get_post_meta( $post->ID, '_plgnr_plugin_required_plugins', true ) : array();


/**
 * Check if a required plugin is alredy in the list
 *
 * @param string $rp The required plugin.
 * @param string $name One of the names in the plugin list.
 */
function is_required_plugin_in_list( $rp, $name ) {
	// compare the passed required plugin ($rp) to the provided name.
	return strtolower( $rp ) === strtolower( $name ) ? true : false;
}

$count           = count( $plugin_names );
$installed_count = 0;
$plugins_in_list = array();
if ( 0 < $count ) {
	for ( $i = 0; $i < $count; $i++ ) {
		if ( ! empty( $plugin_names[ $i ] ) ) {
			$plugin_dir       = WP_PLUGIN_DIR . '/' . $plugin_slugs[ $i ] . '/';
			$plugin_main_file = Pluginer_Helper::find_main_plugin_file( $plugin_slugs[ $i ] );
			$is_installed     = $plugin_main_file && file_exists( WP_PLUGIN_DIR . '/' . $plugin_main_file );
			$class            = $is_installed ? 'already-installed' : 'not-installed';
			if ( 'already-installed' === $class ) {
				++$installed_count;
			}
			$plugins_in_list[] = array(
				'name'             => $plugin_names[ $i ],
				'slug'             => $plugin_slugs[ $i ],
				'icon'             => $plugin_icons[ $i ],
				'required_plugins' => count( $required_plugins ) > 0 ? $required_plugins [ $i ] : 'NR',
				'class'            => $class,
				'is-installed'     => $is_installed,
			);
		}
	}
}
usort(
	$plugins_in_list,
	function ( $a, $b ) {
		return strcasecmp( $a['name'], $b['name'] ); // ordina ignorando maiuscole/minuscole.
	}
);
$disabled_all = $installed_count === $count ? 'disabled' : '';

?>


<?php
$is_premium = apply_filters( 'pluginer_is_premium', false );

$disabled = '';
if ( ! $is_premium || $disabled_all ) {
	$disabled = 'disabled';
}

// Pre-generate nonces once and reuse to avoid duplicate calls in the markup.
$install_nonce = wp_create_nonce( 'plgnr_installpluginlist' );
$export_nonce  = wp_create_nonce( 'plgnrexportpluginlist' );
?>
<div id="pluginer-options" class="wrap">
	<div class="inline-buttons">
		<button class="button-primary install_list_button install_list_action" <?php echo esc_attr( $disabled ); ?> id="install_selected_plugins_button" data-nonce="<?php echo esc_attr( $install_nonce ); ?>" data-post-id="<?php echo esc_attr( $post->ID ); ?>">Install selected plugins</button>
		<button class="button-primary install_list_button install_list_action" id="install_all_plugin_button" data-post-id="<?php echo esc_attr( $post->ID ); ?>" <?php echo esc_attr( $disabled_all ); ?> data-nonce="<?php echo esc_attr( $install_nonce ); ?>">Install all</button>
		<?php wp_nonce_field( 'plgnrexportpluginlist', '_wpnonce_export_list' ); ?>
		<button <?php echo ( get_transient( 'plgnr_post_saved_' . get_the_ID() ) ) ? '' : 'disabled'; ?> class="button-primary export_list_button" data-post-id="<?php echo esc_attr( $post->ID ); ?>"
			data-nonce="<?php echo esc_attr( $export_nonce ); ?>">Export
			list</button>
	</div>

	<div id="plugins-list-table" class="plgnr-list">
		<div class="table-head">
			<div class="table-row">
				<div class="table-column"></div>
				<div class="table-column">Plugin icon</div>
				<div class="table-column">Requirements</div>
				<div class="table-column">Plugin name</div>
				<div class="table-column">Plugin slug</div>
				<div class="table-column">Action</div>
			</div>
		</div>
		<div class="table-body">
			<?php
			$count           = count( $plugins_in_list );
			$installed_count = 0;
			if ( 0 < $count ) {
				// iterate only up to count-1 to avoid undefined index access.
				for ( $i = 0; $i < $count; $i++ ) {
					$title_text = '';
					if ( ! empty( $plugins_in_list[ $i ]['name'] ) ) {
						$nirq = Pluginer_Helper::check_required_plugins( $plugins_in_list[ $i ]['required_plugins'], $post->ID );
						// 'is-installed' is stored as a boolean when building the list
						if ( ! empty( $plugins_in_list[ $i ]['is-installed'] ) && $plugins_in_list[ $i ]['is-installed'] ) {
							$title_text = 'This plugin is already installed.';
						}
						$is_required = 'data-is-required=no';
						if ( in_array( strtolower( $plugins_in_list[ $i ]['name'] ), array_map( 'strtolower', $required_plugins ), true ) ) {
							$is_required = 'data-is-required=yes';
						}
						$pd = $plugins_in_list[ $i ]['icon'] . ';' . $plugins_in_list[ $i ]['name'] . ';' . $plugins_in_list[ $i ]['slug'];
						?>
				<div class="table-row <?php echo esc_attr( $plugins_in_list[ $i ]['class'] ); ?>" <?php echo esc_attr( $is_required ); ?> title="<?php echo esc_attr( $title_text ); ?>" id="<?php echo esc_attr( $plugins_in_list[ $i ]['slug'] ); ?>">
				<div class="table-column">
					<input type="checkbox" <?php echo esc_attr( $disabled ); ?> name="plugin_to_install[]" class="plugin_to_install" value="<?php echo esc_attr( $pd ); ?>" <?php echo esc_attr( $is_required ); ?> data-required-plugins="<?php echo esc_attr( $plugins_in_list[ $i ]['required_plugins'] ); ?>" data-nirq="<?php echo esc_attr( strtolower( $nirq ) ); ?>" data-plugname="<?php echo esc_attr( strtolower( $plugins_in_list[ $i ]['name'] ) ); ?>" />
					<img class="installed-check" src="<?php echo esc_url( PLUGINER_URL . '/img/check.png' ); ?>" />
				</div>
				<div class="table-column">
					<img src="<?php echo esc_url( $plugins_in_list[ $i ]['icon'] ); ?>" />
				</div>
				<div class="table-column">
						<?php
						if ( empty( $nirq ) ) {
							?>
							<img title="<?php echo esc_attr( 'This plugin can be installed and activated' ); ?>" src="<?php echo esc_url( PLUGINER_URL . 'img/okay3.png' ); ?>" />
							<?php
						} else {
							?>
							<img title="<?php echo esc_attr( 'This plugin requires the following plugin(s): ' . $nirq ); ?>" src="<?php echo esc_url( PLUGINER_URL . 'img/warning.png' ); ?>" />
							<?php
						}
						?>
				</div>
				<div class="table-column">
						<?php echo esc_html( $plugins_in_list[ $i ]['name'] ); ?>
				</div>
				<div class="table-column">
						<?php echo esc_html( $plugins_in_list[ $i ]['slug'] ); ?>
				</div>
				<div class="table-column">
					<button data-slug="<?php echo esc_attr( $plugins_in_list[ $i ]['slug'] ); ?>"
						class="button remove_plugin_button">Remove</button>
				</div>
				<input type="hidden" name="plugin_name[]" class="plugin_name"
					value="<?php echo esc_attr( $plugins_in_list[ $i ]['name'] ); ?>" />
				<input type="hidden" name="plugin_slug[]" class="plugin_slug"
					value="<?php echo esc_attr( $plugins_in_list[ $i ]['slug'] ); ?>" />
				<input type="hidden" name="plugin_icon[]" class="plugin_icon"
					value="<?php echo esc_attr( $plugins_in_list[ $i ]['icon'] ); ?>" />
			</div>
						<?php
					}
				}
			}
			?>

		</div>
		<div class="table-footer">
			<div class="table-row">
				<div class="table-column"></div>
				<div class="table-column">Plugin icon</div>
				<div class="table-column">Requirements</div>
				<div class="table-column">Plugin name</div>
				<div class="table-column">Plugin slug</div>
				<div class="table-column">Action</div>
			</div>
		</div>

	</div>
		<div class="inline-buttons">
		<button class="button-primary empty_list" id="empty_plugin_list_button" disabled>Empty list</button>
	</div>

	<p class="note">Keep in mind that many plugins don't use the header 'Requires Plugins', so it could happen that a plugin looks activable but it is not. In this case, you'll have to activate it manually looking at the notices eventually shown in the dashboard.</p>

	<div id="form_wrapper" class="plugins-data-container">
		<?php $uploads = wp_upload_dir(); ?>
		<div id="main_form_wrapper">
			<div class="new_plugin_fields">
				<h3>Search for plugins...</h3>
				<p>Please, start by typing the plugin name.<br>Pluginrer will provide a list of plugins to make easier to
					find the plugin you are looking for. If you find it, just click on it and the form will be automatically
					filled out with the plugin's data.</p>
				<div class="field">
					<label>Plugin name*</label>
					<input type="text" name="plugin_name_input" id="plugin_name" />
					<input type="hidden" name="search_url" id="search_url"
						value="<?php echo esc_url( $uploads['baseurl'] ); ?>" />
					<input type="hidden" name="plugin_icon_input" id="plugin_icon" value="" />
					<input type="hidden" name="plugin_slug_input" id="plugin_slug" />
				</div>

				<div class="inline-buttons">
					<input type="button" id="add_plugin_button" value="Add plugin" class="button-primary" disabled />
					<input type="button" id="cancel_add_plugin_button" value="Cancel" class="button" disabled />
				</div>
				<h3>...or load installed plugins</h3>
				<?php
				$local_disabled = '';
				if ( ! $is_premium ) {
					$local_disabled = 'disabled';
				}
				?>
				<div class="inline-buttons">
					<input type="button" id="load_local_plugins" value="Load local plugins"
						class="button-primary <?php echo esc_attr( $local_disabled ); ?>" />
					<input type="button" id="create_list_with_local_plugins" value="Create e list from installed plugins"
						class="button-primary <?php echo esc_attr( $local_disabled ); ?>" />
				</div>
			</div>
			<div id="plugins_result_list">
				<div id="plugins2-overlay" class="plgnr-page-overlay">
					<div id="loader-container">
						<div class="plgsspinner"></div>
					</div>
				</div>
			</div>
		</div>
		<div class="pagination_buttons">
			<div class="inline-buttons">
				<button id="clear_found_plugins" class="button" disabled>Clear results</button>
				<input type="hidden" name="current_page" id="current_page" vañue="" />
				<input type="button" disabled id="load_prev_plugins" value="Previous"
					class="button-primary paginating paginating-prev" />
				<span id="pagination_info">Page 1</span>					
				<input type="button" disabled id="load_next_plugins" value="Next"
					class="button-primary paginating paginating-next" />
			</div>
		</div>
	</div>
</div>