diff options
Diffstat (limited to 'plugins/jetpack/modules/custom-css/custom-css.php')
-rw-r--r-- | plugins/jetpack/modules/custom-css/custom-css.php | 425 |
1 files changed, 216 insertions, 209 deletions
diff --git a/plugins/jetpack/modules/custom-css/custom-css.php b/plugins/jetpack/modules/custom-css/custom-css.php index 9bc0c211..02fa7586 100644 --- a/plugins/jetpack/modules/custom-css/custom-css.php +++ b/plugins/jetpack/modules/custom-css/custom-css.php @@ -47,6 +47,8 @@ class Jetpack_Custom_CSS { exit; } + add_action( 'admin_enqueue_scripts', array( 'Jetpack_Custom_CSS', 'enqueue_scripts' ) ); + if ( isset( $_GET['page'] ) && 'editcss' == $_GET['page'] && is_admin() ) { // Do migration routine if necessary Jetpack_Custom_CSS::upgrade(); @@ -56,6 +58,9 @@ class Jetpack_Custom_CSS { add_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 ); + add_filter( 'jetpack_content_width', array( 'Jetpack_Custom_CSS', 'jetpack_content_width' ) ); + add_filter( 'editor_max_image_size', array( 'Jetpack_Custom_CSS', 'editor_max_image_size' ), 10, 3 ); + if ( !current_user_can( 'switch_themes' ) && !is_super_admin() ) return; @@ -65,7 +70,7 @@ class Jetpack_Custom_CSS { check_admin_referer( 'safecss' ); $save_result = self::save( array( - 'css' => $_POST['safecss'], + 'css' => stripslashes( $_POST['safecss'] ), 'is_preview' => isset( $_POST['action'] ) && $_POST['action'] == 'preview', 'preprocessor' => isset( $_POST['custom_css_preprocessor'] ) ? $_POST['custom_css_preprocessor'] : '', 'add_to_existing' => isset( $_POST['add_to_existing'] ) ? $_POST['add_to_existing'] == 'true' : true, @@ -108,6 +113,11 @@ class Jetpack_Custom_CSS { $args = wp_parse_args( $args, $defaults ); + if ( $args['content_width'] && intval( $args['content_width']) > 0 && ( ! isset( $GLOBALS['content_width'] ) || $args['content_width'] != $GLOBALS['content_width'] ) ) + $args['content_width'] = intval( $args['content_width'] ); + else + $args['content_width'] = false; + // Remove wp_filter_post_kses, this causes CSS escaping issues remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); @@ -132,7 +142,7 @@ class Jetpack_Custom_CSS { $csstidy->set_cfg( 'preserve_css', true ); $csstidy->set_cfg( 'template', dirname( __FILE__ ) . '/csstidy/wordpress-standard.tpl' ); - $css = $orig = stripslashes( $args['css'] ); + $css = $orig = $args['css']; $css = preg_replace( '/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $prev = $css ); @@ -161,11 +171,6 @@ class Jetpack_Custom_CSS { $css = $csstidy->print->plain(); } - if ( $args['content_width'] && intval( $args['content_width']) > 0 && ( ! isset( $GLOBALS['content_width'] ) || $args['content_width'] != $GLOBALS['content_width'] ) ) - $custom_content_width = intval( $args['content_width'] ); - else - $custom_content_width = false; - if ( $args['add_to_existing'] ) $add_to_existing = 'yes'; else @@ -179,9 +184,12 @@ class Jetpack_Custom_CSS { update_option( 'safecss_preview_rev', intval( get_option( 'safecss_preview_rev' ) ) + 1); update_metadata( 'post', $safecss_revision_id, 'custom_css_add', $add_to_existing ); - update_metadata( 'post', $safecss_revision_id, 'content_width', $custom_content_width ); + update_metadata( 'post', $safecss_revision_id, 'content_width', $args['content_width'] ); update_metadata( 'post', $safecss_revision_id, 'custom_css_preprocessor', $args['preprocessor'] ); + delete_option( 'safecss_add' ); + delete_option( 'safecss_content_width' ); + if ( $args['is_preview'] ) { return $safecss_revision_id; } @@ -198,12 +206,18 @@ class Jetpack_Custom_CSS { update_option( 'safecss_rev', intval( get_option( 'safecss_rev' ) ) + 1 ); update_post_meta( $safecss_post_id, 'custom_css_add', $add_to_existing ); - update_post_meta( $safecss_post_id, 'content_width', $custom_content_width ); + update_post_meta( $safecss_post_id, 'content_width', $args['content_width'] ); update_post_meta( $safecss_post_id, 'custom_css_preprocessor', $args['preprocessor'] ); + + delete_option( 'safecss_add' ); + delete_option( 'safecss_content_width' ); + update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_add', $add_to_existing ); - update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $custom_content_width ); + update_metadata( 'post', $safecss_post_revision['ID'], 'content_width', $args['content_width'] ); update_metadata( 'post', $safecss_post_revision['ID'], 'custom_css_preprocessor', $args['preprocessor'] ); + delete_option( 'safecss_preview_add' ); + return $safecss_post_id; } @@ -230,16 +244,16 @@ class Jetpack_Custom_CSS { $custom_css_post_id = wp_cache_get( 'custom_css_post_id' ); if ( false === $custom_css_post_id ) { - $custom_css_post = array_shift( get_posts( array( + $custom_css_posts = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'safecss', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC' - ) ) ); + ) ); - if ( $custom_css_post ) - $custom_css_post_id = $custom_css_post->ID; + if ( count( $custom_css_posts ) > 0 ) + $custom_css_post_id = $custom_css_posts[0]->ID; else $custom_css_post_id = 0; @@ -350,19 +364,35 @@ class Jetpack_Custom_CSS { } static function skip_stylesheet() { - if ( Jetpack_Custom_CSS::is_customizer_preview() ) { + $skip_stylesheet = apply_filters( 'safecss_skip_stylesheet', null ); + + if ( null !== $skip_stylesheet ) { + return $skip_stylesheet; + } elseif ( Jetpack_Custom_CSS::is_customizer_preview() ) { return false; - } - else { + } else { if ( Jetpack_Custom_CSS::is_preview() ) { $safecss_post = Jetpack_Custom_CSS::get_current_revision(); - return (bool) ( get_option( 'safecss_preview_add' ) == 'no' || get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' ); + if ( $safecss_post ) + return (bool) ( get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' ); + else + return (bool) ( get_option( 'safecss_preview_add' ) == 'no' ); } else { $custom_css_post_id = Jetpack_Custom_CSS::post_id(); - return (bool) ( get_option( 'safecss_add' ) == 'no' || ( $custom_css_post_id && get_post_meta( $custom_css_post_id, 'custom_css_add', true ) == 'no' ) ); + if ( $custom_css_post_id ) { + $custom_css_add = get_post_meta( $custom_css_post_id, 'custom_css_add', true ); + + // It is possible for the CSS to be stored in a post but for the safecss_add option + // to have not been upgraded yet if the user hasn't opened their Custom CSS editor + // since October 2012. + if ( ! empty( $custom_css_add ) ) + return (bool) ( $custom_css_add === 'no' ); + } + + return (bool) ( get_option( 'safecss_add' ) == 'no' ); } } } @@ -494,7 +524,7 @@ class Jetpack_Custom_CSS { if ( $css == '' ) return; - $href = trailingslashit( site_url() ); + $href = home_url( '/' ); $href = add_query_arg( 'custom-css', 1, $href ); $href = add_query_arg( 'csblog', $blog_id, $href ); $href = add_query_arg( 'cscache', 6, $href ); @@ -508,6 +538,8 @@ class Jetpack_Custom_CSS { ?> <link rel="stylesheet" id="custom-css-css" type="text/css" href="<?php echo esc_url( $href ); ?>" /> <?php + + do_action( 'safecss_link_tag_post' ); } static function style_filter( $current ) { @@ -571,7 +603,7 @@ class Jetpack_Custom_CSS { $parent = 'themes.php'; $title = __( 'Edit CSS', 'jetpack' ); $hook = add_theme_page( $title, $title, 'edit_theme_options', 'editcss', array( 'Jetpack_Custom_CSS', 'admin' ) ); - add_action( "admin_print_scripts-$hook", array( 'Jetpack_Custom_CSS', 'enqueue_scripts' ) ); + add_action( "admin_head-$hook", array( 'Jetpack_Custom_CSS', 'admin_head' ) ); add_action( "load-revision.php", array( 'Jetpack_Custom_CSS', 'prettify_post_revisions' ) ); add_action( "load-$hook", array( 'Jetpack_Custom_CSS', 'update_title' ) ); @@ -588,26 +620,6 @@ class Jetpack_Custom_CSS { static function prettify_post_revisions() { add_filter( 'the_title', array( 'Jetpack_Custom_CSS', 'post_title' ), 10, 2 ); - add_action( 'admin_head', array( 'Jetpack_Custom_CSS', 'remove_title_excerpt_from_revisions' ) ); - } - - static function remove_title_excerpt_from_revisions() { - global $post; - - if ( !$post ) { - return; - } - - if ( 'safecss' != $post->post_type ) { - return; - } - ?> - <style type="text/css"> - #revision-field-post_title, #revision-field-post_excerpt { - display: none; - } - </style> - <?php } static function post_title( $title, $post_id ) { @@ -626,159 +638,39 @@ class Jetpack_Custom_CSS { return __( 'Custom CSS Stylesheet', 'jetpack' ); } - static function enqueue_scripts() { + static function enqueue_scripts( $hook ) { + if ( 'appearance_page_editcss' != $hook ) + return; + wp_enqueue_script( 'postbox' ); + wp_enqueue_script( 'custom-css-editor', plugins_url( 'custom-css/js/css-editor.js', __FILE__ ), 'jquery', '20130325', true ); + wp_enqueue_style( 'custom-css-editor', plugins_url( 'custom-css/css/css-editor.css', __FILE__ ) ); if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { $url = plugins_url( 'custom-css/js/', __FILE__ ); + wp_enqueue_script( 'jquery.spin' ); wp_enqueue_script( 'safecss-ace', $url . 'ace/ace.js', array(), '20130213', true ); wp_enqueue_script( 'safecss-ace-css', $url . 'ace/mode-css.js', array( 'safecss-ace' ), '20130213', true ); + wp_enqueue_script( 'safecss-ace-less', $url . 'ace/mode-less.js', array( 'safecss-ace' ), '20130213', true ); + wp_enqueue_script( 'safecss-ace-scss', $url . 'ace/mode-scss.js', array( 'safecss-ace' ), '20130213', true ); wp_enqueue_script( 'safecss-ace-use', $url . 'safecss-ace.js', array( 'jquery', 'safecss-ace-css' ), '20130213', true ); + + wp_enqueue_style( 'custom-css-ace', plugins_url( 'custom-css/css/ace.css', __FILE__ ) ); } } static function admin_head() { - ?> - <style type="text/css"> - #safecssform { - position: relative; - } - - #poststuff { - padding-top: 0; - } - - #safecss { - min-height: 250px; - width: 100%; - } - - .misc-pub-section > span { - font-weight: bold; - } - - .misc-pub-section > div { - margin-top: 3px; - } - - <?php - - if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { - - ?> - #safecss-container { - position: relative; - width: 99.5%; - height: 400px; - border: 1px solid #dfdfdf; - border-radius: 3px; - } - - #safecss-container .ace_editor { - font-family: Consolas, Monaco, Courier, monospace; - } - - #safecss-ace { - width: 100%; - height: 100%; - display: none; /* Hide on load otherwise it looks weird */ - } - - #safecss-ace.ace_editor { - display: block; - } - - #safecss-container .ace-tm .ace_gutter { - background-color: #ededed; - } - <?php - } - + if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { ?> - </style> - <script type="text/javascript"> - /*<![CDATA[*/ - var safecssResize, safecssInit; - + <script type="text/javascript"> + /*<![CDATA[*/ + var SAFECSS_USE_ACE = true; + var safecssAceSrcPath = <?php echo json_encode( parse_url( plugins_url( 'custom-css/js/ace/', __FILE__ ), PHP_URL_PATH ) ); ?>; + /*]]>*/ + </script> <?php - - if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { - ?>var safecssAceSrcPath = <?php echo json_encode( parse_url( plugins_url( 'custom-css/js/ace/', __FILE__ ), PHP_URL_PATH ) ); ?>;<?php - } - - ?> - - ( function ( $ ) { - var safe, win; - - safecssResize = function () { - safe.height( win.height() - safe.offset().top - 250 ); - }; - - safecssInit = function() { - safe = $('#safecss'); - win = $(window); - - postboxes.add_postbox_toggles('editcss'); - safecssResize(); - var button = document.getElementById( 'preview' ); - button.onclick = function ( event ) { - <?php - - // hack for now for previewing. - // TODO: move all of this JS into its own file. - if ( defined( 'SAFECSS_USE_ACE' ) && SAFECSS_USE_ACE ) { echo "\t\taceSyncCSS();\n"; } - - ?> - document.forms["safecssform"].target = "csspreview"; - document.forms["safecssform"].action.value = 'preview'; - document.forms["safecssform"].submit(); - document.forms["safecssform"].target = ""; - document.forms["safecssform"].action.value = 'save'; - - event = event || window.event; - - if ( event.preventDefault ) - event.preventDefault(); - - return false; - } - }; - - window.onresize = safecssResize; - addLoadEvent( safecssInit ); - } )( jQuery ); - - jQuery( function ( $ ) { - $( '.edit-preprocessor' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#preprocessor-select' ).slideDown(); - $( this ).hide(); - } ); - - $( '.cancel-preprocessor' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#preprocessor-select' ).slideUp( function () { - $( '.edit-preprocessor' ).show(); - $( '#preprocessor_choices' ).val( $( '#custom_css_preprocessor' ).val() ); - } ); - } ); - - $( '.save-preprocessor' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#preprocessor-select' ).slideUp(); - $( '#preprocessor-display' ).text( $( '#preprocessor_choices option:selected' ).text() ); - $( '#custom_css_preprocessor' ).val( $( '#preprocessor_choices' ).val() ); - $( '.edit-preprocessor' ).show(); - } ); - } ); - /*]]>*/ - </script> - <?php + } } static function saved_message() { @@ -787,12 +679,12 @@ class Jetpack_Custom_CSS { static function admin() { add_meta_box( 'submitdiv', __( 'Publish', 'jetpack' ), array( __CLASS__, 'publish_box' ), 'editcss', 'side' ); + add_action( 'custom_css_submitbox_misc_actions', array( __CLASS__, 'content_width_settings' ) ); $safecss_post = Jetpack_Custom_CSS::get_post(); if ( ! empty( $safecss_post ) && 0 < $safecss_post['ID'] && wp_get_post_revisions( $safecss_post['ID'] ) ) add_meta_box( 'revisionsdiv', __( 'CSS Revisions', 'jetpack' ), array( __CLASS__, 'revisions_meta_box' ), 'editcss', 'side' ); - ?> <div class="wrap columns-2"> <?php do_action( 'custom_design_header' ); ?> @@ -833,6 +725,103 @@ class Jetpack_Custom_CSS { <?php } + /** + * Content width setting callback + */ + static function content_width_settings() { + $safecss_post = Jetpack_Custom_CSS::get_current_revision(); + + $custom_content_width = get_post_meta( $safecss_post['ID'], 'content_width', true ); + + // If custom content width hasn't been overridden and the theme has a content_width value, use that as a default. + if ( $custom_content_width <= 0 && ! empty( $GLOBALS['content_width'] ) ) + $custom_content_width = $GLOBALS['content_width']; + + if ( ! $custom_content_width || ( isset( $GLOBALS['content_width'] ) && $custom_content_width == $GLOBALS['content_width'] ) ) + $custom_content_width = ''; + + ?> + <div class="misc-pub-section"> + <label><?php esc_html_e( 'Content Width:', 'jetpack' ); ?></label> + <span id="content-width-display" data-default-text="<?php esc_attr_e( 'Default', 'jetpack' ); ?>" data-custom-text="<?php esc_attr_e( '%s px', 'jetpack' ); ?>"><?php echo $custom_content_width ? sprintf( esc_html__( '%s px', 'jetpack' ), $custom_content_width ) : esc_html_e( 'Default', 'jetpack' ); ?></span> + <a class="edit-content-width hide-if-no-js" href="#content-width"><?php echo esc_html_e( 'Edit', 'jetpack' ); ?></a> + <div id="content-width-select" class="hide-if-js"> + <input type="hidden" name="custom_content_width" id="custom_content_width" value="<?php echo esc_attr( $custom_content_width ); ?>" /> + <p> + <?php + + printf( + __( 'Limit width to %1$s pixels for videos, full size images, and other shortcodes. (<a href="%2$s">More info</a>.)', 'jetpack' ), + '<input type="text" id="custom_content_width_visible" value="' . esc_attr( $custom_content_width ) . '" size="4" />', + apply_filters( 'safecss_limit_width_link', 'http://jetpack.me/support/custom-css/#limited-width' ) + ); + + ?> + </p> + <?php + + if ( !empty( $GLOBALS['content_width'] ) && $custom_content_width != $GLOBALS['content_width'] ) { + if ( function_exists( 'wp_get_theme' ) ) + $current_theme = wp_get_theme()->Name; + else + $current_theme = get_current_theme(); + + ?> + <p><?php printf( __( 'The default content width for the %s theme is %d pixels.', 'jetpack' ), $current_theme, intval( $GLOBALS['content_width'] ) ); ?></p> + <?php + } + + ?> + <a class="save-content-width hide-if-no-js button" href="#content-width"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> + <a class="cancel-content-width hide-if-no-js" href="#content-width"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> + </div> + <script type="text/javascript"> + jQuery( function ( $ ) { + var defaultContentWidth = <?php echo isset( $GLOBALS['content_width'] ) ? json_encode( intval( $GLOBALS['content_width'] ) ) : 0; ?>; + + $( '.edit-content-width' ).bind( 'click', function ( e ) { + e.preventDefault(); + + $( '#content-width-select' ).slideDown(); + $( this ).hide(); + } ); + + $( '.cancel-content-width' ).bind( 'click', function ( e ) { + e.preventDefault(); + + $( '#content-width-select' ).slideUp( function () { + $( '.edit-content-width' ).show(); + $( '#custom_content_width_visible' ).val( $( '#custom_content_width' ).val() ); + } ); + } ); + + $( '.save-content-width' ).bind( 'click', function ( e ) { + e.preventDefault(); + + $( '#content-width-select' ).slideUp(); + + var newContentWidth = parseInt( $( '#custom_content_width_visible' ).val(), 10 ); + + if ( newContentWidth && newContentWidth != defaultContentWidth ) { + $( '#content-width-display' ).text( + $( '#content-width-display' ) + .data( 'custom-text' ) + .replace( '%s', $( '#custom_content_width_visible' ).val() ) + ); + } + else { + $( '#content-width-display' ).text( $( '#content-width-display' ).data( 'default-text' ) ); + } + + $( '#custom_content_width' ).val( $( '#custom_content_width_visible' ).val() ); + $( '.edit-content-width' ).show(); + } ); + } ); + </script> + </div> + <?php + } + static function publish_box() { ?> <div id="minor-publishing"> @@ -897,34 +886,6 @@ class Jetpack_Custom_CSS { <a class="save-css-mode hide-if-no-js button" href="#css-mode"><?php esc_html_e( 'OK', 'jetpack' ); ?></a> <a class="cancel-css-mode hide-if-no-js" href="#css-mode"><?php esc_html_e( 'Cancel', 'jetpack' ); ?></a> </div> - <script type="text/javascript"> - jQuery( function ( $ ) { - $( '.edit-css-mode' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#css-mode-select' ).slideDown(); - $( this ).hide(); - } ); - - $( '.cancel-css-mode' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#css-mode-select' ).slideUp( function () { - $( '.edit-css-mode' ).show(); - $( 'input[name=add_to_existing_display][value=' + $( '#add_to_existing' ).val() + ']' ).attr( 'checked', true ); - } ); - } ); - - $( '.save-css-mode' ).bind( 'click', function ( e ) { - e.preventDefault(); - - $( '#css-mode-select' ).slideUp(); - $( '#css-mode-display' ).text( $( 'input[name=add_to_existing_display]:checked' ).val() == 'true' ? 'Add-on' : 'Replacement' ); - $( '#add_to_existing' ).val( $( 'input[name=add_to_existing_display]:checked' ).val() ); - $( '.edit-css-mode' ).show(); - } ); - } ); - </script> </div> <?php do_action( 'custom_css_submitbox_misc_actions' ); ?> </div> @@ -1012,9 +973,15 @@ class Jetpack_Custom_CSS { update_post_meta( $safecss_post_id, 'custom_css_add', 'yes' ); update_post_meta( $safecss_post_id, 'content_width', false ); update_post_meta( $safecss_post_id, 'custom_css_preprocessor', '' ); + + delete_option( 'safecss_add' ); + delete_option( 'safecss_content_width' ); + update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', 'yes' ); update_metadata( 'post', $safecss_revision['ID'], 'content_width', false ); update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', '' ); + + delete_option( 'safecss_preview_add' ); } static function is_customizer_preview() { @@ -1072,9 +1039,15 @@ class Jetpack_Custom_CSS { update_metadata( 'post', $safecss_revision['ID'], 'content_width', $content_width ); update_metadata( 'post', $safecss_revision['ID'], 'custom_css_add', $custom_css_add ); update_metadata( 'post', $safecss_revision['ID'], 'custom_css_preprocessor', $preprocessor ); + + delete_option( 'safecss_add' ); + delete_option( 'safecss_content_width' ); + update_post_meta( $_post->ID, 'content_width', $content_width ); update_post_meta( $_post->ID, 'custom_css_add', $custom_css_add ); update_post_meta( $_post->ID, 'custom_css_preprocessor', $preprocessor ); + + delete_option( 'safecss_preview_add' ); } /** @@ -1194,6 +1167,40 @@ class Jetpack_Custom_CSS { return esc_url_raw( $post_link ); } + + /** + * When on the edit screen, make sure the custom content width + * setting is applied to the large image size. + */ + static function editor_max_image_size( $dims, $size = 'medium', $context = null ) { + list( $width, $height ) = $dims; + + if ( 'large' == $size && 'edit' == $context ) + $width = Jetpack::get_content_width(); + + return array( $width, $height ); + } + + /** + * Override the content_width with a custom value if one is set. + */ + static function jetpack_content_width( $content_width ) { + $custom_content_width = 0; + + if ( Jetpack_Custom_CSS::is_preview() ) { + $safecss_post = Jetpack_Custom_CSS::get_current_revision(); + $custom_content_width = intval( get_post_meta( $safecss_post['ID'], 'content_width', true ) ); + } else if ( ! Jetpack_Custom_CSS::is_freetrial() ) { + $custom_css_post_id = Jetpack_Custom_CSS::post_id(); + if ( $custom_css_post_id ) + $custom_content_width = intval( get_post_meta( $custom_css_post_id, 'content_width', true ) ); + } + + if ( $custom_content_width > 0 ) + $content_width = $custom_content_width; + + return $content_width; + } } class Jetpack_Safe_CSS { |