Skip to content

Sync: Introduce helper function and CPT for Activity Log entries#48567

Open
coder-karen wants to merge 13 commits intotrunkfrom
add/sync-custom-post-types-for-activity-log
Open

Sync: Introduce helper function and CPT for Activity Log entries#48567
coder-karen wants to merge 13 commits intotrunkfrom
add/sync-custom-post-types-for-activity-log

Conversation

@coder-karen
Copy link
Copy Markdown
Contributor

@coder-karen coder-karen commented May 6, 2026

Fixes ACTLOG-57

Proposed changes

Adds a Jetpack Sync helper for creating custom customer-visible Activity Log entries.

  • Introduce Automattic\Jetpack\Sync\Activity_Log_Event as the API for creating custom Activity Log events.
  • Register a private jp_act_log_event CPT for transporting custom Activity Log event payloads through Sync.
  • Store the event payload as JSON in post_content.
  • Validate and sanitize event input:
    • required title and content
    • optional source
    • optional severity limited to info, success, warning, or error
  • Route valid custom event posts through the existing jetpack_published_post Sync pipeline.
  • Allow normal save/delete Sync actions for jp_act_log_event posts so remote/cache DB state stays consistent.
  • Reuse the custom event payload validation for direct jp_act_log_event inserts so invalid severity values, empty sanitized content, and malformed payloads are not enqueued through jetpack_published_post.
  • Prevent Activity Log event posts from being publicized or included in Jetpack sitemaps.
  • Add Sync package tests for validation, sanitization, CPT creation, and enqueue behavior.

Related product discussion/links

  • This is part of an RSM project to add custom entries to the Activity Log. See Linear link above for more.

  • Requires the matching WPcom Activity Log handling in 215249-ghe-Automattic/wpcom, which translates valid jp_act_log_event posts into custom_event__created entries and prevents malformed payloads from falling through as generic post publish events.

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

No.

Testing instructions

To test, apply this PR on a test site using the Jetpack Beta tester plugin, for the Jetpack plugin (or apply the PR if testing locally).

  • Using a code snippets plugin, add the following snippet: 3b2d3-pb/
  • Visit yoursite.com/wp-admin/?test_activity_log=1
  • You may want to log the args on the WPcom end to confirm they're received and with the right content, eg. under case 'jetpack_published_post': in Jetpack_Sync_Server_Replicator
  • Alternatively, apply 215249-ghe-Automattic/wpcom, and ensure your Jetpack site is API sandboxed. Then, follow the same steps as above, and you should be able to view the Activity Log entries

@coder-karen coder-karen self-assigned this May 6, 2026
@github-actions github-actions Bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label May 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

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 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 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

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


Backup plugin:

No scheduled milestone found for this plugin.

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


Boost plugin:

No scheduled milestone found for this plugin.

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


Search plugin:

No scheduled milestone found for this plugin.

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


Social plugin:

No scheduled milestone found for this plugin.

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


Starter Plugin plugin:

No scheduled milestone found for this plugin.

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


Protect plugin:

No scheduled milestone found for this plugin.

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


Videopress plugin:

No scheduled milestone found for this plugin.

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


Mu Wpcom plugin:

  • Next scheduled release: WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2)

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


Wpcomsh plugin:

  • Next scheduled release: Atomic deploys happen twice daily on weekdays (p9o2xV-2EN-p2)

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


Automattic For agencies client plugin:

No scheduled milestone found for this plugin.

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


Wpcloud Sso plugin:

No scheduled milestone found for this plugin.

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

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 WoA dev 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 (Jetpack or WordPress.com Site Helper), and enable the add/sync-custom-post-types-for-activity-log branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/sync-custom-post-types-for-activity-log
bin/jetpack-downloader test jetpack-mu-wpcom-plugin add/sync-custom-post-types-for-activity-log

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

@jp-launch-control
Copy link
Copy Markdown

jp-launch-control Bot commented May 6, 2026

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/sync/src/class-actions.php 178/437 (40.73%) -0.09% 1 ❤️‍🩹
projects/packages/sync/src/modules/class-posts.php 62/317 (19.56%) 12.53% -36 💚

1 file is newly checked for coverage.

File Coverage
projects/packages/sync/src/class-activity-log-event.php 93/111 (83.78%) 💚

Full summary · PHP report · JS report

@github-actions github-actions Bot added [Plugin] Automattic For Agencies Client [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Plugin] Wpcloud Sso [Plugin] Wpcomsh labels May 6, 2026
@coder-karen coder-karen marked this pull request as ready for review May 6, 2026 15:58
@coder-karen coder-karen requested a review from a team as a code owner May 6, 2026 15:58
@coder-karen coder-karen removed the request for review from a team May 6, 2026 15:58
Copy link
Copy Markdown
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Great work here, Karen! Here are some generic thoughts before manually testing this:

composer.json and global function approach

Thinking about this more, I wonder if it makes sense to expose logging a custom event to the Jetpack Activity Log via a global function. If we introduced a new class, for example, we'd be more inline with the rest of the Sync package functionality plus because the Sync package already classmaps src/, a new class file under projects/packages/sync/src/ should not require a composer.json autoload.files change.

The new class just needs to be referenced from an existing loaded path, e.g. Actions::init() for CPT registration and Modules\Posts for validation.

Responsibilities:

  • Activity_Log_Event
    • owns POST_TYPE = 'jp_act_log_event'
    • registers the CPT
    • sanitizes title, content, source, severity
    • builds the JSON payload for post_content
    • validates existing CPT posts before Sync sends them
    • creates the post via wp_insert_post()
  • Modules\Posts
    • calls Activity_Log_Event::is_valid_post( $post ) before enqueueing jetpack_published_post
    • otherwise stays focused on Sync behavior

One more thing I'd like to discuss is preventing edits/deletes from being synced for this CPT. I think we should allow the normal save/delete Sync actions through so the remote site and Cache site DB stay consistent. The important boundary is on the Activity Log side: jp_act_log_event saves/deletes should be blacklisted from generic post activities, and only the initial valid jetpack_published_post should create a custom_event__created Activity Log entry.

Comment thread projects/packages/sync/src/functions-activity-log.php Outdated
@coder-karen
Copy link
Copy Markdown
Contributor Author

Thinking about this more, I wonder if it makes sense to expose logging a custom event to the Jetpack Activity Log via a global function.

Yes, good call. I used a global function because it’s a familiar WordPress-style API and makes the caller side very simple, but I agree a class fits this package better. It avoids the extra autoload.files handling and gives the Activity Log event code one clear home for the CPT, sanitization, payload creation, and validation.

I think we should allow the normal save/delete Sync actions through so the remote site and Cache site DB stay consistent.

Also agreed on that. I was thinking of the CPT as write-once from an Activity Log perspective, but normal Sync should still keep the remote/cache DB in step like you say.

@coder-karen coder-karen added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels May 7, 2026
Copy link
Copy Markdown
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Many thanks for working on this, Karen!

Tested manually and it works as described 👍

Left a few inline comments.

return false;
}

foreach ( array( 'title', 'content' ) as $field ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps we could reuse the extra checks from build_payload( $data ) here by adding a check like false !== self::build_payload( $data ) after we decode.
This way posts with invalid severity, scalar content that sanitizes to empty, etc. wii not enqueue through jetpack_published_post

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great idea. Added along with other changes in aebafc0

/**
* Logs a custom event to the Jetpack Activity Log.
*
* Call this on or after the `init` action so the Activity Log post type and Sync listeners are registered.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I saw we added this comment in 813be65 to make it clear that create should be called after init but perhaps we could attempt to register the post type inside this method to be more defensive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense! Added in aebafc0

@fgiannar fgiannar added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels May 7, 2026
@coder-karen coder-karen added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels May 7, 2026
…lso match current behavior, and try to prevent invalid CPTs on creation with core hooks - based on internal reviews
@coder-karen coder-karen removed [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Plugin] mu wpcom jetpack-mu-wpcom plugin [Plugin] Wpcomsh [Plugin] Automattic For Agencies Client [Plugin] Wpcloud Sso labels May 7, 2026
Copy link
Copy Markdown
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

LGTM 🚢

Tested with a Jetpack site (local env) but also in my sandboxed Simple site and everything works as expected 👍

Copy link
Copy Markdown
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Apologies I missed this!

'show_ui' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_rest' => false,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry it just occurred to me that we need this to be true in order to be able to create custom Activity Log entries via the REST API. Probably also need to add 'rest_base' => 'jp-activity-log-events' or similar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Sync [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants