<?php
/**
 * Classic Widget Options
 */
add_action( 'after_setup_theme', 'maglume_classicwidget_options', 999 );
function maglume_classicwidget_options() {
	if ( ! current_theme_supports( 'widgets-block-editor' ) ) {
		add_action( 'in_widget_form', 'maglume_in_widget_form', 5, 3 );
		add_filter( 'widget_update_callback', 'maglume_in_widget_form_update', 5, 3 );
		add_filter( 'dynamic_sidebar_params', 'maglume_parts_dynamic_sidebar_params', 5, 3 );
		add_action( 'admin_enqueue_scripts', 'maglume_enqueue_admin_widget_styles_scripts' );
	}
}

function maglume_enqueue_admin_widget_styles_scripts( $hook ){
	/* Load widgets for SiteOrigin Page Builder plugin on Edit screens */
	$widgetload = ( ( 'post.php' == $hook || 'post-new.php' == $hook ) && ( defined( 'SITEORIGIN_PANELS_VERSION' ) && version_compare( SITEORIGIN_PANELS_VERSION, '2.0' ) >= 0 ) ) ? true : false;

	if ( 'widgets.php' == $hook || $widgetload ):
		wp_enqueue_style( 'hoot-classicwidgets', hoot_data()->incuri . 'admin/css/classicwidgets.css', array(),  hoot_data()->hoot_version );
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'hoot-classicwidgets', hoot_data()->incuri . 'admin/js/classicwidgets.js', array( 'jquery', 'wp-color-picker' ),  hoot_data()->hoot_version, true );
	endif;
}

/**
 * Add input fields
 *
 * @since 1.4.0
 * @access public
 */
if ( !function_exists( 'maglume_in_widget_form' ) ):
function maglume_in_widget_form( $widget, $return, $instance ){
	$initclass = '';
	if ( !isset($instance['title']) ) $initclass = 'maglume-notinit';
	if ( is_object($widget) && isset($widget->id_base) && (
		$widget->id_base == 'newsletterwidget' || $widget->id_base == 'newsletterwidgetminimal' || $widget->id_base == 'mailpoet_form'
		) ) $initclass = 'maglume-notinit';

	if ( !isset($instance['wtitle_bg']) )
		$instance['wtitle_bg'] = null;
	if ( !isset($instance['wtitle_font']) )
		$instance['wtitle_font'] = null;
	if ( !isset($instance['wtitle_nostyle']) )
		$instance['wtitle_nostyle'] = null;
	$default_bg = $default_font = '';
	?>
	<div class="maglume-widget-field maglume-widget-field-type-parts maglume-widget-title <?php echo sanitize_html_class( $initclass ); ?>">
		<div class="maglume-widget-field-collapse" data-id="titlestyle">
			<div class="maglume-collapse-head"><h3>Title Style <i class="fas fa-sort"></i></h3></div>
			<div class="maglume-collapse-body" style="display: none;">
				<div class="maglume-widget-field maglume-widget-field-type-color">
					<label for="<?php echo $widget->get_field_id( 'wtitle_bg' ) ?>"><?php _e( 'Title Background:', 'magazine-lume'); ?></label>
					<input id="<?php echo $widget->get_field_id( 'wtitle_bg' ) ?>" class="hoot-maglume-color" name="<?php echo $widget->get_field_name( 'wtitle_bg' ) ?>" type="text" value="<?php echo esc_attr( $instance['wtitle_bg'] ) ?>" <?php echo $default_bg ?> />
					<div class="maglume-widget-field-description"><small><?php _e( 'Leave empty to use default colors.', 'magazine-lume'); ?></small></div><div class="clear"></div>
				</div>
				<div class="maglume-widget-field maglume-widget-field-type-checkbox">
					<label for="<?php echo $widget->get_field_id( 'wtitle_nostyle' ) ?>"><?php _e( 'Transparent Title Background', 'magazine-lume'); ?>&nbsp;
					<input id="<?php echo $widget->get_field_id( 'wtitle_nostyle' ) ?>" class="hoot-maglume-checkbox" name="<?php echo $widget->get_field_name( 'wtitle_nostyle' ) ?>" type="checkbox" <?php checked( $instance['wtitle_nostyle'] ); ?> />
					</label>
					<div class="maglume-widget-field-description"><small><?php _e( 'This will entirely remove any title background. (You can still select the Title Font Color below)', 'magazine-lume'); ?></small></div><div class="clear"></div>
				</div>
				<div class="maglume-widget-field maglume-widget-field-type-color">
					<label for="<?php echo $widget->get_field_id( 'wtitle_font' ) ?>"><?php _e( 'Title Font:', 'magazine-lume'); ?></label>
					<input id="<?php echo $widget->get_field_id( 'wtitle_font' ) ?>" class="hoot-maglume-color" name="<?php echo $widget->get_field_name( 'wtitle_font' ) ?>" type="text" value="<?php echo esc_attr( $instance['wtitle_font'] ) ?>" <?php echo $default_font ?> />
					<div class="maglume-widget-field-description"><small><?php _e( 'Leave empty to use default colors.', 'magazine-lume'); ?></small></div><div class="clear"></div>
				</div>
			</div>
			<div class="clear"></div>
		</div>
	</div>
	<?php

}
endif;

/**
 * Callback function for options update
 *
 * @since 1.4.0
 * @access public
 */
if ( !function_exists( 'maglume_in_widget_form_update' ) ):
function maglume_in_widget_form_update( $instance, $new_instance, $old_instance ){
	$instance['wtitle_nostyle'] = ( empty( $new_instance['wtitle_nostyle'] ) ) ? 0 : 1;
	$instance['wtitle_bg'] = isset( $instance['wtitle_bg'] ) ? sanitize_hex_color( $new_instance['wtitle_bg'] ) : '';
	$instance['wtitle_font'] = isset( $instance['wtitle_font'] ) ? sanitize_hex_color( $new_instance['wtitle_font'] ) : '';
	return $instance;
}
endif;

/**
 * Apply Options
 *
 * @since 1.4.0
 * @access public
 */
if ( !function_exists( 'maglume_parts_dynamic_sidebar_params' ) ):
function maglume_parts_dynamic_sidebar_params( $params ){
	static $options = array();
	if ( !empty( $params[0]['widget_id'] ) ) {
		$id = $params[0]['widget_id'];
		$idnoarr = array();
		if ( preg_match( '#(\d+)$#', $id, $idnoarr ) ) {
			$idno = $idnoarr[0];
			$id = rtrim( $id, '-' . $idno );
			$options[$id] = ( empty( $options[$id] ) ) ? get_option( 'widget_' . $id ) : $options[$id];
			if ( !empty( $options[$id][$idno] ) && isset( $options[$id][$idno]['title'] ) ) {
				if ( !isset($options[$id][$idno]['wtitle_bg']) )
					$options[$id][$idno]['wtitle_bg'] = null;
				if ( !isset($options[$id][$idno]['wtitle_font']) )
					$options[$id][$idno]['wtitle_font'] = null;
				if ( !isset($options[$id][$idno]['wtitle_nostyle']) )
					$options[$id][$idno]['wtitle_nostyle'] = null;
				if ( $options[$id][$idno]['wtitle_nostyle'] || $options[$id][$idno]['wtitle_bg'] || $options[$id][$idno]['wtitle_font'] ) {
					$nobg = !! $options[$id][$idno]['wtitle_nostyle'];
					$bg = ( $options[$id][$idno]['wtitle_bg'] ) ? sanitize_hex_color( $options[$id][$idno]['wtitle_bg'] ) : false;
					$font = ( $options[$id][$idno]['wtitle_font'] ) ? sanitize_hex_color( $options[$id][$idno]['wtitle_font'] ) : false;
					$inlinestyle = ' style="';
					$class = '';

					if ( $nobg ) {
						$inlinestyle .= 'background:none;';
						$class .= ' remove-style';
						if ( $font ) {
							$inlinestyle .= 'color:' . $font .';border-color:' . $font .';';
							$class .= ' has-custom-font';
						} else {
							$inlinestyle .= 'color:inherit';
						}
					} else {
						if ( $bg ) {
							$inlinestyle .= 'background:' . $bg .';border-color:' . $bg .';';
							$class .= ' has-custom-bg';
						}
						if ( $font ) {
							$inlinestyle .= 'color:' . $font .';';
							$class .= ' has-custom-font';
						}
					}

					$inlinestyle .= '" ';
					$spanclass = '';

					$params[0]['before_title'] = apply_filters( 'maglume_parts_dynamic_sidebar_params_before_title', '<h3 class="widget-title title-customstyle ' . $class . '"><span class="' . $spanclass . '"' . $inlinestyle . '>', $params[0], $id, $idno, $options[$id] );
				}
			}
		}
	}
	return $params;
}
endif;

