<?php

/**
 * CMB2 Genesis Settings Metabox
 *
 * To fetch these options, use `genesis_get_option()`, e.g.
 *      $color = genesis_get_option( 'test_colorpicker' );
 *
 * @version 0.1.0
 */
class Myprefix_Genesis_Settings_Metabox {

	/**
	 * Option key. Could maybe be 'genesis-seo-settings', or other section?
	 *
	 * @var string
	 */
	protected $key = 'genesis-settings';

	/**
	 * The admin page slug.
	 *
	 * @var string
	 */
	protected $admin_page = 'genesis';

	/**
	 * Options page metabox id
	 *
	 * @var string
	 */
	protected $metabox_id = 'myprefix_genesis_settings';

	/**
	 * Admin page hook
	 *
	 * @var string
	 */
	protected $admin_hook = 'toplevel_page_genesis';

	/**
	 * Holds an instance of CMB2
	 *
	 * @var CMB2
	 */
	protected $cmb = null;

	/**
	 * Holds an instance of the object
	 *
	 * @var Myprefix_Genesis_Settings_Metabox
	 */
	protected static $instance = null;

	/**
	 * Returns the running object
	 *
	 * @return Myprefix_Genesis_Settings_Metabox
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
			self::$instance->hooks();
		}

		return self::$instance;
	}

	/**
	 * Constructor
	 *
	 * @since 0.1.0
	 */
	protected function __construct() {
	}

	/**
	 * Initiate our hooks
	 *
	 * @since 0.1.0
	 */
	public function hooks() {
		add_action( 'admin_menu', array( $this, 'admin_hooks' ) );
		add_action( 'cmb2_admin_init', array( $this, 'init_metabox' ) );
	}

	/**
	 * Add menu options page
	 *
	 * @since 0.1.0
	 */
	public function admin_hooks() {
		// Include CMB CSS in the head to avoid FOUC.
		add_action( "admin_print_styles-{$this->admin_hook}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );

		// Hook into the genesis cpt setttings save and add in the CMB2 sanitized values.
		add_filter( "sanitize_option_{$this->key}", array( $this, 'add_sanitized_values' ), 999 );

		// Hook up our Genesis metabox.
		add_action( 'genesis_theme_settings_metaboxes', array( $this, 'add_meta_box' ) );
	}


	/**
	 * Hook up our Genesis metabox.
	 *
	 * @since 0.1.0
	 */
	public function add_meta_box() {
		$cmb = $this->init_metabox();
		add_meta_box(
			$cmb->cmb_id,
			$cmb->prop( 'title' ),
			array( $this, 'output_metabox' ),
			$this->admin_hook,
			$cmb->prop( 'context' ),
			$cmb->prop( 'priority' )
		);
	}

	/**
	 * Output our Genesis metabox.
	 *
	 * @since 0.1.0
	 */
	public function output_metabox() {
		$cmb = $this->init_metabox();
		$cmb->show_form( $cmb->object_id(), $cmb->object_type() );
	}

	/**
	 * If saving the cpt settings option, add the CMB2 sanitized values.
	 *
	 * @since 0.1.0
	 *
	 * @param array $new_value Array of values for the setting.
	 *
	 * @return array Updated array of values for the setting.
	 */
	public function add_sanitized_values( $new_value ) {
		if ( ! empty( $_POST ) ) {
			$cmb = $this->init_metabox();

			$new_value = array_merge(
				$new_value,
				$cmb->get_sanitized_values( $_POST )
			);
		}

		return $new_value;
	}

	/**
	 * Register our Genesis metabox and return the CMB2 instance.
	 *
	 * @since  0.1.0
	 *
	 * @return CMB2 instance.
	 */
	public function init_metabox() {
		if ( null !== $this->cmb ) {
			return $this->cmb;
		}

		$cmb = cmb2_get_metabox( array(
			'id'           => 'slider_metabox',
			'title'        => __( 'متاباکس های اسلایدر', 'myprefix' ),
		//	'hookup'       => false, // We'll handle ourselves. (add_sanitized_values())
		//	'cmb_styles'   => false, // We'll handle ourselves. (admin_hooks())
			'context'      => 'normal', // Important for Genesis.
			 'priority'     => 'high', // Defaults to 'high'.
			'object_types' => array('slider'),//در چه پست تایپی نمایش داده شود
		//	'show_on'      => array(
				// These are important, don't remove.
			//	'key'   => 'options-page',
			//	'value' => array( $this->key ),
		//	),
        ) //,$this->key, 'options-page'
     );

		// Set our CMB2 fields.
	$cmb->add_field( array(
			'name' => __( 'عنوان', 'myprefix' ),
			'desc' => __( 'عنوان مربوط به دکمه', 'myprefix' ),
			'id'   => 'text',
			'type' => 'text',
			// 'default' => 'Default Text',
		) );

		$cmb->add_field( array(
			'name'    => __( 'link', 'myprefix' ),
			'desc'    => __( 'آدرس مربوط به لینک دکمه', 'myprefix' ),
			'id'      => 'link',
			'type'    => 'text',
		//	'default' => '#bada55',
		) );

		//return $this->cmb;


		
		$cmb = cmb2_get_metabox( array(
			'id'           => 'metabox_abute',
			'title'        => __( 'متاباکس های صفحه درباره ما', 'myprefix' ),
		//	'hookup'       => false, // We'll handle ourselves. (add_sanitized_values())
		//	'cmb_styles'   => false, // We'll handle ourselves. (admin_hooks())
			'context'      => 'normal', // Important for Genesis.
			 'priority'     => 'high', // Defaults to 'high'.
			'object_types' => array('page'),//در چه پست تایپی نمایش داده شود
		'show_on'      => array(
				// These are important, don't remove.
				'key'   => 'id',
				'value' => array( '88' ),
			),
        ) //,$this->key, 'options-page'
     );

		// Set our CMB2 fields.
		$cmb->add_field( array(
			'name' => __( 'تصویر زمینه', 'myprefix' ),
			'id'   => 'back_page',
			'type' => 'file',
			// 'default' => 'Default Text',
		) );
		$cmb->add_field( array(
			'name' => __( 'متن جایگزین تصویر', 'myprefix' ),
			'id'   => 'alt_back',
			'type' => 'text',
			// 'default' => 'Default Text',
		) );
	$cmb->add_field( array(
			'name' => __( 'ویدئو', 'myprefix' ),
			'desc' => __( 'فیلم را وارد کنید', 'myprefix' ),
			'id'   => 'video',
			'type' => 'file',
			// 'default' => 'Default Text',
		) );
		$cmb->add_field( array(
			'name' => __( 'تیتر ', 'myprefix' ),
			'desc' => __( 'تیتر مربوط به بخش تیم ما', 'myprefix' ),
			'id'   => 'title',
			'type' => 'text',
			// 'default' => 'Default Text',
		) );
		
		$cmb->add_field( array(
			'name' => __( 'توضیحات ', 'myprefix' ),
			'desc' => __( 'توضیحات مربوط به بخش تیم ما', 'myprefix' ),
			'id'   => 'discription',
			'type' => 'text',
			// 'default' => 'Default Text',
		) );


		$cmb_mygroup_metabox = cmb2_get_metabox( array(
			'id'           => 'cmb_mygroup_metabox',
			'title'        => __( 'متاباکس های صفحه درباره ما گروه بندی', 'myprefix' ),
		//	'hookup'       => false, // We'll handle ourselves. (add_sanitized_values())
		//	'cmb_styles'   => false, // We'll handle ourselves. (admin_hooks())
			'context'      => 'normal', // Important for Genesis.
			 'priority'     => 'high', // Defaults to 'high'.
			'object_types' => array('page'),//در چه پست تایپی نمایش داده شود
		'show_on'      => array(
				// These are important, don't remove.
			'key'   => 'id',
				'value' => array('88' ),
			),
        ) //,$this->key, 'options-page'
     );
$group_metabox= $cmb_mygroup_metabox->add_field( array(
	'id'          => 'group_metabox',
	'type'        => 'group',
	'repeatable'  => true, // use false if you want non-repeatable group
	'options'     => array(
		'group_title'       => 'افراد تیم', // since version 1.1.4, {#} gets replaced by row number
		'closed'         => true,
	),
) );

		// Set our CMB2 fields.
	$cmb_mygroup_metabox->add_group_field($group_metabox, array(
			'name' => __( 'عکس پرسنلی', 'myprefix' ),
			//'desc' => 
			'id'   => 'pic',
			'type' => 'file',
			// 'default' => 'Default Text',
		) );

		$cmb_mygroup_metabox->add_group_field($group_metabox,array(
			'name'    => 'نام و نام خانوادگی',
			'id'      => 'name',
			'type'    => 'text',
		));
		
		$cmb_mygroup_metabox->add_group_field($group_metabox,array(
			'name'    => 'سمت',
			'id'      => 'job',
			'type'    => 'text',
		));
		
		$cmb_mygroup_metabox->add_group_field($group_metabox,array(
			'name'    => 'سابقه کار',
			'id'      => 'time_work',
			'type'    => 'text',
		));


		
		$cmb = cmb2_get_metabox( array(
			'id'           => 'metabox_background',
			'title'        => __( 'متاباکس تصویر زمینه', 'myprefix' ),
		//	'hookup'       => false, // We'll handle ourselves. (add_sanitized_values())
		//	'cmb_styles'   => false, // We'll handle ourselves. (admin_hooks())
			'context'      => 'normal', // Important for Genesis.
			 'priority'     => 'high', // Defaults to 'high'.
			'object_types' => array('post'),//در چه پست تایپی نمایش داده شود
		
        ) //,$this->key, 'options-page'
     );
	 $cmb->add_field( array(
		'name' => __( 'تصویر زمینه ', 'myprefix' ),
		'id'   => 'pic',
		'type' => 'file',
		// 'default' => 'Default Text',
	) );

	
	$cmb_mygroup_metabox_sample = cmb2_get_metabox( array(
		'id'           => 'cmb_mygroup_metabox_sample',
		'title'        => __( 'متاباکس های صفحه نمونه کارها گروه بندی', 'myprefix' ),
	//	'hookup'       => false, // We'll handle ourselves. (add_sanitized_values())
	//	'cmb_styles'   => false, // We'll handle ourselves. (admin_hooks())
		'context'      => 'normal', // Important for Genesis.
		 'priority'     => 'high', // Defaults to 'high'.
		'object_types' => array('sampele-work'),//در چه پست تایپی نمایش داده شود
//	'show_on'      => array(
			// These are important, don't remove.
	//	'key'   => 'id',
		//	'value' => array('88' ),
	//	),
	) //,$this->key, 'options-page'
 );
$group_metabox_sample= $cmb_mygroup_metabox_sample->add_field( array(
'id'          => 'group_metabox_sample',
'type'        => 'group',
'repeatable'  => true, // use false if you want non-repeatable group
'options'     => array(
	'group_title'       => 'گالری عکس ', // since version 1.1.4, {#} gets replaced by row number
	'closed'         => true,
),
) );

$cmb_mygroup_metabox_sample->add_group_field($group_metabox_sample, array(
	'name' => __( 'تصویر پروژه', 'myprefix' ),
	//'desc' => 
	'id'   => 'pic',
	'type' => 'file',
	// 'default' => 'Default Text',
) );





	}

	/**
	 * Public getter method for retrieving protected/private variables.
	 *
	 * @since 0.1.0
	 *
	 * @param string $field Field to retrieve.
	 *
	 * @throws Exception Throws an exception if the field is invalid.
	 *
	 * @return mixed Field value or exception is thrown
	 */
	public function __get( $field ) {
		if ( 'cmb' === $field ) {
			return $this->init_metabox();
		}

		// Allowed fields to retrieve.
		if ( in_array( $field, array( 'key', 'admin_page', 'metabox_id', 'admin_hook' ), true ) ) {
			return $this->{$field};
		}

		throw new Exception( 'Invalid property: ' . $field );
	}

}

/**
 * Helper function to get/return the Myprefix_Genesis_Settings_Metabox object
 *
 * @since 0.1.0
 *
 * @return Myprefix_Genesis_Settings_Metabox object
 */
function myprefix_genesis_settings_metabox() {
	return Myprefix_Genesis_Settings_Metabox::get_instance();
}

// Get it started.
myprefix_genesis_settings_metabox();