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

Enable JITMs in WPAdmin on Simple sites #41252

Open
wants to merge 27 commits into
base: trunk
Choose a base branch
from
Open

Conversation

getdave
Copy link
Contributor

@getdave getdave commented Jan 22, 2025

Fixes Automattic/wp-calypso#95651
Related to: 170707-ghe-Automattic/wpcom

Proposed changes:

Enables JITMs on Simple sites in WPAdmin (i.e. outside of Calypso). Must be partnered with 170707-ghe-Automattic/wpcom.

  • enables the JITM module on Dotcom Simple codebase.
  • Registers (and includes) a new wpcom/v2/jitm-v2 endpoint which replaces the current jetpack/v4/jitm endpoint. This makes the endpoint available for both Simple and Jetpack Connected sites (including WoA).
  • Updates the JavaScript to call the new API endpoint.
  • API endpoint sends requests via existing Jetpack JITM class "plumbing"
    • the is_jetpack_authorized_for_site check on the existing wpcom/v2/jitm (not to be confused with the new endpoint above!) is disabled for sites running on the Dotcom Simple codebase (via a filter).
    • the Client::wpcom_json_api_request_as_blog means there is no additional network request when running on Dotcom Simple)

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

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

Testing instructions:

  • Apply 170707-ghe-Automattic/wpcom to your sandbox. This will (amongst other things):
    • ensure the Jetpack JITM module is loaded on Simple.
    • include/load the new wpcom/v2/jitm-v2 endpoint on the Dotcom Simple Public API.
    • update the message_path to target /wp:themes:admin_notices/ (display in "WPAdmin" on the "Themes" path in the "admin_notices" action).
    • modify certain "scoring" functions to always ignore requests from non-jetpack connected sites.

Simple site

  • Add the following to an mu-plugin:
<?php
add_filter( 'jetpack_just_in_time_msg_cache', '__return_false', 1000 );


add_filter('jetpack_jitm_should_return_jitms', '__return_true', 1000);

add_filter('jetpack_jitm_should_ignore_dismissals', '__return_true', 1000);
  • Use the bin/jetpack-downloader command (see Github bot comment below) to apply this PR to your sandbox.
  • Go to https://{YOUR_SANDBOXED_SIMPLE_SITE}.wordpress.com/wp-admin/themes.php
  • See the Let us build your dream website JITM at the top of the page.

Atomic site 🤦

  • Get an WoA Dev Site
  • Transfer to the pool (see the FG)
  • Go to WPAdmin - upload the Jetpack Beta Plugin (get a copy from the Gitub repo)
  • Go to Jetpack -> Beta Tester
  • In the list of modules look for the Jetpack entry and click "Manage"
  • Search for try/jitm-on-simple and enable it.
  • SSH into your WoA site (hint - go to https://wordpress.com/hosting-config/MY_WoA_SITE_HERE and enable SSH.
  • edit the wp-config.php file in the root and add the define( 'JETPACK__SANDBOX_DOMAIN', 'MY_SANDBOX_HERE' );. This will ensure the WoA site uses your sandbox for the Public API. This will ensure that the changes from 170707-ghe-Automattic/wpcom are used by the WoA site.
  • Add the following to an mu-plugin:
<?php
add_filter( 'jetpack_just_in_time_msg_cache', '__return_false', 1000 );


add_filter('jetpack_jitm_should_return_jitms', '__return_true', 1000);

add_filter('jetpack_jitm_should_ignore_dismissals', '__return_true', 1000);
  • Go to https://{YOUR_WoA_SITE}/wp-admin/themes.php
  • See the Let us build your dream website JITM at the top of the page.

@getdave getdave self-assigned this Jan 22, 2025
@@ -139,6 +177,7 @@ public function prepare_jitms( $screen ) {
* @return bool True if the current page is a Jetpack or WooCommerce admin page, else false.
*/
public function is_a8c_admin_page() {
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Short-circuiting this for testing purposes.

Comment on lines 309 to 310
$.get( window.jitm_config.api_root + 'jetpack/v4/jitm', {
// Original code
// const url = jitm_config.api_root + 'jetpack/v4/jitm';

// make an WP Ajax request to local site as REST API is not initialized on Simple sites.
const dotComURL = window.ajaxurl;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Obviously this will need to be made conditional based on whether run on Simple or not.

Copy link
Contributor

github-actions bot commented Jan 22, 2025

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 try/jitm-on-simple 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 try/jitm-on-simple
    
    bin/jetpack-downloader test jetpack-mu-wpcom-plugin try/jitm-on-simple
    

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

Copy link
Contributor

github-actions bot commented Jan 22, 2025

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 a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ 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.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!


Jetpack plugin:

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

  • WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for none scheduled (scheduled code freeze on undefined).

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

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Jan 22, 2025
@jeryj jeryj force-pushed the try/jitm-on-simple branch from f6a951b to ce3b588 Compare January 31, 2025 22:33
Copy link
Contributor

github-actions bot commented Jan 31, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/jitm/src/class-jitm.php 68/123 (55.28%) 1.12% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-jitm-v2.php 16/48 (33.33%) ❤️‍🩹

Full summary · PHP report · JS report

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Feb 10, 2025
@getdave getdave marked this pull request as ready for review February 10, 2025 12:43
@getdave getdave requested review from jeherve and scruffian February 10, 2025 14:54
@getdave getdave added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Team Review labels Feb 11, 2025
@getdave
Copy link
Contributor Author

getdave commented Feb 11, 2025

Note to self. Ensure that changes to the jetpack/v4 endpoint are rebased into this PR.

Comment on lines 95 to 97
$query = $request['query'];
if ( ! empty( $request['s'] ) ) {
$query['s'] = $request['s'];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's bring in changes outlined in #41542 once that PR is merged.

@getdave getdave requested a review from cpapazoglou February 11, 2025 17:05
@getdave getdave changed the title Try enabling JITM on Simple sites Enable JITMs on Simple sites Feb 11, 2025
@getdave getdave changed the title Enable JITMs on Simple sites Enable JITMs in WPAdmin on Simple sites Feb 11, 2025
@cpapazoglou
Copy link
Contributor

cpapazoglou commented Feb 11, 2025

Thanks for working on this Dave and the detailed testing instructions. While testing it worked find in a Simple site

CleanShot 2025-02-11 at 22 46 16@2x

In a WoA site, the were no messages returned though

CleanShot 2025-02-11 at 22 56 44@2x

I wonder if that's expected or I missed something from the testing instructions?
Update: the public-api wasn't sandboxed. Works well in Atomic too!
CleanShot 2025-02-11 at 23 05 46@2x

cpapazoglou
cpapazoglou previously approved these changes Feb 11, 2025
Copy link
Contributor

@cpapazoglou cpapazoglou left a comment

Choose a reason for hiding this comment

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

LGTM after reviewing and testing. I added some minor comments.

( externalLink ? EXTERNAL_LINK_ICON : '' )
'</a>';
( externalLink ? EXTERNAL_LINK_ICON : '' );
( '</a>' );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All whitespace changes can be shipped independently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #41745

Comment on lines 159 to 164
$.ajax( {
url: window.jitm_config.api_root + 'jetpack/v4/jitm',
method: 'POST', // using DELETE without permalinks is broken in default nginx configuration
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', window.jitm_config.nonce );
},
apiFetch( {
path: '/wpcom/v2/jitm-v2',
method: 'POST',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change to apiFetch can be shipped independently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #41745

@@ -351,8 +357,11 @@ public function get_messages( $message_path, $query, $full_jp_logo_exists ) {

$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;

// If ignore_dismissible is true, skip the dismiss check
$ignore_dismissible = apply_filters( 'jetpack_jitm_should_ignore_dismissals', false );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's ship this as a standalone PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #41746

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I closed that PR. Let's remove this code entirely as we can work around it using filters and providing a fake JITM for testing purposes.

@@ -36,7 +49,7 @@ public static function configure() {
* @return Post_Connection_JITM|Pre_Connection_JITM JITM instance.
*/
public static function get_instance() {
if ( ( new Connection_Manager() )->is_connected() ) {
if ( ( new Connection_Manager() )->is_connected() || defined( 'IS_WPCOM' ) && IS_WPCOM ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@scruffian You asked about whether the IS_WPCOM check should be part of Connection_Manager. The answer is I have no idea. It could be.

We'd need input from the Jetpack team on this I think.

@@ -0,0 +1,151 @@
<?php
/**
* REST API endpoint for retrieving JITMs from the WPCOM API.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@scruffian Do you agree we should pull this out into a separate PR?

That way the code can be available on Simple, Atomic and JP and we can enable it on Simple by way of 170707-ghe-Automattic/wpcom when we are ready.

Also we can write some tests for it.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah good idea

Copy link
Member

@jeherve jeherve 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! I left a few minor remarks below.

Sorry for the late review! I had forgotten to submit my review after adding the comments!

@@ -36,7 +49,7 @@ public static function configure() {
* @return Post_Connection_JITM|Pre_Connection_JITM JITM instance.
*/
public static function get_instance() {
if ( ( new Connection_Manager() )->is_connected() ) {
if ( ( new Connection_Manager() )->is_connected() || defined( 'IS_WPCOM' ) && IS_WPCOM ) {
Copy link
Member

Choose a reason for hiding this comment

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

Since we have the Status package as a requirement for the JITM package, we can use ( new Automattic\Jetpack\Status\Host() )->is_wpcom_simple() here.

@@ -351,8 +357,11 @@ public function get_messages( $message_path, $query, $full_jp_logo_exists ) {

$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;

// If ignore_dismissible is true, skip the dismiss check
$ignore_dismissible = apply_filters( 'jetpack_jitm_should_ignore_dismissals', false );
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a docblock for this filter, so it can be picked up by the codex.

In that docblock, you can use $$next-version$$ for the version number:

### Package Version Annotations
When needing to add a package version number inside a DocBlock, please use `$$next-version$$` as such:
- `@since $$next-version$$`
- `@deprecated $$next-version$$`
- `@deprecated since $$next-version$$`
- `_deprecated_function( __METHOD__, 'package-$$next-version$$' );` (other WordPress deprecation functions also work, but note it must be all on one line).
The `$$next-version$$` specifier will be automatically replaced with the correct package version number the next time a new version of that package is released.

@@ -1,8 +1,13 @@
import jQuery from 'jquery';
Copy link
Member

Choose a reason for hiding this comment

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

At this point, maybe we could get rid of jQuery in a follow-up PR? :)


import '../css/jetpack-admin-jitm.scss';

jQuery( document ).ready( function ( $ ) {
// Site ID will be automatically added to the request.// Site ID will be automatically added to the request.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Site ID will be automatically added to the request.// Site ID will be automatically added to the request.
// Site ID will be automatically added to the request.

CHECKMARK_ICON +
text +
'</li>';
html += '<li>' + CHECKMARK_ICON + text + '</li>';
Copy link
Member

Choose a reason for hiding this comment

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

If we're to make changes, maybe we could switch to template strings, here and below, so it would be more readable?

Comment on lines +310 to +312
message_path: message_path,
query: query,
full_jp_logo_exists: full_jp_logo_exists,
Copy link
Member

Choose a reason for hiding this comment

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

Since the keys and values are the same, can't we use shorthand here and avoid duplication?

if ( 'object' === typeof response && response[ '1' ] ) {
response = [ response[ '1' ] ];
if ( 'object' === typeof response && response[ 'data' ] ) {
response = [ response[ 'data' ][ 0 ] ];
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to account for situations when response[ 'data' ] would not be an array?

@@ -0,0 +1,167 @@
<?php
/**
* REST API endpoint for retrieving JITMs from the WPCOM API.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could ship this endpoint in the JITM package instead, so things stay close together?

$jitm = Automattic\Jetpack\JITMS\JITM::get_instance();

if ( ! $jitm->jitms_enabled() ) {
return rest_ensure_response( array() );
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to return more than just an empty array in those situations, to make debugging easier?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Jitm [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Status] Needs Team Review [Tests] Includes Tests [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Untangling Themes
4 participants