<?php
/**
 * Recommended plugins card for the Get Started admin page.
 *
 * Replaces the previously bundled plugin-activation library. Plugins listed here
 * are strictly optional: the theme never nags, never blocks and never requires them.
 *
 * @package Journy
 * @since 1.2.0
 */

/**
 * Returns the plugins recommended alongside the theme.
 *
 * @since 1.2.0
 *
 * @return array Associative array keyed by wordpress.org plugin slug. Each value
 *               holds the plugin `name`, its main plugin `file` relative to the
 *               plugins directory, and a short `description`.
 */
function journy_get_recommended_plugins() {
	return array(
		'contact-form-7' => array(
			'name'        => 'Contact Form 7',
			'file'        => 'contact-form-7/wp-contact-form-7.php',
			'description' => __( 'Powers the form in the Contact Us template.', 'journy' ),
		),
	);
}

/**
 * Builds the action markup for a single recommended plugin.
 *
 * Depending on the plugin state and the current user's capabilities this is an
 * "Active" badge, an activation link, an installation link, or a plain link to
 * the plugin page on wordpress.org.
 *
 * @since 1.2.0
 *
 * @param string $slug Plugin slug on wordpress.org.
 * @param string $file Main plugin file, relative to the plugins directory.
 * @return string Escaped HTML for the plugin action.
 */
function journy_recommended_plugin_action( $slug, $file ) {
	if ( ! function_exists( 'is_plugin_active' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	if ( is_plugin_active( $file ) ) {
		return '<span class="journy-plugin-active">' . esc_html__( 'Active', 'journy' ) . '</span>';
	}

	if ( file_exists( WP_PLUGIN_DIR . '/' . $file ) && current_user_can( 'activate_plugins' ) ) {
		$activate_url = wp_nonce_url(
			self_admin_url( 'plugins.php?action=activate&plugin=' . rawurlencode( $file ) ),
			'activate-plugin_' . $file
		);

		return '<a class="bg-color" href="' . esc_url( $activate_url ) . '">' . esc_html__( 'Activate', 'journy' ) . '</a>';
	}

	if ( current_user_can( 'install_plugins' ) ) {
		$install_url = wp_nonce_url(
			self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ),
			'install-plugin_' . $slug
		);

		return '<a class="bg-color" href="' . esc_url( $install_url ) . '">' . esc_html__( 'Install', 'journy' ) . '</a>';
	}

	$details_url = 'https://wordpress.org/plugins/' . $slug . '/';

	return '<a class="bg-color" href="' . esc_url( $details_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Learn more', 'journy' ) . '</a>';
}

/**
 * Prints the "Recommended plugins" card on the Get Started page.
 *
 * @since 1.2.0
 *
 * @return void
 */
function journy_recommended_plugins_section() {
	$plugins = journy_get_recommended_plugins();

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

	echo '<hr>';
	echo '<h4>' . esc_html__( 'Recommended plugins', 'journy' ) . '</h4>';
	echo '<p>' . esc_html__( 'These plugins are optional. Journy works fine without them, but they add features some templates are designed around.', 'journy' ) . '</p>';

	foreach ( $plugins as $slug => $plugin ) {
		echo '<h5>' . esc_html( $plugin['name'] ) . '</h5>';
		echo '<p>' . esc_html( $plugin['description'] ) . '</p>';
		echo wp_kses_post( journy_recommended_plugin_action( $slug, $plugin['file'] ) );
	}
}
