<?php
/**
 * Elementor Addon
 *
 * @package Elementor
 */

defined( 'ABSPATH' ) || exit( 'Please don&rsquo;t call the plugin directly. Thanks :)' );

/**
 * Autoloader
 *
 * @param   string $class Class.
 *
 * @return  boolean
 */
function wp_seopress_elementor_addon_autoloader( $class ) {
	$dir = '/inc';

	switch ( $class ) {
		case false !== strpos( $class, 'WPSeoPressElementorAddon\\Admin\\' ):
										$class = strtolower( str_replace( 'WPSeoPressElementorAddon\\Admin', '', $class ) );
										$dir  .= '/admin';
			break;
		case false !== strpos( $class, 'WPSeoPressElementorAddon\\Controls\\' ):
										$class = strtolower( str_replace( 'WPSeoPressElementorAddon\\Controls', '', $class ) );
										$dir  .= '/controls';
			break;
		case false !== strpos( $class, 'WPSeoPressElementorAddon\\' ):
										$class = strtolower( str_replace( 'WPSeoPressElementorAddon', '', $class ) );
			break;
		default:
			return;
	}

	$filename = __DIR__ . $dir . str_replace( '_', '-', str_replace( '\\', '/class-', $class ) ) . '.php';

	if ( file_exists( $filename ) ) {
		require_once $filename;

		if ( class_exists( $class ) ) {
			return true;
		}
	}

	return false;
}
spl_autoload_register( 'wp_seopress_elementor_addon_autoloader' );

/**
 * WP SeoPress Elementor Addon
 */
final class WP_SeoPress_Elementor_Addon {
	/**
	 * Class instance
	 *
	 * @var \WP_SeoPress_Elementor_Addon Instance.
	 */
	private static $instance = null;

	/**
	 * Load instance of the class
	 *
	 * @return  \WP_SeoPress_Elementor_Addon Instance.
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
			self::$instance->_constants();
			self::$instance->_load_objects();
		}

		return self::$instance;
	}

	/**
	 * Constructor private
	 *
	 * @return  void Void.
	 */
	private function __construct() {
	}

	/**
	 * Define plugin constants
	 *
	 * @return  void Void.
	 */
	private function _constants() {
		if ( ! defined( 'SEOPRESS_ELEMENTOR_ADDON_DIR' ) ) {
			define( 'SEOPRESS_ELEMENTOR_ADDON_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
		}

		if ( ! defined( 'SEOPRESS_ELEMENTOR_ADDON_URL' ) ) {
			define( 'SEOPRESS_ELEMENTOR_ADDON_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
		}
	}

	/**
	 * Initiate classes
	 *
	 * @return  void Void.
	 */
	private function _load_objects() {
		if ( is_admin() ) {
			\WPSeoPressElementorAddon\Register_Controls::get_instance();
			\WPSeoPressElementorAddon\Admin\Seopress_Meta_Helper::get_meta_fields();
			\WPSeoPressElementorAddon\Admin\Document_Settings_Section::get_instance();
		}
	}
}

WP_SeoPress_Elementor_Addon::get_instance();

/**
 * Seopress elementor tabs seo start
 *
 * @return  void Void.
 */
function seopress_elementor_tabs_seo_start() {
	ob_start();
}

/**
 * Seopress elementor tabs seo end
 *
 * @return  void Void.
 */
function seopress_elementor_tabs_seo_end() {
	$output  = \ob_get_clean();
	$search  = '/(<(div|button) class="elementor-component-tab elementor-panel-navigation-tab" data-tab="global">.*<\/(div|button)>)/m';
	$replace = '${1}<${2} id="seopress-seo-tab" class="elementor-component-tab elementor-panel-navigation-tab" data-tab="seo">SEO</${2}>';
	echo \preg_replace(
		$search,
		$replace,
		$output
	);
}
add_action( 'elementor/editor/footer', 'seopress_elementor_tabs_seo_start', 0 );
add_action( 'elementor/editor/footer', 'seopress_elementor_tabs_seo_end', 999 );
