<?php
/**
 * Setup of widget areas in the theme
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Clears any widgets auto-mapped to the footer areas on the theme's first activation.
 *
 * WordPress's wp_map_sidebars_widgets() groups 'sidebar-2' and 'footer' together
 * (see $common_slug_groups in wp-includes/widgets.php). On a fresh WordPress
 * install the default 'sidebar-2' carries two block widgets (Archives and
 * Categories). When this theme is activated for the first time, those two
 * widgets are auto-mapped to 'footer-widget-area' because the slug contains
 * 'footer', which is in the same group as 'sidebar-2'.
 *
 * This hook runs at priority 20 — after _wp_sidebars_changed (priority 10)
 * has already performed the mapping — and moves those widgets to the inactive
 * pool, so both footer widget areas start empty by default.
 *
 * The check for an existing 'sidebars_widgets' theme mod short-circuits the
 * function on subsequent activations: WordPress saves the departing theme's
 * widget layout in that mod, and restoring it is the correct behavior then.
 */
function multimagazine_clear_auto_mapped_footer_widgets() {
	// A non-empty 'sidebars_widgets' theme mod means the theme was previously
	// active and its widget layout was saved — honour that layout and bail.
	if ( ! empty( get_theme_mod( 'sidebars_widgets' ) ) ) {
		return;
	}

	$sidebars_widgets = get_option( 'sidebars_widgets', array() );
	$changed          = false;

	foreach ( array( 'footer-widget-area', 'above-footer-widget-area' ) as $sidebar_id ) {
		if ( ! empty( $sidebars_widgets[ $sidebar_id ] ) ) {
			$sidebars_widgets['wp_inactive_widgets'] = array_merge(
				(array) $sidebars_widgets['wp_inactive_widgets'],
				$sidebars_widgets[ $sidebar_id ]
			);
			$sidebars_widgets[ $sidebar_id ]         = array();
			$changed                                 = true;
		}
	}

	if ( $changed ) {
		update_option( 'sidebars_widgets', $sidebars_widgets );
	}
}
add_action( 'after_switch_theme', 'multimagazine_clear_auto_mapped_footer_widgets', 20 );

/**
 * Creates widget areas
 */
function multimagazine_widgets() {
	$multimagazine_heading_style = multimagazine_heading_style();
	$column_count                = get_theme_mod( 'multi_footer_column_count', MULTIMAGAZINE_FOOTER_COLUMN_COUNT );
	$multimagazine_col_class     = 'col' . $column_count;

	register_sidebar(
		array(
			'name'          => __( 'Sidebar Widget Area', 'multimagazine' ),
			'id'            => 'sidebar-widget-area',
			'description'   => __( 'The sidebar widget area', 'multimagazine' ),
			'before_widget' => '<div class="widget %2$s" id="%1$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title spec-heading ' . esc_attr( $multimagazine_heading_style ) . '"><span>',
			'after_title'   => '</span></h3>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Footer Widget Area', 'multimagazine' ),
			'id'            => 'footer-widget-area',
			'description'   => __( 'Widget area in the footer', 'multimagazine' ),
			'before_widget' => '<div class="widget col ' . esc_attr( $multimagazine_col_class ) . ' %2$s" id="%1$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title spec-heading ' . esc_attr( $multimagazine_heading_style ) . '"><span>',
			'after_title'   => '</span></h3>',
		)
	);

	register_sidebar(
		array(
			'name'          => __( 'Above footer widget area', 'multimagazine' ),
			'id'            => 'above-footer-widget-area',
			'description'   => __( 'Widget area placed at the end of page content', 'multimagazine' ),
			'before_widget' => '<div class="widget %2$s" id="%1$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title spec-heading ' . esc_attr( $multimagazine_heading_style ) . '"><span>',
			'after_title'   => '</span></h3>',
		)
	);

	$custom_sidebar = get_option( 'multi_custom_sidebar' ) ? get_option( 'multi_custom_sidebar' ) : array();
	if ( count( $custom_sidebar ) > 0 ) {
		foreach ( $custom_sidebar as $sidebar ) {
			register_sidebar(
				array(
					'name'          => $sidebar,
					'id'            => multimagazine_generate_slug( $sidebar, 45 ),
					'before_widget' => '<div id="%1$s" class="widget %2$s">',
					'after_widget'  => '</div>',
					'before_title'  => '<h3 class="widget-title spec-heading ' . esc_attr( $multimagazine_heading_style ) . '"><span>',
					'after_title'   => '</span></h3>',
				)
			);
		}
	}
}
