<?php
	/*
	Plugin Name: PixeBuilder
	Plugin URI: https://pixebuilder.com
	Description: Advanced page builder
	Version: 1.0.5
	Author: PixeBuilder
	Author URI: https://pixebuilder.com/about-us/
	License: GPLv3
	License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
	*/

	// prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

	// define necessary variable for the site.
	define('PIXE_VERSION', '1.0.5');
	define('PIXE_URL', plugins_url('', __FILE__));
	define('PIXE_LOCAL_URL', dirname(__FILE__));
	define('PIXE_PRICING_URL', 'https://pixebuilder.com/pricing');
	define('PIXE_PREBUILT_TEMPLATE_DIR', 'https://templates.pixebuilder.com/wp-content/uploads');

	// include files
	include_once(PIXE_LOCAL_URL . '/admin/data.php');
	include_once(PIXE_LOCAL_URL . '/admin/data-template.php');
	include_once(PIXE_LOCAL_URL . '/admin/data-block.php');
	include_once(PIXE_LOCAL_URL . '/admin/data-preset.php');
	include_once(PIXE_LOCAL_URL . '/admin/settings-init.php');

	include_once(PIXE_LOCAL_URL . '/admin/utility.php');
	include_once(PIXE_LOCAL_URL . '/admin/media.php');
	include_once(PIXE_LOCAL_URL . '/admin/widget.php');
	include_once(PIXE_LOCAL_URL . '/admin/request.php');
	include_once(PIXE_LOCAL_URL . '/admin/dependency.php');
	include_once(PIXE_LOCAL_URL . '/admin/dependency-script.php');
	include_once(PIXE_LOCAL_URL . '/admin/dependency-style.php');
	include_once(PIXE_LOCAL_URL . '/admin/popup.php');
	include_once(PIXE_LOCAL_URL . '/admin/display.php');
	include_once(PIXE_LOCAL_URL . '/admin/heartbeat.php');
	
	include_once(PIXE_LOCAL_URL . '/admin/switch-editor.php');
	include_once(PIXE_LOCAL_URL . '/admin/builder.php');
	include_once(PIXE_LOCAL_URL . '/admin/editor.php');
	include_once(PIXE_LOCAL_URL . '/admin/template.php');
	include_once(PIXE_LOCAL_URL . '/admin/template-woocommerce.php');
	include_once(PIXE_LOCAL_URL . '/include/include-script.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/utility.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/collection.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/wrapper.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/column.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/item.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/preset.php');
	include_once(PIXE_LOCAL_URL . '/admin/elements/shape-divider.php');
	
	// admin settings
	if( is_admin() ){
		include_once(PIXE_LOCAL_URL . '/admin/settings/settings.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/main.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/sites.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/template.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/general.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/integration.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/tools.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/license.php');
		include_once(PIXE_LOCAL_URL . '/admin/settings/getting-started.php');
	}
	
	// plugin
	include_once(PIXE_LOCAL_URL . '/admin/plugins/woocommerce.php');
	include_once(PIXE_LOCAL_URL . '/admin/plugins/wpml.php');
	include_once(PIXE_LOCAL_URL . '/admin/plugins/seo.php');

	function pixe_autoload_directory($dir){
		$files = glob($dir . '/*.php');

		foreach( $files as $file ){
			if( file_exists($file) ){
				include_once($file);
			}
		}
	}

	// include elements
	pixe_autoload_directory(PIXE_LOCAL_URL . '/include/element');
	pixe_autoload_directory(PIXE_LOCAL_URL . '/include/element/composite');
	pixe_autoload_directory(PIXE_LOCAL_URL . '/include/element/query');

	add_action('plugins_loaded', 'pixe_load_plugin_elements');
	function pixe_load_plugin_elements(){
		if( class_exists( 'WooCommerce' ) ){
			pixe_autoload_directory(PIXE_LOCAL_URL . '/include/element/woocommerce-item');
			pixe_autoload_directory(PIXE_LOCAL_URL . '/include/element/woocommerce-single');
		}
	}

	// on plugin acitvation
	register_activation_hook(__FILE__, 'pixe_plugin_activation');
	function pixe_plugin_activation(){
		pixe_builder_table_init();
		pixe_template_table_init();
		pixe_block_table_init();
		pixe_settings_init();
		pixe_init_data();

		set_transient('pixe_plugin_activated', true, 30);
	}
	function pixe_getting_started_redirect(){
		if( get_transient('pixe_plugin_activated') ){
			delete_transient('pixe_plugin_activated');

			global $pagenow;
			if( $pagenow == 'admin.php' && isset($_GET['page']) && $_GET['page'] == 'pixe_plugin_settings_sites' ){ // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Nonce not required
				return;
			}

			if( isset($_GET['activate-multi']) ){ // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Nonce not required
				return;
			}

			wp_safe_redirect(admin_url('admin.php?page=pixe_plugin_settings_getting_started'));
			exit;
		}
	}
	add_action('admin_init', 'pixe_getting_started_redirect');
	
	// option scripts ( both builder and editor)
	add_action('pixe_enqueue_utility_scripts', 'pixe_enqueue_utility_scripts');
	function pixe_enqueue_utility_scripts(){
		wp_enqueue_script('pixe-ss-option-js', PIXE_URL . '/include/js/ss-option.js', array('jquery'), PIXE_VERSION, true);
		wp_localize_script('pixe-ss-option-js', 'pixeContactMessage', array(
			'success' => esc_html__('Success! Your message was sent. We\'ll be in touch soon.', 'pixebuilder'),
			'wrong' => esc_html__('The format of this field is incorrect.', 'pixebuilder'),
			'blank' => esc_html__('This field cannot be left blank.', 'pixebuilder'),
			'acceptance' => esc_html__('Please accept the terms and conditions.', 'pixebuilder'),
			'error' => esc_html__('Unable to send your message due to a server issue. Please try again later.', 'pixebuilder'),
		));
	}

	// builder settings
	add_action('pixe_enqueue_builder_scripts', 'pixe_enqueue_builder_settings');
	function pixe_enqueue_builder_settings($hook){
		wp_enqueue_script('pixe-element-settings-js', PIXE_URL . '/include/js/settings.js', array('jquery'), PIXE_VERSION, true);
	}

	// editor scripts
	add_action('pixe_enqueue_editor_scripts', 'pixe_enqueue_editor_scripts');
	function pixe_enqueue_editor_scripts(){
		wp_enqueue_script('pixe-element-js', PIXE_URL . '/include/js/element.js', array('jquery'), PIXE_VERSION, true);
		wp_enqueue_script('pixe-element-tab-js', PIXE_URL . '/include/js/tab.js', array('jquery'), PIXE_VERSION, true);
		wp_enqueue_script('pixe-element-media-js', PIXE_URL . '/include/js/media.js', array('jquery'), PIXE_VERSION, true);
		wp_enqueue_script('pixe-element-query-js', PIXE_URL . '/include/js/query.js', array('jquery'), PIXE_VERSION, true);

		// for script translation
		wp_set_script_translations('pixe-element-js', 'pixebuilder', PIXE_LOCAL_URL . '/languages');
	}

	// body class
	add_filter('body_class', 'pixe_body_class');
	function pixe_body_class( $classes = array() ){
		$classes[] = 'pixe-body';

		if( !is_admin() && !Pixe_Editor::is_editor() ){
			$classes[] = 'pixe-front';
		}
		return $classes;
	}

	// theme support
	function pixe_add_theme_support(){
		add_theme_support('post-thumbnails', ['post', 'page']);
	}
	add_action('after_setup_theme', 'pixe_add_theme_support');

	// allow specific upload file format
	add_filter('upload_mimes', 'pixe_custom_upload_mimes', 99);
    function pixe_custom_upload_mimes( $mimes = array() ){
        $general_settings = get_option('pixe_general', array());

        if( !empty($general_settings['upload-file-type']) ){
            foreach($general_settings['upload-file-type'] as $type){
                switch($type){
                    case 'fonts':
                        $mimes['woff'] = 'font/woff';
                        $mimes['woff2'] = 'font/woff2';
                        break;
                    
                    case 'json':
                        $mimes['json'] = 'application/json';
                        break;

                    case 'svg':
                        $mimes['svg'] = 'image/svg+xml';
                        break;
                }
            }
        }

        return $mimes;
    }
	
	// link to settings page
	function pixe_add_plugin_action_links($links){
		$settings_url = admin_url('admin.php?page=pixe_plugin_settings_main');
		$docs_url     = 'https://docs.pixebuilder.com/';

		array_unshift($links,
			'<a href="' . esc_url($settings_url) . '">Settings</a>',
			'<a href="' . esc_url($docs_url) . '" target="_blank">Document</a>'
		);

		return $links;
	}
	add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pixe_add_plugin_action_links');
