<?php

if (!defined('ABSPATH')) {
  exit; // Exit if accessed directly
}

global $bookyourtravel_theme_admin;

if ( ! function_exists( 'get_plugins' ) ) {
	require_once wp_normalize_path( ABSPATH . 'wp-admin/includes/plugin.php' );
}

$plugins                         = TGM_Plugin_Activation::$instance->plugins;
$installed_plugins               = get_plugins();

if ( ! function_exists( 'plugins_api' ) ) {
	include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // For plugins_api.
}

?>
<div class="wrap bookyourtravel-plugins-wrap bookyourtravel-wrap">
  <h1><?php echo esc_html( esc_html__( 'Plugins', 'bookyourtravel' ) ); ?></h1>
  <?php $bookyourtravel_theme_admin->render_admin_screen_tabs('plugins'); ?>

  <div id="bookyourtravel-install-plugins" class="bookyourtravel-install-plugins">
    <div class="feature-section theme-browser rendered">

    <ul class="plugin-list">
    <?php
    foreach ($plugins as $plugin) {
      $plugin_status = '';
      $plugin_css_class = 'plugin plugin-' . $plugin['slug'];

      if ( BookYourTravel_Theme_Utils::is_plugin_activated(  $plugin['file_path'] ) ) {
        $plugin_css_class .= ' plugin-activated';
      }

      if (isset($plugin['required']) && $plugin['required']) {
        $plugin_css_class .= ' plugin-required';
      }

      $installed_plugin_version = '';
      if (isset( $installed_plugins[ $plugin['file_path'] ])) {
          $plugin_css_class .= ' plugin-installed';
          if (isset($installed_plugins[ $plugin['file_path'] ]['Version'])) {
              $installed_plugin_version = $installed_plugins[ $plugin['file_path'] ]['Version'];
          }
      }

      if ($installed_plugin_version && $plugin['version'] && $plugin['version'] !== $installed_plugin_version) {
        $plugin_css_class .= ' plugin-needs-update';
        $plugin_status = 'needs-update';
      }

      $install_url = wp_nonce_url(
        add_query_arg(
          [
            'page'          => rawurlencode( TGM_Plugin_Activation::$instance->menu ),
            'plugin'        => rawurlencode( $plugin['slug'] ),
            'plugin_name'   => rawurlencode( $plugin['name'] ),
            'tgmpa-install' => 'install-plugin',
            'return_url'    => 'bookyourtravel-plugins',
          ],
          TGM_Plugin_Activation::$instance->get_tgmpa_url()
        ),
        'tgmpa-install',
        'tgmpa-nonce'
      );

      $activate_url = esc_url(
				add_query_arg(
					[
						'plugin'                 => rawurlencode( $plugin['slug'] ),
						'plugin_name'            => rawurlencode( $plugin['name'] ),
						'bookyourtravel-activate'       => 'activate-plugin',
						'bookyourtravel-activate-nonce' => wp_create_nonce( 'bookyourtravel-activate' ),
					],
					admin_url( 'admin.php?page=bookyourtravel-plugins' )
				)
			);

			$deactivate_url = esc_url(
				add_query_arg(
					[
						'plugin'                 => rawurlencode( $plugin['slug'] ),
						'plugin_name'            => rawurlencode( $plugin['name'] ),
						'bookyourtravel-deactivate'       => 'deactivate-plugin',
						'bookyourtravel-deactivate-nonce' => wp_create_nonce( 'bookyourtravel-deactivate' ),
					],
					admin_url( 'admin.php?page=bookyourtravel-plugins' )
				)
      );

      $update_url = wp_nonce_url(
        add_query_arg(
          [
            'page'          => rawurlencode( TGM_Plugin_Activation::$instance->menu ),
            'plugin'        => rawurlencode( $plugin['slug'] ),
            'plugin_name'   => rawurlencode( $plugin['name'] ),
            'tgmpa-update' => 'update-plugin',
            'return_url'    => 'bookyourtravel-plugins',
          ],
          TGM_Plugin_Activation::$instance->get_tgmpa_url()
        ),
        'tgmpa-update',
        'tgmpa-nonce'
      );

      ?>
      <li>
        <div class="<?php echo esc_attr($plugin_css_class); ?>">
		
            <?php if (isset($plugin['required']) && $plugin['required']) { ?>
              <span class="required"><?php echo esc_html__('Required', 'bookyourtravel'); ?></span>
            <?php } ?>
		 <figure>
            <img src="<?php echo esc_url($plugin['image_url']); ?>" alt="<?php echo esc_attr($plugin['name']); ?>" />
         </figure>
          <div class="actions">
			<div class="title">
			  <div class="name"><?php echo esc_html($plugin['name']); ?></div>
              <div class="version"><?php echo isset($plugin['version']) && strlen($plugin['version']) > 0 ? 'v' . esc_html($plugin['version']) . ' - ' : ''; ?><a href="<?php echo esc_url($plugin['plugin_url']); ?>" target="_blank"><?php echo esc_html($plugin['author']); ?></a></div>
			</div>
			<div class="links">
              <a class="install button button-large button-primary" href="<?php echo esc_url($install_url); ?>"><?php echo esc_html__('Install', 'bookyourtravel'); ?></a>
              <a class="activate button button-large button-primary" href="<?php echo esc_url($activate_url); ?>"><?php echo esc_html__('Activate', 'bookyourtravel'); ?></a>
              <a class="deactivate button button-large button-primary" href="<?php echo esc_url($deactivate_url); ?>"><?php echo esc_html__('Deactivate', 'bookyourtravel'); ?></a>
              <a class="update button button-large button-primary" href="<?php echo esc_url($update_url); ?>"><?php echo esc_html__('Update', 'bookyourtravel'); ?></a>
            </div>
          </div>
        </div>
      </li>
      <?php
    }
    ?>
    </ul>
    </div>
  </div>

  <?php $bookyourtravel_theme_admin->render_admin_screen_footer('plugins'); ?>
</div>
