<?php

/**
 *
 * @link              https://envytheme.com
 * @since             1.0.5
 * @package           Endless_Addons
 *
 * @wordpress-plugin
 * Plugin Name:       Endless Addons for Elementor
 * Plugin URI:        https://wordpress.org/plugins/endless-addons-for-elementor
 * Description:       Endless Addons is a WordPress plugin that works seamlessly with Elementor Website Builder. It has 30+ widgets with multiple styles.
 * Version:           1.0.5
 * Author:            EnvyTheme
 * Author URI:        https://envytheme.com
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       endless-addons
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Plugin directory path
 */
define('ENDLESS_ADDONS_ACC_PATH', plugin_dir_path(__FILE__));

define('ENDLESS_ADDONS_ASSET_PATH', wp_upload_dir()['basedir'] . '/endless-addons-data');
define('ENDLESS_ADDONS_ASSET_URL', wp_upload_dir()['baseurl'] . '/endless-addons-data');
define('ENDLESS_ADDONS_PLUGIN_PATH', trailingslashit(plugin_dir_path(__FILE__)));
define('ENDLESS_ADDONS_PLUGIN_URL', trailingslashit(plugins_url('/', __FILE__)));
define('ENDLESS_ADDONS_PLUGIN_VERSION', '1.0.5');

/**
 * Including plugin config.
 */
$GLOBALS['endless_addons_config'] = require_once ENDLESS_ADDONS_PLUGIN_PATH . 'config.php';

/**
 * Currently plugin version.
 */
define( 'ENDLESS_ADDONS_VERSION', '1.0.5' );

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-endless-addons-activator.php
 */
function endless_addons_activate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-endless-addons-activator.php';
	Endless_Addons_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-endless-addons-deactivator.php
 */
function endless_addons_deactivate() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-endless-addons-deactivator.php';
	Endless_Addons_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'endless_addons_activate' );
register_deactivation_hook( __FILE__, 'endless_addons_deactivate' );

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-endless-addons.php';

/*
* Clone enable option
*/
/*$endless_addons_clone_enable = (array)get_option('new_settings3');
if( isset( $endless_addons_clone_enable['endless_addons_clone_enable'] ) ) :
    $endless_addons_clone_enable_new = $endless_addons_clone_enable['endless_addons_clone_enable'];
else:
    $endless_addons_clone_enable_new = '';
endif;*/

//if( $endless_addons_clone_enable_new ) :

/**
 * The class responsible for endless addons clone functionality of the plugin.
 */
require plugin_dir_path( __FILE__ ) . 'includes/clone/class-endless-addons-clone.php';

//endif;

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.5
 */
function endless_addons_run() {

	$plugin = new Endless_Addons();
	$plugin->run();

}
endless_addons_run();

/**
 * Main Endless Addons Class
 *
 * The main class that initiates and runs the plugin.
 *
 * @since 1.0.5
 */
final class Elementor_Endless_Addons {

	/**
	 * Plugin Version
	 *
	 * @since 1.0.5
	 *
	 * @var string The plugin version.
	 */
	const VERSION = '1.0.5';

	/**
	 * Minimum Elementor Version
	 *
	 * @since 1.0.5
	 *
	 * @var string Minimum Elementor version required to run the plugin.
	 */
	const MINIMUM_ELEMENTOR_VERSION = '2.0.0';

	/**
	 * Minimum PHP Version
	 *
	 * @since 1.0.5
	 *
	 * @var string Minimum PHP version required to run the plugin.
	 */
	const MINIMUM_PHP_VERSION = '7.0';

	/**
	 * Instance
	 *
	 * @since 1.0.5
	 *
	 * @access private
	 * @static
	 *
	 * @var Elementor_Endless_Addons The single instance of the class.
	 */
	private static $_instance = null;

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 * @static
	 *
	 * @return Elementor_Endless_Addons An instance of the class.
	 */
	public static function instance() {

		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;

	}

	/**
	 * Constructor
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function __construct() {

		add_action( 'plugins_loaded', [ $this, 'on_plugins_loaded' ] );

		add_action('elementor/init', [$this, 'ea_register_new_category']);

	}

	/**
	 * Load Textdomain
	 *
	 * Load plugin localization files.
	 *
	 * Fired by `init` action hook.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function i18n() {

		load_plugin_textdomain( 'endless-addons' );

	}

	/**
	 * On Plugins Loaded
	 *
	 * Checks if Elementor has loaded, and performs some compatibility checks.
	 * If All checks pass, inits the plugin.
	 *
	 * Fired by `plugins_loaded` action hook.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function on_plugins_loaded() {

		if ( $this->is_compatible() ) {
			add_action( 'elementor/init', [ $this, 'init' ] );
		}

	}

	/**
	 * Compatibility Checks
	 *
	 * Checks if the installed version of Elementor meets the plugin's minimum requirement.
	 * Checks if the installed PHP version meets the plugin's minimum requirement.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function is_compatible() {

		// Check if Elementor installed and activated
		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_missing_main_plugin' ] );
			return false;
		}

		// Check for required Elementor version
		if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
			return false;
		}

		// Check for required PHP version
		if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
			return false;
		}

		return true;

	}

	/**
	 * Initialize the plugin
	 *
	 * Load the plugin only after Elementor (and other plugins) are loaded.
	 * Load the files required to run the plugin.
	 *
	 * Fired by `plugins_loaded` action hook.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function init() {
	
		$this->i18n();

		// Add Plugin actions
		add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] );
	}

	public function ea_register_new_category() {
		\Elementor\Plugin::instance()->elements_manager->add_category(
			'endless-elements',
			[
				'title' => esc_html__('Endless Addons', 'endless-addons'),
				'icon'  => 'fa fa-image',
			],
		);
	}

	/**
	 * Init Widgets
	 *
	 * Include widgets files and register them
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function init_widgets() {

        // Include enable options file
		require_once( __DIR__ . '/includes/class-endless-addons-enable.php' );

		// Include widget files
		require_once( __DIR__ . '/includes/widgets/endless-addons-banner-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-card-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-flipbox-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-infobox-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-pricing-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-team-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-testimonial-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-timeline-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-accordion-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-button-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-contact-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-logo-carousel-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-newsletter-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-progress-bar-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-video-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-fun-factor-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-icon-box-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-image-box-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-image-gallery-widget.php' );
		//require_once( __DIR__ . '/includes/widgets/endless-addons-line-chart-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-social-media-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-menu-bar-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-business-hour-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-roadmap-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-wp-form-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-ninja-form-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-event-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-review-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-blog-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-tabs-widget.php' );
		require_once( __DIR__ . '/includes/widgets/endless-addons-footer-widget.php' );

		// Register v1 widget
		if( $endless_addons_feature_banner_enable_new ) : 
			\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Banner_Widget() );
		endif;
		if( $endless_addons_feature_card_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Card_Widget() );
		endif;
		if( $endless_addons_feature_flipbox_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_FlipBox_Widget() );
		endif;
		if( $endless_addons_feature_infobox_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_InfoBox_Widget() );
		endif;
		if( $endless_addons_feature_pricing_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Pricing_Widget() );
        endif;
        if( $endless_addons_feature_team_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Team_Widget() );
        endif;
        if( $endless_addons_feature_testimonial_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Testimonial_Widget() );
        endif;
        if( $endless_addons_feature_timeline_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Timeline_Widget() );
        endif;

        // Register v3 widget
		if( $endless_addons_feature_accordion_enable_new ) : 
			\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Accordion_Widget() );
		endif;
		if( $endless_addons_feature_button_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Button_Widget() );
		endif;
		if( $endless_addons_feature_contact_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Contact_Form_7_Widget() );
		endif;
		if( $endless_addons_feature_logo_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Logo_Carousel_Widget() );
		endif;
		if( $endless_addons_feature_newsletter_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Newsletter_Widget() );
        endif;
        if( $endless_addons_feature_progress_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Progress_Bar_Widget() );
        endif;
        if( $endless_addons_feature_video_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_EA_Video_Widget() );
        endif;

        // Register v4 widget
		if( $endless_addons_feature_fun_enable_new ) : 
			\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Fun_Factor_Widget() );
		endif;
		if( $endless_addons_feature_icon_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Iconbox_Widget() );
		endif;
		if( $endless_addons_feature_image_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Imagebox_Widget() );
		endif;
		if( $endless_addons_feature_images_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Image_Gallery_Widget() );
		endif;
		/*if( $endless_addons_feature_line_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Linechart_Widget() );
        endif;*/
        if( $endless_addons_feature_social_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Social_Media_Widget() );
        endif;
        if( $endless_addons_feature_menu_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Menu_Bar_Widget() );
        endif;
        if( $endless_addons_feature_business_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Business_Hour_Widget() );
        endif;

        // Register v5 widget
        if( $endless_addons_feature_roadmap_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Roadmap_Widget() );
		endif;
		if( $endless_addons_feature_wp_form_enable_new ) : 
			\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_WP_Form_Widget() );
		endif;
		if( $endless_addons_feature_ninja_form_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Ninja_Form_Widget() );
        endif;
        if( $endless_addons_feature_event_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Event_Widget() );
		endif;
        if( $endless_addons_feature_review_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Review_Widget() );
        endif;
        if( $endless_addons_feature_blog_enable_new ) : 
            \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Blog_Widget() );
        endif;
        if( $endless_addons_feature_tabs_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Tabs_Widget() );
		endif; 
		if( $endless_addons_feature_footer_enable_new ) : 
		    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Footer_Widget() );
		endif;    
	}
	
	/**
	 * Admin notice
	 *
	 * Warning when the site doesn't have Elementor installed or activated.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function admin_notice_missing_main_plugin() {

		if ( file_exists( WP_PLUGIN_DIR . '/elementor/elementor.php' ) ) {
	        $ea_notice_title = __( 'Activate Elementor', 'endless-addons' );
	        $ea_notice_url = wp_nonce_url( 'plugins.php?action=activate&plugin=elementor/elementor.php&plugin_status=all&paged=1', 'activate-plugin_elementor/elementor.php' );
	    }else{
	        $ea_notice_title = __( 'Install Elementor', 'endless-addons' );
	        $ea_notice_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ), 'install-plugin_elementor' );
	    }

	    $ea_notice = ea_kses_intermediate( sprintf(
	        __( '%1$s requires %2$s to be installed and activated. %3$s', 'endless-addons' ),
	        '<strong>' . __( 'Endless Addons', 'endless-addons' ) . '</strong>',
	        '<strong>' . __( 'Elementor', 'endless-addons' ) . '</strong>',
	        '<a href="' . esc_url( $ea_notice_url ) . '">' . $ea_notice_title . '</a>'
	    ) );

	    printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $ea_notice );

	}

	/**
	 * Admin notice
	 *
	 * Warning when the site doesn't have a minimum required Elementor version.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function admin_notice_minimum_elementor_version() {

		if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );

		$message = sprintf(
			/* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'endless-addons' ),
			'<strong>' . esc_html__( 'Endless Addons for Elementor', 'endless-addons' ) . '</strong>',
			'<strong>' . esc_html__( 'Elementor', 'endless-addons' ) . '</strong>',
			 self::MINIMUM_ELEMENTOR_VERSION
		);

		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );

	}

	/**
	 * Admin notice
	 *
	 * Warning when the site doesn't have a minimum required PHP version.
	 *
	 * @since 1.0.5
	 *
	 * @access public
	 */
	public function admin_notice_minimum_php_version() {

		if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );

		$message = sprintf(
			/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'endless-addons' ),
			'<strong>' . esc_html__( 'Endless Addons for Elementor', 'endless-addons' ) . '</strong>',
			'<strong>' . esc_html__( 'PHP', 'endless-addons' ) . '</strong>',
			 self::MINIMUM_PHP_VERSION
		);

		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );

	}

}

Elementor_Endless_Addons::instance();

// Add settings options
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'endless_addons_plugin_page_settings_link');
function endless_addons_plugin_page_settings_link( $links ) {
	$links[] = '<a href="' .
		admin_url( 'admin.php?page=endless-addons-plugin-options' ) .
		'">' . __('Settings', 'endless-addons') . '</a>';
	return $links;
}

// Add doc options
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'endless_addons_plugin_docs_link');
function endless_addons_plugin_docs_link( $links ) {
	$links[] = '<a href="https://docs.envytheme.com/docs/endless-addons-plugin-documentation/
		">' . __('Docs', 'endless-addons') . '</a>';
	return $links;
}

// Redirect after plugin activate
register_activation_hook(__FILE__, 'endless_addons_plugin_activate');
add_action('admin_init', 'endless_addons_redirect');

function endless_addons_plugin_activate() {
    add_option('endless_addons_do_activation_redirect', true);
}

function endless_addons_redirect() {
    if (get_option('endless_addons_do_activation_redirect', false)) {
        delete_option('endless_addons_do_activation_redirect');
        wp_redirect("admin.php?page=endless-addons-plugin-options");
        exit;
    }
}
