<?php
/**
 *
 * @link              https://organicthemes.com
 * @since             1.0.0
 * @package           Organic_Widgets_Pro
 *
 * @wordpress-plugin
 * Plugin Name:       Organic Builder Widgets Pro
 * Plugin URI:        https://organicthemes.com/organic-customizer-widgets
 * Description:       The premium Organic Builder Widgets plugin that transforms the core WordPress Customizer into a page builder using custom widgets.
 * Version:           2.3.5.3
 * Author:            Organic Themes
 * Author URI:        https://organicthemes.com
 * License:           Copyright Invulu, Inc. All Rights Reserved.
 * Text Domain:       organic-widgets
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Current Version (Keep in sync with Version # above).
define( 'ORGANIC_WIDGETS_PRO_CURRENT_VERSION', '2.3.5.3' );
define( 'ORGANIC_WIDGETS_PRO_OT_STORE_URL', 'https://organicthemes.com' ); // The URL of our store.
define( 'ORGANIC_WIDGETS_PRO_PRODUCT_ID', 348835 ); // The EDD product ID.
define( 'ORGANIC_WIDGETS_PRO_LICENSE_PAGE', 'organic-widgets-pro-license' ); // The name of the settings page for the license input.

if ( ! class_exists( 'Organic_Widgets_Pro_Plugin_Updater' ) ) {
	// Load our custom updater class.
	include dirname( __FILE__ ) . '/includes/updater/organic-widgets-updater-class.php';
	// Load our custom updater admin.
	include dirname( __FILE__ ) . '/includes/updater/organic-widgets-updater-admin.php';
}

/**
 * The code that compares version numbers for updates.
 */
function organic_widgets_pro_plugin_updater() {

	// Retrieve our license key from the DB.
	$license_key = trim( get_option( 'organic_widgets_pro_license_key' ) );

	// Setup the updater.
	$edd_updater = new Organic_Widgets_Pro_Plugin_Updater(
		ORGANIC_WIDGETS_PRO_OT_STORE_URL,
		__FILE__,
		array(
			'version' => ORGANIC_WIDGETS_PRO_CURRENT_VERSION, // Current version number, i.e. '1.0'.
			'license' => $license_key, // License key (used get_option above to retrieve from DB).
			'item_id' => ORGANIC_WIDGETS_PRO_PRODUCT_ID, // ID of the product.
			'author'  => 'Organic Themes', // Author of this plugin.
			'beta'    => false,
		)
	);

}
add_action( 'admin_init', 'organic_widgets_pro_plugin_updater', 0 );

/* ------------------------------------ Let's Begin ------------------------------------ */

register_activation_hook( __FILE__, 'activate_organic_widgets_pro' );
register_deactivation_hook( __FILE__, 'deactivate_organic_widgets_pro' );
add_action( 'admin_init', 'organic_widgets_pro_welcome_redirect' );

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-organic-widgets-activator.php
 */
function activate_organic_widgets_pro() {

	global $wp_version;
	$wp  = '4.8';
	$php = '5.3.29';

	$license = get_option( 'organic_widgets_pro_license_key' );
	$status  = get_option( 'organic_widgets_pro_license_status' );

	// Compare PHP and WP versions and make sure the plugin can run.
	if ( version_compare( PHP_VERSION, $php, '<' ) ) {
		$flag = 'PHP';
	} elseif ( version_compare( $wp_version, $wp, '<' ) ) {
		$flag = 'WordPress';
	} else {
		// Activate.
		if ( is_plugin_active( 'organic-customizer-widgets/organic-widgets.php' ) ) {
			add_action( 'update_option_active_plugins', 'deactivate_organic_widgets_lite_version' );
		}
		add_option( 'organic_widgets_pro_install_date', date( 'Y-m-d h:i:s' ) );
		add_option( 'organic_widgets_pro_activation_redirect', true );
		require_once plugin_dir_path( __FILE__ ) . 'includes/class-organic-widgets-activator.php';
		Organic_Widgets_Pro_Activator::activate();
		return;
	}

	// Notify User that versions are too old, and deactivate plugin.
	$version = 'PHP' == $flag ? $php : $wp;
	deactivate_plugins( basename( __FILE__ ) );
	wp_die(
		'<p>The <strong>Organic Builder Widgets</strong> plugin requires ' . $flag . '  version ' . $version . ' or greater.</p>',
		'Plugin Activation Error',
		array(
			'response'  => 200,
			'back_link' => true,
		)
	);

}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-organic-widgets-deactivator.php
 */
function deactivate_organic_widgets_pro() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-organic-widgets-deactivator.php';
	Organic_Widgets_Pro_Deactivator::deactivate();
}

/**
 * This function deactivates the free plugin version upon activation.
 */
function deactivate_organic_widgets_lite_version() {
	deactivate_plugins( 'organic-customizer-widgets/organic-widgets.php' );
}

/**
 * This function redirects users to the welcome page upon activation.
 */
function organic_widgets_pro_welcome_redirect() {
	if ( get_option( 'organic_widgets_pro_activation_redirect', false ) ) {
		delete_option( 'organic_widgets_pro_activation_redirect' );
		if ( ! isset( $_GET['activate-multi'] ) ) {
			wp_redirect( add_query_arg( array( 'page' => 'organic-widgets' ), admin_url( 'admin.php' ) ) );
		}
	}
}

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-organic-widgets.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since 1.0.0
 */
function run_organic_widgets_pro() {

	$plugin = new Organic_Widgets_Pro();

	// Define Constants.
	define( 'ORGANIC_WIDGETS_PRO_BASE_DIR', plugin_dir_url( __FILE__ ) );
	define( 'ORGANIC_WIDGETS_PRO_ADMIN_IMG_DIR', plugin_dir_url( __FILE__ ) . 'admin/img/' );
	define( 'ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR', plugin_dir_url( __FILE__ ) . 'admin/js/' );
	define( 'ORGANIC_WIDGETS_PRO_ADMIN_CSS_DIR', plugin_dir_url( __FILE__ ) . 'admin/css/' );

	$plugin->run();

}
run_organic_widgets_pro();

/**
 * Check if Block Editor is active.
 * Must only be used after plugins_loaded action is fired.
 *
 * @return bool
 */
function organic_widgets_pro_gutenberg_active() {
	// Gutenberg plugin is installed and activated.
	$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

	// Block editor since 5.0.
	$block_editor = version_compare( get_bloginfo( 'version' ), '5.0-beta', '>=' );

	if ( ! $gutenberg && ! $block_editor ) {
		return false;
	}

	if ( organic_widgets_pro_classic_editor_plugin_active() ) {
		$editor_option       = get_option( 'classic-editor-replace' );
		$block_editor_active = array( 'no-replace', 'block' );

		return in_array( $editor_option, $block_editor_active, true );
	}

	return true;
}

/**
 * Check if Classic Editor plugin is active.
 *
 * @return bool
 */
function organic_widgets_pro_classic_editor_plugin_active() {
	if ( ! function_exists( 'is_plugin_active' ) ) {
		include_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
		return true;
	}

	return false;
}

/**
 * Register Organic Widgets menu pages.
 *
 * @since 1.0.0
 */
function organic_widgets_pro_welcome_screen() {

	$icon_svg = 'data:image/svg+xml;base64,' . base64_encode(
		'<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="40px" height="40px" viewBox="120 10 40 40" xml:space="preserve">
		<g>
			<path fill="#a0a5aa" d="M144.128,11.221c-7.733,0-13.455,1.824-17.002,5.421c-8.137,8.252-5.41,17.112-4.38,19.634
				c0.906,2.217,2.021,3.613,2.875,4.35l2.957-2.609l-0.278-13.13l2.999,10.728l4.374-3.86l0.438-10.677l1.894,8.617l10.528-8.433
				l-8.292,10.76l8.57,1.933l-10.595,0.444l-3.776,4.422l10.614,3.049l-12.974-0.278l-2.522,2.956c0.092,0.11,0.194,0.228,0.315,0.344
				c1.9,1.938,5.897,3.889,10.54,3.889c3.257,0,8.112-0.991,12.775-5.72c8.079-8.19,4.882-25.648,3.841-30.338
				C154.816,12.222,149.721,11.221,144.128,11.221L144.128,11.221L144.128,11.221z"/>
		</g>
		</svg>'
	);

	// Add Menu Item.
	add_menu_page(
		esc_html__( 'Builder Widgets', 'organic-widgets-pro' ),
		esc_html__( 'Builder Widgets', 'organic-widgets-pro' ),
		'manage_options',
		'organic-widgets',
		'organic_widgets_pro_welcome_screen_content',
		$icon_svg,
		110
	);

	// Add Settings Page.
	add_submenu_page(
		'organic-widgets',
		'Organic Widgets Settings',
		esc_html__( 'Settings', 'organic-widgets-pro' ),
		'manage_options',
		'organic-widgets-settings',
		'organic_widgets_pro_settings_screen_content'
	);

	add_settings_section(
		'organic_widgets_pro_settings_section',
		'Organic Widgets Settings',
		'organic_widgets_pro_settings_callback',
		'organic-widgets-settings'
	);

	// Add License Page.
	add_submenu_page(
		'organic-widgets',
		'Organic Widgets License',
		esc_html__( 'License', 'organic-widgets-pro' ),
		'manage_options',
		ORGANIC_WIDGETS_PRO_LICENSE_PAGE,
		'organic_widgets_pro_license_page'
	);

	register_setting(
		'organic-widgets-settings',
		'organic_widgets_pro_settings',
		array(
			'sanitize_callback' => 'organic_widgets_pro_settings_sanitize_callback',
		)
	);

}
add_action( 'admin_menu', 'organic_widgets_pro_welcome_screen' );

function organic_widgets_pro_settings_sanitize_callback( $options ) {

	$organic_widgets = organic_widgets_pro_get_organic_widgets();

	foreach ( $organic_widgets as $key => $organic_widget ) {
		if ( ! array_key_exists( $organic_widget['settings-activate-slug'], $options ) ) {
			$options[ $organic_widget['settings-activate-slug'] ] = 0;
		}
	}

	return $options;
}

function organic_widgets_pro_settings_callback() {

	$options = get_option( 'organic_widgets_pro_settings' ) ? get_option( 'organic_widgets_pro_settings' ) : array();
	$organic_widgets = organic_widgets_pro_get_organic_widgets();
	?>

	<!-- BEGIN Active Widgets Settings -->

	<div class="organic-widgets-display-settings">

		<h3><?php _e( 'Active Widgets', 'organic-widgets-pro' ); ?></h3>

		<?php
		foreach ( $organic_widgets as $organic_widget ) {

			$slug = $organic_widget['settings-activate-slug'];
			$name = $organic_widget['settings-name'];
			if ( ! array_key_exists( $slug, $options ) ) {
				$options[ $slug ] = 1;
			}
			?>

			<div class="organic-widgets-display-toggle"><label><input type="checkbox" name="organic_widgets_pro_settings[<?php echo esc_attr( $slug ); ?>]" value="1" <?php checked( $options[ $slug ], 1, 1 ); ?> /> <?php esc_html_e( $name, 'organic-widgets-pro' ); ?></label></div>

		<?php } ?>

	</div>

	<!-- END Active Widgets Settings -->

	<?php
}

/**
 * Get All Organic Custom Widgets
 *
 * @since 1.0.0
 */
function organic_widgets_pro_get_organic_widgets() {

	$organic_widgets = array();

	foreach ( get_declared_classes() as $widget ) {
		if ( is_subclass_of( $widget, 'Organic_Widgets_Pro_Custom_Widget' ) ) {
			$settings_name              = str_replace( '_', ' ', str_replace( 'Organic_Widgets_Pro_', '', $widget ) );
			$settings_activate_slug     = $widget . '_activate';
			$organic_widgets[ $widget ] = array(
				'settings-activate-slug' => $settings_activate_slug,
				'settings-name'          => $settings_name,
			);
		}
	}

	return $organic_widgets;

}

/**
 * Include welcome screen content.
 *
 * @since    1.0.0
 */
function organic_widgets_pro_welcome_screen_content() {
	include_once plugin_dir_path( __FILE__ ) . '/admin/partials/organic-widgets-welcome-page.php';
}

/**
 * Include settings screen content.
 *
 * @since    1.0.0
 */
function organic_widgets_pro_settings_screen_content() {
	include_once plugin_dir_path( __FILE__ ) . '/admin/partials/organic-widgets-settings-page.php';
}

/**
 * Include license screen content.
 *
 * @since    1.0.0
 */
function organic_widgets_pro_license_page() {
	include_once plugin_dir_path( __FILE__ ) . '/admin/partials/organic-widgets-license-page.php';
}

/**
 * Register license option.
 *
 * @since 1.0.0
 */
function organic_widgets_pro_register_option() {
	// Creates our settings in the options table.
	register_setting( 'organic_widgets_pro_license', 'organic_widgets_pro_license_key', 'organic_widgets_pro_sanitize_license' );
}
add_action( 'admin_init', 'organic_widgets_pro_register_option' );

/**
 * Sanitize license.
 *
 * @param array $new Update old instance.
 * @since 1.0.0
 */
function organic_widgets_pro_sanitize_license( $new ) {
	$old = get_option( 'organic_widgets_pro_license_key' );
	if ( $old && $old != $new ) {
		delete_option( 'organic_widgets_pro_license_status' ); // New license has been entered, so must reactivate.
	}
	return $new;
}
