<?php
/**
 * Module to highlight the global content plugin inside the SUITE backend
 * 
 * @since 0.8.6.5
 */
if( ! defined( 'ABSPATH' ) ) exit;


new global_content_extension();
class global_content_extension {

	/**
	 * Constructor
	 */
	public function __construct() {

		if ( !is_admin() || !current_user_can( "manage_options" ) ) return false;

		add_filter( "greyd_required_plugins", array($this, "add_to_greyd_plugins") );
		add_filter( "greyd_dashboard_panels", array($this, "add_dashboard_panel"), 10, 2 );

		add_action( "admin_notices", array($this, "display_notice"), 99 );
		add_action( "admin_notices", array($this, "ignore_notice"), 1 );
	}

	public function add_to_greyd_plugins( $plugins ) {

        if ( $this->available() ) {
            $plugins[] = array(
                'name'              => 'GREYD Global Content',
                'slug'              => 'greyd_globalcontent',
                'required'          => false, // this plugin is recommended
                'source'            => 'https://update.greyd.io/review/plugins/greyd_globalcontent/greyd_globalcontent.zip',
                'version'           => '1.8.2',
                'force_activation'  => false,
                'force_deactivation'=> true,
            );
        }

		return $plugins;
	}

	public function add_dashboard_panel($panels, $config) {

		$panel = array(
			'icon'   => 'globalcontent',
			'title'  => __('Global Content', 'core'),
			'descr'  => __('Global Content ermöglicht es dir Posts, Templates oder ganze Seiten mit mehreren Websites deiner Multisite zu verknüpfen.', 'core'),
			'btn'   => array(
				array( 'text' => __('Abo Lizenz kaufen', 'core'), 'url' => __($config->pricing, 'greyd_hub'), 'target' => '_blank', 'icon' => 'external' )
			),
			'cap'  => "manage_options",
			'state' => array(
				"style" => "info",
				"text" => __('benötigt Abo Lizenz', 'core'),
			),
		);

		if ( $this->available() ) {
			$panel["state"] = array(
				"style" => "green",
				"text" => __("Abo Lizenz", 'core'),
			);

			// plugin is active
			if ( is_plugin_active( "greyd_globalcontent/init.php" ) ) {
				$panel["state"]["text"] = __("Plugin aktiv", 'core');

				// ...but needs update
				if ( method_exists("Greyd\Global_Contents\manage", "hub_up_to_date") && Greyd\Global_Contents\manage::hub_up_to_date() === false ) {
					$panel["state"] = array(
						"style" => "warning",
						"text" => __("Update benötigt", "core"),
					);
					$panel["btn"] = array(array(
						"text" => __("Plugin aktualisieren", "core"),
						"url" => admin_url('plugins.php'),
					));
				}
				// and everything is up to date
				else {
					$panel["btn"] = array(array(
						"text" => __("zur Übersicht", "core"),
						"url" => admin_url('admin.php?page=global_contents'),
					));
				}
			}
			// plugin is not active
			else {
				$panel["btn"] = array(array(
					"text" => __("Plugin aktivieren", "core"),
					"url" => admin_url('admin.php?page=install-required-plugins'),
				));
			}
		}

		$panels = array_merge(
			array_slice($panels, 0, 4),
			array(
				"globalcontent" => $panel
			),
			array_slice($panels, 4)
		);

		return $panels;
	}

	public function display_notice() {

		// not available AND already active
		if ( !$this->available() && is_plugin_active( "greyd_globalcontent/init.php" ) ) {
			echo "<div class='error notice'>
				<p>".sprintf(
					__("Du benötigst eine aktive Abo-Lizenz der GREYD.SUITE, um das Plugin %sGlobal Content%s auf dieser Seite nutzen zu können.", 'core'),
					"<strong>",
					"</strong>"
				)."</p>
			</div>";
		}

		// not available or already active
		if ( !$this->available() || is_plugin_active( "greyd_globalcontent/init.php" ) ) return;

		// don't display on 'GREYD.SUITE > Plugins' page
		if ( isset($_GET["page"]) && $_GET["page"] === "install-required-plugins" ) return;

		// user dismissed notice
		global $current_user;
		$user_id = $current_user->ID;
		if ( get_user_meta( $user_id, "greyd_gc_available_ignore", true ) ) return;

		echo "<div class='updated notice is-dismissible' style='border-left-color: #9b51e0;'>
			<div class='flex' style='padding: 12px 0; flex-direction: row; justify-content: flex-start; align-items: center;'>
				<img style='width: 46px; display: inline-block; margin: 0 20px 0 10px;' src='".get_template_directory_uri()."/assets/icon/greyd-logo-dark.svg'/>
				<div>
					<p>".sprintf(
						__("Vielen Dank, dass du dich für eine Abo-Lizenz der GREYD.SUITE entschieden hast! Möchtest du jetzt das Plugin %sGlobal Content%s nutzen?", 'core'),
						"<strong>",
						"</strong>"
					)."</p>
					<p>
						<a class='' href=".admin_url('admin.php?page=install-required-plugins').">".__("Plugin aktivieren", 'core')."</a>
						&nbsp;|&nbsp;
						<a style='color: inherit;' href=".add_query_arg( "gc_ignore", "true" ).">".__("diese Meldung verbergen", 'core')."</a>
					</p>
				</div>
			</div>
		</div>";
	}

	public function ignore_notice() {
		if ( isset($_GET["gc_ignore"]) && $_GET["gc_ignore"] === "true" ) {
			global $current_user;
			$user_id = $current_user->ID;
			add_user_meta($user_id, "greyd_gc_available_ignore", "true", true);
		}
	}

	public function available() {
		$option = get_option(base64_decode("Z3RwbA=="));
		return (
			!empty($option)
			&& is_array($option)
			&& isset($option[base64_decode("c3RhdHVz")])
			&& $option[base64_decode("c3RhdHVz")] === base64_decode("aXNfdmFsaWQ=")
			&& isset($option[base64_decode("cG9saWN5")])
			&& in_array( $option[base64_decode("cG9saWN5")], array(base64_decode("QWdlbmN5"), base64_decode("RnJlZWxhbmNlcg=="), base64_decode("Q29ycG9yYXRl")) )
		);
	}
}