<?php

/**
 * Register sidebars
 */
function restorator_widgets_init() {
	
	//	Blog Sidebar
	register_sidebar( array(
		'name'          	=> esc_html__( 'Blog Sidebar', 'restorator' ),
		'id'            	=> 'blog-sidebar',
		'before_widget' 	=> '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  	=> '</aside>',
		'before_title'  	=> '<h4 class="widget-title">',
		'after_title'   	=> '</h4>',
	) );
	
	//	Prefooter Area
	global $restorator_options;
	$restorator_widgets_grid = isset( $restorator_options['prefooter_col'] ) ? $restorator_options['prefooter_col'] : '4-4-4';
	$i = 1;
	foreach (explode('-', $restorator_widgets_grid) as $restorator_widgets_g) {
		register_sidebar(array(
			'name' 				=> esc_html__('Prefooter Area ', 'restorator') . $i,
			'id' 				=> "footer-area-$i",
			'description' 		=> esc_html__('The prefooter area widgets', 'restorator'),
			'before_widget' 	=> '<aside id="%1$s" class="widget %2$s">',
			'after_widget' 		=> '</aside>',
			'before_title' 		=> '<h4 class="widget-title">',
			'after_title'   	=> '</h4>',
		));
		$i++;
	}
	
	//	Shop Sidebar
	if( restorator_woo_enabled() ) {
		register_sidebar( array(
			'name'          	=> esc_html__( 'Shop Sidebar', 'restorator' ),
			'id'            	=> 'shop-sidebar',
			'before_widget' 	=> '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  	=> '</aside>',
			'before_title'  	=> '<h4 class="widget-title">',
			'after_title'   	=> '</h4>',
		) );
	}
	
	if (!empty($restorator_options['unlimited_sidebar'])) {	
		if( class_exists( 'ReduxFrameworkPlugin' ) && sizeof(restorator_options('unlimited_sidebar') > 0 ) ) {
			foreach( restorator_options('unlimited_sidebar') as $restorator_sidebar ) {
				if ( isset( $restorator_sidebar ) && !empty( $restorator_sidebar ) ) {
					register_sidebar( array(
						'name' 				=> $restorator_sidebar,
						'id' 				=> restorator_generateSlug($restorator_sidebar, 45),
						'before_widget' 	=> '<aside id="%1$s" class="widget %2$s">',
						'after_widget' 		=> '</aside>',
						'before_title' 		=> '<h4 class="widget-title">',
						'after_title'   	=> '</h4>',
					));
				}
			}
		}
	}
	
}
add_action( 'widgets_init', 'restorator_widgets_init' );


/**
 * Unlimited Sidebars
 */

function restorator_generateSlug($phrase, $maxLength) {
    $result = strtolower($phrase);
    $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
    $result = trim(preg_replace("/[\s-]+/", " ", $result));
    $result = trim(substr($result, 0, $maxLength));
    $result = preg_replace("/\s/", "-", $result);
    return $result;
}


/**
 * Include widgets
 */

require_once( get_template_directory() . '/inc/widgets/widget-flickr.php' );
require_once( get_template_directory() . '/inc/widgets/widget-posts.php' );
require_once( get_template_directory() . '/inc/widgets/widget-social.php' );


/**
 * Register widgets
 */

function restorator_register_widgets() {
	register_widget( 'restorator_widget_flickr' );
	register_widget( 'restorator_widget_post_thumb' );
	register_widget( 'restorator_widget_social' );
}

add_action( 'widgets_init', 'restorator_register_widgets' );