<?php
/**
 * Add Simple Metaboxes Settings
 *
 * Author: Gian MR - http://www.gianmr.com
 *
 * @since 1.0.0
 * @package Majalahpro Core
 */

/**
 * Register a meta box using a class.
 *
 * @since 1.0.0
 */
class Majalahpro_Core_Metabox_Settings {
	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_footer', array( $this, 'majalahpro_core_admin_enqueue_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'majalahpro_core_admin_enqueue_style' ) );
		add_action( 'load-post.php', array( $this, 'post_metabox_setup' ) );
		add_action( 'load-post-new.php', array( $this, 'post_metabox_setup' ) );
	}

	/**
	 * Metabox setup function
	 */
	public function post_metabox_setup() {
		add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
		add_action( 'save_post', array( $this, 'save' ), 10, 2 );
	}

	/**
	 * Register the JavaScript.
	 */
	public function majalahpro_core_admin_enqueue_scripts() {
		global $post_type;
		if ( 'post' === $post_type ) {
			?>
			<script type="text/javascript">
				(function( $ ) {
					'use strict';

					/**
					 * From this point every thing related to metabox
					 */
					$('document').ready(function(){

						$('h3.nav-tab-wrapper span:first').addClass('current');
						$('.tab-content:first').addClass('current');
						$('h3.nav-tab-wrapper span').click(function(){
							var t = $(this).attr('id');

							$('h3.nav-tab-wrapper span').removeClass('current');
							$('.tab-content').removeClass('current');

							$(this).addClass('current');
							$('#'+ t + 'C').addClass('current');
						});

						// First tab inner
						$('ul.nav-tab-wrapper li:first').addClass('current');
						$('.tab-content-inner:first').addClass('current');
						$('ul.nav-tab-wrapper li').click(function(){
							var t = $(this).attr('id');

							$('ul.nav-tab-wrapper li').removeClass('current');
							$('.tab-content-inner').removeClass('current');

							$(this).addClass('current');
							$('#'+ t + 'C').addClass('current');
						});

					});
				})( jQuery );
			</script>
			<?php
		}
	}

	/**
	 * Register Style.
	 */
	public function majalahpro_core_admin_enqueue_style() {
		global $post_type;
		if ( 'post' === $post_type ) {
			?>
			<style type="text/css">
			body.post-new-php #titlediv #title-prompt-text {display: none !important;}
			ul.nav-tab-wrapper {display:block;width: 100%;}
			ul.nav-tab-wrapper li{background: none;color: #0073aa;padding: 3px 5px;display: inline-block;cursor: pointer;margin-right:3px;}
			h3.nav-tab-wrapper span{background: none;color: #0073aa;display: inline-block;padding: 10px 15px;cursor: pointer;}
			ul.nav-tab-wrapper li.current,
			h3.nav-tab-wrapper span.current{background: #ededed;color: #222;cursor: default;}
			.tab-content-inner,
			.tab-content{display: none;}
			.tab-content-inner.current,
			.tab-content.current{display: inherit;padding-top: 20px;}
			.majalahpro-core-metabox-common-fields p {margin-bottom: 20px;}
			.majalahpro-core-metabox-common-fields input.display-block,
			.majalahpro-core-metabox-common-fields textarea.display-block{display:block;width:100%;}
			.majalahpro-core-metabox-common-fields input[type="button"].display-block {margin-top:10px;}
			.majalahpro-core-metabox-common-fields label {display: block;margin-bottom: 5px;}
			.majalahpro-core-metabox-common-fields input[disabled] {background: #ddd;}
			.majalahpro-core-metabox-common-fields .nav-tab-active,
			.majalahpro-core-metabox-common-fields .nav-tab-active:focus,
			.majalahpro-core-metabox-common-fields .nav-tab-active:focus:active,
			.majalahpro-core-metabox-common-fields .nav-tab-active:hover {border-bottom: 1px solid #ffffff !important;background: #ffffff !important;color: #000;}
			</style>
			<?php
		}
	}

	/**
	 * Adds the meta box.
	 *
	 * @param string $post_type Post type.
	 */
	public function add_meta_box( $post_type ) {
		$post_types = array( 'post' );
		if ( in_array( $post_type, $post_types, true ) ) {
			add_meta_box(
				'majalahpro_core_video_meta_metabox',
				__( 'Post Settings', 'majalahpro-core' ),
				array( $this, 'metabox_callback' ),
				$post_type,
				'advanced', // Context.
				'default' // Priority.
			);
		}
	}

	/**
	 * Save the meta box.
	 *
	 * @param int $post_id Post ID.
	 * @param int $post Post ID.
	 *
	 * @return int $post_id
	 */
	public function save( $post_id, $post ) {
		/* Verify the nonce before proceeding. */
		if ( ! isset( $_POST['majalahpro_core_video_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['majalahpro_core_video_meta_nonce'] ) ), basename( __FILE__ ) ) ) {
			return $post_id;
		}

		/* Get the post type object. */
		$post_type = get_post_type_object( $post->post_type );

		/* Check if the current user has permission to edit the post. */
		if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
			return $post_id;
		}

		/* List of meta box fields (name => meta_key) */
		$fields = array(
			'source-value' => 'MAJPRO_Source',
			'eombed-value' => 'MAJPRO_Oembed',
			'iframe-value' => 'MAJPRO_Iframe',
		);

		foreach ( $fields as $name => $meta_key ) {
			/* Check if meta box fields has a proper value */
			if ( isset( $_POST[ $name ] ) && 'N/A' !== $_POST[ $name ] ) {
				$allowed = array(
					'iframe' => array(
						'align'           => array(),
						'allowfullscreen' => array(),
						'allow'           => array(),
						'width'           => array(),
						'height'          => array(),
						'frameborder'     => array(),
						'name'            => array(),
						'src'             => array(),
						'id'              => array(),
						'class'           => array(),
						'style'           => array(),
						'scrolling'       => array(),
						'marginwidth'     => array(),
						'marginheight'    => array(),
					),
				);

				$new_meta_value = wp_kses( $_POST[ $name ], $allowed );
			} else {
				$new_meta_value = '';
			}

			/* Get the meta value of the custom field key */
			$meta_value = get_post_meta( $post_id, $meta_key, true );

			if ( ! empty( $new_meta_value ) ) {
				update_post_meta( $post_id, $meta_key, $new_meta_value );
			} else {
				/*
				 * Do you really expect to have multiple meta keys named exactly the same ('city_id')?
				 * If you don't, you can skip the third parameter from 'delete_post_meta'.
				 */

				delete_post_meta( $post_id, $meta_key );
			}
		}
	}

	/**
	 * Meta box html view
	 *
	 * @param array  $object Object Post Type.
	 * @param string $box returning string.
	 */
	public function metabox_callback( $object, $box ) {
		// Add an nonce field so we can check for it later.
		wp_nonce_field( basename( __FILE__ ), 'majalahpro_core_video_meta_nonce' );
		?>
		<div id="col-container">
			<div class="metabox-holder majalahpro-core-metabox-common-fields">

				<h3 class="nav-tab-wrapper">
					<span class="nav-tab tab-link" id="tab-1"><?php esc_attr_e( 'Post Settings:', 'majalahpro-core' ); ?></span>
					<span class="nav-tab tab-link" id="tab-2"><?php esc_attr_e( 'Video Settings:', 'majalahpro-core' ); ?></span>
				</h3>
				<div id="tab-1C" class="group tab-content">
					<p>
						<label for="opsi-source"><strong><?php esc_attr_e( 'Source URL:', 'majalahpro-core' ); ?></strong></label>
						<input type="url" class="regular-text display-block" id="opsi-source" placeholder="http://" name="source-value" value="<?php echo esc_attr( get_post_meta( $object->ID, 'MAJPRO_Source', true ) ); ?>" />
						<span class="howto"><?php esc_attr_e( 'Please insert post source URL.', 'majalahpro-core' ); ?></span>
					</p>
				</div>
				<div id="tab-2C" class="group tab-content">
					<ul class="subsubsub nav-tab-wrapper">
						<li class="nav-tab tab-link" id="tabserver-1"><?php esc_attr_e( 'Oembed', 'majalahpro-core' ); ?></li>
						<li class="nav-tab tab-link" id="tabserver-2"><?php esc_attr_e( 'Iframe', 'majalahpro-core' ); ?></li>
					</ul>
					<div class="clear"></div>
					<p id="tabserver-1C" class="innergroup tab-content-inner">
						<label for="opsi-player1"><strong><?php esc_attr_e( 'Oembed URL:', 'majalahpro-core' ); ?></strong></label>
						<input type="url" class="regular-text display-block" id="opsi-player1" placeholder="http://" name="eombed-value" value="<?php echo esc_attr( get_post_meta( $object->ID, 'MAJPRO_Oembed', true ) ); ?>" />
						<span class="howto"><?php esc_attr_e( 'Please insert full URL from youtube, vimeo or other oembed service, please see https://codex.wordpress.org/Embeds for information.', 'majalahpro-core' ); ?></span>
					</p>
					<p id="tabserver-2C" class="innergroup tab-content-inner">
						<label for="opsi-player2"><strong><?php esc_attr_e( 'Iframe Code:', 'majalahpro-core' ); ?></strong></label>
						<textarea name="iframe-value" id="opsi-player2" rows="4" cols="55" class="regular-text display-block"><?php echo esc_attr( get_post_meta( $object->ID, 'MAJPRO_Iframe', true ) ); ?></textarea>
						<span class="howto"><?php esc_attr_e( 'Please insert html iframe here, if you using oembed url, this will not display. Only support iframe code.', 'majalahpro-core' ); ?></span>
					</p>
				</div>
			</div>
		</div>
		<?php
	}

}

/* Load only if dashboard */
if ( is_admin() ) {
	new Majalahpro_Core_Metabox_Settings();
}
