<?php
/**
 * This file contains the initialization functionality for the additional TinyMCE Editor buttons.
 */

add_action( 'admin_init', 'pexeto_init_buttons' );

if ( !isset( $pexeto->styling_buttons ) ) {
	$pexeto->styling_buttons = array( 'pexetotitle', 'pexetotitle2', 'pexetohighlight1',
		'pexetohighlight2', 'pexetodropcaps', '|', 'pexetolistcheck', 'pexetoliststar',
		'pexetolistarrow', 'pexetolistarrow2', 'pexetolistarrow4', 'pexetolistplus',
		'|', 'pexetolinebreak', 'pexetoframe', 'pexetolightbox', '|', 'pexetobutton',
		'pexetoinfoboxes' );
}

if ( !isset( $pexeto->content_buttons ) ) {
	$pexeto->content_buttons = array( 'pexetotwocolumns', 'pexetothreecolumns',
		'pexetofourcolumns', '|', 'pexetoyoutube', 'pexetovimeo', 'pexetoflash',
		'|', 'pexetotestimonials', 'pexetoservices', 'pexetocarousel' );
}



/**
 * Inits the buttons functionality - adds the CSS style for the editor and the needed filters.
 */
if ( !function_exists( 'pexeto_init_buttons' ) ) {
	function pexeto_init_buttons() {
		add_editor_style( 'functions/formatting-buttons/custom-editor-style.css' );

		if ( get_user_option( 'rich_editing' ) == 'true' ) {
			add_filter( 'mce_external_plugins', 'pexeto_add_btn_tinymce_plugin' );
			add_filter( 'mce_buttons_3', 'pexeto_register_styling_buttons' );
			add_filter( 'mce_buttons_4', 'pexeto_register_content_buttons' );
		}
	}
}


add_action( 'admin_footer', 'pexeto_load_tinymce_styles', 20 );

if(!function_exists('pexeto_load_tinymce_styles')){

	/**
	 * Loads the CSS files that are required for the custom TinyMCE buttons 
	 * design.
	 */
	function pexeto_load_tinymce_styles(){
		global $current_screen;

		if ( $current_screen->base=='post' ) {
			wp_enqueue_style('pexeto-buttons', PEXETO_FUNCTIONS_URL.'formatting-buttons/buttons.css');
		}
	}
}

/**
 * Registers the buttons. Adds the custom defined buttons to the default
 * WordPress buttons.
 *
 * @param $buttons the full array of buttons containing both WordPress buttons
 * and the custom theme buttons.
 */
if ( !function_exists( 'pexeto_register_styling_buttons' ) ) {
	function pexeto_register_styling_buttons( $buttons ) {
		global $pexeto;

		array_push( $buttons, implode( ',', $pexeto->styling_buttons ) );
		return $buttons;
	}
}

/**
 * Registers a JavaScript file that will handle the functionality from the
 * custom buttons.
 *
 * @param $plugin_array
 */
if ( !function_exists( 'pexeto_add_btn_tinymce_plugin' ) ) {
	function pexeto_add_btn_tinymce_plugin( $plugin_array ) {
		global $pexeto;
		$merged_buttons=array_merge( $pexeto->styling_buttons, $pexeto->content_buttons );
		foreach ( $merged_buttons as $btn ) {
			$plugin_array[$btn] = PEXETO_FUNCTIONS_URL.'formatting-buttons/editor-plugin.js';
		}
		return $plugin_array;
	}
}

/**
 * Registers the buttons.
 *
 * @param $buttons the full array of buttons containing both WordPress buttons
 * and the custom theme buttons.
 */
if ( !function_exists( 'pexeto_register_content_buttons' ) ) {
	function pexeto_register_content_buttons( $buttons ) {
		global $pexeto;

		array_push( $buttons, implode( ',', $pexeto->content_buttons ) );
		return $buttons;
	}
}


add_action( 'admin_print_scripts', 'pexeto_load_button_data_sets' );

if ( !function_exists( 'pexeto_load_button_data_sets' ) ) {

	/**
	 * Loads some of the data sets that will be used from the formatting buttons
	 * to allow the user to select from them when inserting the element. Prints
	 * some initialization JavaScript code.
	 */
	function pexeto_load_button_data_sets() {
		global $current_screen;

		if ( $current_screen->base=='post' ) {

			//load the services boxes
			$service_terms = get_terms( PEXETO_SERVICES_POSTTYPE.PexetoCustomPage::term_suffix, array( 'hide_empty'=>false ) );
			$service_sets = array();
			foreach ( $service_terms as $term ) {
				$service_sets[]=array( 'id'=>$term->term_id, 'name'=>$term->name );
			}

			//load the portfolio categories
			$cats = pexeto_get_portfolio_categories();
			array_unshift( $cats, array( 'id'=>'-1', 'name'=>'All categories' ) );

			echo '<script type="text/javascript">'
				.'var PEXETO = PEXETO || {};'
				.'PEXETO.servicesBoxes = '.json_encode( $service_sets ).';'
				.'PEXETO.portfolioCategories = '.json_encode( $cats ).';'
				.'</script>';
		}
	}
}
