/**
         * Displays a meta box of radio selectors on the post editing screen, which allows theme users to select
         * the layout they wish to use for the specific post.
         *
         * @since  1.0.0
         * @param  object $post    The post object currently being edited.
         * @param  array  $metabox Specific information about the meta box being loaded.
         * @return void
         */
        public function callback_metabox($post, $metabox)
        {
            wp_nonce_field(basename(__FILE__), 'cherry-sidebar-nonce');
            global $wp_registered_sidebars;
            $Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
            $cusotm_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
            unset($cusotm_sidebar_array['cherry-sidebar-manager-counter']);
            $wp_registered_sidebars = array_merge($wp_registered_sidebars, $cusotm_sidebar_array);
            $select_sidebar = $this->get_post_sidebar($post->ID);
            $sidebars = array('post-main-sidebar' => array('title' => __('Main Sidebar:', 'cherry-sidebar-manager'), 'id' => 'cherry-post-main-sidebar', 'value' => is_array($select_sidebar['cherry-post-main-sidebar']) && isset($select_sidebar['cherry-post-main-sidebar']) ? $select_sidebar['cherry-post-main-sidebar'] : ''), 'post-secondary-sidebar' => array('title' => __('Secondary Sidebar:', 'cherry-sidebar-manager'), 'id' => 'cherry-post-secondary-sidebar', 'value' => is_array($select_sidebar['cherry-post-secondary-sidebar'] && isset($select_sidebar['cherry-post-secondary-sidebar'])) ? $select_sidebar['cherry-post-secondary-sidebar'] : ''));
            $select_options = array('' => __('Sidebar not selected', 'cherry-sidebar-manager'));
            foreach ($wp_registered_sidebars as $sidebar => $sidebar_value) {
                $sidebar_id = $sidebar_value['id'];
                $sidebar_name = $sidebar_value['name'];
                $select_options[$sidebar_id] = $sidebar_name;
            }
            foreach ($sidebars as $sidebar => $sidebar_value) {
                $output = '<p><strong>' . $sidebar_value['title'] . '</strong></p>';
                $UI_Select = new UI_Select(array('id' => $sidebar_value['id'], 'name' => $sidebar_value['id'], 'value' => $sidebar_value['value'], 'options' => $select_options));
                $output .= $UI_Select->render();
                echo $output;
            }
            ?>
				<p class="howto"><?php 
            printf(__('You can choose page sidebars or create a new sidebar on %swidgets page%s .', 'cherry-sidebar-manager'), '<a href="widgets.php" target="_blank" title="' . __('Widgets Page') . '">', '</a>');
            ?>
</p>
			<?php 
        }
 public function edit_wp_registered_sidebars()
 {
     global $wp_registered_sidebars;
     $Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
     $cusotm_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
     unset($cusotm_sidebar_array['cherry-sidebar-manager-counter']);
     $sidebar_array_lengh = count($cusotm_sidebar_array);
     foreach ($cusotm_sidebar_array as $sidebar => $cusotm_sidebar) {
         unset($wp_registered_sidebars[$sidebar]);
     }
 }
 /**
  * Set custom sidebar in global array $wp_registered_sidebars.
  *
  * @since 1.0.0
  */
 public function set_custom_sidebar()
 {
     global $wp_registered_sidebars;
     $object_id = get_queried_object_id();
     $this->post_sidebars = get_post_meta(apply_filters('cherry_sidebar_manager_object_id', $object_id), 'post_sidebar', true);
     if (is_array($this->post_sidebars)) {
         $Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
         $custom_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
         unset($custom_sidebar_array['cherry-sidebar-manager-counter']);
         $wp_registered_sidebars = array_merge($wp_registered_sidebars, $custom_sidebar_array);
         add_filter('cherry_get_main_sidebar', array($this, 'set_main_sidebar'), 1, 1);
         add_filter('cherry_get_secondary_sidebar', array($this, 'set_secondary_sidebar'), 1, 1);
     }
 }
 /**
  * Set custom sidebar in global array $wp_registered_sidebars.
  *
  * @since 1.0.0
  */
 public function set_custom_sidebar($widgets)
 {
     $object_id = get_queried_object_id();
     if (function_exists('is_shop')) {
         if (is_shop() || is_tax('product_cat') || is_tax('product_tag')) {
             $object_id = wc_get_page_id('shop');
         }
     }
     $post_sidebars = get_post_meta(apply_filters('cherry_sidebar_manager_object_id', $object_id), 'post_sidebar', true);
     //var_dump($post_sidebars);
     if ($post_sidebars && !empty($post_sidebars)) {
         $Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
         $custom_sidebar = $Custom_Sidebars_Methods->get_custom_sidebar_array();
         foreach ($post_sidebars as $sidebar => $sidebar_value) {
             //var_dump($widgets);
             //var_dump($sidebar);
             //var_dump($sidebar_value);
             if ('' !== $sidebar_value && isset($widgets[$sidebar]) || false !== strripos($sidebar_value, 'cherry-sidebar-manager') && array_key_exists($sidebar_value, $custom_sidebar)) {
                 $widgets[$sidebar] = $widgets[$sidebar_value];
             }
         }
     }
     return $widgets;
 }
/**
 * Custom sidebar removing function.
 *
 * @since 1.0.0
 * @return void
 */
function remove_custom_sidebar()
{
    check_ajax_referer('remove_custom_sidebar', 'security');
    $nonce = isset($_GET['security']) ? $_GET['security'] : $security;
    if (!wp_verify_nonce($nonce, 'remove_custom_sidebar')) {
        exit;
    }
    $id = isset($_GET['id']) ? $_GET['id'] : $id;
    var_dump($id);
    $Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
    $cusotm_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
    var_dump($cusotm_sidebar_array);
    unset($cusotm_sidebar_array[$id]);
    var_dump($cusotm_sidebar_array);
    $Cherry_Custom_Sidebars_Methods->set_custom_sidebar_array($cusotm_sidebar_array);
}
	</div>
	<div id="cherry-sidebar-manager" class="sidebars-holder">
		<p class="description cherry-default-description"><?php 
_e('You can create a custom sidebar yourself and enable if for any page or post. This can be done on page editing stage.', 'cherry-sidebar-manager');
?>
</p>
		<span class="cherry-ui-core"><a class="thickbox button button-default_ btn-create-sidebar" href="#TB_inline?width=600&height=300&inlineId=new-sidebar-manager-wrap"><?php 
_e('Create a new sidebar', 'cherry-sidebar-manager');
?>
</a></span>

		<div id="cherry-sidebar-manager-holder">
			<div class="sidebars-column-1">
			<?php 
global $wp_registered_sidebars;
$Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
$cusotm_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
unset($cusotm_sidebar_array['cherry-sidebar-manager-counter']);
$sidebar_counter = count($cusotm_sidebar_array) - 1;
$last_sidebar = end($cusotm_sidebar_array);
$counter = 0;
$wp_registered_sidebars = array_merge($wp_registered_sidebars, $cusotm_sidebar_array);
if (empty($cusotm_sidebar_array)) {
    echo '</div><div class="sidebars-column-2">';
}
foreach ($cusotm_sidebar_array as $sidebar => $cusotm_sidebar) {
    if ($counter === intval($sidebar_counter / 2) + 1 || $sidebar_counter === 0) {
        echo '</div><div class="sidebars-column-2">';
    }
    $wrap_class = 'widgets-holder-wrap';
    if (!empty($cusotm_sidebar['class'])) {
 public function registrate_dynamic_sidebar()
 {
     global $wp_registered_sidebars;
     $Cherry_Custom_Sidebars_Methods = new Cherry_Custom_Sidebars_Methods();
     $cusotm_sidebar_array = $Cherry_Custom_Sidebars_Methods->get_custom_sidebar_array();
     unset($cusotm_sidebar_array['cherry-sidebar-manager-counter']);
     $wp_registered_sidebars = array_merge($wp_registered_sidebars, $cusotm_sidebar_array);
 }