WordPress.org

Plugin Directory

Changeset 631022


Ignore:
Timestamp:
11/28/12 03:20:30 (17 months ago)
Author:
cfinke
Message:

Custom CSS: Support for CSS preprocessing, with LESS and SCSS included.

Plugins can add additional preprocessors (e.g., Stylus) or overwrite the LESS and SCSS processors with their own. See modules/custom-css/custom-css/preprocessors.php for documentation on registering a preprocessor.

scss.inc.php comes from https://github.com/leafo/scssphp/blob/master/scss.inc.php
lessc.inc.php comes from https://github.com/leafo/lessphp/blob/master/lessc.inc.php

props @betzster

Fixes #1596.

Location:
jetpack/trunk/modules/custom-css
Files:
5 added
1 edited

Legend:

Unmodified
Added
Removed
  • jetpack/trunk/modules/custom-css/custom-css.php

    r627844 r631022  
    201201 *                  or revision was saved, returns the post ID. 
    202202 */ 
    203 function save_revision( $css, $is_preview = false ) { 
     203function save_revision( $css, $is_preview = false, $preprocessor = '' ) { 
    204204    $safecss_post = get_safecss_post(); 
    205205 
    206     $compressed_css = custom_css_minify( $css ); 
     206    $compressed_css = custom_css_minify( $css, $preprocessor ); 
    207207 
    208208    // If null, there was no original safecss record, so create one 
     
    372372            $warnings[] = 'kses found stuff'; 
    373373 
    374         do_action( 'safecss_parse_pre', $csstidy, $css ); 
    375  
    376         $csstidy->parse($css); 
    377  
    378         do_action( 'safecss_parse_post', $csstidy, $warnings ); 
    379  
    380         $css = $csstidy->print->plain(); 
     374        // if we're not using a preprocessor 
     375        if ( ! isset( $_POST['custom_css_preprocessor'] ) || empty( $_POST['custom_css_preprocessor'] ) ) { 
     376            do_action( 'safecss_parse_pre', $csstidy, $css ); 
     377 
     378            $csstidy->parse($css); 
     379 
     380            do_action( 'safecss_parse_post', $csstidy, $warnings ); 
     381 
     382            $css = $csstidy->print->plain(); 
     383        } 
    381384 
    382385        if ( isset( $_POST['custom_content_width'] ) && intval($_POST['custom_content_width']) > 0 ) 
     
    390393            $add_to_existing = 'no'; 
    391394 
     395        $preprocessor = isset( $_POST['custom_css_preprocessor'] ) ? $_POST['custom_css_preprocessor'] : ''; 
     396 
    392397        if ( $_POST['action'] == 'preview' || safecss_is_freetrial() ) { 
    393398            // Save the CSS 
    394             $safecss_revision_id = save_revision( $css, true ); 
     399            $safecss_revision_id = save_revision( $css, true, $preprocessor ); 
    395400 
    396401            // Cache Buster 
     
    399404            update_metadata( 'post', $safecss_revision_id, 'custom_css_add', $add_to_existing ); 
    400405            update_metadata( 'post', $safecss_revision_id, 'content_width', $custom_content_width ); 
     406            update_metadata( 'post', $safecss_revision_id, 'custom_css_preprocessor', $preprocessor ); 
    401407 
    402408            if ( $_POST['action'] == 'preview' ) { 
     
    409415 
    410416        // Save the CSS 
    411         $safecss_post_id = save_revision( $css ); 
     417        $safecss_post_id = save_revision( $css, false, $preprocessor ); 
    412418 
    413419        $safecss_post_revision = get_current_revision(); 
     
    417423        update_post_meta( $safecss_post_id, 'custom_css_add', $add_to_existing ); 
    418424        update_post_meta( $safecss_post_id, 'content_width', $custom_content_width ); 
     425        update_post_meta( $safecss_post_id, 'custom_css_preprocessor', $preprocessor ); 
    419426        update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_add', $add_to_existing ); 
    420427        update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $custom_content_width ); 
     428        update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_preprocessor', $preprocessor ); 
    421429 
    422430        add_action('admin_notices', 'safecss_saved'); 
     
    474482        $css = $safecss_post['post_content']; 
    475483        $css = stripslashes( $css ); 
    476         $css = custom_css_minify( $css ); 
     484        $css = custom_css_minify( $css, get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ) ); 
    477485    } 
    478486 
     
    930938    <?php 
    931939 
     940    $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
     941 
     942    if ( ! empty( $preprocessors ) ) { 
     943        ?> 
     944        <p> 
     945            <?php esc_html_e( 'Preprocessor:' ); ?> 
     946            <select name="custom_css_preprocessor"> 
     947                <option value=""><?php esc_html_e( 'None' ); ?></option> 
     948                <?php 
     949 
     950                foreach ( $preprocessors as $preprocessor_key => $preprocessor ) { 
     951                    ?> 
     952                    <option value="<?php echo esc_attr( $preprocessor_key ); ?>" <?php selected( get_post_meta( $safecss_post['ID'], 'custom_css_preprocessor', true ), $preprocessor_key ); ?>><?php echo esc_html( $preprocessor['name'] ); ?></option> 
     953                    <?php 
     954                } 
     955 
     956                ?> 
     957            </select> 
     958        </p> 
     959        <?php 
     960 
     961    } 
     962 
    932963    do_action( 'custom_css_meta_fields' ); 
    933964 
     
    10391070    update_post_meta( $safecss_post_id, 'custom_css_add', 'yes' ); 
    10401071    update_post_meta( $safecss_post_id, 'content_width', false ); 
     1072    update_post_meta( $safecss_post_id, 'custom_css_preprocessor', '' ); 
    10411073    update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', 'yes' ); 
    10421074    update_metadata( 'post', $safecss_revision['ID'], 'content_width', false ); 
     1075    update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', '' ); 
    10431076} 
    10441077 
     
    10521085} 
    10531086 
    1054 function custom_css_minify( $css ) { 
     1087function custom_css_minify( $css, $preprocessor = '' ) { 
    10551088    if ( ! $css ) 
    10561089        return ''; 
     1090 
     1091    if ( $preprocessor ) { 
     1092        $preprocessors = apply_filters( 'jetpack_custom_css_preprocessors', array() ); 
     1093 
     1094        if ( isset( $preprocessors[$preprocessor] ) ) { 
     1095            $css = call_user_func( $preprocessors[$preprocessor]['callback'], $css ); 
     1096        } 
     1097    } 
    10571098 
    10581099    safecss_class(); 
     
    10871128    $content_width = get_post_meta( $_revision_id, 'content_width', true ); 
    10881129    $custom_css_add = get_post_meta( $_revision_id, 'custom_css_add', true ); 
     1130    $preprocessor = get_post_meta( $_revision_id, 'custom_css_preprocessor', true ); 
    10891131 
    10901132    update_metadata( 'post', $safecss_revision['ID'], 'content_width', $content_width ); 
    10911133    update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', $custom_css_add ); 
     1134    update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', $preprocessor ); 
    10921135    update_post_meta( $_post->ID, 'content_width', $content_width ); 
    10931136    update_post_meta( $_post->ID, 'custom_css_add', $custom_css_add ); 
     1137    update_post_meta( $_post->ID, 'custom_css_preprocessor', $preprocessor ); 
    10941138} 
    10951139 
    10961140add_action( 'wp_restore_post_revision', 'custom_css_restore_revision', 10, 2 ); 
     1141 
     1142include dirname( __FILE__ ) . '/custom-css/preprocessors.php'; 
Note: See TracChangeset for help on using the changeset viewer.