/**
  * Custom Colors
  *
  * This feature is experimental. It relies on Jetpack's Custom CSS module
  * and the availability of a SASS preprocessor function.
  */
 public static function custom_colors()
 {
     $colors = get_theme_mod('semicolon-colors', self::$defaults['colors']);
     $colors = wp_parse_args($colors, self::$defaults['colors']);
     $custom = false;
     foreach (self::$defaults['colors'] as $key => $default) {
         if (strtolower($colors[$key]) != $default) {
             $custom = true;
             break;
         }
     }
     if (get_theme_mod('semicolon-colors-auto-contrast', true) && get_background_color() != get_theme_support('custom-background', 'default-color')) {
         $custom = true;
     }
     // At least one custom color should be set for an override.
     if (!$custom) {
         return;
     }
     $css = get_theme_mod('semicolon-colors-css', false);
     $hash = get_theme_mod('semicolon-colors-hash', false);
     $new_hash = md5(serialize(array_merge($colors, array('version' => self::$colors_css_version, 'background-color' => get_background_color(), 'auto-contrast' => get_theme_mod('semicolon-colors-auto-contrast', true)))));
     // Cache with a hash and then smash.
     if ($hash !== $new_hash) {
         // Somebody can preview Semicolon without activating it, let's not
         // pollute the database with our theme mods.
         if (!self::is_customize_preview()) {
             // Set these early, just in case everything else fails or fatals.
             set_theme_mod('semicolon-colors-hash', $new_hash);
             set_theme_mod('semicolon-colors-css', '');
         }
         // There's a special semicolon-override marker in the .sass file.
         $override = sprintf('$color-background: #%s;' . PHP_EOL, get_background_color());
         foreach ($colors as $key => $value) {
             $override .= sprintf('$color-%s: %s;' . PHP_EOL, $key, $value);
         }
         if (get_theme_mod('semicolon-colors-auto-contrast', true)) {
             $override .= '$auto-contrast: true;' . PHP_EOL;
         }
         // Retrieve the Sass file and then run some replacements.
         $sass = self::custom_colors_get_sass();
         if (empty($sass)) {
             return;
         }
         $sass = preg_replace('/^.*?semicolon-override.*$/im', $override, $sass);
         $css = jetpack_sass_css_preprocess($sass);
         // Something went wrong, so don't display raw sass.
         if (empty($css) || strpos($css, 'this string should never appear in the compiled stylesheet') !== false) {
             return;
         }
         // Minify the CSS if possible.
         if (method_exists('Jetpack_Custom_CSS', 'minify')) {
             $css = Jetpack_Custom_CSS::minify($css);
         }
         // Don't write to the db in preview mode.
         if (!self::is_customize_preview()) {
             set_theme_mod('semicolon-colors-css', $css);
         }
     }
     // Dequeue the default colors css.
     wp_dequeue_style('semicolon-colors');
     return $css;
 }
Exemple #2
0
 static function get_css($compressed = false)
 {
     $default_css = apply_filters('safecss_get_css_error', false);
     if ($default_css !== false) {
         return $default_css;
     }
     $option = Jetpack_Custom_CSS::is_preview() || Jetpack_Custom_CSS::is_freetrial() ? 'safecss_preview' : 'safecss';
     if ('safecss' == $option) {
         if (get_option('safecss_revision_migrated')) {
             $safecss_post = Jetpack_Custom_CSS::get_post();
             $css = $compressed && $safecss_post['post_content_filtered'] ? $safecss_post['post_content_filtered'] : $safecss_post['post_content'];
         } else {
             $current_revision = Jetpack_Custom_CSS::get_current_revision();
             if (false === $current_revision) {
                 $css = '';
             } else {
                 $css = $compressed && $current_revision['post_content_filtered'] ? $current_revision['post_content_filtered'] : $current_revision['post_content'];
             }
         }
         // Fix for un-migrated Custom CSS
         if (empty($safecss_post)) {
             $_css = get_option('safecss');
             if (!empty($_css)) {
                 $css = $_css;
             }
         }
     } else {
         if ('safecss_preview' == $option) {
             $safecss_post = Jetpack_Custom_CSS::get_current_revision();
             $css = $safecss_post['post_content'];
             $css = stripslashes($css);
             $css = Jetpack_Custom_CSS::minify($css, get_post_meta($safecss_post['ID'], 'custom_css_preprocessor', true));
         }
     }
     $css = str_replace(array('\\\\00BB \\\\0020', '\\0BB \\020', '0BB 020'), '\\00BB \\0020', $css);
     if (empty($css)) {
         $css = "/*\n" . wordwrap(apply_filters('safecss_default_css', __("Welcome to Custom CSS!\n\nCSS (Cascading Style Sheets) is a kind of code that tells the browser how to render a web page. You may delete these comments and get started with your customizations.\n\nBy default, your stylesheet will be loaded after the theme stylesheets, which means that your rules can take precedence and override the theme CSS rules. Just write here what you want to change, you don't need to copy all your theme's stylesheet content.", 'jetpack'))) . "\n*/";
     }
     $css = apply_filters('safecss_css', $css);
     return $css;
 }
Exemple #3
0
 static function get_css($compressed = false)
 {
     /**
      * Filter the Custom CSS returned.
      * Can be used to return an error, or no CSS at all.
      *
      * @module custom-css
      *
      * @since 1.7.0
      *
      * @param bool false Should we return an error instead of the Custom CSS. Default to false.
      */
     $default_css = apply_filters('safecss_get_css_error', false);
     if ($default_css !== false) {
         return $default_css;
     }
     $option = Jetpack_Custom_CSS::is_preview() || Jetpack_Custom_CSS::is_freetrial() ? 'safecss_preview' : 'safecss';
     $css = '';
     if ('safecss' == $option) {
         // Don't bother checking for a migrated 'safecss' option if it never existed.
         if (false === get_option('safecss') || get_option('safecss_revision_migrated')) {
             $safecss_post = Jetpack_Custom_CSS::get_post();
             if (!empty($safecss_post)) {
                 $css = $compressed && $safecss_post['post_content_filtered'] ? $safecss_post['post_content_filtered'] : $safecss_post['post_content'];
             }
         } else {
             $current_revision = Jetpack_Custom_CSS::get_current_revision();
             if (false === $current_revision) {
                 $css = '';
             } else {
                 $css = $compressed && $current_revision['post_content_filtered'] ? $current_revision['post_content_filtered'] : $current_revision['post_content'];
             }
         }
         // Fix for un-migrated Custom CSS
         if (empty($safecss_post)) {
             $_css = get_option('safecss');
             if (!empty($_css)) {
                 $css = $_css;
             }
         }
     } else {
         if ('safecss_preview' == $option) {
             $safecss_post = Jetpack_Custom_CSS::get_current_revision();
             $css = $safecss_post['post_content'];
             $css = stripslashes($css);
             $css = Jetpack_Custom_CSS::minify($css, get_post_meta($safecss_post['ID'], 'custom_css_preprocessor', true));
         }
     }
     $css = str_replace(array('\\\\00BB \\\\0020', '\\0BB \\020', '0BB 020'), '\\00BB \\0020', $css);
     if (empty($css)) {
         $css = "/*\n" . wordwrap(apply_filters('safecss_default_css', __("Welcome to Custom CSS!\n\nTo learn how this works, see http://wp.me/PEmnE-Bt", 'jetpack'))) . "\n*/";
     }
     /**
      * Filter the Custom CSS returned from the editor.
      *
      * @module custom-css
      *
      * @since 1.7.0
      *
      * @param string $css Custom CSS.
      */
     $css = apply_filters('safecss_css', $css);
     return $css;
 }
 /**
  * Parse some CSS and make sure it's valid before outputting it in the page
  */
 function parse_css($css = null)
 {
     if (empty($css)) {
         return null;
     }
     if (!defined('JETPACK__VERSION')) {
         return $this->inst_css_parser($css);
     } else {
         if (!class_exists('Jetpack_Custom_CSS', false)) {
             if (function_exists('jetpack_load_custom_css')) {
                 jetpack_load_custom_css();
             }
             // Still here? Load module manually.
             if (!class_exists('Jetpack_Custom_CSS', false)) {
                 require JETPACK__PLUGIN_DIR . 'modules/custom-css/custom-css.php';
             }
         }
         $css = @Jetpack_Custom_CSS::minify($css, 'sass');
         if (empty($css)) {
             return null;
         }
         return $css;
     }
 }