<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Draly_Dropshipping_Starter_Meta_Box' ) ) {
	class Draly_Dropshipping_Starter_Meta_Box {
		protected static $initialized = false;

		public static function initialize() {
			if ( self::$initialized ) {
				return;
			}
			add_action( 'add_meta_boxes', array( __CLASS__, 'draly_dropshipping_starter_add_custom_meta_boxes' ) );
			add_action( 'save_post', array( __CLASS__, 'draly_dropshipping_starter_save_meta_box_data' ) );
			self::$initialized = true;
		}

		public static function draly_dropshipping_starter_add_custom_meta_boxes() {
			add_meta_box( 'draly_dropshipping_starter_page_options', __( 'Custom Page Options', 'draly-dropshipping-starter' ), array(
				__CLASS__,
				'render_page_meta_box'
			), 'page', 'normal', 'default' );
		}

		public static function draly_dropshipping_starter_get_meta_value( $post_id, $key, $default = '' ) {
			$value = get_post_meta( $post_id, $key, true );

			return ! empty( $value ) ? $value : $default;
		}

		public static function draly_dropshipping_starter_get_sidebar_options() {
			global $wp_registered_sidebars;
			$sidebars = array();
			foreach ( $wp_registered_sidebars as $id => $sidebar ) {
				$sidebars[ $id ] = $sidebar['name'];
			}

			return $sidebars;
		}

		public static function render_page_meta_box( $post ) {
			wp_nonce_field( 'draly_dropshipping_starter_save_meta_box', 'draly_dropshipping_starter_meta_box_nonce' );

			$page_sidebar_layout = self::draly_dropshipping_starter_get_meta_value( $post->ID, 'page_sidebar_layout', 'left' );
			$page_sidebar        = self::draly_dropshipping_starter_get_meta_value( $post->ID, 'page_sidebar', 'widget-area' );
			$page_extra_class    = self::draly_dropshipping_starter_get_meta_value( $post->ID, 'page_extra_class' );
			$sidebars            = self::draly_dropshipping_starter_get_sidebar_options();
			?>
            <p class="page_sidebar_layout">
                <label><?php echo esc_html__( 'Sidebar Layout', 'draly-dropshipping-starter' ); ?></label>
                <select name="page_sidebar_layout" class="widefat">
                    <option value="full" <?php selected( $page_sidebar_layout, 'full' ); ?>><?php echo esc_html__( 'Full', 'draly-dropshipping-starter' ) ?></option>
                    <option value="left" <?php selected( $page_sidebar_layout, 'left' ); ?>><?php echo esc_html__( 'Left', 'draly-dropshipping-starter' ) ?></option>
                    <option value="right" <?php selected( $page_sidebar_layout, 'right' ); ?>><?php echo esc_html__( 'Right', 'draly-dropshipping-starter' ) ?></option>
                </select>
            </p>

            <p class="page_sidebar"><label><?php echo esc_html__( 'Sidebar', 'draly-dropshipping-starter' ); ?></label>
                <select name="page_sidebar" class="widefat">
					<?php foreach ( $sidebars as $id => $name ) : ?>
                        <option value="<?php echo esc_attr( $id ); ?>" <?php selected( $page_sidebar, $id ); ?>><?php echo esc_html( $name ); ?></option>
					<?php endforeach; ?>
                </select>
            </p>

            <p><label><?php echo esc_html__( 'Extra Page Class', 'draly-dropshipping-starter' ); ?></label>
                <input type="text" name="page_extra_class" value="<?php echo esc_attr( $page_extra_class ); ?>"
                       class="widefat"/></p>
			<?php
		}

		public static function draly_dropshipping_starter_save_meta_box_data( $post_id ) {
			if ( ! isset( $_POST['draly_dropshipping_starter_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['draly_dropshipping_starter_meta_box_nonce'], 'draly_dropshipping_starter_save_meta_box' ) ) {
				return;
			}
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
				return;
			}
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
				return;
			}

			$fields = array_merge(
				[
					// page
					'page_sidebar_layout',
					'page_sidebar',
					'page_extra_class',
				]
			);
			foreach ( $fields as $field ) {
				if ( isset( $_POST[ $field ] ) ) {
					$value = $_POST[ $field ];
					switch ( $field ) {
						case 'page_extra_class':
							$value = sanitize_textarea_field( $value );
							break;

						default:
							$value = sanitize_text_field( $value );
							break;
					}
					update_post_meta( $post_id, $field, $value );
				} else {
					delete_post_meta( $post_id, $field );
				}
			}
		}

	}
}