Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom CSS: copy module to jetpack-mu-wpcom package #37794

Merged
merged 42 commits into from
Jun 27, 2024

Conversation

monsieur-z
Copy link
Contributor

@monsieur-z monsieur-z commented Jun 11, 2024

Fixes https://github.com/Automattic/vulcan/issues/361

Proposed changes:

The Custom CSS feature in Jetpack will soon be deprecated. To keep the feature working for WoA sites, we're copying the module to the jetpack-mu-wpcom package (note that this PR doesn't remove the Custom CSS module from Jetpack yet).

Simple sites load their own implementation of the Custom CSS feature and should not be impacted by this PR.

The file diff is a bit intimidating but we've mostly copied files. In more detail, here's what this PR does:

  • Copy the Custom CSS code to the jetpack-mu-wpcom package
    • Update the webpack config of jetpack-mu-wpcom to build the custom CSS assets
    • Refactor script registration to use Assets::register_script
    • Move script registration to the wp_admin_enqueue_scripts hook to prevent errors when running tests
    • The module doesn't require the codemirror library since the WPCom version is used
  • Require the scssphp package in jetpack-mu-wpcom
  • Load the copied code in class-jetpack-mu-wpcom.php for WoA sites only
  • Add guards before class and function declarations in both the existing Jetpack module and the code copied over to the jetpack-mu-wpcom package. This prevents PHP errors no matter the loading order of Jetpack and WordPress Features plugins.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

pfwV0U-4M-p2

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

Let's do some regression testing.

Simple sites

  • Spin up your sandbox test site
  • Install and activate a classic theme, such as Twenty-Eleven
  • In your sandbox, checkout this branch with bin/jetpack-downloader test jetpack update/copy-custom-css-to-mu-wpcom
  • In the admin, visit Appearance > Additional CSS
  • Make sure you see these options and that you can write additional CSS
Screenshot 2024-06-11 at 4 55 12 PM

Self-hosted sites

  • Spin up a Jetpack self-hosted site with this branch
  • Install and activate a classic theme, such as Twenty-Eleven
  • From /wp-admin/admin.php?page=jetpack_modules, activate the Custom CSS module
  • In the admin, visit Appearance > Additional CSS
  • Make sure you see the options above and that you can write additional CSS

WoA sites

  • Spin up a WoA developer site
  • Install and activate a classic theme, such as Twenty-Eleven
  • From /wp-admin/admin.php?page=jetpack_modules, activate the Custom CSS module
  • Install the Jetpack Beta Tester plugin
  • In WPadmin, go to Jetpack > Beta Tester
  • Select this branch for the Jetpack plugin
Screenshot 2024-06-11 at 11 30 52 AM
  • Select this branch for the WordPress.com Features
Screenshot 2024-06-11 at 4 44 34 PM
  • In the admin, visit Appearance > Additional CSS
  • Make sure you see the options above and that you can write additional CSS

Optionally, ssh to your WoA site and delete both the custom-css.php file and the custom-css folder in ~/htdocs/wp-content/plugins/jetpack-dev/modules. Ensure the Custom CSS feature works as before.

@github-actions github-actions bot added [Package] Jetpack mu wpcom WordPress.com Features [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [mu wpcom Feature] Custom Css labels Jun 11, 2024
Copy link
Contributor

github-actions bot commented Jun 11, 2024

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen daily.
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for July 2, 2024 (scheduled code freeze on July 1, 2024).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Mu Wpcom plugin:

  • Next scheduled release: July 2, 2024.
  • Scheduled code freeze: June 24, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Wpcomsh plugin:

  • Next scheduled release: July 2, 2024.
  • Scheduled code freeze: June 24, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

Copy link
Contributor

github-actions bot commented Jun 11, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the update/copy-custom-css-to-mu-wpcom branch.

    • For jetpack-mu-wpcom changes, also add define( 'JETPACK_MU_WPCOM_LOAD_VIA_BETA_PLUGIN', true ); to your wp-config.php file.
  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack update/copy-custom-css-to-mu-wpcom
    
    bin/jetpack-downloader test jetpack-mu-wpcom-plugin update/copy-custom-css-to-mu-wpcom
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@monsieur-z monsieur-z force-pushed the update/copy-custom-css-to-mu-wpcom branch from 15c7565 to a2ba305 Compare June 11, 2024 15:37
@monsieur-z monsieur-z force-pushed the update/copy-custom-css-to-mu-wpcom branch from 4b0070e to fa1cbbf Compare June 11, 2024 21:00
@monsieur-z monsieur-z marked this pull request as ready for review June 11, 2024 21:08
@github-actions github-actions bot added the [Plugin] mu wpcom jetpack-mu-wpcom plugin label Jun 11, 2024
@monsieur-z monsieur-z requested a review from a team June 11, 2024 21:55
@monsieur-z monsieur-z self-assigned this Jun 11, 2024
Copy link
Contributor

@coder-karen coder-karen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests well for me on self-hosted, Atomic and Simple.

I may still be misunderstanding the difference between the Simple implementation and what was in Jetpack before (I think I understood what you mentioned before to be that there was a difference between what is loaded between Atomic and Simple?), but I still wonder why the references in mu-plugins/custom-css and related files can't now point to the corresponding file in the jetpack-mu-wpcom package (in contrast to pointing to the Jetpack version before), if those are loaded for Simple sites?

I think that would be an aside though (not a blocker).

As for the Phan check failing, I'm baffled there as I'm not seeing anything showing, nor can I run phan for the jetpack-mu-wpcom package locally. It is getting stuck when attempting to 'analyze'. In the end I was able to begin to get it working by removing all directories except custom-css.php, which could be a help in beginning to look at some of the Phan issues that aren't related to removing files anyway.

@monsieur-z
Copy link
Contributor Author

Thanks for your review @coder-karen.

I may still be misunderstanding the difference between the Simple implementation and what was in Jetpack before

Just so every reader has the same context: Simple sites load the custom-css-in-customizer.php and custom-css.php files from the WPcom mu-plugins, which hold the logic for the Custom CSS feature in WPcom. They don't load the Custom CSS module present in the Jetpack module.

The features in both implementations seem equivalent. But given the scope and timeline of the overarching project, I recommend we keep them separate, at least for the moment. That's why in the jetpack-mu-wpcom package, we're only loading the copied Jetpack implementation for Atomic sites.

I still wonder why the references in mu-plugins/custom-css and related files can't now point to the corresponding file in the jetpack-mu-wpcom package (in contrast to pointing to the Jetpack version before), if those are loaded for Simple sites?

We should probably do that. This would prevent unnecessary code duplication and possibly help us get rid of the PHP warnings triggered by the csstidy library. I believe we need to deploy this PR first, though.

I hope this clarifies things a little and doesn't just reiterate what you already knew.

@monsieur-z
Copy link
Contributor Author

As for the static analysis failing, I'm glad it's not me missing an obvious log! I haven't been able to locate the culprit in the pipeline or locally either.

This conversation (p1718210071497239-slack-CBG1CP4EN) points to the lessc.inc.php file. I'll try excluding it from the analysis.

Copy link
Contributor

@coder-karen coder-karen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do that. This would prevent unnecessary code duplication and possibly help us get rid of the https://github.com/Automattic/vulcan/issues/362. I believe we need to deploy this PR first, though.

Yes that's what I was initially thinking of in terms of the duplication :) But yes, that'd be a next step anyway.

I've left a few comments as I've looked at this some more.

One of the main things I've noticed is that we should be building the CSS and JS files (.min, .map) using Webpack, as with all other packages. We don't include anything other than the .css and .js files in the monorepo, but only in production after building.
Let me know if you'd like me to help with this :)

Edit - actually the JS files are fine as they are (copied over directly). But the jetpack-mu-wpcom webpack.config file would ideally need to be updated to build the custom-css and csstidy CSS.


wp_register_script(
'jetpack-customizer-css-preview',
Assets::get_file_url_for_environment(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the other similar comment, it might be worth rewriting this in relation to get_file_url_for_environment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm that other comment didn't get added - re-added now: https://github.com/Automattic/jetpack/pull/37794/files#r1637844413

wp_register_style( 'jetpack-customizer-css', plugins_url( 'custom-css/css/customizer-control.css', __FILE__ ), array(), '20140728' );
wp_register_script( 'jetpack-codemirror', plugins_url( 'custom-css/js/codemirror.min.js', __FILE__ ), array(), '3.16', true );

$src = Assets::get_file_url_for_environment(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the suggestion I added here didn't post initially so re-adding it. With get_file_url_for_environment, there was a suggestion to avoid using this if possible in packages. Slack ref: p1715614401962229-slack-CBG1CP4EN

Here is how I changed it in Calypsoify: https://github.com/Automattic/jetpack/pull/37351/files

@monsieur-z monsieur-z force-pushed the update/copy-custom-css-to-mu-wpcom branch 5 times, most recently from b9250ed to 9abc1d2 Compare June 17, 2024 20:07
Copy link
Contributor

@coder-karen coder-karen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the linked issue is listed as 'in progress' so this might not be open for re-review just yet, but since I was taking another look I thought I'd drop some more comments in case it could be helpful.

I notice that the CSSTidy built files have also been copied over to this PR - CSSTidy was also being built only in production before. In the monorepo it is just .css and .php files at the moment.

In production, we weren't including .map files (they should only be relevant for development builds, rather than production builds).

Also the currently committed minified versions are not actually minified (I'm guessing, as with the other built files in the csstidy folder, due to having being built with a development build locally, then copied over from the Jetpack plugin modules folder?)

With the Assets changes, I understand the Assets workaround here now is to use the non-minified versions. Another alternative would include building to the same directory the non minified files are in. Garage might also have insight about why using the build path wasn't working (if that was still the issue from before).

Also I'm curious about why the Codemirror files were removed.

Apologies if this was all still in progress :D

@monsieur-z monsieur-z force-pushed the update/copy-custom-css-to-mu-wpcom branch 2 times, most recently from e1051cb to 6b5ca9b Compare June 18, 2024 18:04
@monsieur-z monsieur-z force-pushed the update/copy-custom-css-to-mu-wpcom branch from 1f3f20e to 82a9f6f Compare June 26, 2024 14:39
@sergeymitr sergeymitr added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Team Review labels Jun 27, 2024
@monsieur-z monsieur-z merged commit b7be799 into trunk Jun 27, 2024
57 checks passed
@monsieur-z monsieur-z deleted the update/copy-custom-css-to-mu-wpcom branch June 27, 2024 13:40
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Jun 27, 2024
@mgozdis
Copy link

mgozdis commented Jul 2, 2024

Seeing some fatals on sites that appears to be related to this change. It seems to be an issue if a site/theme is using their own implementation of lessc. Is there anyway to disable the Jetpack module entirely on AT sites so that it does not load? Deactivating the Custom CSS module in BRC did not seem to help.

Here is a stack trace from a user report where their theme is loading it. I have another request from another user that wants to disable lessc from Jetpack entirely as well.

8421952-zd-a8c
8369108-zd-a8c

PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string * float in /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php:1272
Stack trace:
#0 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(1257): lessc->lib_luma(Array)
#1 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(1511): lessc->lib_contrast(Array, Object(lessc))
#2 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(1475): lessc->reduce(Array)
#3 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(704): lessc->reduce(Array)
#4 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(282): lessc->compileProp(Array, Object(stdClass), Object(stdClass))
#5 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(220): lessc->compileProps(Object(stdClass), Object(stdClass))
#6 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(194): lessc->compileCSSBlock(Object(stdClass))
#7 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(692): lessc->compileBlock(Object(stdClass))
#8 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(282): lessc->compileProp(Array, Object(stdClass), Object(stdClass))
#9 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(220): lessc->compileProps(Object(stdClass), Object(stdClass))
#10 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(194): lessc->compileCSSBlock(Object(stdClass))
#11 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(692): lessc->compileBlock(Object(stdClass))
#12 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(160): lessc->compileProp(Array, Object(stdClass), Object(stdClass))
#13 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(802): lessc->compileImportedProps(Array, Object(stdClass), Object(stdClass), Object(lessc_parser), '/srv/htdocs/wp-...')
#14 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(160): lessc->compileProp(Array, Object(stdClass), Object(stdClass))
#15 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(802): lessc->compileImportedProps(Array, Object(stdClass), Object(stdClass), Object(lessc_parser), '/srv/htdocs/wp-...')
#16 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(282): lessc->compileProp(Array, Object(stdClass), Object(stdClass))
#17 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(276): lessc->compileProps(Object(stdClass), Object(stdClass))
#18 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(191): lessc->compileRoot(Object(stdClass))
#19 /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php(1902): lessc->compileBlock(Object(stdClass))
#20 /srv/htdocs/wp-content/themes/adventure-tours/vendor/themedelight/components/TdLessProcessor.php(190): lessc->compile('@import "main-v...')
#21 /srv/htdocs/wp-content/themes/adventure-tours/vendor/themedelight/components/TdLessProcessor.php(48): TdLessProcessor->renderLessFile('/srv/htdocs/wp-...', Array)
#22 /srv/htdocs/wp-content/themes/adventure-tours/includes/classes/AtApplication.php(81): TdLessProcessor->generateCss(Array, false, 'adventure-tours...')
#23 /srv/htdocs/wp-content/themes/adventure-tours/functions.php(713): AtApplication->generateCustomCss('/assets/less/ma...', Array, 'main-custom')
#24 /wordpress/core/6.5.5/wp-includes/class-wp-hook.php(324): adventure_tours_filter_theme_styles(Array)
#25 /wordpress/core/6.5.5/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
#26 /srv/htdocs/wp-content/themes/adventure-tours/functions.php(369): apply_filters('get-theme-style...', Array)
#27 /wordpress/core/6.5.5/wp-includes/class-wp-hook.php(324): adventure_tours_init_theme_asserts('')
#28 /wordpress/core/6.5.5/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#29 /wordpress/core/6.5.5/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#30 /wordpress/core/6.5.5/wp-includes/script-loader.php(2265): do_action('wp_enqueue_scri...')
#31 /wordpress/core/6.5.5/wp-includes/class-wp-hook.php(324): wp_enqueue_scripts('')
#32 /wordpress/core/6.5.5/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#33 /wordpress/core/6.5.5/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#34 /wordpress/core/6.5.5/wp-includes/general-template.php(3050): do_action('wp_head')
#35 /srv/htdocs/wp-content/themes/adventure-tours/header-clean.php(14): wp_head()
#36 /wordpress/core/6.5.5/wp-includes/template.php(812): require('/srv/htdocs/wp-...')
#37 /wordpress/core/6.5.5/wp-includes/template.php(745): load_template('/srv/htdocs/wp-...', false, Array)
#38 /wordpress/core/6.5.5/wp-includes/general-template.php(206): locate_template(Array, true, false, Array)
#39 /srv/htdocs/wp-content/themes/adventure-tours/header.php(10): get_template_part('header', 'clean')
#40 /wordpress/core/6.5.5/wp-includes/template.php(810): require_once('/srv/htdocs/wp-...')
#41 /wordpress/core/6.5.5/wp-includes/template.php(745): load_template('/srv/htdocs/wp-...', true, Array)
#42 /wordpress/core/6.5.5/wp-includes/general-template.php(48): locate_template(Array, true, true, Array)
#43 /srv/htdocs/wp-content/themes/adventure-tours/page.php(10): get_header()
#44 /wordpress/core/6.5.5/wp-includes/template-loader.php(106): include('/srv/htdocs/wp-...')
#45 /wordpress/core/6.5.5/wp-blog-header.php(19): require_once('/wordpress/core...')
#46 /wordpress/core/6.5.5/index.php(17): require('/wordpress/core...')
#47 {main}
  thrown in /wordpress/plugins/wpcomsh/3.27.2/vendor/automattic/jetpack-mu-wpcom/src/features/custom-css/custom-css/preprocessors/lessc.inc.php on line 1272

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Custom CSS [mu wpcom Feature] Custom Css [Package] Jetpack mu wpcom WordPress.com Features [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Wpcomsh
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants