<?php
/**
 * This class handles all functionality for the page builder buttons from the admin edit pages
 *
 * @category   Pagebuilder
 * @package    ZnFramework
 * @author     Balasa Sorin Stefan ( Zauan )
 * @copyright  Copyright (c) Balasa Sorin Stefan
 * @link       http://themeforest.net/user/hogash
 */

class ZnPageBuilderAdmin{

	function __construct() {

		$this->zn_wrap_editor();

		// Enqueue the page builder javascript
		add_action( 'admin_enqueue_scripts', array(&$this, 'zn_register_scripts') );
		add_filter( 'zn_metabox_elements', array(&$this, 'zn_meta_pb_fields') );
		add_filter( 'zn_metabox_locations', array(&$this, 'zn_meta_pb_locations') );

	}

	function zn_meta_pb_fields( $elements ){
		// Page description
		$elements[] = array ( 
							"name"	=> 'zn_page_builder_status',
							"slug" => array( 'page_options' , 'portfolio_options', 'post_options' ),
							"id" => "zn_page_builder_status",
							"std" => false,
							"type" => "hidden",
							"class" => "zn_pb_input_status"
						);

		return $elements;
	}

	function zn_meta_pb_locations( $locations ){
		// Page description
		$locations[] = array( 	'title' =>  'PB', 'slug'=>'zn_pb_options', 'page'=>array('page','portfolio','post'), 'context'=>'side', 'priority'=>'default' );
		
		return $locations;
	}

/**
 *  
 * Wrap wordpress editor with our wrapper so we can add the buttons
 * 
 */
	function zn_wrap_editor() {
		add_action( 'edit_form_after_title',array( &$this, 'zn_wrap_editor_start' ));
		add_action( 'edit_form_after_editor', array($this, 'zn_wrap_editor_end' ), 1); 
	}

/**
 *  
 * Script that handles the ajax requests for the admin editor
 * 
 */
	function zn_register_scripts( $hook ){

		if ( $hook == 'post.php' || $hook == 'post-new.php' ) {
			wp_enqueue_script( 'zn_page_builder', FW_URL .'/pagebuilder/assets/js/zn_page_builder.js','jquery','',true );
		}
		
	}

/**
 *  
 * Add the start of our wrapper
 * @uses $post
 * 
 */
	function zn_wrap_editor_start() {

		global $post;

        $enable_text 	= 'Enable page builder';
        $disable_text  	= 'Disable page builder';
        $status 		= get_post_meta( $post->ID, 'zn_page_builder_status', true );
        $active_text  = $status == 'enabled' ? $disable_text : $enable_text;
        $button_css   = $status == 'enabled' ? "" : "style=\"display:none;\"";
		$preview_link = get_permalink( $post->ID );
		$preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'zn_pb_edit', 'true', $preview_link ) ) );

		echo '<div class="zn_pb_buttons">';
			wp_nonce_field( 'ZnNonce', 'ZnNonce', false, true );
			echo '<div type="button" name="publish" id="zn_enable_pb" data-status="'.$status.'" data-postid="'.$post->ID.'" data-active-text="'.$disable_text.'" data-inactive-text="'.$enable_text.'" class="button button-zn_save button-large"><span class="spinner"></span> <span class="zn_bt_text">'.$active_text.'</span></div>';
			echo '<a href="'.$preview_link.'" id="zn_edit_page" class="button button-primary button-large zn_pb_button" '.$button_css.'>Edit this page with pagebuilder</a>';
		echo '</div>';
		echo '<div class="zn_editor_wrap">';

	}

/**
 *  
 * Close our wrapper
 * 
 */
	function zn_wrap_editor_end(){
		echo '</div>';
	}

}
?>