<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * 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.
 *
 * @since      1.0.0
 * @package    Storyposts
 * @subpackage Storyposts/includes
 * @author     thunder-team
 * @license    GPL-2.0-or-later
 * @link       https://www.gnu.org/licenses/gpl-2.0.txt
 */
class Storyposts {
    /**
     * The loader that's responsible for maintaining and registering all hooks that power
     * the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      Storyposts_Loader    $loader    Maintains and registers all hooks for the plugin.
     */
    protected $loader;

    /**
     * The unique identifier of this plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $plugin_name    The string used to uniquely identify this plugin.
     */
    protected $plugin_name;

    /**
     * The current version of the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $version    The current version of the plugin.
     */
    protected $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    1.0.0
     */
    public function __construct() {
        if ( defined( 'STORYPOSTS_VERSION' ) ) {
            $this->version = STORYPOSTS_VERSION;
        } else {
            $this->version = '1.0.0';
        }
        $this->plugin_name = 'storyposts';
        $this->load_dependencies();
        if ( storyposts_component_exists( 'class-storyposts-i18n.php', 'file' ) ) {
            $this->set_locale__premium_only();
        }
        $this->define_admin_hooks();
        $this->define_public_hooks();
        // Initialize Settings
        new Storyposts_Settings();
        // CPTs
        new Storyposts_Story_CPT();
        new Storyposts_Collection_CPT();
        new Storyposts_Story_Meta();
        new Storyposts_Collection_Meta();
        new Storyposts_Category_Meta();
        // Collection Stories
        new Storyposts_Get_Collection_Stories();
        // User Stories (Pro only)
        if ( storyposts_component_exists( 'user-stories' ) ) {
            if ( Storyposts_Settings::get( 'enable_user_stories', 'on' ) === 'on' ) {
                new Storyposts_Upload_Story();
                new Storyposts_Get_User_Stories();
                new Storyposts_Delete_Story();
            }
        }
        // Blog Stories (Pro only)
        if ( storyposts_component_exists( 'blog-stories' ) ) {
            new Storyposts_Get_Blog_Stories();
        }
        // Product Stories (Pro only)
        if ( storyposts_component_exists( 'product-stories' ) ) {
            new Storyposts_Get_Product_Stories();
        }
        // Shortcodes
        new Storyposts_Shortcode();
    }

    /**
     * Load the required dependencies for this plugin.
     *
     * Include the following files that make up the plugin:
     *
     * - Storyposts_Loader. Orchestrates the hooks of the plugin.
     * - Storyposts_i18n. Defines internationalization functionality.
     * - Storyposts_Admin. Defines all hooks for the admin area.
     * - Storyposts_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    1.0.0
     * @access   private
     */
    private function load_dependencies() {
        /**
         * Load CMB2 if not already loaded
         */
        /**
         * Load Isolated CMB2
         */
        if ( !class_exists( 'Storyposts_CMB2' ) ) {
            if ( file_exists( STORYPOSTS_PATH . 'includes/libraries/cmb2/init.php' ) ) {
                require_once STORYPOSTS_PATH . 'includes/libraries/cmb2/init.php';
            }
        }
        /**
         * The class responsible for orchestrating the actions and filters of the
         * core plugin.
         */
        require_once STORYPOSTS_PATH . 'includes/class-storyposts-loader.php';
        /**
         * The class responsible for defining internationalization functionality
         * of the plugin.
         */
        if ( file_exists( STORYPOSTS_PATH . 'includes/class-storyposts-i18n.php' ) ) {
            require_once STORYPOSTS_PATH . 'includes/class-storyposts-i18n.php';
        }
        /**
         * The class responsible for defining all actions that occur in the admin area.
         */
        require_once STORYPOSTS_PATH . 'admin/class-storyposts-admin.php';
        /**
         * The class responsible for defining the settings page.
         */
        require_once STORYPOSTS_PATH . 'admin/class-storyposts-settings.php';
        /**
         * The class responsible for defining all actions that occur in the public-facing
         * side of the site.
         */
        require_once STORYPOSTS_PATH . 'public/class-storyposts-public.php';
        /**
         * Global Helper Functions
         */
        require_once STORYPOSTS_PATH . 'includes/storyposts-functions.php';
        /**
         * Register and configure custom post types and meta boxes
         */
        require_once STORYPOSTS_PATH . 'includes/post-types/class-storyposts-story-cpt.php';
        require_once STORYPOSTS_PATH . 'includes/post-types/class-storyposts-collection-cpt.php';
        require_once STORYPOSTS_PATH . 'includes/meta/class-storyposts-story-meta.php';
        require_once STORYPOSTS_PATH . 'includes/meta/class-storyposts-collection-meta.php';
        require_once STORYPOSTS_PATH . 'includes/meta/class-storyposts-category-meta.php';
        /**
         * Collection stories handlers
         */
        require_once STORYPOSTS_PATH . 'includes/collection-stories/class-storyposts-get-collection-stories.php';
        /**
         * User stories handlers (Pro only)
         */
        if ( storyposts_component_exists( 'user-stories' ) ) {
            $_storyposts_user_stories_dir = STORYPOSTS_PATH . 'includes/user-stories/';
            require_once $_storyposts_user_stories_dir . 'class-storyposts-upload-story.php';
            require_once $_storyposts_user_stories_dir . 'class-storyposts-get-user-stories.php';
            require_once $_storyposts_user_stories_dir . 'class-storyposts-delete-story.php';
        }
        /**
         * Blog stories handlers (Pro only)
         */
        if ( storyposts_component_exists( 'blog-stories' ) ) {
            require_once STORYPOSTS_PATH . 'includes/blog-stories/class-storyposts-get-blog-stories.php';
        }
        /**
         * Product stories handlers (Pro only)
         */
        if ( storyposts_component_exists( 'product-stories' ) ) {
            require_once STORYPOSTS_PATH . 'includes/product-stories/class-storyposts-get-product-stories.php';
        }
        /**
         * Shortcodes
         */
        require_once STORYPOSTS_PATH . 'includes/shortcodes/class-storyposts-shortcode.php';
        $this->loader = new Storyposts_Loader();
    }

    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks() {
        $plugin_admin = new Storyposts_Admin($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
    }

    /**
     * Register all of the hooks related to the public-facing functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_public_hooks() {
        $plugin_public = new Storyposts_Public($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
        $this->loader->add_action( 'bp_before_directory_activity', $plugin_public, 'render_story_slider' );
    }

    /**
     * Run the loader to execute all of the hooks with WordPress.
     *
     * @since    1.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     1.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     1.0.0
     * @return    Storyposts_Loader    Orchestrates the hooks of the plugin.
     */
    public function get_loader() {
        return $this->loader;
    }

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

}
