<?php
/**
 * Plugin Name:     Lightning G3 Pro Unit
 * Plugin URI:      https://www.vektor-inc.co.jp/
 * Description:
 * Author:          Vektor,Inc.
 * Author URI:      https://www.vektor-inc.co.jp/
 * Text Domain:     lightning-g3-pro-unit
 * Domain Path:     /languages
 * Version:         0.17.1
 *
 * @package         LIGHTNING_G3_PRO_UNIT
 */

// Your code starts here.

// Get Plugin version.
$version = get_file_data( __FILE__, array( 'version' => 'Version' ) );
define( 'LIGHTNING_G3_PRO_UNIT_VERSION', $version['version'] );

require_once dirname( __FILE__ ) . '/vendor/autoload.php';

/**
 * Register update license key
 *
 * @param array $query_args : updatechacker array.
 * @return $query_args
 */
function ltg3pro_wsh_filter_update_checks( $query_args ) {

	$license = esc_html( get_option( 'lightning-g3-pro-unit-license-key' ) );

	if ( ! empty( $license ) ) {
		$query_args['lightning-g3-pro-unit-license-key'] = $license;
	}

	return $query_args;
}

// Load updater.
add_action(
	'after_setup_theme',
	function() {

		// Get Plugin version.

		$current_theme = get_template();

		define( 'LTG3_PRO_DIRECTORY_PATH', dirname( __FILE__ ) );
		define( 'LTG3_PRO_DIRECTORY_URL', plugins_url( '', __FILE__ ) );
		require LTG3_PRO_DIRECTORY_PATH . '/packages.php';
		require LTG3_PRO_DIRECTORY_PATH . '/admin/admin.php';

		$data = get_file_data( __FILE__, array( 'version' => 'Version' ) );
		global $ltg3_pro_version;
		$ltg3_pro_version = $data['version'];

		load_plugin_textdomain( 'lightning-g3-pro-unit', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

		// Cope with : WP HTTP Error: cURL error 60: SSL certificate problem: certificate has expired.
		add_filter( 'https_ssl_verify', '__return_false' );

		$license = esc_html( get_option( 'lightning-g3-pro-unit-license-key' ) );
		require 'inc/plugin-update-checker/plugin-update-checker.php';
		$checker = Puc_v4_Factory::buildUpdateChecker(
			'https://vws.vektor-inc.co.jp/updates/?action=get_metadata&slug=lightning-g3-pro-unit',
			__FILE__,
			'lightning-g3-pro-unit'
		);
		// ここでライセンスキーをoptionから受け取って投げ返している
		$checker->addQueryArgFilter( 'ltg3pro_wsh_filter_update_checks' );

		if ( is_admin() && current_user_can( 'edit_theme_options' ) ) {
			$network_runnning_pro = false;

			if ( is_multisite() ) {
				$network_options = get_site_option( 'active_sitewide_plugins', array() );
				if ( isset( $network_options['lightning-original-brand-unit/lightning-original-brand-unit.php'] ) ) {
					$network_runnning_pro = true;
				}
			}

			$opt = get_option( 'active_plugins', array() );
			if ( ! $network_runnning_pro && ! in_array( 'lightning-original-brand-unit/lightning-original-brand-unit.php', $opt, true ) ) {

				$state  = $checker->getUpdateState();
				$update = $state->getUpdate();

				// ライセンスキーが未入力の場合.
				if ( empty( get_option( 'lightning-g3-pro-unit-license-key', false ) ) ) {
					add_action(
						'admin_notices',
						function() {
							echo '<div class="error"><p>';
							echo wp_kses_post( __( 'License Key has no registerd.', 'lightning-g3-pro-unit' ) );
							echo wp_kses_post( __( 'You need register License Key at Settings > Lightning G3 Pro Unit Setting > License Key.', 'lightning-g3-pro-unit' ) );
							echo '</p><p>';
							/* translators: %s: 再読み込みURL */
							echo wp_kses_post( sprintf( __( 'ライセンスキーを入力してもこの表示が消えない場合は<a href="%s">更新を再読み込み</a>してください。', 'lightning-g3-pro-unit' ), admin_url() . '/update-core.php?force-check=1' ) );
							echo '</p></div>';
						}
					);
				}

				// ライセンス認証に失敗した場合.
				if (
					! empty( $update )
					&& version_compare( $update->version, $ltg3_pro_version, '>' )
					&& empty( $update->download_url )
				) {

					add_action(
						'admin_notices',
						function() {
							echo '<div class="error"><p>';
							echo wp_kses_post( __( 'Your Lightning G3 Pro Unit license key is expired.', 'lightning-g3-pro-unit' ) );
							if ( get_locale() === 'ja' ) {
								echo wp_kses_post( __( 'Please purchase the license key from <a href="https://vws.vektor-inc.co.jp/product/lightning-g3-pro-pack" target="_blank">this page</a>.', 'lightning-g3-pro-unit' ) );
							}
							echo '</p></div>';
						}
					);
				}

				// テーマは Lightning だが世代が G3 じゃない場合.
				if ( 'g3' !== esc_html( get_option( 'lightning_theme_generation' ) ) && 'lightning' === $current_theme ) {

					add_action(
						'admin_notices',
						function() {
							echo '<div class="error"><p>';
							echo wp_kses_post( __( 'The Lightning G3 Pro Unit only works in G3 mode. To change to G3 mode, change to Generation 3 from Appearance > Customize > Lightning Feature Settings.', 'lightning-g3-pro-unit' ) );
							echo '</p></div>';
						}
					);
				}

				// そもそもテーマが Lightning でない場合.
				if ( 'lightning' !== $current_theme ) {

					add_action(
						'admin_notices',
						function() {
							echo '<div class="error"><p>';
							echo wp_kses_post( __( 'The Lightning G3 Pro Unit only works Lightning(free) theme. Plese install and activate Lightning(free) theme.', 'lightning-g3-pro-unit' ) );
							echo '</p></div>';
						}
					);
				}
			}
		}
	}
);

/**
 * Add a link to this plugin's settings page
 *
 * @param array $links : existing links.
 */
function ltg3pro_set_plugin_meta( $links ) {
	$settings_link = '<a href="options-general.php?page=lightning_g3_pro_unit_options">' . __( 'Setting', 'lightning-g3-pro-unit' ) . '</a>';
	array_unshift( $links, $settings_link );
	return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'ltg3pro_set_plugin_meta', 10, 1 );

/**
 * Plugin activation
 */
if ( is_admin() ) {
	require_once dirname( __FILE__ ) . '/inc/tgm-plugin-activation/config.php';
}
