<?php
/*
 * Plugin Name:			Envision Page Builder
 * Plugin URI:			https://pluginenvision.com/plugins/envision-page-builder
 * Description:			Craft Your Dream Website with Envision Page Builder
 * Version:				0.22
 * Requires at least:	6.5
 * Requires PHP:		7.2
 * Author:				Plugin Envision
 * Author URI:			https://pluginenvision.com
 * License:				GPLv3 or later
 * License URI:			https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:			envision-page-builder
 * Domain Path:			/languages
  */

if ( !defined( 'ABSPATH' ) ) { exit; }

if( function_exists( 'evpb_fs' ) ) {
	evpb_fs()->set_basename( false, __FILE__ );
}else {
	define( 'EVPB_VERSION', isset( $_SERVER['HTTP_HOST'] ) && 'localhost' === $_SERVER['HTTP_HOST'] ? time() : '0.22' );
	define( 'EVPB_DIR_URL', plugin_dir_url( __FILE__ ) );
	define( 'EVPB_BUILD_URL', EVPB_DIR_URL . 'build/' );
	define( 'EVPB_DIR_PATH', plugin_dir_path( __FILE__ ) );
	define( 'EVPB_HAS_FS', file_exists( EVPB_DIR_PATH . 'vendor/freemius/start.php' ) );

	if( EVPB_HAS_FS ) {
		require_once EVPB_DIR_PATH . 'includes/fs-config.php';
	}else{
		require_once EVPB_DIR_PATH . 'includes/fs-lite-config.php';
	}

	function evpbWusul() {
		if( EVPB_HAS_FS ) {
			return evpb_fs()->can_use_premium_code();
		}else {
			return false;
		}
	}

	require_once 'includes/Admin/Submenu.php';
	require_once 'includes/Tools/Helper.php';
	require_once 'includes/Tools/ToCSS.php';
	require_once 'includes/Tools/AdvancedCSS.php';
	require_once 'includes/DesignLibrary/DesignLibrary.php';

	if( !class_exists( 'EVPBPlugin' ) ) {
		class EVPBPlugin {
			private $blocks = [ 'button', 'cell', 'container', 'icon-box', 'row-columns', 'text' ];

			function __construct() {
				add_filter( 'block_categories_all', [ $this, 'blockCategories' ] );
				add_action( 'init', [ $this, 'onInit' ] );
				add_action( 'wp_enqueue_scripts', [ $this, 'enqueueScripts' ] );
				add_action( 'enqueue_block_assets', [ $this, 'enqueueBlockAssets' ] );
				add_action( 'enqueue_block_editor_assets', [ $this, 'enqueueBlockEditorAssets' ] );
			}

			function blockCategories( $categories ) {
				return array_merge( [ [
					'slug' => 'evb-blocks',
					'title' => 'Envision Page Builder'
				] ], $categories );
			}

			function onInit() {
				foreach( $this->blocks as $block ) {
					register_block_type( __DIR__ . "/build/$block/block.json" );
				}
			}

			function enqueueScripts() {
				wp_enqueue_script( 'evb-script', EVPB_BUILD_URL . 'script.js', [], EVPB_VERSION, true );
				wp_set_script_translations( 'evb-script', 'envision-page-builder', EVPB_DIR_PATH . 'languages' );
			}
			function enqueueBlockAssets() {
				wp_register_style( 'aos', EVPB_DIR_URL . 'public/css/aos.css', [], '2.3.1' );
				wp_register_script( 'aos', EVPB_DIR_URL . 'public/js/aos.js', [], '2.3.1', false );
				wp_set_script_translations( 'aos', 'envision-page-builder', EVPB_DIR_PATH . 'languages' );

				wp_enqueue_style( 'evb-style', EVPB_BUILD_URL . 'script.css', [], EVPB_VERSION );
			}
			function enqueueBlockEditorAssets() {
				$indexAssets = require EVPB_DIR_PATH . 'build/index.asset.php';
				$indexDependencies = array_merge( [ 'aos' ], $indexAssets['dependencies'] );

				wp_enqueue_script( 'evb-index-script', EVPB_BUILD_URL . 'index.js', $indexDependencies, EVPB_VERSION, true ); // All
				wp_add_inline_script( 'evb-index-script', "const evpbNonce = '" . wp_create_nonce( 'evpb_ajax' ) . "';const evpbWusul = '" . evpbWusul() . "';", 'before' );

				wp_set_script_translations( 'evb-index-script', 'envision-page-builder', EVPB_DIR_PATH . 'languages' );

				wp_enqueue_style( 'evb-index-style', EVPB_BUILD_URL . 'index.css', [ 'aos' ], EVPB_VERSION );

				// Deregister front scripts in the editor
				foreach ( $this->blocks as $block ) {
					wp_deregister_script( "evb-$block-editor-script" );
					wp_deregister_script( "evb-$block-script" );
				}
			}
		}
		new EVPBPlugin();
	}
}