<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://flothemes.com/
 * @since             1.0.0
 * @package           Flo_Flex_Builder
 *
 * @wordpress-plugin
 * Plugin Name:       Flex Block
 * Plugin URI:        https://flothemes.com/flo-flex-builder/
 * Description:       Drag and Drop builder for FloThemes
 * Version:           3.8.0
 * Author:            Flothemes
 * Author URI:        https://flothemes.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       flo-flex-builder
 * Domain Path:       /languages
 */

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

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
$allowed_themes = ["mono", "june", "faro", "elise", "lyra", "velvet", "verso", "cotton", "nessa", "nomade", "zephyr", "ember"];

define( 'Flo_Flex_Builder_Version', '3.8.0' );
define('FLEX_ALLOWED_THEMES', $allowed_themes);
/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-flo-flex-builder-activator.php
 */
function activate_Flo_Flex_Builder() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-flo-flex-builder-activator.php';
	Flo_Flex_Builder_Activator::activate();
}

/**
 * The code that runs during plugin update.
 * This action is documented in includes/class-flo-flex-builder-update.php
 * @param $upgrader_object Array
 * @param $options Array
 */

function flo_theme_upgrade_completed( $upgrader_object, $options ) {
	$allowed_themes = defined('FLEX_ALLOWED_THEMES') ? FLEX_ALLOWED_THEMES : [];

    if( $options['action'] == 'update' && $options['type'] == 'theme' && isset( $options['themes'] )) {
        require_once plugin_dir_path( __FILE__ ) . 'includes/class-flo-flex-builder-update.php';
        foreach( $options['themes'] as $theme ) {
            if(in_array($theme, $allowed_themes)) {
                Flo_Flex_Builder_Update::handle_theme_update();
            }
        }
    }
}
add_action( 'upgrader_process_complete', 'flo_theme_upgrade_completed', 10, 2 );

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-flo-flex-builder-deactivator.php
 */
function deactivate_Flo_Flex_Builder() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-flo-flex-builder-deactivator.php';
	Flo_Flex_Builder_Deactivator::deactivate();
}


// // handle the plugin updates
// if ( ! class_exists( 'Flo_Flex_Builder_License_Menu' ) ) {
// 	require_once( plugin_dir_path( __FILE__ ) . 'api-manager/am-license-menu.php' );
//
// 	/**
// 	 * @param string $file             Must be __FILE__ from the root plugin file, or theme functions file.
// 	 * @param string $software_title   Must be exactly the same as the Software Title in the product.
// 	 * @param string $software_version This product's current software version.
// 	 * @param string $plugin_or_theme  'plugin' or 'theme'
// 	 * @param string $api_url          The URL to the site that is running the API Manager. Example: https://www.toddlahman.com/
// 	 *
// 	 * @return \AM_License_Submenu|null
// 	 */
// 	//Flo_Flex_Builder_License_Menu::instance( __FILE__, 'FloPack1', Flo_Flex_Builder_Version, 'plugin', 'http://localhost/flocom' );
// 	//Flo_Flex_Builder_License_Menu::instance( __FILE__, 'FloBakery', Flo_Flex_Builder_Version, 'plugin', 'https://flothemes.staging.wpengine.com/' ); // staging enviroment
// 	Flo_Flex_Builder_License_Menu::instance( __FILE__, 'FloPack1', Flo_Flex_Builder_Version, 'plugin', 'https://flothemes.com/' );  // production enviroment
// }

register_activation_hook( __FILE__, 'activate_Flo_Flex_Builder' );
register_deactivation_hook( __FILE__, 'deactivate_Flo_Flex_Builder' );

/**
 * 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-flo-flex-builder.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_Flo_Flex_Builder() {

	$plugin = new Flo_Flex_Builder();
	$plugin->run();

}
run_Flo_Flex_Builder();

add_action("admin_init", "include_flo_flex_builder_field");

if(!function_exists("include_flo_flex_builder_field")) {
	function include_flo_flex_builder_field() {
		include_once plugin_dir_path( __FILE__ ) . 'admin/partials/acf-flo-flex-builder/acf-flo-flex-builder.php';
	}
}

if(!function_exists("flex_meets_requirements")) {
	function flex_meets_requirements() {
		if (version_compare(phpversion(), '5.4.0', '<')) {
			return new WP_Error( 'broke', "Your current PHP version on the server is " . phpversion() . ". The minimum PHP version this plugin can work on is PHP 5.4.0. Please contact your hosting along this issue, or change the version in your cPanel." );
		}

		$not_allowed_flothemes = [
			'Blanco',
			'Como',
			'Cozy',
			'Crowd',
			'Fiji',
			'Granite',
			'Lovely',
			'Matte',
			'Melbourne',
			'Mimal',
			'Monobooth',
			'Monte',
			'Pilot',
			'Porto',
			'Rebecca',
			'Rosemary',
			'Splittone',
			'Sydney',
			'Vivi'
		];
		
		// Check Theme
		$user_theme = wp_get_theme();
		
		if ( strlen($user_theme->get('Template')) ) { // child theme
			$style_parent_theme = wp_get_theme(get_template());
			$theme_name = $style_parent_theme->get( 'Name' );
			$author = $style_parent_theme->get( 'Author' );
		} else {
			$theme_name = $user_theme->get( 'Name' );
			$author = $user_theme->get('Author');
		}
		
		if (($author != 'Flothemes' && $author != 'Noticeable') || in_array($theme_name, $not_allowed_flothemes)) {
			return new WP_Error( 'incompatible', "<p>Your current theme <b>(" . $theme_name . ")</b> is not compatible with the Flex Block Plugin.</p>" );
		}

		// We have met all requirements
		return true;
	}
}

// disable flex if current theme is not compatible with interface
add_action("admin_init", "flex_check_if_compatible", 1);
function flex_check_if_compatible() {
	$req = flex_meets_requirements();
	if(is_wp_error($req)) {
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		deactivate_plugins(plugin_basename(__FILE__));
		header("Refresh:0");
		die($req->get_error_message());
	}
}

if(!function_exists('flex_get_theme')) {
	function flex_get_theme() {
		$active = wp_get_theme();
		$name = strtolower(str_replace(' Child', '', $active->Name));
		return $name;
	}
}

/**
 * Fallback for ACF get_field option, in case it's unninstalled
 */
if ( ! function_exists('flex_get_field') ) {
	function flex_get_field($op, $source = 'options') {
			if ( function_exists('get_field') ) {
				return get_field($op, $source);
			}
			return false;
	}
}



/**
 * debug
 */

if ( ! function_exists('deb') ) {

	function deb( $data ){
			print '<pre style="margin:10px; max-width: 90vw; border:1px dashed #999999; padding:10px; color:#333; background:#ffffff;">';
			$bt = debug_backtrace();
			$caller = array_shift($bt);

			print "--------------------------------------------------------------\n";
			print_r( $data );
			print "</pre>";
	}

}

if ( ! function_exists('debr') ) {

	function debr( $data ){
			print '<pre style="margin:10px; max-width: 70vw; margin-right: 165px; border:1px dashed #999999; padding:10px; color:#333; background:#ffffff; float: right; left: 234px; position: relative;">';
			$bt = debug_backtrace();
			$caller = array_shift($bt);

			print "--------------------------------------------------------------\n";
			print_r( $data );
			print "</pre>";
	}

}
