<?php

/**
 * Fired during plugin activation
 *
 * @link       https://flothemes.com/
 * @since      1.0.0
 *
 * @package    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/includes
 * @author     Flothemes <alexb@flothemes.com>
 */
class Flo_Flex_Builder_Activator {
	
	
	/**
	 * Check that all plugin requirements are met
	 *
	 * @since  0.1.0
	 * @return boolean | WP_Error
	 */
	public static function 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', "Your current theme <b>(" . $theme_name . ")</b> is not compatible with the Flex Block Plugin." );
		}

		// We have met all requirements
		return true;

	}

	public static function activate() {
		$requirements = Flo_Flex_Builder_Activator::meets_requirements();
		if ( is_wp_error($requirements) ) {
			die( $requirements->get_error_message() );
		}
	}

}
