<?php
/*
Plugin Name: FloShortcodes
Plugin URI: http://www.flothemes.com
Description: Shortcode generator. 
Version: 1.2
Author: Flothemes
*/

class CosmoShortcodes {

    function __construct() 
    {	
    	require_once( plugin_dir_path( __FILE__ ) .'functions.php');
    	require_once( plugin_dir_path( __FILE__ ) .'shortcodes.php' );
    	define('COSMO_TINYMCE_URI', plugin_dir_url( __FILE__ ) .'tinymce');
		define('COSMO_TEMPLATE_PATH', 'floshortcodes');
		
        add_action('init', array(&$this, 'init'));
        add_action('admin_init', array(&$this, 'admin_init'));

	}
	
	/**
	 * Registers TinyMCE rich editor buttons
	 *
	 * @return	void
	 */
	function init()
	{

		$siteurl = get_option('siteurl');
        if( !empty($siteurl) ){
            $siteurl = rtrim( $siteurl , '/') . '/wp-admin/admin-ajax.php' ;
        }else{
            $siteurl = home_url('/wp-admin/admin-ajax.php');
        }

		add_action('wp_ajax_cosmo_send_contact' , 'cosmo_send_contact' );
		add_action('wp_ajax_nopriv_cosmo_send_contact' , 'cosmo_send_contact' );

		if( ! is_admin() )
		{
			wp_enqueue_style( 'cosmo-shortcodes', plugin_dir_url( __FILE__ ) . 'css/shortcodes.css' );
			//wp_enqueue_script( 'jquery-ui-accordion' );
			//wp_enqueue_script( 'jquery-ui-tabs' );
			wp_enqueue_script( 'cosmo-shortcodes-lib', plugin_dir_url( __FILE__ ) . 'js/cosmo-shortcodes-lib.js', array('jquery', 'jquery-ui-accordion', 'jquery-ui-tabs'), 1.0, true );
			wp_localize_script( 'cosmo-shortcodes-lib', 'ajaxurl', $siteurl);

			
		}

		if( is_admin() ){
			wp_enqueue_script( 'actionsjs', plugin_dir_url( __FILE__ ) . 'js/actions.js', array('jquery') );

			
			wp_localize_script( 'actionsjs', 'ajaxurl', $siteurl);
			wp_localize_script( 'actionsjs', 'ajaxTaxNonce', wp_create_nonce( 'ajax-Tax-nonce' ));
			
			
			// colorpicker
			wp_enqueue_style( 'wp-color-picker' );
	    	wp_enqueue_script( 'wp-color-picker' );
		}

		if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
			return;
	
		if ( get_user_option('rich_editing') == 'true' )
		{
			add_filter( 'mce_external_plugins', array(&$this, 'add_rich_plugins') ); // filter which is executed when the buttons are about to be loaded
			add_filter( 'mce_buttons', array(&$this, 'register_rich_buttons') ); // filter which is executed when the editor loads the plugins
		}

		load_plugin_textdomain( 'textdomain', false, dirname( plugin_basename( __FILE__ ) ) . "/languages" );
	}
	
	// --------------------------------------------------------------------------
	
	/**
	 * Defins TinyMCE rich editor js plugin
	 *
	 * @return	void
	 */
	function add_rich_plugins( $plugin_array )
	{
		$plugin_array['cosmoShortcodes'] = COSMO_TINYMCE_URI . '/plugin.js'; // The JavaScript file that is used to register the TinyMCE plugin through the TinyMCE API
		return $plugin_array;
	}
	
	// --------------------------------------------------------------------------
	
	/**
	 * Adds TinyMCE rich editor buttons
	 *
	 * @return	void
	 */
	function register_rich_buttons( $buttons )
	{
		array_push( $buttons, "|", 'cosmo_button' );
		return $buttons;
	}
	
	/**
	 * Enqueue Scripts and Styles
	 *
	 * @return	void
	 */
	function admin_init()
	{
		// css
		wp_enqueue_style( 'cosmo-popup', COSMO_TINYMCE_URI . '/css/popup.css', false, '1.0', 'all' );
		
		// js
		wp_enqueue_script( 'cosmo-ui-sortable' );
		wp_enqueue_script( 'jquery-livequery', COSMO_TINYMCE_URI . '/js/jquery.livequery.js', false, '1.1.1', false );
		wp_enqueue_script( 'jquery-appendo', COSMO_TINYMCE_URI . '/js/jquery.appendo.js', false, '1.0', false );
		wp_enqueue_script( 'base64', COSMO_TINYMCE_URI . '/js/base64.js', false, '1.0', false );
		wp_enqueue_script( 'cosmo-popup', COSMO_TINYMCE_URI . '/js/popup.js', false, '1.0', false );
		
		wp_localize_script( 'jquery', 'CosmoShortcodes', array('plugin_folder' => WP_PLUGIN_URL .'/flo-shortcodes') );

		// check if the current wp version is > then 3.9
		// we need that for the 
		global $wp_version;
		if( version_compare($wp_version, '3.9', '>=') ){
			$is_wp_39 = '1';
		}else{
			$is_wp_39 = '0';
		}

		wp_localize_script( 'cosmo-popup', 'is_wp_39', $is_wp_39 );

		add_action('wp_ajax_get_c_post_tax' , 'flo_getTaxonomies' );
		//add_action('get_c_post_tax' , 'cs_getTaxonomies' );
	}
    

}
$cosmo_shortcodes = new CosmoShortcodes();

// used for dinamicaly changing the Taxonomy options via ajax depanding on selected post type
function flo_getTaxonomies(){

	if(isset($_POST['ajaxTaxNonce'])  && isset($_POST['post_type'])){
            
        $nonce = $_POST['ajaxTaxNonce'];
		
		if ( ! wp_verify_nonce( $nonce, 'ajax-Tax-nonce' ) )
	        die ( 'Busted! Wrong Nonce');

	    $options = '<option value="">'.__('All','textdomain').'</option>';

		$taxonomies = get_object_taxonomies($_POST['post_type']);
		
		if(is_array($taxonomies) && sizeof($taxonomies)){

			foreach ($taxonomies as $key => $value) {
				if($value != 'post_format'){
					$options .= '<option value="'.$value.'">'.$value.'</option>';	
				}
				
			}
			
		}
	    
	    echo $options;

	}

    exit();
}

function cosmo_send_contact(){
	
	if(isset($_POST['action']) && $_POST['action'] == 'cosmo_send_contact'){
		$result = array();
	    
	    $tomail = $_POST['cosmo-contact-email'];
	    $frommail = '';

	    if( isset( $_POST['cosmo-name'] ) && strlen( $_POST['cosmo-name'] )  ) {
	        $name =  trim( $_POST['cosmo-name'] );
	    }else{
	        $result['contact_name'] = '<p>'. __('Error, name is required field. ','textdomain').'</p>';
	    }

	    if( isset( $_POST['cosmo-email'] ) && is_email( $_POST['cosmo-email'] ) ){
            $frommail = trim( $_POST['cosmo-email'] );
        }else{
            
            $result['contact_email'] = '<p>'. __('Error, please enter a valid email address. ','textdomain').'</p>';

        }

        $message = '';
        if( isset($_POST['cosmo-name']) ){
            $message .= __('Contact name: ','textdomain'). trim($_POST['cosmo-name'])."\n";
        }
        if( isset($_POST['cosmo-email']) ){
            $message .= __('Contact email: ','textdomain'). trim($_POST['cosmo-email'])."\n";
        }
        if( isset($_POST['cosmo-phone']) ){
            $message .= __('Contact phone: ','textdomain'). trim($_POST['cosmo-phone'])."\n\n";
        }
        if( isset($_POST['cosmo-subject']) ){
            $message .= __('Subject: ','textdomain'). trim($_POST['cosmo-subject'])."\n\n";
        }

        $message .= trim( $_POST['cosmo-message'] );

        if( is_email( $tomail ) && strlen( $tomail ) && strlen( $frommail ) &&  strlen( $name ) && strlen( $message ) ){
            $subject = __('New email from','textdomain'). ' '.get_bloginfo('name'). '.'.__('Sent via contact form.','textdomain');
            wp_mail($tomail, $subject , $message);
            $result['message'] = '<span class="success" >' . __('Email was sent successfully ','textdomain') . '</span>';
            
        }

        echo json_encode( $result );
	}
	

	exit();
}


add_action('admin_head', 'flo_add_tc_button');

function flo_add_tc_button() {
    global $typenow;
    // check user permissions
    if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
    return;
    }
    // verify the post type
    if( ! in_array( $typenow, array( 'post', 'page' ) ) )
        return;
    // check if WYSIWYG is enabled
    if ( get_user_option('rich_editing') == 'true') {
        add_filter("mce_external_plugins", "flo_add_tinymce_plugin");
        add_filter('mce_buttons', 'flo_register_my_tc_button');
    }
}

function flo_add_tinymce_plugin($plugin_array) {
    $plugin_array['flo_tc_button'] = plugins_url( 'flo-shortcodes/tinymce/plugin.js' ); // CHANGE THE BUTTON SCRIPT HERE
    return $plugin_array;
}

function flo_register_my_tc_button($buttons) {
   array_push($buttons, "flo_tc_button");
   return $buttons;
}


?>