<?php

/////////////////////////////////////////////////
//                                             //
//        hosek Admin Pages Functions         //
//                                             //
/////////////////////////////////////////////////


// Remove default text editor 
	add_action( 'admin_init', 'hide_admin_editor' );

	function hide_admin_editor() {
		if( !isset( $_GET['post'] ) && !isset( $_GET['post_ID'] ) ) return;
		
		$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
		
		$template_file = get_post_meta($post_id, '_wp_page_template', true);
		
		if ($template_file == 'page-homepage.php') {
			/*remove_post_type_support('page', 'editor');*/
			remove_post_type_support('page', 'thumbnail');
		} 
		elseif ($template_file == 'page-subpage.php') {
			/*remove_post_type_support('page', 'editor');*/
			/*remove_post_type_support('page', 'thumbnail');*/
		}
	}


// Register custom meta boxes
	add_action( 'add_meta_boxes_page', 'add_page_meta_boxes' );


// Implement custom meta boxes
	function add_page_meta_boxes() {
		global $post;
		if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
			add_meta_box( 'homepage_slider_text_section', 'Slider Text Section', 'display_homepage_slider_text_section', 'page', 'normal', 'high' );
			add_meta_box( 'homepage_services_section', 'Services Section', 'display_homepage_services_section', 'page', 'normal', 'high' );
			/*add_meta_box( 'homepage_appointment_section', 'Appointment Section', 'display_homepage_appointment_section', 'page', 'normal', 'high' );*/		
		}
		elseif ( 'page-subpage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
			/*add_meta_box( 'subpage_appointment_section', 'Appointment Section', 'display_subpage_appointment_section', 'page', 'normal', 'high' );*/
		}
	}


// Define custom meta box - HOMEPAGE Slider Text Section
	function display_homepage_slider_text_section ( $page ) {
		$homepage_slider_text_header = get_post_meta( $page->ID, 'homepage_slider_text_header', true );
		$homepage_slider_text_subheader = get_post_meta( $page->ID, 'homepage_slider_text_subheader', true );
		$homepage_slider_text_button = get_post_meta( $page->ID, 'homepage_slider_text_button', true );
		?>
		<p><input style="width: 100%;" type="text" name="homepage_slider_text_header" id="homepage_slider_text_header" value="<?php echo $homepage_slider_text_header; ?>" placeholder="Header" /></p>
        <p><input style="width: 100%;" type="text" name="homepage_slider_text_subheader" id="homepage_slider_text_subheader" value="<?php echo $homepage_slider_text_subheader; ?>" placeholder="Sub-header" /></p>
		<p><input style="width: 100%;" type="text" name="homepage_slider_text_button" id="homepage_slider_text_button" value="<?php echo $homepage_slider_text_button; ?>" placeholder="Call to action button" /></p>
        <?php
	}

// Define custom meta box - HOMEPAGE Services Section
	function display_homepage_services_section ( $page ) {
		$homepage_first_service_header = get_post_meta( $page->ID, 'homepage_first_service_header', true );
		$homepage_first_service_text = get_post_meta( $page->ID, 'homepage_first_service_text', true );
		$homepage_second_service_header = get_post_meta( $page->ID, 'homepage_second_service_header', true );
		$homepage_second_service_text = get_post_meta( $page->ID, 'homepage_second_service_text', true );
		$homepage_third_service_header = get_post_meta( $page->ID, 'homepage_third_service_header', true );
		$homepage_third_service_text = get_post_meta( $page->ID, 'homepage_third_service_text', true );
		?>
        <p><input style="width: 100%;" type="text" name="homepage_first_service_header" id="homepage_first_service_header" value="<?php echo $homepage_first_service_header; ?>" placeholder="First service header" /></p>
        <?php wp_editor($homepage_first_service_text, 'homepage_first_service_text', array("textarea_name" => 'homepage_first_service_text', "textarea_rows" => 7, 'media_buttons' => false));?>
        <p><input style="width: 100%;" type="text" name="homepage_second_service_header" id="homepage_second_service_header" value="<?php echo $homepage_second_service_header; ?>" placeholder="Second service header" /></p>
        <?php wp_editor($homepage_second_service_text, 'homepage_second_service_text', array("textarea_name" => 'homepage_second_service_text', "textarea_rows" => 7, 'media_buttons' => false));?>
        <p><input style="width: 100%;" type="text" name="homepage_third_service_header" id="homepage_third_service_header" value="<?php echo $homepage_third_service_header; ?>" placeholder="Third service header" /></p>
        <?php wp_editor($homepage_third_service_text, 'homepage_third_service_text', array("textarea_name" => 'homepage_third_service_text', "textarea_rows" => 7, 'media_buttons' => false));
	}
	
/*// Define custom meta box - HOMEPAGE Appointment Section
	function display_homepage_appointment_section ( $page ) {
		$homepage_appointment_text = get_post_meta( $page->ID, 'homepage_appointment_text', true );
		?>
		<?php wp_editor($homepage_appointment_text, 'homepage_appointment_text', array("textarea_name" => 'homepage_appointment_text', "textarea_rows" => 7, 'media_buttons' => false));
	}*/

/*// Define custom meta box - SUBPAGE Appointment Section
	function display_subpage_appointment_section ( $page ) {
		$subpage_appointment_text = get_post_meta( $page->ID, 'subpage_appointment_text', true );
		?>
		<?php wp_editor($subpage_appointment_text, 'subpage_appointment_text', array("textarea_name" => 'subpage_appointment_text', "textarea_rows" => 7, 'media_buttons' => false));
	}*/


// Implement save post function
	function save_custom_meta_boxes( $post_id, $post ) {
		if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
		
		$save_elements = array();		
		if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
			
			$save_elements = array('homepage_slider_text_header' 	         => 'noautop',
								   'homepage_slider_text_subheader' 	     => 'noautop',
								   'homepage_slider_text_button'      		 => 'noautop',
								   'homepage_first_service_header'       	 => 'noautop',
								   'homepage_first_service_text'        	 => 'wpautop',
								   'homepage_second_service_header'          => 'noautop',
								   'homepage_second_service_text'         	 => 'wpautop',
								   'homepage_third_service_header'     		 => 'noautop',
								   'homepage_third_service_text'      		 => 'wpautop',
								   /*'homepage_appointment_text'       		 => 'wpautop',*/
								   );							   
		}
		elseif ( 'page-subpage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
			
			/*$save_elements = array('subpage_appointment_text'      			 => 'wpautop',			
								   );*/							   
		}
		
				
		foreach ( $save_elements as $save_elem_name => $autop ) {
				
			if ( isset( $_POST[$save_elem_name] ) ) {
				$save_elem_data = get_post_meta( $post_id, $save_elem_name, true );
				
				if ( 'wpautop' == $autop ) {
					$new_save_elem_data = wpautop($_POST[$save_elem_name]);
				} else {
					$new_save_elem_data = $_POST[$save_elem_name];
				}
				
				if ( $new_save_elem_data != $save_elem_data )
					update_post_meta( $post_id, $save_elem_name, $new_save_elem_data );
				elseif ( '' == $new_save_elem_data && $save_elem_data )
				delete_post_meta( $post_id, $save_elem_name, $save_elem_data );
			}
		}
	}


// Register save post function
add_action( 'save_post', 'save_custom_meta_boxes', 10, 2 );


// Save post revisions
	function save_revisions_custom_meta_boxes( $post_id ) {
	
		$parent_id = wp_is_post_revision( $post_id );
	
		if ( $parent_id ) {
			$parent  = get_post( $parent_id );
			
			$revision_elements = array('homepage_slider_text_header',
									   'homepage_slider_text_subheader',
									   'homepage_slider_text_button',
									   'homepage_first_service_header',
									   'homepage_first_service_text',
									   'homepage_second_service_header',
									   'homepage_second_service_text',
									   'homepage_third_service_header',
									   'homepage_third_service_text',
									   /*'homepage_appointment_text',
									   'subpage_appointment_text',*/
									   );
									   
			foreach ( $revision_elements as $rev_elem_name ) {
				
				$rev_elem_data = get_post_meta( $parent->ID, $rev_elem_name, true );
				if ( false !== $rev_elem_data )
					add_metadata( 'post', $post_id, $rev_elem_name, $rev_elem_data );
			}	
		}
	}

// Register save revision function
add_action( 'save_post', 'save_revisions_custom_meta_boxes', 10, 2);


// Define revision fields
	function register_revision_fields( $fields ) {
		
		// Homepage
		$fields['homepage_slider_text_header'] = 'Homepage - Slider Text Header';
		$fields['homepage_slider_text_subheader'] = 'Homepage - Slider Text Subheader';
		$fields['homepage_slider_text_button'] = 'Homepage - Slider Text Button';
		$fields['homepage_first_service_header'] = 'Homepage - First Service Header';
		$fields['homepage_first_service_text'] = 'Homepage - First Service Text';
		$fields['homepage_second_service_header'] = 'Homepage - Second Service Header';
		$fields['homepage_second_service_text'] = 'Homepage - Second Service Text';
		$fields['homepage_third_service_header'] = 'Homepage - Third Service Header';
		$fields['homepage_third_service_text'] = 'Homepage - Third Service Text';
		/*$fields['homepage_appointment_text'] = 'Homepage - Appointment Text';	
				
		// Subpage
		$fields['subpage_appointment_text'] = 'Subpage - Slider Text Header';*/
		
		// Homepage
		add_filter('_wp_post_revision_field_homepage_slider_text_header', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_slider_text_subheader', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_homepage_slider_text_button', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_first_service_header', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_first_service_text', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_second_service_header', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_second_service_text', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_third_service_header', 'revision_field', 10, 4);
		add_filter('_wp_post_revision_field_homepage_third_service_text', 'revision_field', 10, 4);
		/*add_filter('_wp_post_revision_field_homepage_appointment_text', 'revision_field', 10, 4);		
		
		// Subpage
		add_filter('_wp_post_revision_field_subpage_appointment_text', 'revision_field', 10, 4);*/
		
		return $fields;
	}


// Register revision fields
add_filter( '_wp_post_revision_fields',  'register_revision_fields', 10, 2  );


// Get revision data
	function revision_field( $value, $field , $post = null, $direction = false) {
		$post_id = $post->ID;
		return get_metadata( 'post', $post_id, $field, true );
	}

	
// Implement revision restore
	function restore_revision( $post_id, $revision_id ) {
		$post     = get_post( $post_id );
		$revision = get_post( $revision_id );
		
		$restore_elements = array('homepage_slider_text_header',
								  'homepage_slider_text_subheader',
								  'homepage_slider_text_button',
								  'homepage_first_service_header',
								  'homepage_first_service_text',
								  'homepage_second_service_header',
								  'homepage_second_service_text',
								  'homepage_third_service_header',
								  'homepage_third_service_text',
								  /*'homepage_appointment_text',
								  'subpage_appointment_text',*/
								  );
		
		foreach ( $restore_elements as $rest_elem_name ) {
			
			$rest_elem_data  = get_metadata( 'post', $revision->ID, $rest_elem_name, true );
			if ( !$rest_elem_data )
				delete_post_meta( $post_id, $rest_elem_name );
			else
				update_post_meta( $post_id, $rest_elem_name, $rest_elem_data );
		}
	}


// Register revision restore
add_action( 'wp_restore_post_revision', 'restore_revision', 10, 2 );