<?php
/**
 * Import page section template.
 *
 * @since 3.0
 */

use WP_science\Logger\Logger;

defined( 'ABSPATH' ) || exit;

// Debug mode.
$science_log_description = '';

if ( science_direct_filesystem()->exists( Logger::get_log_file_path() ) ) {
	$science_stats = Logger::get_log_file_stats();

	if ( ! is_wp_error( $science_stats ) ) {
		// translators: %1$s = formatted file size, %2$s = formatted number of entries (don't use %2$d).
		$science_log_description .= sprintf( esc_html__( 'Files size: %1$s. Number of entries: %2$s.', 'science' ), '<strong>' . esc_html( $science_stats['bytes'] ) . '</strong>', '<strong>' . esc_html( $science_stats['entries'] ) . '</strong>' );

		// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
		$science_log_description .= '<br/>' . sprintf( esc_html__( '%1$sDownload the file%2$s.', 'science' ), '<a href="' . esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=science_download_debug_file' ), 'download_debug_file' ) ) . '">', '</a>' );

		// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
		$science_log_description .= ' - ' . sprintf( esc_html__( '%1$sDelete the file%2$s.', 'science' ), '<a href="' . esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=science_delete_debug_file' ), 'delete_debug_file' ) ) . '">', '</a>' );
	}
}
?>

<div id="tools" class="wpr-Page">
	<div class="wpr-sectionHeader">
		<h2 class="wpr-title1 wpr-icon-tools"><?php esc_html_e( 'Tools', 'science' ); ?></h2>
	</div>
	<div class="wpr-tools">
		<div class="wpr-tools-col">
			<div class="wpr-title3 wpr-tools-label wpr-icon-export"><?php esc_html_e( 'Export settings', 'science' ); ?></div>
			<div class="wpr-field-description"><?php esc_html_e( 'Download a backup file of your settings', 'science' ); ?></div>
		</div>
		<div class="wpr-tools-col">
			<?php
			$this->render_action_button(
				'link',
				'science_export',
				[
					'label'      => __( 'Download settings', 'science' ),
					'attributes' => [
						'class' => 'wpr-button wpr-button--icon wpr-button--small wpr-button--purple wpr-icon-chevron-down',
					],
				]
			);
			?>
		</div>
	</div>

	<?php $this->render_import_form(); ?>

	<div class="wpr-tools">
		<div class="wpr-tools-col">
			<div class="wpr-title3 wpr-tools-label wpr-icon-rollback"><?php esc_html_e( 'Rollback', 'science' ); ?></div>
			<div class="wpr-field-description">
				<?php
				// translators: %s = WP science version number.
				printf( esc_html__( 'Has version %s caused an issue on your website?', 'science' ), esc_html( WP_science_VERSION ) );
				?>
				<br><br>
				<?php
				// translators: %s = <br>.
				printf( esc_html__( 'You can rollback to the previous major version here.%sThen send us a support request.', 'science' ), '<br/>' );
				?>
			</div>
		</div>
		<div class="wpr-tools-col">
			<?php
			$this->render_action_button(
				'link',
				'science_rollback',
				[
					// translators: %s = WP science previous version.
					'label'      => sprintf( __( 'Reinstall version %s', 'science' ), WP_science_LASTVERSION ),
					'attributes' => [
						'class' => 'wpr-button wpr-button--icon wpr-button--small wpr-button--red wpr-icon-refresh',
					],
				]
			);
			?>
		</div>
	</div>

	<?php
	/**
	 * Fires after the Settings Tools page content
	 *
	 * @since 3.6
	 */
	do_action( 'science_settings_tools_content' );
	?>

	<!-- Temporary hide the option. The logger can still be activated by adding the following to the wp-config.php file: define( 'WP_science_DEBUG', true );
	<div class="wpr-tools">
		<div class="wpr-tools-col wpr-radio">
			<div class="wpr-title3 wpr-tools-label">
				<input id="debug_enabled" name="wp_science_settings[debug_enabled]" value="1"<?php checked( true, Logger::debug_enabled() ); ?> type="checkbox">
				<label for="debug_enabled">
					<span data-l10n-active="On" data-l10n-inactive="Off" class="wpr-radio-ui"></span>
					<?php esc_html_e( 'Debug mode', 'science' ); ?>
				</label>
			</div>

			<div class="wpr-field-description">
				<?php esc_html_e( 'Create a debug log file.', 'science' ); ?>
			</div>
		</div>
		<div class="wpr-tools-col">
			<?php echo $science_log_description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view. ?>
		</div>
	</div>
	-->
</div>
