<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Admin
 */

/**
 * Exposes shortlinks in a global, so that we can pass them to our Javascript components.
 */
class WPSEO_Expose_Shortlinks implements WPSEO_WordPress_Integration {

	/**
	 * Array containing the keys and shortlinks.
	 *
	 * @var array
	 */
	private $shortlinks = [
		'shortlinks.advanced.allow_search_engines'                  => 'https://wptopr.com/allow-search-engines',
		'shortlinks.advanced.follow_links'                          => 'https://wptopr.com/follow-links',
		'shortlinks.advanced.meta_robots'                           => 'https://wptopr.com/meta-robots-advanced',
		'shortlinks.advanced.breadcrumbs_title'                     => 'https://wptopr.com/breadcrumbs-title',
		'shortlinks.metabox.schema.explanation'                     => 'https://wptopr.com/400',
		'shortlinks.metabox.schema.page_type'                       => 'https://wptopr.com/402',
		'shortlinks.sidebar.schema.explanation'                     => 'https://wptopr.com/401',
		'shortlinks.sidebar.schema.page_type'                       => 'https://wptopr.com/403',
		'shortlinks.focus_keyword_info'                             => 'https://wptopr.com/focus-keyword',
		'shortlinks.nofollow_sponsored'                             => 'https://wptopr.com/nofollow-sponsored',
		'shortlinks.snippet_preview_info'                           => 'https://wptopr.com/snippet-preview',
		'shortlinks.cornerstone_content_info'                       => 'https://wptopr.com/1i9',
		'shortlinks.upsell.social_preview.social'                   => 'https://wptopr.com/social-preview-facebook',
		'shortlinks.upsell.social_preview.x'                        => 'https://wptopr.com/social-preview-twitter',
		'shortlinks.upsell.sidebar.news'                            => 'https://wptopr.com/get-news-sidebar',
		'shortlinks.upsell.sidebar.premium_seo_analysis_button'     => 'https://wptopr.com/premium-seo-analysis-sidebar',
		'shortlinks.upsell.sidebar.additional_link'                 => 'https://wptopr.com/textlink-keywords-sidebar',
		'shortlinks.upsell.sidebar.additional_button'               => 'https://wptopr.com/add-keywords-sidebar',
		'shortlinks.upsell.sidebar.word_complexity'                 => 'https://wptopr.com/word-complexity-sidebar',
		'shortlinks.upsell.sidebar.internal_linking_suggestions'    => 'https://wptopr.com/internal-linking-suggestions-sidebar',
		'shortlinks.upsell.sidebar.highlighting_seo_analysis'       => 'https://wptopr.com/highlighting-seo-analysis',
		'shortlinks.upsell.sidebar.highlighting_readability_analysis' => 'https://wptopr.com/highlighting-readability-analysis',
		'shortlinks.upsell.sidebar.highlighting_inclusive_analysis' => 'https://wptopr.com/highlighting-inclusive-analysis',
		'shortlinks.upsell.sidebar.content_blocks'                  => 'https://wptopr.com/content-blocks-sidebar',
		'shortlinks.upsell.metabox.news'                            => 'https://wptopr.com/get-news-metabox',
		'shortlinks.upsell.metabox.go_premium'                      => 'https://wptopr.com/pe-premium-page',
		'shortlinks.upsell.metabox.premium_seo_analysis_button'     => 'https://wptopr.com/premium-seo-analysis-metabox',
		'shortlinks.upsell.metabox.additional_link'                 => 'https://wptopr.com/textlink-keywords-metabox',
		'shortlinks.upsell.metabox.additional_button'               => 'https://wptopr.com/add-keywords-metabox',
		'shortlinks.upsell.metabox.word_complexity'                 => 'https://wptopr.com/word-complexity-metabox',
		'shortlinks.upsell.metabox.internal_linking_suggestions'    => 'https://wptopr.com/internal-linking-suggestions-metabox',
		'shortlinks.upsell.metabox.content_blocks'                  => 'https://wptopr.com/content-blocks-metabox',
		'shortlinks.upsell.gsc.create_redirect_button'              => 'https://wptopr.com/redirects',
		'shortlinks.readability_analysis_info'                      => 'https://wptopr.com/readability-analysis',
		'shortlinks.inclusive_language_analysis_info'               => 'https://wptopr.com/inclusive-language-analysis',
		'shortlinks.activate_premium_info'                          => 'https://wptopr.com/activate-subscription',
		'shortlinks.wincher.seo_performance'                        => 'https://wptopr.com/wincher-integration',
		'shortlinks-insights-estimated_reading_time'                => 'https://wptopr.com/4fd',
		'shortlinks-insights-flesch_reading_ease'                   => 'https://wptopr.com/34r',
		'shortlinks-insights-flesch_reading_ease_sidebar'           => 'https://wptopr.com/4mf',
		'shortlinks-insights-flesch_reading_ease_metabox'           => 'https://wptopr.com/4mg',
		'shortlinks-insights-flesch_reading_ease_article'           => 'https://wptopr.com/34s',
		'shortlinks-insights-keyword_research_link'                 => 'https://wptopr.com/keyword-research-metabox',
		'shortlinks-insights-upsell-sidebar-prominent_words'        => 'https://wptopr.com/prominent-words-upsell-sidebar',
		'shortlinks-insights-upsell-metabox-prominent_words'        => 'https://wptopr.com/prominent-words-upsell-metabox',
		'shortlinks-insights-upsell-elementor-prominent_words'      => 'https://wptopr.com/prominent-words-upsell-elementor',
		'shortlinks-insights-word_count'                            => 'https://wptopr.com/word-count',
		'shortlinks-insights-upsell-sidebar-text_formality'         => 'https://wptopr.com/formality-upsell-sidebar',
		'shortlinks-insights-upsell-metabox-text_formality'         => 'https://wptopr.com/formality-upsell-metabox',
		'shortlinks-insights-upsell-elementor-text_formality'       => 'https://wptopr.com/formality-upsell-elementor',
		'shortlinks-insights-text_formality_info_free'              => 'https://wptopr.com/formality-free',
		'shortlinks-insights-text_formality_info_premium'           => 'https://wptopr.com/formality',
	];

	/**
	 * Registers all hooks to WordPress.
	 *
	 * @return void
	 */
	public function register_hooks() {
		add_filter( 'wpseo_admin_l10n', [ $this, 'expose_shortlinks' ] );
	}

	/**
	 * Adds shortlinks to the passed array.
	 *
	 * @param array $input The array to add shortlinks to.
	 *
	 * @return array The passed array with the additional shortlinks.
	 */
	public function expose_shortlinks( $input ) {
		foreach ( $this->get_shortlinks() as $key => $shortlink ) {
			$input[ $key ] = WPSEO_Shortlinker::get( $shortlink );
		}

		$input['default_query_params'] = WPSEO_Shortlinker::get_query_params();

		return $input;
	}

	/**
	 * Retrieves the shortlinks.
	 *
	 * @return array The shortlinks.
	 */
	private function get_shortlinks() {
		if ( ! $this->is_term_edit() ) {
			return $this->shortlinks;
		}

		$shortlinks = $this->shortlinks;

		$shortlinks['shortlinks.upsell.metabox.additional_link']              = 'https://wptopr.com/textlink-keywords-metabox-term';
		$shortlinks['shortlinks.upsell.metabox.additional_button']            = 'https://wptopr.com/add-keywords-metabox-term';
		$shortlinks['shortlinks.upsell.metabox.word_complexity']              = 'https://wptopr.com/word-complexity-metabox-term';
		$shortlinks['shortlinks.upsell.metabox.internal_linking_suggestions'] = 'https://wptopr.com/internal-linking-suggestions-metabox-term';

		return $shortlinks;
	}

	/**
	 * Checks if the current page is a term edit page.
	 *
	 * @return bool True when page is term edit.
	 */
	private function is_term_edit() {
		global $pagenow;

		return WPSEO_Taxonomy::is_term_edit( $pagenow );
	}
}
