Making WordPress.org


Ignore:
Timestamp:
12/23/2016 05:27:38 PM (8 years ago)
Author:
iandunn
Message:

WordCamp Site Cloner: Refactor for compatibility with WP 4.7 and Jetpack 4.2.2

WordPress 4.7 added a Customizer feature that allows editing CSS. Jetpack 4.2.2 replaced the Custom CSS module with an entirely new one that is compatible with the Customizer feature, but broke backwards-compatibility with the Site Cloner and Remote CSS plugins.

This commit refactors the Site Cloner to restore compatibility with Jetpack, and add support for the new Core feature.

https://make.wordpress.org/core/2016/10/11/feature-proposal-better-theme-customizations-via-custom-css-with-live-previews/
https://make.wordpress.org/core/2016/11/26/extending-the-custom-css-editor/
https://github.com/Automattic/jetpack/issues/5539
https://github.com/Automattic/jetpack/pull/5804
https://core.trac.wordpress.org/ticket/35395
https://core.trac.wordpress.org/ticket/38672
https://core.trac.wordpress.org/ticket/39270

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-site-cloner/includes/source-site-id-setting.php

    r4398 r4577  
    22
    33namespace WordCamp\Site_Cloner;
     4use \WordCamp\RemoteCSS;
     5use Jetpack_Custom_CSS_Enhancements;
     6
    47defined( 'WPINC' ) or die();
    5 
    6 use \WordCamp\RemoteCSS;
    78
    89/**
     
    1213 * the site that that will be cloned. There are actually many data points that we'll import, but they don't fit
    1314 * into the default model that the Customizer expects.
     15 *
     16 * @todo - We may no longer need a setting; see https://make.wordpress.org/core/2016/03/10/customizer-improvements-in-4-5/
    1417 */
    1518class Source_Site_ID_Setting extends \WP_Customize_Setting {
     
    2932
    3033        add_action( 'wp_head',                 array( $this, 'preview_source_site_css'  ), 99 );   // wp_print_styles is too early; the theme's stylesheet would get enqueued later and take precedence
    31         add_filter( 'get_post_metadata',       array( $this, 'preview_jetpack_postmeta' ), 10, 4 );
    32         add_filter( 'safecss_skip_stylesheet', array( $this, 'preview_skip_stylesheet'  ) );
     34        add_filter( 'safecss_skip_stylesheet', array( $this, 'preview_skip_stylesheet'  ), 15 );   // After Jetpack_Custom_CSS_Enhancements::preview_skip_stylesheet()
    3335
    3436        // Disable the current site's Custom CSS from being output
    35         remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
     37        remove_action( 'wp_head', 'wp_custom_css_cb', 11 ); // todo compat with WP 4.7. this line can be removed when r39616 is merged to the 4.7 branch
     38        remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
    3639    }
    3740
    3841    /**
    3942     * Print the source site's custom CSS in an inline style block
    40      *
    41      * It can't be easily enqueued as an external stylesheet because Jetpack_Custom_CSS::link_tag() returns early
    42      * in the Customizer if the theme being previewed is different from the live theme.
    4343     */
    4444    public function preview_source_site_css() {
    45         if ( method_exists( '\Jetpack', 'get_module_path' ) ) {
    46             require_once( \Jetpack::get_module_path( 'custom-css' ) );
    47         } else {
    48             return;
    49         }
    50 
    5145        switch_to_blog( $this->preview_source_site_id );
    5246
     
    5448            '<style id="wcsc-source-site-custom-css">%s %s</style>',
    5549            $this->get_cached_remote_css(),
    56             \Jetpack_Custom_CSS::get_css( true )
     50            wp_get_custom_css()
    5751        );
    5852
     
    6155
    6256    /**
    63      * Overwrite the previewing site's Custom CSS settings with values from the source site
    64      *
    65      * @param mixed  $pre_filter_value
    66      * @param int    $post_id
    67      * @param string $meta_key
    68      * @param bool   $return_single_value
    69      *
    70      * @return mixed
    71      */
    72     public function preview_jetpack_postmeta( $pre_filter_value, $post_id, $meta_key, $return_single_value ) {
    73         if ( ! in_array( $meta_key, array( 'content_width' ) ) ) {
    74             return $pre_filter_value;
    75         }
    76 
    77         remove_filter( 'get_post_metadata', array( $this, 'preview_jetpack_postmeta' ), 10, 4 );    // avoid infinite recursion
    78         switch_to_blog( $this->preview_source_site_id );
    79 
    80         if ( $source_cite_css_post = \Jetpack_Custom_CSS::get_current_revision() ) {
    81             $pre_filter_value = get_post_meta( $source_cite_css_post['ID'], $meta_key, $return_single_value );
    82         }
    83 
    84         restore_current_blog();
    85         add_filter( 'get_post_metadata', array( $this, 'preview_jetpack_postmeta' ), 10, 4 );
    86 
    87         return $pre_filter_value;
    88     }
    89 
    90     /**
    9157     * Determine whether or not to skip the primary stylesheet
    9258     *
    93      * When the Custom CSS value for `Mode` is `Replacement`, Jetpack will prevent the primary stylesheet from
     59     * When the `Start Fresh` theme mod is set, Jetpack will prevent the primary stylesheet from
    9460     * being enqueued. We need to trigger that behavior based on the value on the source site, rather than the
    9561     * current site.
    96      *
    97      * This could be actually handled along with other Jetpack options in preview_jetpack_postmeta(), if it
    98      * weren't for the fact that Jetpack_Custom_CSS::skip_stylesheet always returns false in the Previewer.
    9962     *
    10063     * @param bool $skip
     
    10366     */
    10467    public function preview_skip_stylesheet( $skip ) {
     68        remove_filter( 'safecss_skip_stylesheet', array( $this, 'preview_skip_stylesheet'  ), 15 ); // Avoid infinite recursion. Also, use same priority as originally set in preview()
    10569        switch_to_blog( $this->preview_source_site_id );
    10670
    107         if ( $source_cite_css_post = \Jetpack_Custom_CSS::get_current_revision() ) {
    108             $skip = 'no' === get_post_meta( $source_cite_css_post['ID'], 'custom_css_add', true );
    109         }
     71        $skip = Jetpack_Custom_CSS_Enhancements::skip_stylesheet();
    11072
    11173        restore_current_blog();
     74        add_filter( 'safecss_skip_stylesheet', array( $this, 'preview_skip_stylesheet'  ), 15 );
    11275
    11376        return $skip;
     
    13497                array( '\\\00BB \\\0020', '\0BB \020', '0BB 020' ),
    13598                '\00BB \0020',
    136                 $post->post_content_filtered
     99                $post->post_content
    137100            );
    138101        }
     
    154117        switch_to_blog( $source_site_id );
    155118
    156         if ( ! $source_cite_css_post = \Jetpack_Custom_CSS::get_current_revision() ) {
    157             restore_current_blog();
    158             return;
    159         }
    160 
     119        $source_site_theme_mods = get_theme_mod( 'jetpack_custom_css' );
    161120        $source_site_css = '';
    162121
     
    174133        }
    175134
    176         // Add Jetpack Custom CSS second
    177         if ( $custom_css = \Jetpack_Custom_CSS::get_css( false ) ) {
     135        /*
     136         * Add Core Custom CSS second to maintain correct cascading order.
     137         *
     138         * Processed CSS and vanilla CSS are stored in `post_content`, but pre-processed CSS is stored in
     139         * `post_content_filtered`.
     140         */
     141        if ( $custom_css_post = wp_get_custom_css_post() ) {
     142            if ( empty( $source_site_theme_mods['preprocessor'] ) ) {
     143                $custom_css = $custom_css_post->post_content;
     144            } else {
     145                $custom_css = $custom_css_post->post_content_filtered;
     146            }
     147
    178148            $source_site_css .= sprintf(
    179149                "/* %s */\n%s\n\n",
    180150                sprintf(
    181151                    esc_html__( 'Custom CSS from %s', 'wordcamporg' ),
    182                     get_bloginfo( 'name' )
     152                    parse_url( home_url(), PHP_URL_HOST )
    183153                ),
    184154                $custom_css
     
    186156        }
    187157
    188         $source_site_preprocessor  = get_post_meta( $source_cite_css_post['ID'], 'custom_css_preprocessor', true );
    189         $source_site_mode          = get_post_meta( $source_cite_css_post['ID'], 'custom_css_add', true );
    190         $source_site_content_width = get_post_meta( $source_cite_css_post['ID'], 'content_width', true );
    191 
    192158        restore_current_blog();
    193159
    194         \Jetpack_Custom_CSS::save( array(
    195             'css'             => $source_site_css,
    196             'is_preview'      => false,
    197             'preprocessor'    => $source_site_preprocessor,
    198             'add_to_existing' => 'yes' === $source_site_mode ? true : false,
    199             'content_width'   => $source_site_content_width,
    200         ) );
     160        wp_update_custom_css_post( $source_site_css );
     161        set_theme_mod( 'jetpack_custom_css', $source_site_theme_mods );
    201162    }
    202163}
Note: See TracChangeset for help on using the changeset viewer.