<?php

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */

function moesia_contact_box() {
$post_id = get_the_ID();

	$screens = array( 'page' );

	foreach ( $screens as $screen ) {
		if ( 'page_contact.php' == get_post_meta( $post_id, '_wp_page_template', true ) ) {
			add_meta_box(
				'moesia_sectionid',
				__( 'Contact info', 'moesia' ),
				'moesia_meta_box_callback',
				$screen
			);
		}
	}
}
add_action( 'add_meta_boxes', 'moesia_contact_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function moesia_meta_box_callback( $post ) {
$post_id = get_the_ID();

	if ( 'page_contact.php' == get_post_meta( $post_id, '_wp_page_template', true ) ) {

		// Add an nonce field so we can check for it later.
		wp_nonce_field( 'moesia_meta_box', 'moesia_meta_box_nonce' );

		/*
		 * Use get_post_meta() to retrieve an existing value
		 * from the database and use the value for the form.
		 */
		$address = get_post_meta( $post->ID, '_moesia_map_address', true );
		$marker = get_post_meta( $post->ID, '_moesia_map_marker', true );
		$contact = get_post_meta( $post->ID, '_moesia_map_contact', true );

		echo '<p><label for="moesia_new_field">';
		_e( 'Enter your address for the map marker', 'moesia' );
		echo '</label></p>';
		echo '<input class="widefat" type="text" id="moesia_new_field" name="moesia_new_field" value="' . esc_attr( $address ) . '" size="25" />';

		echo '<p><label for="moesia_new_marker">';
		_e( 'If you want to replace the default red marker with your own custom image, add the URL for that image in this field. Just make sure your image is small enough to display nicely.', 'moesia' );
		echo '</label></p>';
		echo '<input class="widefat" type="text" id="moesia_new_marker" name="moesia_new_marker" value="' . esc_attr( $marker ) . '" size="25" />';

		echo '<p><label for="moesia_new_contact">';
		_e( 'Enter your Contact Form 7 ID', 'moesia' );
		echo '</label></p>';
		echo '<input class="widefat" type="text" id="moesia_new_contact" name="moesia_new_contact" value="' . esc_attr( $contact ) . '" size="25" />';	
	}
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function moesia_save_meta_box_data( $post_id ) {
	$post_id = get_the_ID();

		if ( 'page_contact.php' == get_post_meta( $post_id, '_wp_page_template', true ) ) {

	/*
	 * We need to verify this came from our screen and with proper authorization,
	 * because the save_post action can be triggered at other times.
	 */

	// Check if our nonce is set.
	if ( ! isset( $_POST['moesia_meta_box_nonce'] ) ) {
		return;
	}

	// Verify that the nonce is valid.
	if ( ! wp_verify_nonce( $_POST['moesia_meta_box_nonce'], 'moesia_meta_box' ) ) {
		return;
	}

	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	// Check the user's permissions.
	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

		if ( ! current_user_can( 'edit_page', $post_id ) ) {
			return;
		}

	} else {

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}
	}

	/* OK, it's safe for us to save the data now. */
	
	// Make sure that it is set.
	if ( ! isset( $_POST['moesia_new_field'] ) ) {
		return;
	}

	// Sanitize user input.
	$new_address = sanitize_text_field( $_POST['moesia_new_field'] );
	$new_marker = sanitize_text_field( $_POST['moesia_new_marker'] );
	$new_contact = sanitize_text_field( $_POST['moesia_new_contact'] );

	// Update the meta field in the database.
	update_post_meta( $post_id, '_moesia_map_address', $new_address );
	update_post_meta( $post_id, '_moesia_map_marker', $new_marker );
	update_post_meta( $post_id, '_moesia_map_contact', $new_contact );
}
}
add_action( 'save_post', 'moesia_save_meta_box_data' );