<?php
/**
 * SeoHub Framework: shortcodes manipulations
 *
 * @package	seohub
 * @since	seohub 1.0
 */

// Disable direct call
if ( ! defined( 'ABSPATH' ) ) { exit; }

// Theme init
if (!function_exists('seohub_sc_theme_setup')) {
	add_action( 'seohub_action_init_theme', 'seohub_sc_theme_setup', 1 );
	function seohub_sc_theme_setup() {
		// Add sc stylesheets
		add_action('seohub_action_add_styles', 'seohub_sc_add_styles', 1);
	}
}

if (!function_exists('seohub_sc_theme_setup2')) {
	add_action( 'seohub_action_before_init_theme', 'seohub_sc_theme_setup2' );
	function seohub_sc_theme_setup2() {

		if ( !is_admin() || isset($_POST['action']) ) {
			// Enable/disable shortcodes in excerpt
			add_filter('the_excerpt', 					'seohub_sc_excerpt_shortcodes');
	
			// Prepare shortcodes in the content
			if (function_exists('seohub_sc_prepare_content')) seohub_sc_prepare_content();
		}

		// Add init script into shortcodes output in VC frontend editor
		add_filter('seohub_shortcode_output', 'seohub_sc_add_scripts', 10, 4);

		// AJAX: Send contact form data
		add_action('wp_ajax_send_form',			'seohub_sc_form_send');
		add_action('wp_ajax_nopriv_send_form',	'seohub_sc_form_send');

		// Show shortcodes list in admin editor
		add_action('media_buttons',				'seohub_sc_selector_add_in_toolbar', 11);

	}
}


// Add shortcodes styles
if ( !function_exists( 'seohub_sc_add_styles' ) ) {
	//add_action('seohub_action_add_styles', 'seohub_sc_add_styles', 1);
	function seohub_sc_add_styles() {
		// Shortcodes
		seohub_enqueue_style( 'seohub-shortcodes-style',	seohub_get_file_url('shortcodes/theme.shortcodes.css'), array(), null );
	}
}


// Add shortcodes init scripts
if ( !function_exists( 'seohub_sc_add_scripts' ) ) {
	//add_filter('seohub_shortcode_output', 'seohub_sc_add_scripts', 10, 4);
	function seohub_sc_add_scripts($output, $tag='', $atts=array(), $content='') {

		global $SEOHUB_GLOBALS;
		
		if (empty($SEOHUB_GLOBALS['shortcodes_scripts_added'])) {
			$SEOHUB_GLOBALS['shortcodes_scripts_added'] = true;
			//seohub_enqueue_style( 'seohub-shortcodes-style', seohub_get_file_url('shortcodes/theme.shortcodes.css'), array(), null );
			seohub_enqueue_script( 'seohub-shortcodes-script', seohub_get_file_url('shortcodes/theme.shortcodes.js'), array('jquery'), null, true );	
		}
		
		return $output;
	}
}


/* Prepare text for shortcodes
-------------------------------------------------------------------------------- */

// Prepare shortcodes in content
if (!function_exists('seohub_sc_prepare_content')) {
	function seohub_sc_prepare_content() {
		if (function_exists('seohub_sc_clear_around')) {
			$filters = array(
				array('seohub', 'sc', 'clear', 'around'),
				array('widget', 'text'),
				array('the', 'excerpt'),
				array('the', 'content')
			);
			if (function_exists('seohub_exists_woocommerce') && seohub_exists_woocommerce()) {
				$filters[] = array('woocommerce', 'template', 'single', 'excerpt');
				$filters[] = array('woocommerce', 'short', 'description');
			}
			if (is_array($filters) && count($filters) > 0) {
				foreach ($filters as $flt)
					add_filter(join('_', $flt), 'seohub_sc_clear_around', 1);	// Priority 1 to clear spaces before do_shortcodes()
			}
		}
	}
}

// Enable/Disable shortcodes in the excerpt
if (!function_exists('seohub_sc_excerpt_shortcodes')) {
	function seohub_sc_excerpt_shortcodes($content) {
		if (!empty($content)) {
			$content = do_shortcode($content);
			//$content = strip_shortcodes($content);
		}
		return $content;
	}
}



/*
// Remove spaces and line breaks between close and open shortcode brackets ][:
[trx_columns]
	[trx_column_item]Column text ...[/trx_column_item]
	[trx_column_item]Column text ...[/trx_column_item]
	[trx_column_item]Column text ...[/trx_column_item]
[/trx_columns]

convert to

[trx_columns][trx_column_item]Column text ...[/trx_column_item][trx_column_item]Column text ...[/trx_column_item][trx_column_item]Column text ...[/trx_column_item][/trx_columns]
*/
if (!function_exists('seohub_sc_clear_around')) {
	function seohub_sc_clear_around($content) {
		if (!empty($content)) $content = preg_replace("/\](\s|\n|\r)*\[/", "][", $content);
		return $content;
	}
}






/* Shortcodes support utils
---------------------------------------------------------------------- */

// SeoHub shortcodes load scripts
if (!function_exists('seohub_sc_load_scripts')) {
	function seohub_sc_load_scripts() {
		seohub_enqueue_script( 'seohub-shortcodes-script', seohub_get_file_url('core/core.shortcodes/shortcodes_admin.js'), array('jquery'), null, true );
		seohub_enqueue_script( 'seohub-selection-script',  seohub_get_file_url('js/jquery.selection.js'), array('jquery'), null, true );
	}
}

// SeoHub shortcodes prepare scripts
if (!function_exists('seohub_sc_prepare_scripts')) {
	function seohub_sc_prepare_scripts() {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS['shortcodes_prepared'])) {
			$SEOHUB_GLOBALS['shortcodes_prepared'] = true;
			$json_parse_func = 'eval';	// 'JSON.parse'
			?>
			<script type="text/javascript">
				jQuery(document).ready(function(){
					try {
						SEOHUB_GLOBALS['shortcodes'] = <?php echo trim($json_parse_func); ?>(<?php echo json_encode( seohub_array_prepare_to_json($SEOHUB_GLOBALS['shortcodes']) ); ?>);
					} catch (e) {}
					SEOHUB_GLOBALS['shortcodes_cp'] = '<?php echo is_admin() ? (!empty($SEOHUB_GLOBALS['to_colorpicker']) ? $SEOHUB_GLOBALS['to_colorpicker'] : 'wp') : 'custom'; ?>';	// wp | tiny | custom
				});
			</script>
			<?php
		}
	}
}

// Show shortcodes list in admin editor
if (!function_exists('seohub_sc_selector_add_in_toolbar')) {
	//add_action('media_buttons','seohub_sc_selector_add_in_toolbar', 11);
	function seohub_sc_selector_add_in_toolbar(){

		if ( !seohub_options_is_used() ) return;

		seohub_sc_load_scripts();
		seohub_sc_prepare_scripts();

		global $SEOHUB_GLOBALS;

		$shortcodes = $SEOHUB_GLOBALS['shortcodes'];
		$shortcodes_list = '<select class="sc_selector"><option value="">&nbsp;'.esc_html__('- Select Shortcode -', 'seohub').'&nbsp;</option>';

		if (is_array($shortcodes) && count($shortcodes) > 0) {
			foreach ($shortcodes as $idx => $sc) {
				$shortcodes_list .= '<option value="'.esc_attr($idx).'" title="'.esc_attr($sc['desc']).'">'.esc_html($sc['title']).'</option>';
			}
		}

		$shortcodes_list .= '</select>';

		echo trim($shortcodes_list);
	}
}

// SeoHub shortcodes builder settings
require_once seohub_get_file_dir('core/core.shortcodes/shortcodes_settings.php');

// VC shortcodes settings
if ( class_exists('WPBakeryShortCode') ) {
	require_once seohub_get_file_dir('core/core.shortcodes/shortcodes_vc.php');
}

// SeoHub shortcodes implementation
seohub_autoload_folder( 'shortcodes/trx_basic' );
seohub_autoload_folder( 'shortcodes/trx_optional' );
?>