<?php
/**
 * Adds a help tab to any CoCart page providing useful
 * and helpful information for the users.
 *
 * @author  Sébastien Dumont
 * @package CoCart\Admin
 * @since   3.10.0 Introduced.
 * @license GPL-3.0
 */

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

if ( ! class_exists( 'CoCart_Admin_Help_Tab' ) ) {

	class CoCart_Admin_Help_Tab {

		/**
		 * Constructor
		 *
		 * @access public
		 */
		public function __construct() {
			add_action( 'current_screen', array( $this, 'add_help_tabs' ), 50 );
		} // END __construct()

		/**
		 * Adds help tabs to the plugin pages.
		 *
		 * @access public
		 */
		public function add_help_tabs() {
			if ( isset( $_GET['page'] ) && strpos( trim( sanitize_key( wp_unslash( $_GET['page'] ) ) ), 'cocart' ) === 0 ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
				$screen = get_current_screen();

				$campaign_args = CoCart_Helpers::cocart_campaign(
					array(
						'utm_source'   => 'CoCartCore',
						'utm_medium'   => 'plugin-admin',
						'utm_campaign' => 'help-tab',
						'utm_content'  => 'help-tab',
					)
				);

				$screen->add_help_tab(
					array(
						'id'      => 'cocart_core_support_tab',
						'title'   => esc_html__( 'Help & Support', 'cocart-core' ),
						'content' =>
							'<h2>' . esc_html__( 'Help & Support', 'cocart-core' ) . '</h2>' .

							'<p>' . sprintf(
								/* translators: %s CoCart */
								__( "We are fanatical about support, and want you to get the best out of %s's REST API. If you run into any difficulties, there are several places you can find help:", 'cocart-core' ),
								'CoCart'
							) . '</p>' .

							'<ul>' .
							'<li>' . sprintf(
								/* translators: %1$s: Hyperlink opening, %2$s: Hyperlink closing */
								__( '%1$sDocumentation%2$s. Our extensive documentation contains the API reference and some examples.', 'cocart-core' ),
								'<a href="' . esc_url( CoCart_Helpers::build_shortlink( add_query_arg( $campaign_args, esc_url( COCART_DOCUMENTATION_URL ) ) ) ) . '" aria-label="' . esc_attr__( 'View API Reference', 'cocart-core' ) . '" title="' . esc_attr__( 'View API Reference', 'cocart-core' ) . '" target="_blank" rel="noopener noreferrer">',
								'</a>'
							) . '</li>' .
							'<li>' . sprintf(
								/* translators: %1$s: Hyperlink opening, %2$s: Hyperlink closing, %3$s: Discord, %4$s: CoCart */
								__( '%1$sDiscussions%2$s. We have an active and friendly community on our %3$s server who may be able to help you figure out the how-tos of %4$s.', 'cocart-core' ),
								'<a href="' . esc_url( CoCart_Helpers::build_shortlink( add_query_arg( $campaign_args, esc_url( COCART_STORE_URL . 'community/' ) ) ) ) . '" aria-label="' . esc_attr__( 'View API Reference', 'cocart-core' ) . '" title="' . esc_attr__( 'View API Reference', 'cocart-core' ) . '" target="_blank" rel="noopener noreferrer">',
								'</a>',
								'Discord',
								'CoCart'
							) . '</li>' .
							'<li>' . sprintf(
								/* translators: %1$s: Hyperlink opening, %2$s: Hyperlink closing, %3$s: CoCart */
								__( '%1$sTranslate%2$s. %3$s is in need of translations. Is the plugin not translated in your language or do you spot errors with the current translations? Helping out is easy! %1$sTranslate %3$s%2$s.', 'cocart-core' ),
								'<a href="' . esc_url( COCART_TRANSLATION_URL ) . '" aria-label="' . esc_html__( 'Help translate', 'cocart-core' ) . '" title="' . esc_html__( 'Help translate', 'cocart-core' ) . ' "target="_blank" rel="noopener noreferrer">',
								'</a>',
								'CoCart'
							) . '</li>' .
							'<li>' . sprintf(
								/* translators: %1$s: Link to report issues, %2$s: Link to contribute instructions, %3$s: Hyperlink closing */
								__( '%2$sFound a bug?%4$s If you find a bug within %1$s core, please create a bug report on the %2$sGitHub repository%4$s. Ensure you read the %3$scontribution guide%4$s prior to submitting your report. To help solve your issue as fast as possible, please be as descriptive as possible by filling in the template provided as requested.', 'cocart-core' ),
								'CoCart',
								'<a href="' . esc_url( COCART_REPO_URL . '/issues' ) . '" target="_blank" rel="noopener noreferrer">',
								'<a href="' . esc_url( COCART_REPO_URL . '/blob/trunk/.github/CONTRIBUTING.md' ) . '" target="_blank" rel="noopener noreferrer">',
								'</a>'
							) . '</li>' .
							'</ul>',
					)
				);

				// Show only if we are not on the Setup Wizard page.
				if ( strpos( trim( sanitize_key( wp_unslash( $_GET['page'] ) ) ), 'cocart-setup' ) !== 0 ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					$screen->add_help_tab(
						array(
							'id'      => 'cocart_wizard_tab',
							'title'   => esc_html__( 'Setup Wizard', 'cocart-core' ),
							'content' =>
								'<h2>' . esc_html__( 'Setup Wizard', 'cocart-core' ) . '</h2>' .

								'<p>' . esc_html__( 'Need to access the setup wizard again? Press on the button below.', 'cocart-core' ) . '</p>' .

								'<p><a href="' . add_query_arg(
									array(
										'page'            => 'cocart-setup',
										'reset-cc-wizard' => true,
									),
									admin_url( 'admin.php' )
								) . '" class="button button-primary cocart-button" aria-label="' . sprintf(
									/* translators: %s CoCart */
									esc_attr__( 'View %s setup wizard', 'cocart-core' ),
									'CoCart'
								) . '">' . esc_html__( 'Setup Wizard', 'cocart-core' ) . '</a></p>',
						)
					);
				}

				$screen->set_help_sidebar(
					'<p><strong>' . esc_html__( 'Information', 'cocart-core' ) . '</strong></p>' .

					'<p><span class="dashicons dashicons-admin-plugins"></span> ' . esc_html__( 'Version', 'cocart-core' ) . ' ' . COCART_VERSION . '</p>' .

					'<p><span class="dashicons dashicons-admin-home"></span> <a href="' . esc_url( CoCart_Helpers::build_shortlink( add_query_arg( $campaign_args, esc_url( COCART_STORE_URL ) ) ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Visit website', 'cocart-core' ) . '</a></p>' .

					'<p><span class="dashicons dashicons-wordpress"></span> <a href="' . esc_url( COCART_PLUGIN_URL ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'View details', 'cocart-core' ) . '</a></p>' .

					'<p><span class="dashicons dashicons-external"></span> <a href="https://github.com/co-cart/co-cart/" target="_blank" rel="noopener noreferrer">' . esc_html__( 'GitHub', 'cocart-core' ) . '</a></p>'
				);
			}
		} // END add_help_tabs()
	} // END class.

} // END if class exists.

return new CoCart_Admin_Help_Tab();
