<?php
/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @link        https://wpsmartpost.com/
 * @since      2.0.0
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/includes
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use SmartPostShow\Blocks\Helper;

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
class Smart_Post_Show_Pro {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    2.0.0
	 * @access   protected
	 * @var      Smart_Post_Show_Pro_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	public $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    2.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	public $plugin_name = 'smart-post-show-pro';

	/**
	 * The current version of the plugin.
	 *
	 * @since    2.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	public $version = SMART_POST_SHOW_PRO_VERSION;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    2.0.0
	 */
	public function __construct() {
		$this->define_constants();
		$this->load_dependencies();
		$this->define_common_hooks();
		$this->define_builder_hooks();
		$this->define_custom_font_hooks();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}
	/**
	 * Define constant if not already set
	 *
	 * @since 2.0.0
	 *
	 * @param string      $name Define constant.
	 * @param string|bool $value Define constant.
	 */
	public function define( $name, $value ) {
		if ( ! defined( $name ) ) {
			define( $name, $value );
		}
	}
	/**
	 * Define constants
	 *
	 * @since 2.0.0
	 */
	public function define_constants() {
		define( 'SP_PCP_VERSION', $this->version );
		define( 'SP_SPSP_STORE_URL', 'https://account.shapedplugin.com' );
		define( 'SP_SPSP_DEMO_URL', 'https://wpsmartpost.com' );
		define( 'SP_SPSP_ITEM_ID', 8104 );
		define( 'SP_PCP_PLUGIN_NAME', $this->plugin_name );
		define( 'SP_PCP_PATH', plugin_dir_path( __DIR__ ) );
		define( 'SP_PCP_TEMPLATE_PATH', plugin_dir_path( __DIR__ ) . 'public/template/' );
		define( 'SP_PCP_URL', plugin_dir_url( __DIR__ ) );
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Smart_Post_Show_Pro_Loader. Orchestrates the hooks of the plugin.
	 * - Smart_Post_Show_Pro_i18n. Defines internationalization functionality.
	 * - Smart_Post_Show_Pro_Admin. Defines all hooks for the admin area.
	 * - Smart_Post_Show_Pro_Public. Defines all hooks for the public side of the site.
	 * - Smart_Post_Show_Pro_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    2.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-loader.php';
		$this->loader = new Smart_Post_Show_Pro_Loader();

		/**
		 * The class responsible for defining register post type.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-post-types.php';

		/**
		 * The class responsible for defining register builder post type.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-builder-post-types.php';

		/**
		 * The class responsible for defining register custom font post type.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-custom-font-type.php';

		/**
		 * The class responsible for defining license option.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-license.php';

		/**
		 * The class responsible for defining export import options.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-import-export.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once SP_PCP_PATH . 'admin/class-smart-post-show-pro-admin.php';

		/**
		 * The class responsible for defining metabox setup that occur in the admin area.
		 */
		require_once SP_PCP_PATH . 'admin/views/sp-framework/classes/setup.class.php';

		/**
		 * The class responsible for defining metabox config that occur in the admin area.
		 */
		require_once SP_PCP_PATH . 'admin/helpers/class-smart-post-show-pro-image-resizer.php';

		/**
		 * The class responsible for defining cron job in the admin area.
		 */
		require_once SP_PCP_PATH . 'admin/helpers/class-smart-post-show-pro-cron.php';

		/**
		 * The class responsible for admin live preview.
		 */
		require_once SP_PCP_PATH . 'admin/preview/class-spsp-preview.php';

		/**
		 * The class responsible for defining replace layout.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-replace-layout.php';

		/**
		 * The class responsible for Image gallery and Video Upload meta fields.
		 */
		require_once SP_PCP_PATH . 'admin/class-image-and-video-meta-field.php';
		new Image_And_Video_Meta_Field();

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once SP_PCP_PATH . 'public/class-smart-post-show-pro-public.php';
		/**
		 * Gutenberg block.
		 */
		if ( version_compare( $GLOBALS['wp_version'], '5.3', '>=' ) ) {
			require_once SP_PCP_PATH . 'admin/class-smart-post-show-pro-gutenberg-block.php';
			new Smart_Post_Show_Pro_Gutenberg_Block();
		}
		/**
		 * Admin menu block settings page.
		 */
		if ( version_compare( $GLOBALS['wp_version'], '5.3', '>=' ) ) {
			require_once SP_PCP_PATH . 'admin/blocks-settings/class-blocks-settings-page.php';
			new Sp_Smart_Post_Block_Admin_Menu_Page();
		}
		/**
		 * Gutenberg block.
		 */
		if ( version_compare( $GLOBALS['wp_version'], '5.3', '>=' ) ) {
			require_once SP_PCP_PATH . 'blocks/class-smart-post-show-pro-blocks.php';
			Sp_Smart_Post_Blocks_Init::instance();
		}

		/**
		 * Saved Template.
		 */
		require_once SP_PCP_PATH . 'includes/class-smart-post-show-pro-saved-template.php';

		$integration_options = Helper::get_integration_options();

		$divi_integration     = $integration_options['divi'] ?? false;
		$oxygen_integration   = $integration_options['oxygen'] ?? false;
		$wpbakery_integration = $integration_options['wpbakery'] ?? false;
		$beaber_integration   = $integration_options['beaver'] ?? false;
		$bricks_integration   = $integration_options['bricks'] ?? false;

		/**
		 * Divi.
		 */
		if ( $divi_integration ) {
			require_once SP_PCP_PATH . 'admin/page-builder/divi/smart-post-show-divi-module.php';
		}

		/**
		 * Oxygen.
		 */
		if ( $oxygen_integration ) {
			require_once SP_PCP_PATH . 'admin/page-builder/oxygen/init.php';
		}

		/**
		 * WPBakery.
		 */
		if ( $wpbakery_integration && defined( 'WPB_VC_VERSION' ) ) {
			require_once SP_PCP_PATH . 'admin/page-builder/wpBakery/smart-post-show-wpbakery.php';
		}

		/**
		 * Beaver Builder.
		 */
		if ( $beaber_integration && class_exists( 'FLBuilder' ) ) {
			require_once SP_PCP_PATH . 'admin/page-builder/beaver/smart-post-beaver-module.php';
		}

		/**
		 * Bricks Builder.
		 */
		if ( $bricks_integration ) {
			require_once SP_PCP_PATH . 'admin/page-builder/bricks/init.php';
		}
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {
		$post_types   = get_post_types( array( 'show_in_nav_menus' => true ) );
		$plugin_admin = new Smart_Post_Show_Pro_Admin( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );
		// Load admin scripts and styles.
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		// Admin custom column.
		$this->loader->add_filter( 'manage_sp_post_carousel_posts_columns', $plugin_admin, 'filter_carousel_admin_column' );
		$this->loader->add_action( 'manage_sp_post_carousel_posts_custom_column', $plugin_admin, 'display_carousel_admin_fields', 10, 2 );
		// Admin shortcode duplicate .
		$this->loader->add_action( 'admin_action_sp_duplicate_smart_post', $plugin_admin, 'duplicate_smart_post_shortcode' );
		// Upload Placeholder image.
		$this->loader->add_action( 'admin_init', $plugin_admin, 'create_placeholder_img' );

		$this->loader->add_filter( 'post_row_actions', $plugin_admin, 'sp_duplicate_smart_post_link', 10, 2 );

		$this->loader->add_filter( 'plugin_row_meta', $plugin_admin, 'after_pcp_row_meta', 10, 4 );
		// Post save and update messages.
		$this->loader->add_filter( 'post_updated_messages', $plugin_admin, 'sppcp_update', 10, 1 );

		// License Page.
		$manage_license = new Smart_Post_Show_Pro_License( SMART_POST_SHOW_PRO_FILE, SP_PCP_VERSION, 'ShapedPlugin', SP_SPSP_STORE_URL, SP_SPSP_ITEM_ID, SP_PCP_PLUGIN_NAME );

		if ( class_exists( 'SitePress' ) ) {
			$this->loader->add_filter( 'wpml_get_home_url', $manage_license, 'wpml_home_url', 99, 5 );
		}

		$this->loader->add_action( 'smart_post_show_pro_weekly_scheduled_events', $manage_license, 'check_license_status' );

		// Init Updater.
		$this->loader->add_action( 'admin_init', $manage_license, 'init_updater', 0 );
		// Display notices to admins.
		$this->loader->add_action( 'admin_notices', $manage_license, 'license_active_notices' );
		$this->loader->add_action( 'in_plugin_update_message-' . SP_PCP_BASENAME, $manage_license, 'plugin_row_license_missing', 10, 2 );

		$import_export = new Smart_Post_Show_Pro_Import_Export( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );

		$this->loader->add_action( 'wp_ajax_pcp_export_shortcodes', $import_export, 'export_shortcodes' );
		$this->loader->add_action( 'wp_ajax_pcp_import_shortcodes', $import_export, 'import_shortcodes' );

		// Help Page.
		$help_menu = new SPSP_Help();
		// $this->loader->add_action( 'admin_menu', $help_menu, 'help_page_menu', 25 );

		// Blocks Page.
		$blocks_menu = new SPSP_Blocks();
		$this->loader->add_action( 'admin_menu', $blocks_menu, 'blocks_page_menu', 10 );

		// Redirect after active.
		$this->loader->add_action( 'activated_plugin', $plugin_admin, 'redirect_to' );

		// Elementor shortcode addons.
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
		if ( ( is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active_for_network( 'elementor/elementor.php' ) ) ) {
			require_once SP_PCP_PATH . 'admin/smart_post_show_pro_element_shortcode_addons.php';
		}

		// Saved Template.
		$saved_template = new Smart_Post_Show_Pro_Saved_Template();
		$this->loader->add_action( 'init', $saved_template, 'register_template_shortcode' );
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 */
	private function define_public_hooks() {
		$plugin_public = new Smart_Post_Show_Pro_Public( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_loaded', $plugin_public, 'register_all_scripts' );
	}

	/**
	 * Register common hooks.
	 *
	 * @since 2.0.0
	 * @access private
	 */
	private function define_common_hooks() {
		$plugin_public = new Smart_Post_Show_Pro_Post_Type( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );
		$this->loader->add_action( 'init', $plugin_public, 'post_carousel_post_type', 10 );
	}

	/**
	 * Register builder hooks.
	 *
	 * @since 2.0.0
	 * @access private
	 */
	private function define_builder_hooks() {
		$plugin_public = new Smart_Post_Show_Pro_Builder_Post_Type( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );
		$this->loader->add_action( 'init', $plugin_public, 'smart_post_builder_post_type', 10 );
	}
	/**
	 * Register builder hooks.
	 *
	 * @since 2.0.0
	 * @access private
	 */
	private function define_custom_font_hooks() {
		$plugin_public = new Smart_Post_Show_Pro_Custom_Font_Type( SP_PCP_PLUGIN_NAME, SP_PCP_VERSION );
		$this->loader->add_action( 'init', $plugin_public, 'smart_post_custom_font_post_type', 10 );
		$this->loader->add_action( 'add_meta_boxes', $plugin_public, 'smart_post_custom_font_add_meta_boxes', 10 );
		$this->loader->add_action( 'save_post', $plugin_public, 'smart_post_custom_font_save_meta_boxes', 10 );

		$this->loader->add_action( 'upload_mimes', $plugin_public, 'smart_add_file_types_to_uploads' );
		$this->loader->add_action( 'wp_check_filetype_and_ext', $plugin_public, 'smart_font_correct_filetypes', 10, 5 );
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    2.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     2.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     2.0.0
	 * @return    Smart_Post_Show_Pro_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     2.0.0
	 * @return    string The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}
}
