add_section( 'bootswatch', [ 'title' => 'Bootswatch', 'priority' => 0, ] ); /** * Necessary since custom header CSS is in the HTML head. */ $wp_customize->get_setting( 'external_header_video' )->transport = 'refresh'; $wp_customize->get_setting( 'header_image' )->transport = 'refresh'; $wp_customize->get_setting( 'header_video' )->transport = 'refresh'; } ); /** * Add theme option. */ bootswatch_create_option_select( 'theme', __( 'Theme', 'bootswatch' ), bootswatch_themes_list(), 'bootswatch' ); /** * Add header size option. */ bootswatch_create_option_input( 'number' , 'custom_header_percentage_size' , __( 'Custom Header Size', 'bootswatch' ) , [ 'max' => 100, 'min' => 20, ] , 'bootswatch' , function() { ?> add_setting( $id, [ 'sanitize_callback' => function( $value ) { return $value; }, 'transport' => $preview_cb ? 'postMessage' : 'refresh', ] ); if ( $preview_cb ) { add_action( 'wp_footer', $preview_cb ); } $wp_customize->add_control( $id, [ 'settings' => $id, 'label' => $label, 'type' => $type, 'input_attrs' => $input_attrs, 'section' => $section, ] ); } ); } /** * Registers a new option which is a dropdown or a radio. * * @param String $type `select` or `radio`. * @param String $id ID. * @param String $label Label. * @param String|Array $choices Choices array, accepts also `noyes` and 'yesno'. * @param String $section Section ID. * @param String|Function $preview_cb Function. */ function bootswatch_create_option_choice( $type, $id, $label, $choices = 'noyes', $section = 'bootswatch', $preview_cb = false, $selective_refresh = [] ) { if ( ! in_array( $type, [ 'select', 'radio' ] ) ) { return; } add_action( 'customize_register', function( $wp_customize ) use ( $type, $id, $label, $choices, $section, $preview_cb, $selective_refresh ) { /** * Prepare ID. */ $id = sprintf( 'bootswatch[%s]', $id ); /** * Handle yes/no choices. */ switch ( $choices ) { case 'noyes': $choices = [ 'no' => __( 'No', 'bootswatch' ), 'yes' => __( 'Yes', 'bootswatch' ), ]; break; case 'yesno': $choices = [ 'yes' => __( 'Yes', 'bootswatch' ), 'no' => __( 'No', 'bootswatch' ), ]; break; default: break; } /** * Add setting. */ $wp_customize->add_setting( $id, [ 'sanitize_callback' => function( $value ) { return ( preg_match( '/^[a-z]+$/', $value ) ) ? $value : '' ; }, 'transport' => ( $preview_cb || $selective_refresh ) ? 'postMessage' : 'refresh', ] ); if ( $preview_cb ) { add_action( 'wp_footer', $preview_cb ); } /** * Add control. */ $wp_customize->add_control( $id, [ 'settings' => $id, 'label' => $label, 'type' => $type, 'choices' => $choices, 'section' => $section, ] ); /** * Maybe add partial. */ if ( $selective_refresh ) { $wp_customize->selective_refresh->add_partial( $id, $selective_refresh ); } } ); } /** * Registers a new option which is a dropdown. * * @param String $id ID. * @param String $label Label. * @param String|Array $choices Choices array, accepts also `noyes` and 'yesno'. * @param String $section Section ID. * @param String|Function $preview_cb Function. */ function bootswatch_create_option_select( $id, $label, $choices = 'noyes', $section = 'bootswatch', $preview_cb = false, $selective_refresh = [] ) { bootswatch_create_option_choice( 'select', $id, $label, $choices, $section, $preview_cb, $selective_refresh ); } /** * Registers a new option which is a radio. * * @param String $id ID. * @param String $label Label. * @param String|Array $choices Choices array, accepts also `noyes` and 'yesno'. * @param String $section Section ID. * @param String|Function $preview_cb Function. */ function bootswatch_create_option_radio( $id, $label, $choices = 'noyes', $section = 'bootswatch', $preview_cb = false ) { bootswatch_create_option_choice( 'radio', $id, $label, $choices, $section, $preview_cb ); } /** * Gets an option. * * @param string $option_id The option id. * @return mixed The option value. * @param mixed $default Default value. */ function bootswatch_get_option( $option_id, $default = false ) { $mods = get_theme_mod( 'bootswatch', [] ); return array_key_exists( $option_id, $mods ) && $mods[ $option_id ] ? $mods[ $option_id ] : $default ; } /** * Checks if an option is being used. * * @param string $option_id The Option id. * @return boolean Weither or not that option is being used */ function bootswatch_has( $option_id ) { switch ( $option_id ) { default: return 'yes' === bootswatch_get_option( $option_id ); break; } } /** * Returns bootswatch theme variable.less file path. * * @param String $theme The theme. * @param String $part Which part. * @return String|Bolean The theme path or false. */ function bootswatch_get_theme_part_path( $theme, $part ) { if ( ! array_key_exists( $theme, bootswatch_themes_list() ) ) { return false; } $bootswatch_light = get_template_directory() . '/vendor/kadimi/bootswatch-light/light/'; switch ( $part ) { case 'bootswatch': return $bootswatch_light . "$theme/" . 'bootswatch.less'; case 'variables': return $bootswatch_light . "$theme/" . 'variables.less'; } } /** * Print Bootstrap Part URI. * * @param String $part `style`, `theme` or `script`. */ function bootswatch_bootstrap_part_uri( $part ) { echo (string) bootswatch_get_bootstrap_part_uri( $part ); // XSS OK. } /** * Get bootstrap part URI. * * @param String $part `style`, `theme` or `script`. * @return String|null The URI. */ function bootswatch_get_bootstrap_part_uri( $part ) { return bootswatch_get_bootstrap_part( $part, 'uri' ); } /** * Get bootstrap part path. * * @param String $part `style`, `theme` or `script`. * @return String|null The path. */ function bootswatch_get_bootstrap_part_path( $part ) { return bootswatch_get_bootstrap_part( $part, 'path' ); } /** * Get bootstrap part path. * * @param String $part `style`, `theme` or `script`. * @param String $type `uri` or `part`. * @return String|null The path. */ function bootswatch_get_bootstrap_part( $part, $type ) { if ( 'uri' === $type ) { $bootswatch_light = get_template_directory_uri() . '/vendor/kadimi/bootswatch-light/light/'; } else if ( 'path' === $type ) { $bootswatch_light = get_template_directory() . '/vendor/kadimi/bootswatch-light/light/'; } else { return false; } switch ( $part ) { case 'less': return $bootswatch_light . 'less/bootstrap.less'; case 'variables': return $bootswatch_light . 'less/variables.less'; case 'script': return $bootswatch_light . 'js/bootstrap.min.js'; case 'bootstrap': return $bootswatch_light . 'css/bootstrap.min.css'; case 'bootstrap.less': return $bootswatch_light . 'less/bootstrap.less'; case 'theme': return $bootswatch_light . 'css/bootstrap-theme.min.css'; case 'theme.less': return $bootswatch_light . 'less/theme.less'; } return false; } /** * Returns a list of available themes. * * @return Array The list. */ function bootswatch_themes_list() { return [ '' => __( 'Just Bootstrap', 'bootswatch' ), 'cerulean' => 'Cerulean', 'cosmo' => 'Cosmo', 'cyborg' => 'Cyborg', 'darkly' => 'Darkly', 'flatly' => 'Flatly', 'journal' => 'Journal', 'lumen' => 'Lumen', 'paper' => 'Paper', 'readable' => 'Readable', 'sandstone' => 'Sandstone', 'simplex' => 'Simplex', 'slate' => 'Slate', 'solar' => 'Solar', 'spacelab' => 'Spacelab', 'superhero' => 'Superhero', 'united' => 'United', 'yeti' => 'Yeti', ]; } function bootswatch_get_default_overrides( $theme = null ) { $overrides = [ '@icon-font-path' => '../vendor/kadimi/bootswatch-light/light/fonts/', ]; $overrides = apply_filters( 'bootswatch_default_overrides', $overrides, $theme ); return $overrides; }