<?php
if ( ! function_exists( '_st_plugin_support_get_container' ) ) {
	function _st_plugin_support_get_container( $id ) {
		return isset( $GLOBALS[ $id ] ) ? $GLOBALS[ $id ] : null;
	}
}

if ( ! function_exists( '_st_plugin_support_get' ) ) {
	function _st_plugin_support_get( $constant, $id ) {
		if ( ! defined( $constant ) ) {
			return null;
		}

		$container_id = constant( $constant );
		$container    = _st_plugin_support_get_container( $container_id );

		if ( $container === null || ! isset( $container[ $id ] ) ) {
			return null;
		}

		return $container[ $id ];
	}
}

if ( ! function_exists( '_st_plugin_support_is_enabled' ) ) {
	function _st_plugin_support_is_enabled( $constant, $value = null) {
		if ( ! defined( $constant ) ) {
			return false;
		}

		$id        = constant( $constant );
		$container = _st_plugin_support_get_container( $id );

		if ( $container === null || ! isset( $container['plugin_meta'] ) ) {
			return false;
		}

		if ( $value !== null ) {
			$plugin_meta = $container['plugin_meta'];

			return ( $plugin_meta->get_slug() === $value );
		}

		return true;
	}
}

if ( ! function_exists( '_st_plugin_support_version_compare' ) ) {
	function _st_plugin_support_version_compare( $constant, $version, $operator = null ) {
		if ( ! defined( $constant ) ) {
			return false;
		}

		$version_1 = strtolower( constant( $constant ) );
		$version_2 = strtolower( $version );

		return version_compare( $version_1, $version_2, $operator );
	}
}

add_action(
	'after_setup_theme',
	function () {
		if ( _st_plugin_support_is_enabled( 'ST_TEMPLATE_MANAGER' ) &&
		     _st_plugin_support_is_enabled( 'ST_REPLACE_META_BOX' ) &&
		     _st_plugin_support_version_compare( 'ST_REPLACE_META_BOX_API_VERSION', '1.1.0', '==' ) ) {
			$post_types = _st_plugin_support_get( 'ST_TEMPLATE_MANAGER', 'post_types');

			if (defined('ST_REPLACE_META_BOX_API_PREFIX') && isset($post_types['template'])) {
				$post_type = $post_types['template']->get_meta()->get_name();

				add_filter(
					ST_REPLACE_META_BOX_API_PREFIX . '_post_types',
					function ( array $post_types = array() ) use ( $post_type ) {
						$post_types[] = $post_type;

						return $post_types;
					}
				);
			}
		}

		if ( _st_plugin_support_is_enabled( 'ST_CARD_EX', 'st-card-ex' ) &&
		     _st_plugin_support_version_compare( 'ST_CARD_EX_API_VERSION', '1.1.0', '==' ) &&
		     function_exists( 'St\Plugin\CardEx\boot' ) ) {
			St\Plugin\CardEx\boot();
		}

		if ( _st_plugin_support_is_enabled( 'ST_LAZY_LOAD', 'st-lazy-load' ) &&
		     _st_plugin_support_version_compare( 'ST_LAZY_LOAD_API_VERSION', '1.1.0', '>=' ) &&
		     function_exists( 'St\Plugin\Lazy_Load\run' ) ) {
			St\Plugin\Lazy_Load\run();
		}

		if ( _st_plugin_support_is_enabled( 'ST_TEMPLATE_MANAGER', 'st-template-manager' ) &&
		     function_exists( 'St\Plugin\Template_Manager\run' ) ) {
			St\Plugin\Template_Manager\run();
		}

		if ( _st_plugin_support_is_enabled( 'ST_EXPORT_META_BOX', 'st-export-meta-box' ) &&
		     function_exists( 'St\Plugin\Export_Meta_Box\boot' ) ) {
			St\Plugin\Export_Meta_Box\boot();
		}

		if ( _st_plugin_support_is_enabled( 'ST_REPLACE_META_BOX', 'st-replace-meta-box' ) ) {
			if ( isset( $GLOBALS[ ST_REPLACE_META_BOX ]['plugin'] ) ) {
				$GLOBALS[ ST_REPLACE_META_BOX ]['plugin']->boot();
			}
		}

		if ( _st_plugin_support_is_enabled( 'ST_CUSTOM_SEARCH', 'st-custom-search' ) &&
		     function_exists( 'St\Plugin\Custom_Search\boot' ) ) {
			St\Plugin\Custom_Search\run();
		}

		if ( _st_plugin_support_is_enabled( 'ST_MULTI_INSTALLER', 'st-multi-installer' ) &&
		     function_exists( 'St\Plugin\Multi_Installer\boot' ) ) {
			St\Plugin\Multi_Installer\run();
		}
	}
);

add_filter(
	'st_export_meta_box_download_filename',
	function ( $filename ) {

		return '%datetime_Y%_%datetime_m%%datetime_d%_%datetime_H%_%datetime_i%_%post_title%_%site_name%.txt';
	}
);

add_action(
	'st_template_manager_wp_head',
	function () {
		get_template_part( 'st-richjs' );

		$width = (int) get_option('stdata-128', '1060') - 320;

		echo <<<HTML
<style>
.st-template-manager-preview-view {
	box-sizing: border-box;
	display: block;
	margin: auto;
	padding: 20px 15px;
}

@media only screen and (min-width: 600px) {
	.st-template-manager-preview-view {
		padding: 20px 30px;
	}
}

@media only screen and (min-width: 960px) {
	.st-template-manager-preview-view {
		padding: 30px 50px;
		max-width: {$width}px;
	}
}
</style>
HTML;
	}
);

add_filter('st_cs_enqueue_shortcode_styles', '__return_false');
