<?php

defined( 'ABSPATH' ) || exit;

/**
 * Add menu in admin bar.
 * From this menu, you can preload the cache files, clear entire domain cache or post cache (front & back-end).
 *
 * @since 1.3.5 Compatibility with qTranslate
 * @since 1.3.0 Compatibility with WPML
 * @since 1.0
 *
 * @param Object $wp_admin_bar Admin bar object.
 */
function science_admin_bar( $wp_admin_bar ) {
	global $pagenow, $post;

	if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
		$referer = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
		$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer ) );
	} else {
		$referer = '';
	}

	$has_cap = false;

	$capabilities = [
		'science_manage_options',
		'science_purge_cache',
		'science_preload_cache',
		'science_regenerate_critical_css',
		'science_remove_unused_css',
	];

	foreach ( $capabilities as $cap ) {
		if ( current_user_can( $cap ) ) {
			$has_cap = true;
			break;
		}
	}

	if ( $has_cap ) {
		/**
		 * Parent.
		 */
		$wp_admin_bar->add_menu(
			[
				'id'    => 'wordpress',
				'title' => WP_science_PLUGIN_NAME,
				'href'  => current_user_can( 'science_manage_options' ) ? admin_url( 'options-general.php?page=' . WP_science_PLUGIN_SLUG ) : false,
			]
		);
	}

	if ( current_user_can( 'science_manage_options' ) ) {
		/**
		 * Settings.
		 */
		$wp_admin_bar->add_menu(
			[
				'parent' => 'wordpress',
				'id'     => 'science-settings',
				'title'  => __( 'Settings', 'science' ),
				'href'   => admin_url( 'options-general.php?page=' . WP_science_PLUGIN_SLUG ),
			]
		);
	}

	if ( current_user_can( 'science_purge_cache' ) ) {
		/**
		 * Purge Cache.
		 */
		$action = 'purge_cache';

		if ( science_valid_key() ) {
			$i18n_plugin = science_has_i18n();

			if ( $i18n_plugin ) {
				// Parent.
				$wp_admin_bar->add_menu(
					[
						'parent' => 'wordpress',
						'id'     => 'purge-all',
						'title'  => (bool) get_science_option( 'manual_preload', false ) ? __( 'Clear and preload cache', 'science' ) : __( 'Clear cache', 'science' ),
					]
				);

				$langlinks_default = [];

				// Add submenu for each active langs.
				switch ( $i18n_plugin ) {
					case 'wpml':
						$langlinks = get_science_wpml_langs_for_admin_bar();
						break;
					case 'qtranslate':
						$langlinks = get_science_qtranslate_langs_for_admin_bar();
						break;
					case 'qtranslate-x':
						$langlinks = get_science_qtranslate_langs_for_admin_bar( 'x' );
						break;
					case 'polylang':
						$langlinks = get_science_polylang_langs_for_admin_bar();
						break;
					default:
						/**
						 * Filters the value of the lang links menu
						 *
						 * @param array $langlinks Array of languages.
						 */
						$langlinks = apply_filters( 'science_i18n_admin_bar_menu', [] );

						if ( ! is_array( $langlinks ) ) {
							$langlinks = $langlinks_default;
						}
				}

				if ( $langlinks ) {
					foreach ( $langlinks as $lang ) {
						$wp_admin_bar->add_menu(
							[
								'parent' => 'purge-all',
								'id'     => 'purge-all-' . $lang['code'],
								'title'  => $lang['flag'] . '&nbsp;' . $lang['anchor'],
								'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=all&lang=' . $lang['code'] . $referer ), $action . '_all' ),
							]
						);
					}

					if ( 'wpml' !== $i18n_plugin ) {
						// Add subemnu "All langs" (the one for WPML is already printed).
						$wp_admin_bar->add_menu(
							[
								'parent' => 'purge-all',
								'id'     => 'purge-all-all',
								'title'  => '<div class="dashicons-before dashicons-admin-site" style="line-height:1.5"> ' . __( 'All languages', 'science' ) . '</div>',
								'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=all&lang=all' . $referer ), $action . '_all' ),
							]
						);
					}
				}
			} else {
				// Purge All.
				$wp_admin_bar->add_menu(
					[
						'parent' => 'wordpress',
						'id'     => 'purge-all',
						'title'  => (bool) get_science_option( 'manual_preload', false ) ? __( 'Clear and preload cache', 'science' ) : __( 'Clear cache', 'science' ),
						'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=all' . $referer ), $action . '_all' ),
					]
				);
			}

			/**
			 * Filters the science clear post admin bar menu.
			 *
			 * @since 3.11.4
			 *
			 * @param bool  $should_skip Should skip adding clear post to science option in admin bar.
			 * @param type  $post Post object.
			 */
			if ( ! apply_filters( 'science_skip_admin_bar_cache_purge_option', false, $post ) && science_can_display_options() ) {
				if ( is_admin() ) {
					/**
					 * Purge a post.
					 */
					if ( $post && 'post.php' === $pagenow && isset( $_GET['action'], $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
						$wp_admin_bar->add_menu(
							[
								'parent' => 'wordpress',
								'id'     => 'purge-post',
								'title'  => __( 'Clear this post', 'science' ),
								'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=post-' . $post->ID . $referer ), $action . '_post-' . $post->ID ),
							]
						);

					}
				} else {
					/**
					 * Purge this URL (frontend).
					 */
					$wp_admin_bar->add_menu(
						[
							'parent' => 'wordpress',
							'id'     => 'purge-url',
							'title'  => __( 'Purge this URL', 'science' ),
							'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=url' . $referer ), $action . '_url' ),
						]
					);
				}
			}
		}
	}

	if ( current_user_can( 'science_purge_sucuri_cache' ) ) {
		/**
		 * Purge Sucuri cache if Sucuri is active.
		 */
		if ( get_science_option( 'sucury_waf_cache_sync', 0 ) ) {
			$action = 'science_purge_sucuri';

			$wp_admin_bar->add_menu(
				[
					'parent' => 'wordpress',
					'id'     => 'purge-sucuri',
					'title'  => __( 'Purge Sucuri cache', 'science' ),
					'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . $referer ), $action ),
				]
			);
		}
	}

	/**
	 * Fires when adding WP science admin bar items
	 *
	 * @since 3.6
	 *
	 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
	 */
	do_action( 'science_admin_bar_items', $wp_admin_bar );

	if ( current_user_can( 'science_manage_options' ) ) {
		$sciencecdn_status = get_transient( 'sciencecdn_status' );

		if ( isset( $sciencecdn_status['subscription_active'] ) && 'running' === $sciencecdn_status['subscription_active'] ) {
			$wp_admin_bar->add_menu(
				[
					'parent' => 'wordpress',
					'id'     => 'purge-cdn-cache',
					'title'  => __( 'Purge scienceCDN cache', 'science' ),
					'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=science_purge_sciencecdn' . $referer ), 'science_purge_sciencecdn' ),
				]
			);
		}

		/**
		 * Go to WP science Documentation.
		 */
		$wp_admin_bar->add_menu(
			[
				'parent' => 'wordpress',
				'id'     => 'docs',
				'title'  => __( 'Documentation', 'science' ),
				'href'   => get_science_documentation_url(),
			]
		);

		/**
		 * Go to WP science FAQ.
		 */
		$wp_admin_bar->add_menu(
			[
				'parent' => 'wordpress',
				'id'     => 'faq',
				'title'  => __( 'FAQ', 'science' ),
				'href'   => get_science_faq_url(),
			]
		);

		/**
		 * Go to WP science Support.
		 */
		$wp_admin_bar->add_menu(
			[
				'parent' => 'wordpress',
				'id'     => 'support',
				'title'  => __( 'Support', 'science' ),
				'href'   => science_get_external_url(
					'support',
					[
						'utm_source' => 'wp_plugin',
						'utm_medium' => 'wp_science',
					]
				),
			]
		);
	}
}
add_action( 'admin_bar_menu', 'science_admin_bar', PHP_INT_MAX - 10 );
