function receptar_custom_css_replacements($replacements, $theme_options, $output) { //Helper variables $option_key = 100 . 'colors' . 210; //Preparing output if (!empty($replacements) && isset($theme_options[$option_key]) && isset($theme_options[$option_key]['id'])) { $value = ''; $option_id = $theme_options[$option_key]['id']; if (isset($theme_options[$option_id]['default'])) { $value = $theme_options[$option_id]['default']; } if ($mod = get_theme_mod($option_id)) { $value = $mod; } $value = '#' . trim($value, '#'); $replacements['[[' . $option_id . '|alpha=20]]'] = receptar_color_hex_to_rgba($value, 20); } //Output return $replacements; }
function receptar_custom_styles($set_cache = false, $return = true) { //Helper variables global $wp_customize; if (!isset($wp_customize) || !is_object($wp_customize)) { $wp_customize = null; } $output = (string) apply_filters('wmhook_custom_styles', ''); $theme_options = (array) apply_filters('wmhook_theme_options', array()); $replacements = array_filter((array) get_transient(WM_THEME_SHORTNAME . '-customizer-values')); //There have to be values (defaults) set! /** * Force caching during the first theme display when no cache set (default * values will be used). * Cache is being set only after saving Customizer. */ if (empty($replacements)) { $set_cache = true; } //Preparing output /** * Setting up replacements array when no cache exists. * Also, creates a new cache for replacements values. * The cache is being created only when saving the Customizer settings. */ if (!empty($theme_options) && ($wp_customize && $wp_customize->is_preview() || empty($replacements))) { foreach ($theme_options as $theme_option) { //Reset variables $option_id = $value = ''; //Set option ID if (isset($theme_option['id'])) { $option_id = $theme_option['id']; } //If no option ID set, jump to next option if (empty($option_id)) { continue; } //If we have an ID, get the default value if set if (isset($theme_option['default'])) { $value = $theme_option['default']; } //Get the option value saved in database and apply it when exists if ($mod = get_theme_mod($option_id)) { $value = $mod; } //Make sure the color value contains '#' if ('color' === $theme_option['type']) { $value = '#' . trim($value, '#'); } //Make sure the image URL is used in CSS format if ('image' === $theme_option['type']) { if (is_array($value) && isset($value['id'])) { $value = absint($value['id']); } if (is_numeric($value)) { $value = wp_get_attachment_image_src($value, 'full'); $value = $value[0]; } if (!empty($value)) { $value = "url('" . esc_url($value) . "')"; } else { $value = 'none'; } } //Value filtering $value = apply_filters('wmhook_receptar_custom_styles_value', $value, $theme_option); //Make array to string as otherwise the strtr() function throws error if (is_array($value)) { $value = (string) implode(',', (array) $value); } //Finally modify the output string $replacements['[[' . $option_id . ']]'] = $value; /** * Add also rgba() color interpratation * * Note that only alpha=0 is added as replacement option. * Other alpha values has to be added via custom function * hooked onto the $replacements filter below. */ if ('color' === $theme_option['type']) { $replacements['[[' . $option_id . '|alpha=0]]'] = receptar_color_hex_to_rgba($value, 0); } } // /foreach //Add WordPress Custom Background and Header support //Background color if ($value = get_background_color()) { $replacements['[[background_color]]'] = '#' . trim($value, '#'); $replacements['[[background_color|alpha=0]]'] = receptar_color_hex_to_rgba($value, 0); } //Background image if ($value = esc_url(get_background_image())) { $replacements['[[background_image]]'] = "url('" . $value . "')"; } else { $replacements['[[background_image]]'] = 'none'; } //Header text color if ($value = get_header_textcolor()) { $replacements['[[header_textcolor]]'] = '#' . trim($value, '#'); $replacements['[[header_textcolor|alpha=0]]'] = receptar_color_hex_to_rgba($value, 0); } //Header image if ($value = esc_url(get_header_image())) { $replacements['[[header_image]]'] = "url('" . $value . "')"; } else { $replacements['[[header_image]]'] = 'none'; } $replacements = apply_filters('wmhook_receptar_custom_styles_replace_replacements', $replacements, $theme_options, $output); if ($set_cache && !empty($replacements)) { set_transient(WM_THEME_SHORTNAME . '-customizer-values', $replacements); } } //Replace tags in custom CSS strings with actual values $output_cached = (string) get_transient(WM_THEME_SHORTNAME . '-custom-css'); //When debugging set if (isset($_GET['debug'])) { $output_cached = (string) get_transient(WM_THEME_SHORTNAME . '-custom-css-debug'); } if (empty($output_cached) || $wp_customize && $wp_customize->is_preview()) { //Replace tags in custom CSS strings with actual values $output = strtr($output, $replacements); if ($set_cache) { set_transient(WM_THEME_SHORTNAME . '-custom-css-debug', apply_filters('wmhook_receptar_custom_styles_output_cache_debug', $output)); set_transient(WM_THEME_SHORTNAME . '-custom-css', apply_filters('wmhook_receptar_custom_styles_output_cache', $output)); } } else { $output = $output_cached; } //Output if ($output && $return) { return (string) apply_filters('wmhook_receptar_custom_styles_output', trim($output)); } }