<?php
/**
 * Metabox definitions
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

add_action( 'add_meta_boxes', 'multimagazine_page_metaboxes', 10, 2 );
add_action( 'save_post', 'multimagazine_save_metabox' );

/**
 * Adds metaboxes to page editor
 */
function multimagazine_page_metaboxes() {
	$post_types = array( 'page', 'post' );
	$post_types = apply_filters( 'multimagazine_display_metabox_post_types', $post_types );
	foreach ( $post_types as $type ) {
		add_meta_box( 'display-options', esc_html__( 'Multi Display Options', 'multimagazine' ), 'multimagazine_draw_display_metabox', $type, 'side', 'default' );
	}
}

/**
 * Renders the metabox
 *
 * @param object $post Currently edited post object.
 */
function multimagazine_draw_display_metabox( $post ) {
	global $post;
	$data                                    = get_post_custom( $post->ID );
	$multimagazine_show_title                = isset( $data['multi_show_title'] ) ? $data['multi_show_title'][0] : 0;
	$multimagazine_show_footer               = isset( $data['multi_show_footer'] ) ? $data['multi_show_footer'][0] : 0;
	$multimagazine_show_above_footer_widgets = isset( $data['multi_show_above_footer_widgets'] ) ? $data['multi_show_above_footer_widgets'][0] : 0;
	$show_breadcrumbs                        = isset( $data['multi_show_breadcrumbs'] ) ? $data['multi_show_breadcrumbs'][0] : '';
	$multimagazine_show_featured_image       = isset( $data['multi_show_featured_image'] ) ? $data['multi_show_featured_image'][0] : '';
	$multimagazine_sidebar_position          = isset( $data['multi_sidebar_position'] ) ? $data['multi_sidebar_position'][0] : 0;
	$multimagazine_sidebar                   = isset( $data['multi_custom_sidebar'] ) ? $data['multi_custom_sidebar'][0] : 0;
	$layout                                  = isset( $data['multi_post_layout'] ) ? $data['multi_post_layout'][0] : '';
	$featured                                = isset( $data['multi_featured'] ) ? $data['multi_featured'][0] : false;
	$multimagazine_show_top_slider           = isset( $data['multi_show_top_slider'] ) ? $data['multi_show_top_slider'][0] : '';
	$header_style                            = isset( $data['multi_header_style'] ) ? $data['multi_header_style'][0] : 0;
	$show_subheader                          = isset( $data['multi_show_subheader'] ) ? $data['multi_show_subheader'][0] : 0;
	$bottom_margin                           = isset( $data['multi_content_bottom_margin'] ) ? $data['multi_content_bottom_margin'][0] : '';

	/* Revolution Slider */
	if ( class_exists( 'RevSliderFront' ) ) {
		$revolution_slider = isset( $data['multi_revolution_slider'] ) ? $data['multi_revolution_slider'][0] : '';
	}

	wp_nonce_field( 'multi_display_metabox_nonce', 'display_metabox_nonce' );
	?>
	<div class="components-base-control">
	<div class="components-base-control__field"><label for="hide_subheader" class="components-base-control__label"><?php esc_html_e( 'Site subheader', 'multimagazine' ); ?></label> <select name="hide_subheader" id="hide_subheader" class="widefat">
		<option value="" <?php selected( '', $show_subheader ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $show_subheader ); ?>><?php esc_html_e( 'On', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $show_subheader ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="show_title" class="components-base-control__label"><?php esc_html_e( 'Title location', 'multimagazine' ); ?></label> <select name="show_title" id="show_title" class="widefat">
		<option value="" <?php selected( '', $multimagazine_show_title ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_show_title ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $multimagazine_show_title ); ?>><?php esc_html_e( 'Post header', 'multimagazine' ); ?></option>
		<option value="left middle subheader" <?php selected( 'left middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'left center', 'multimagazine' ); ?></option>
		<option value="center middle subheader" <?php selected( 'center middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'center center', 'multimagazine' ); ?></option>
		<option value="right middle subheader" <?php selected( 'right middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'right center', 'multimagazine' ); ?></option>
		<option value="left bottom subheader" <?php selected( 'left bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'left bottom', 'multimagazine' ); ?></option>
		<option value="center bottom subheader" <?php selected( 'center bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'center bottom', 'multimagazine' ); ?></option>
		<option value="right bottom subheader" <?php selected( 'right bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'right bottom', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="show_breadcrumbs" class="components-base-control__label"><?php esc_html_e( 'Breadcrumbs location', 'multimagazine' ); ?></label> <select name="show_breadcrumbs" id="show_breadcrumbs" class="widefat">
		<option value="" <?php selected( '', $show_breadcrumbs ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $show_breadcrumbs ); ?>><?php esc_html_e( 'Default location', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $show_breadcrumbs ); ?>><?php esc_html_e( "Don't show", 'multimagazine' ); ?></option>
		<option value="left middle subheader" <?php selected( 'left middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'left center', 'multimagazine' ); ?></option>
		<option value="center middle subheader" <?php selected( 'center middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'center center', 'multimagazine' ); ?></option>
		<option value="right middle subheader" <?php selected( 'right middle subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'right center', 'multimagazine' ); ?></option>
		<option value="left bottom subheader" <?php selected( 'left bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'left bottom', 'multimagazine' ); ?></option>
		<option value="center bottom subheader" <?php selected( 'center bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'center bottom', 'multimagazine' ); ?></option>
		<option value="right bottom subheader" <?php selected( 'right bottom subheader', $multimagazine_show_title ); ?>><?php esc_html_e( 'Subheader', 'multimagazine' ); ?>: <?php esc_html_e( 'right bottom', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="show_top_slider" class="components-base-control__label"><?php esc_html_e( 'Top slider', 'multimagazine' ); ?></label> <select name="show_top_slider" id="show_top_slider" class="widefat">
		<option value="" <?php selected( '', $multimagazine_show_top_slider ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="featured" <?php selected( 'featured', $multimagazine_show_top_slider ); ?>><?php esc_html_e( 'Featured posts', 'multimagazine' ); ?></option>
		<option value="related" <?php selected( 'related', $multimagazine_show_top_slider ); ?>><?php esc_html_e( 'Related posts', 'multimagazine' ); ?></option>
		<option value="latest" <?php selected( 'latest', $multimagazine_show_top_slider ); ?>><?php esc_html_e( 'Latest posts', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_show_top_slider ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="hide_featured_image" class="components-base-control__label"><?php esc_html_e( 'Featured image', 'multimagazine' ); ?></label> <select name="hide_featured_image" id="hide_featured_image" class="widefat">
		<option value="" <?php selected( '', $multimagazine_show_featured_image ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $multimagazine_show_featured_image ); ?>><?php esc_html_e( 'On', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_show_featured_image ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="sidebar_position" class="components-base-control__label"><?php esc_html_e( 'Sidebar position', 'multimagazine' ); ?></label> <select name="sidebar_position" id="sidebar_position" class="widefat">
		<option value="" <?php selected( '', $multimagazine_sidebar_position ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="right" <?php selected( 'right', $multimagazine_sidebar_position ); ?>><?php esc_html_e( 'Right', 'multimagazine' ); ?></option>
		<option value="left" <?php selected( 'left', $multimagazine_sidebar_position ); ?>><?php esc_html_e( 'Left', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_sidebar_position ); ?>><?php esc_html_e( "Don't show", 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="sidebar_selection" class="components-base-control__label"><?php esc_html_e( 'Sidebar widget area', 'multimagazine' ); ?></label> <select name="sidebar_selection" id="sidebar_selection" class="widefat">
		<?php
		if ( ! empty( $GLOBALS['wp_registered_sidebars'] ) ) :
			foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) :
				?>
	<option value="<?php echo esc_attr( $sidebar['id'] ); ?>" <?php selected( $multimagazine_sidebar, $sidebar['id'] ); ?>><?php echo esc_html( $sidebar['name'] ); ?></option>
				<?php
			endforeach;
		endif;
		?>
	</select></div>
	<div class="components-base-control__field"><label for="post_layout"><?php esc_html_e( 'Post layout', 'multimagazine' ); ?></label> <select name="post_layout" id="post_layout" class="widefat">
		<option value="" <?php selected( '', $layout ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="standard" <?php selected( 'standard', $layout ); ?>><?php esc_html_e( 'Standard', 'multimagazine' ); ?></option>
		<option value="full-width-header" <?php selected( 'full-width-header', $layout ); ?>><?php esc_html_e( 'Full width header', 'multimagazine' ); ?></option>
		<option value="full-width-header-image" <?php selected( 'full-width-header-image', $layout ); ?>><?php esc_html_e( 'Full width header with image', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="transparent_header" class="components-base-control__label"><?php esc_html_e( 'Site header', 'multimagazine' ); ?></label> <select name="transparent_header" id="transparent_header" class="widefat">
		<option value="" <?php selected( '', $header_style ); ?>><?php esc_html_e( 'On (default)', 'multimagazine' ); ?></option>
		<option value="header-transparent" <?php selected( 'header-transparent', $header_style ); ?>><?php esc_html_e( 'Fully transparent', 'multimagazine' ); ?></option>
		<option value="header-transparent header-transparent-mobile" <?php selected( 'header-transparent header-transparent-mobile', $header_style ); ?>><?php esc_html_e( 'Fully transparent (incl. mobile)', 'multimagazine' ); ?></option>
		<option value="header-transparent with-bg" <?php selected( 'header-transparent with-bg', $header_style ); ?>><?php esc_html_e( 'Transparent with background', 'multimagazine' ); ?></option>
		<option value="header-transparent with-shadow" <?php selected( 'header-transparent with-shadow', $header_style ); ?>><?php esc_html_e( 'Transparent with shadow', 'multimagazine' ); ?></option>
		<option value="header-transparent with-border" <?php selected( 'header-transparent with-border', $header_style ); ?>><?php esc_html_e( 'Transparent with border', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $header_style ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="show_above_footer_widgets" class="components-base-control__label"><?php esc_html_e( 'Above footer widget area', 'multimagazine' ); ?></label> <select name="show_above_footer_widgets" id="show_above_footer_widgets" class="widefat">
		<option value="" <?php selected( '', $multimagazine_show_above_footer_widgets ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $multimagazine_show_above_footer_widgets ); ?>><?php esc_html_e( 'On', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_show_above_footer_widgets ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="show_footer" class="components-base-control__label"><?php esc_html_e( 'Site footer', 'multimagazine' ); ?></label> <select name="show_footer" id="show_footer" class="widefat">
		<option value="" <?php selected( '', $multimagazine_show_footer ); ?>><?php esc_html_e( 'As set in Theme Customizer (default)', 'multimagazine' ); ?></option>
		<option value="on" <?php selected( 'on', $multimagazine_show_footer ); ?>><?php esc_html_e( 'On', 'multimagazine' ); ?></option>
		<option value="off" <?php selected( 'off', $multimagazine_show_footer ); ?>><?php esc_html_e( 'Off', 'multimagazine' ); ?></option>
	</select></div>
	<div class="components-base-control__field"><label for="bottom_margin" class="components-base-control__label"><?php esc_html_e( 'Content bottom margin', 'multimagazine' ); ?></label>
			<input name="bottom_margin" id="bottom_margin" value="<?php echo esc_attr( $bottom_margin ); ?>" class="widefat" type="number" min="0">
	</div>
	<div class="components-base-control__field"><label><input type="checkbox" value="1" name="featured" id="featured" <?php checked( $featured, 1 ); ?>><?php esc_html_e( 'Set post as featured', 'multimagazine' ); ?></label></div>

	<?php
	/* Revolution Slider */
	if ( class_exists( 'RevSliderFront' ) ) {
		$slider = new RevSlider();
		/* Array of current slider "alias" names */
		$arr_sliders = $slider->getArrSlidersShort();
		?>
		<div class="components-base-control__field"><label for="revolution_slider" class="components-base-control__label"><?php esc_html_e( 'Revolution Slider', 'multimagazine' ); ?></label> <select name="revolution_slider" id="revolution_slider" class="widefat">
			<option value="" <?php selected( '', $revolution_slider ); ?>><?php esc_html_e( 'None', 'multimagazine' ); ?></option>
			<?php
			foreach ( $arr_sliders as $rev_id => $rev_slider ) {
				?>
				<option value="<?php echo esc_attr( $rev_id ); ?>" <?php selected( $revolution_slider, $rev_id ); ?>><?php echo esc_html( $rev_slider ); ?></option>
				<?php
			}
			?>
		</select></div>
		<?php
	}
	/* End of Revolution Slider */
	?>
	</div>
	<?php
}

/**
 * Saves text metabox value
 *
 * @param integer $post_id ID of currently edited post.
 * @param string  $meta_key The name of a custom field to save the value to.
 * @param string  $value The new value of the custom field.
 */
function multimagazine_save_meta_textfield( $post_id, $meta_key, $value ) {
	if ( '' === $value && get_post_meta( $post_id, $meta_key, true ) ) {
		delete_post_meta( $post_id, $meta_key );
	}
	if ( '' !== $value ) {
		update_post_meta( $post_id, $meta_key, sanitize_text_field( wp_unslash( $value ) ) );
	}
}

/**
 * Saves number metabox value
 *
 * @param integer $post_id ID of currently edited post.
 * @param string  $meta_key The name of a custom field to save the value to.
 * @param string  $value The new value of the custom field.
 */
function multimagazine_save_meta_number( $post_id, $meta_key, $value ) {
	if ( '' === $value && '' !== get_post_meta( $post_id, $meta_key, true ) ) {
		delete_post_meta( $post_id, $meta_key );
	}
	if ( '' !== $value ) {
		update_post_meta( $post_id, $meta_key, absint( wp_unslash( $value ) ) );
	}
}

/**
 * Saves all the metabox values
 *
 * @param integer $post_id ID of currently edited post.
 */
function multimagazine_save_metabox( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	if ( ! isset( $_POST['display_metabox_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['display_metabox_nonce'] ) ), 'multi_display_metabox_nonce' ) ) {
		return;
	}
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}

	if ( isset( $_POST['show_title'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_title', sanitize_text_field( wp_unslash( $_POST['show_title'] ) ) );
	}
	if ( isset( $_POST['show_breadcrumbs'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_breadcrumbs', sanitize_text_field( wp_unslash( $_POST['show_breadcrumbs'] ) ) );
	}
	if ( isset( $_POST['show_top_slider'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_top_slider', sanitize_text_field( wp_unslash( $_POST['show_top_slider'] ) ) );
	}
	if ( isset( $_POST['hide_featured_image'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_featured_image', sanitize_text_field( wp_unslash( $_POST['hide_featured_image'] ) ) );
	}
	if ( isset( $_POST['sidebar_position'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_sidebar_position', sanitize_text_field( wp_unslash( $_POST['sidebar_position'] ) ) );
	}
	if ( isset( $_POST['sidebar_selection'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_custom_sidebar', sanitize_text_field( wp_unslash( $_POST['sidebar_selection'] ) ) );
	}
	if ( isset( $_POST['post_layout'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_post_layout', sanitize_text_field( wp_unslash( $_POST['post_layout'] ) ) );
	}
	if ( isset( $_POST['transparent_header'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_header_style', sanitize_text_field( wp_unslash( $_POST['transparent_header'] ) ) );
	}
	if ( isset( $_POST['hide_subheader'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_subheader', sanitize_text_field( wp_unslash( $_POST['hide_subheader'] ) ) );
	}
	if ( isset( $_POST['bottom_margin'] ) ) {
		multimagazine_save_meta_number( $post_id, 'multi_content_bottom_margin', sanitize_text_field( wp_unslash( $_POST['bottom_margin'] ) ) );
	}
	if ( isset( $_POST['show_above_footer_widgets'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_above_footer_widgets', sanitize_text_field( wp_unslash( $_POST['show_above_footer_widgets'] ) ) );
	}
	if ( isset( $_POST['show_footer'] ) ) {
		multimagazine_save_meta_textfield( $post_id, 'multi_show_footer', sanitize_text_field( wp_unslash( $_POST['show_footer'] ) ) );
	}

	if ( isset( $_POST['featured'] ) ) {
		update_post_meta( $post_id, 'multi_featured', 1 );
	} else {
		delete_post_meta( $post_id, 'multi_featured' );
	}

	/* Revolution Slider */
	if ( class_exists( 'RevSliderFront' ) ) {
		if ( isset( $_POST['revolution_slider'] ) ) {
			update_post_meta( $post_id, 'multi_revolution_slider', sanitize_text_field( wp_unslash( $_POST['revolution_slider'] ) ) );
		}
	}
	/* End of Revolution Slider */
}
