<?php
/**
 * Recommended plugins notice.
 *
 * Everything listed here is hosted on WordPress.org and entirely optional - the
 * theme is fully functional without any of it. Nothing is bundled and nothing is
 * installed on the user's behalf; the links below hand off to core's own plugin
 * installer, behind a capability check and a nonce.
 *
 * @package Opus_Blog
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'opus_blog_recommended_plugins' ) ) :
	/**
	 * The plugins worth suggesting, in the order they are shown.
	 *
	 * @return array[] Each entry has a slug, the main plugin file and a display name.
	 */
	function opus_blog_recommended_plugins() {

		return array(
			array(
				'slug' => 'one-click-demo-import',
				'file' => 'one-click-demo-import/one-click-demo-import.php',
				'name' => __( 'One Click Demo Import', 'opus-blog' ),
			),
			array(
				'slug' => 'template-sell-demo-importer',
				'file' => 'template-sell-demo-importer/templatesell-demo-importer.php',
				'name' => __( 'Template Sell Demo Importer', 'opus-blog' ),
			),
			array(
				'slug' => 'templatesell-gated-downloads',
				'file' => 'templatesell-gated-downloads/templatesell-gated-downloads.php',
				'name' => __( 'Gated Downloads', 'opus-blog' ),
			),
			array(
				'slug' => 'templatesell-captchaflow',
				'file' => 'templatesell-captchaflow/captchaflow.php',
				'name' => __( 'CaptchaFlow', 'opus-blog' ),
			),
		);
	}
endif;

if ( ! function_exists( 'opus_blog_missing_recommended_plugins' ) ) :
	/**
	 * List the recommended plugins that are not active yet.
	 *
	 * @return array[] Subset of opus_blog_recommended_plugins(), each with an added
	 *                 'installed' flag so the notice can offer the right action.
	 */
	function opus_blog_missing_recommended_plugins() {

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

		$installed = array_keys( get_plugins() );
		$missing   = array();

		foreach ( opus_blog_recommended_plugins() as $plugin ) {
			if ( is_plugin_active( $plugin['file'] ) ) {
				continue;
			}

			$plugin['installed'] = in_array( $plugin['file'], $installed, true );
			$missing[]           = $plugin;
		}

		return $missing;
	}
endif;

if ( ! function_exists( 'opus_blog_plugin_action_url' ) ) :
	/**
	 * Build the install or activate URL for one recommended plugin.
	 *
	 * Both are core screens, nonced the way core nonces them.
	 *
	 * @param array $plugin One entry from opus_blog_recommended_plugins().
	 * @return string Admin URL, or an empty string if the user cannot act on it.
	 */
	function opus_blog_plugin_action_url( $plugin ) {

		if ( $plugin['installed'] ) {
			if ( ! current_user_can( 'activate_plugins' ) ) {
				return '';
			}

			return wp_nonce_url(
				self_admin_url( 'plugins.php?action=activate&plugin=' . rawurlencode( $plugin['file'] ) ),
				'activate-plugin_' . $plugin['file']
			);
		}

		if ( ! current_user_can( 'install_plugins' ) ) {
			return '';
		}

		return wp_nonce_url(
			self_admin_url( 'update.php?action=install-plugin&plugin=' . rawurlencode( $plugin['slug'] ) ),
			'install-plugin_' . $plugin['slug']
		);
	}
endif;

if ( ! function_exists( 'opus_blog_plugin_notice' ) ) :
	/**
	 * Show the recommendation until it is dismissed or everything is active.
	 */
	function opus_blog_plugin_notice() {

		if ( ! current_user_can( 'install_plugins' ) ) {
			return;
		}

		if ( get_user_meta( get_current_user_id(), 'opus_blog_plugin_notice_dismissed', true ) ) {
			return;
		}

		$missing = opus_blog_missing_recommended_plugins();

		if ( empty( $missing ) ) {
			return;
		}

		// Use the active theme's own name, so child themes read correctly too.
		$theme_name = wp_get_theme()->get( 'Name' );

		$names = array();
		foreach ( $missing as $plugin ) {
			$names[] = '<em>' . esc_html( $plugin['name'] ) . '</em>';
		}
		?>
		<div class="notice notice-info is-dismissible opus-blog-plugin-notice">
			<p>
				<?php
				printf(
					/* translators: 1: theme name, 2: comma separated list of plugin names. */
					esc_html__( '%1$s recommends these free plugins: %2$s.', 'opus-blog' ),
					esc_html( $theme_name ),
					wp_kses( implode( ', ', $names ), array( 'em' => array() ) )
				);
				?>
				<?php esc_html_e( 'None of them are required.', 'opus-blog' ); ?>
			</p>
			<p>
				<?php
				foreach ( $missing as $plugin ) {
					$url = opus_blog_plugin_action_url( $plugin );

					if ( '' === $url ) {
						continue;
					}

					printf(
						'<a href="%1$s" class="button button-secondary">%2$s</a> ',
						esc_url( $url ),
						$plugin['installed']
							/* translators: %s: plugin name. */
							? esc_html( sprintf( __( 'Activate %s', 'opus-blog' ), $plugin['name'] ) )
							/* translators: %s: plugin name. */
							: esc_html( sprintf( __( 'Install %s', 'opus-blog' ), $plugin['name'] ) )
					);
				}
				?>
			</p>
			<p>
			<a href="<?php echo esc_url( admin_url( 'themes.php?page=opus-blog-theme' ) ); ?>"><?php esc_html_e( 'Theme options', 'opus-blog' ); ?></a>
			<span aria-hidden="true">|</span>				<a href="<?php echo esc_url( opus_blog_plugin_notice_dismiss_url() ); ?>"><?php esc_html_e( 'Dismiss this notice', 'opus-blog' ); ?></a>
			</p>
		</div>
		<?php
	}
endif;
add_action( 'admin_notices', 'opus_blog_plugin_notice' );

if ( ! function_exists( 'opus_blog_plugin_notice_dismiss_url' ) ) :
	/**
	 * Nonced URL that dismisses the notice for the current user.
	 *
	 * @return string
	 */
	function opus_blog_plugin_notice_dismiss_url() {

		return wp_nonce_url(
			add_query_arg( 'opus_blog_dismiss_plugins', '1' ),
			'opus_blog_dismiss_plugins',
			'opus_blog_plugins_nonce'
		);
	}
endif;

if ( ! function_exists( 'opus_blog_plugin_notice_dismiss' ) ) :
	/**
	 * Store the dismissal against the user.
	 */
	function opus_blog_plugin_notice_dismiss() {

		if ( ! isset( $_GET['opus_blog_dismiss_plugins'] ) ) {
			return;
		}

		if ( ! isset( $_GET['opus_blog_plugins_nonce'] )
			|| ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['opus_blog_plugins_nonce'] ) ), 'opus_blog_dismiss_plugins' )
		) {
			return;
		}

		if ( ! current_user_can( 'install_plugins' ) ) {
			return;
		}

		update_user_meta( get_current_user_id(), 'opus_blog_plugin_notice_dismissed', 1 );
	}
endif;
add_action( 'admin_init', 'opus_blog_plugin_notice_dismiss' );
