<?php
/**
 * CMB2 Theme Options
 * @version 0.1.0
 */
class Myprefix_Admin {
	/**
 	 * Option key, and option page slug
 	 * @var string
 	 */
	protected $key = 'myprefix_options';
	/**
 	 * Options page metabox id
 	 * @var string
 	 */
	protected $metabox_id = 'myprefix_option_metabox';
	/**
	 * Options Page title
	 * @var string
	 */
	protected $title = '';
	/**
	 * Options Page hook
	 * @var string
	 */
	protected $options_page = '';
	/**
	 * Holds an instance of the object
	 *
	 * @var Myprefix_Admin
	 */
	protected static $instance = null;
	/**
	 * Returns the running object
	 *
	 * @return Myprefix_Admin
	 */
	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() {
		// Set our title
		$this->title = __( 'تنظیمات وب سایت', 'myprefix' );
	}
	/**
	 * Initiate our hooks
	 * @since 0.1.0
	 */
	public function hooks() {
		add_action( 'admin_init', array( $this, 'init' ) );
		add_action( 'admin_menu', array( $this, 'add_options_page' ) );
		add_action( 'cmb2_admin_init', array( $this, 'add_options_page_metabox' ) );
	}
	/**
	 * Register our setting to WP
	 * @since  0.1.0
	 */
	public function init() {
		register_setting( $this->key, $this->key );
	}
	/**
	 * Add menu options page
	 * @since 0.1.0
	 */
	public function add_options_page() {
		$this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', $this->key, array( $this, 'admin_page_display' ) );
		// Include CMB CSS in the head to avoid FOUC
		add_action( "admin_print_styles-{$this->options_page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
	}
	/**
	 * Admin page markup. Mostly handled by CMB2
	 * @since  0.1.0
	 */
	public function admin_page_display() {
		?>
		<div class="wrap cmb2-options-page <?php echo $this->key; ?>">
			<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
			<?php cmb2_metabox_form( $this->metabox_id, $this->key ); ?>
		</div>
		<?php
	}
	/**
	 * Add the options metabox to the array of metaboxes
	 * @since  0.1.0
	 */
	function add_options_page_metabox() {
		// hook in our save notices
		add_action( "cmb2_save_options-page_fields_{$this->metabox_id}", array( $this, 'settings_notices' ), 10, 2 );
		$cmb = new_cmb2_box( array(
			'id'         => $this->metabox_id,
			'hookup'     => false,
			'cmb_styles' => false,
			'show_on'    => array(
				// These are important, don't remove
				'key'   => 'options-page',
				'value' => array( $this->key, )
			),
		) );
		// Set our CMB2 fields
	$sadiq_filed_header = $cmb->add_field( array(
	'id'          => 'sadiq_filed_header',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'تنظیمات هدر', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

// Id's for group's fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $sadiq_filed_header, array(
	'name' => 'آدرس شرکت',
	'id'   => 'adress_bar',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$cmb->add_group_field( $sadiq_filed_header, array(
	'name' => 'شماره تماس',
	'id'   => 'phone_bar',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );



$cmb->add_group_field( $sadiq_filed_header, array(
	'name' => 'لوگو',
	'id'   => 'logo',
	'type' => 'file',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$cmb->add_group_field( $sadiq_filed_header, array(
	'name' => 'متن لوگو',
	'id'   => 'logo-text',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$sadiq_filed_social= $cmb->add_field( array(
	'id'          => 'sadiq_filed_social',
	'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, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_social, array(
	'name' => 'آیکون',
	'id'   => 'icon',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_social, array(
	'name' => 'لینک',
	'id'   => 'link',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$sadiq_filed_service= $cmb->add_field( array(
	'id'          => 'sadiq_filed_service',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'عنوان و متن خدمات ما', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_service, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_service, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );


		$sadiq_filed_service_item= $cmb->add_field( array(
	'id'          => 'sadiq_filed_service_item',
	'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, // true to have the groups closed by default
	),
) );

$cmb->add_group_field( $sadiq_filed_service_item, array(
	'name' => 'آیکون',
	'id'   => 'icon',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb->add_group_field( $sadiq_filed_service_item, array(
	'name' => 'لینک',
	'id'   => 'link',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb->add_group_field( $sadiq_filed_service_item, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb->add_group_field( $sadiq_filed_service_item, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );


$sadiq_filed_counter_item= $cmb->add_field( array(
	'id'          => 'sadiq_filed_counter_item',
	'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, // true to have the groups closed by default
	),
) );

$cmb->add_group_field( $sadiq_filed_counter_item, array(
	'name' => 'آیکون',
	'id'   => 'icon',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$cmb->add_group_field( $sadiq_filed_counter_item, array(
	'name' => 'شمارنده',
	'id'   => 'counter',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$cmb->add_group_field( $sadiq_filed_counter_item, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$sadiq_filed_counter= $cmb->add_field( array(
	'id'          => 'sadiq_filed_counter',
	'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, // true to have the groups closed by default
	),
) );

$cmb->add_group_field( $sadiq_filed_counter, array(
	'name' => 'تصویر پس زمینه',
	'id'   => 'cover',
	'type' => 'file',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );


$sadiq_filed_portfolio= $cmb->add_field( array(
	'id'          => 'sadiq_filed_portfolio',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'عنوان و زیر عنوان بخش نمونه کارها', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_portfolio, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_portfolio, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$sadiq_filed_partners= $cmb->add_field( array(
	'id'          => 'sadiq_filed_partners',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'بخش عنوان و زیر عنوان همکاران ما', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_partners, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_partners, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

			$sadiq_filed_partner_item= $cmb->add_field( array(
	'id'          => 'sadiq_filed_partner_item',
	'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, // true to have the groups closed by default
	),
) );

$cmb->add_group_field( $sadiq_filed_partner_item, array(
	'name' => 'تصویر',
	'id'   => 'pic',
	'type' => 'file',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb->add_group_field( $sadiq_filed_partner_item, array(
	'name' => 'لینک',
	'id'   => 'link',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
$cmb->add_group_field( $sadiq_filed_partner_item, array(
	'name' => 'متن جایگزین',
	'id'   => 'alt',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

$sadiq_filed_newsletter= $cmb->add_field( array(
	'id'          => 'sadiq_filed_newsletter',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'عنوان و متن خبرنامه', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_newsletter, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_newsletter, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$sadiq_filed_blog= $cmb->add_field( array(
	'id'          => 'sadiq_filed_blog',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'عنوان و متن بلاگ ', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_blog, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_blog, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$sadiq_filed_contact= $cmb->add_field( array(
	'id'          => 'sadiq_filed_contact',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'عنوان و متن فرم تماس ', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_contact, array(
	'name' => 'عنوان',
	'id'   => 'title',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_contact, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

		$sadiq_filed_footer_about= $cmb->add_field( array(
	'id'          => 'sadiq_filed_footer_about',
	'type'        => 'group',
	 'repeatable'  => false, // use false if you want non-repeatable group
	'options'     => array(
	'group_title'   =>'بخش درباره ما فوتر ', // since version 1.1.4, {#} gets replaced by row number
	 'closed'     => true, // true to have the groups closed by default
	),
) );

	$cmb->add_group_field( $sadiq_filed_footer_about, array(
	'name' => 'لوگو',
	'id'   => 'logo',
	'type' => 'file',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_footer_about, array(
	'name' => 'متن جایگزین',
	'id'   => 'alt',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );

	$cmb->add_group_field( $sadiq_filed_footer_about, array(
	'name' => 'متن',
	'id'   => 'subtitle',
	'type' => 'text',
 'repeatable' => false, // Repeatable fields are supported w/in repeatable groups (for most types)
) );
	
	}
	/**
	 * Register settings notices for display
	 *
	 * @since  0.1.0
	 * @param  int   $object_id Option key
	 * @param  array $updated   Array of updated fields
	 * @return void
	 */
	public function settings_notices( $object_id, $updated ) {
		if ( $object_id !== $this->key || empty( $updated ) ) {
			return;
		}
		add_settings_error( $this->key . '-notices', '', __( 'Settings updated.', 'myprefix' ), 'updated' );
		settings_errors( $this->key . '-notices' );
	}
	/**
	 * Public getter method for retrieving protected/private variables
	 * @since  0.1.0
	 * @param  string  $field Field to retrieve
	 * @return mixed          Field value or exception is thrown
	 */
	public function __get( $field ) {
		// Allowed fields to retrieve
		if ( in_array( $field, array( 'key', 'metabox_id', 'title', 'options_page' ), true ) ) {
			return $this->{$field};
		}
		throw new Exception( 'Invalid property: ' . $field );
	}
}
/**
 * Helper function to get/return the Myprefix_Admin object
 * @since  0.1.0
 * @return Myprefix_Admin object
 */
function myprefix_admin() {
	return Myprefix_Admin::get_instance();
}
/**
 * Wrapper function around cmb2_get_option
 * @since  0.1.0
 * @param  string $key     Options array key
 * @param  mixed  $default Optional default value
 * @return mixed           Option value
 */
function myprefix_get_option( $key = '', $default = null ) {
	if ( function_exists( 'cmb2_get_option' ) ) {
		// Use cmb2_get_option as it passes through some key filters.
		return cmb2_get_option( myprefix_admin()->key, $key, $default );
	}
	// Fallback to get_option if CMB2 is not loaded yet.
	$opts = get_option( myprefix_admin()->key, $key, $default );
	$val = $default;
	if ( 'all' == $key ) {
		$val = $opts;
	} elseif ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
		$val = $opts[ $key ];
	}
	return $val;
}
// Get it started
myprefix_admin();