<?php
add_action( 'init', 'school_time_custom_logo_init' );
add_action( 'customize_save_after', 'school_time_customize_save_after' );
add_action( 'redux/customizer/live_preview', 'school_time_customizer');

function school_time_customizer() {
	$redux = ReduxFrameworkInstances::get_instance( SCHOOL_TIME_THEME_OPTION_NAME );
	$redux->_enqueue_output();
	wp_add_inline_style( 'school-time-style', $redux->compilerCSS );
}

function school_time_set_option( $option_name, $value ) {
	$options = isset( $GLOBALS[ SCHOOL_TIME_THEME_OPTION_NAME ] ) ? $GLOBALS[ SCHOOL_TIME_THEME_OPTION_NAME ] : false;

	if ( $options && is_string( $option_name ) ) {
		$options[ $option_name ] = $value;
		update_option( SCHOOL_TIME_THEME_OPTION_NAME, $options );
		return true;
	}
	return false;
}

function school_time_get_logo_url() {
	$logo_url = '';

	// Get custom page logo
	$logo_url = school_time_get_rwmb_meta_image_url( 'custom_logo' );
	if ( $logo_url ) {
		return $logo_url;
	}

	// Default WP Custom Logo implementation
	$custom_logo_id = get_theme_mod( 'custom_logo' );
	$image = wp_get_attachment_image_src( $custom_logo_id, 'full' );

	if ( isset( $image[0] ) ) {
		$logo_url = $image[0];
	} else {
		// Get default logo
		$logo     = school_time_get_option( 'logo', array() );
		$logo_url = isset( $logo['url'] ) && $logo['url'] ? $logo['url'] : '';
	}
	return $logo_url;
}


function school_time_custom_logo_init() {

	$custom_logo_id = get_theme_mod( 'custom_logo' );

	// do this only the first time when custom_logo does not exist
	if ( $custom_logo_id === false ) {
		$logo     = school_time_get_option( 'logo', array() );
		$logo_url = isset( $logo['url'] ) && $logo['url'] ? $logo['url'] : '';

		$attachment_id = attachment_url_to_postid( $logo_url );

		if ( $attachment_id ) {
			set_theme_mod( 'custom_logo', $attachment_id );
		}
	} 
}

function school_time_customize_save_after( $customizer ) {
	$custom_logo_id = get_theme_mod( 'custom_logo' );
	$image          = wp_get_attachment_image_src( $custom_logo_id, 'full' );
	$logo           = array( 'url' => $image[0] );

	if ( isset( $image[0] ) && ! empty( $image[0] ) ) {
		school_time_set_option( 'logo', $logo );
	}
}

function school_time_set_custom_logo_from_theme_options( $logo ) {
	$logo_url      = isset( $logo['url'] ) && $logo['url'] ? $logo['url'] : '';
	$attachment_id = attachment_url_to_postid( $logo_url );
	if ( $attachment_id ) {
		set_theme_mod( 'custom_logo', $attachment_id );
	}
}
