Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 9.17 KB

AGENTS.md

File metadata and controls

50 lines (34 loc) · 9.17 KB

WordPress ActivityPub Plugin

WordPress plugin implementing the ActivityPub protocol, enabling federation with Mastodon, Pixelfed, Pleroma, and other compatible platforms.

Prefer reading project files and docs/ over relying on training data for WordPress and ActivityPub patterns.

Do NOT:

  • Edit WordPress core files
  • Use remove_all_filters('pre_http_request') in tests
  • Hardcode new version numbers (use 'unreleased')
  • Remove an escaping, sanitizing or capability check, even when a tool reports it as redundant
  • Change the priority or signature of a registered hook (third-party code unhooks against the exact signature)

Common Pitfalls

  • PHP 7.4 compatibility. CI tests against PHP 7.4. No named arguments, no union types, no match (PHP 8.0+).
  • Pre-commit hooks modify files. If the hook changed files, stage and commit again — do not assume the first commit succeeded.
  • remove_all_filters('pre_http_request') is forbidden in tests. The pre-commit hook blocks this. Use targeted filter removal.
  • Changelog entries are end-user friendly and end with punctuation. Users see these in the WordPress update screen. Describe what changed from their perspective — no jargon, class names, or method names.
  • post_date_gmt may be empty. Check for 0000-00-00 or empty values.
  • Cleanups change no behaviour. A lint, static-analysis or refactor task changes docblocks and types, not what the code does. When the analyzer is wrong about a core function, fix its view (a stub), never the output line.
  • A docblock is a claim, not evidence. Read the call sites before changing code because of what a comment says, and never revert on a comment's authority either.
  • A request handler answers only for the actor whose inbox received it. Requests (QuoteRequest, FeatureRequest, any new *Request) ask the owner of their object for consent. The inbox checks blocks for the recipient only, so a handler that resolves the owner itself (post author, featured actor) must reject when that owner is not among $user_ids. Otherwise a request sent to one user's inbox gets an Accept in another user's name, past that user's blocks. See Quote_Request::handle_quote_request() for the check.
  • An owner's block stops every activity that targets their content. Recipients are filtered per recipient, but a reply, reaction or quote lands on a post or comment whose owner is often not among the recipients (the actor's local followers, a mention, nobody). Moderation::activity_is_blocked() therefore also checks the blocks of the post author and the local comment author the activity targets, resolving the target with get_target_post_id(), the same function that stores the interaction, and both inboxes and Scheduler::process_inbox_activity() call it before any handler runs. The owner decides for the whole activity, so other recipients do not get it either (no mention notification). Only a post type enabled for ActivityPub with a non-zero author has an owner: a cached remote object (ap_post, or a companion plugin's own post type) sits in several users' streams, so blocks there apply per viewer when it is shown, not when it arrives. Never add a block check to one handler or to Interactions instead: every other handler and plugin would bypass it. A handler that dispatches an embedded activity (Announce) runs it through the same check. A handler that acts on local content through another property (for example target of an Add) adds that property to the targets in Moderation::activity_is_blocked_by_content_owner(). test_owner_block_stops_activity() pins this for every activity type.
  • A post's stored guid depends on how the row was last written. On insert, core's pre_post_guid filters (sanitize_url, wp_filter_kses) store & as &. On every wp_update_post(), core ignores the guid it is given, reads the stored one in the display context (esc_url()) and writes that back, so & becomes & and ' becomes ' (wp_insert_post(): $guid = get_post_field( 'guid', $post_id )). So wp_update_post() can never change a guid; to change one, write it with $wpdb in the insert form, together with the post_name hash. Every lookup of a stored ID therefore matches every spelling from get_stored_id_forms() (the insert form, the update form and the raw ID that meta values keep), and code that sends or compares the ID undoes the escaping with unescape_guid(). The forms can collide (?a=1&b=2 after an update and ?a=1&b=2 store the same string); no server writes such IDs, so no lookup checks for it. The exact fix would be a lookup by the ID hash in post_name (get_id_hash()) for every collection at once, which needs a backfill first because older rows do not have it. Do not add a check to only one collection. A test for a lookup has to look the row up after an update too, not only after an insert. IDs without & or ' are not affected, which is why this rarely shows.
  • Identify with object_to_id(), link with object_to_uri(). Anything that stores, looks up, compares or fetches an object or an actor uses object_to_id(), which returns only the id (a Link or Mention gives its href). object_to_uri() returns the file of an Image, Audio, Video or Document and otherwise falls back to url, so it is only for links: icons, avatars, emoji, attachments, source_url, profile links. is_same_host(), is_same_actor() and the signature checks in Rest\Verification compare by object_to_id(), so a check and the code after it must read the same form.
  • Check the multisite path for every new piece of stored data. A network shares users and user meta across all sites; posts, comments and options are per site. Data that belongs to one site (tokens, per-site settings or state of a user) is stored as a user option (update_user_option(), key with the site's table prefix), not as plain user meta. Lookups by key and reads use the prefixed key ($wpdb->get_blog_prefix()), because get_user_option() falls back to the key without the prefix. Sites can also share the user tables without multisite (CUSTOM_USER_META_TABLE); treat that like multisite. A migration runs once per site, so a migration of shared data must also be safe on the second site. Run the tests for such changes with WP_MULTISITE=1 too; CI has a multisite PHPUnit job. See OAuth\Token::get_token_key().
  • Scheduled cron handlers must be idempotent if they have user-visible side effects (emails, external API calls, push notifications). WP-Cron can re-enter the same callback via concurrent workers, plugin deactivate→reactivate (which re-runs register_schedules()), wp cron event run, and traffic spikes that fire overlapping loopback requests. Claim the unit of work atomically — add_option( $key, $value, '', false ) only succeeds when the row doesn't yet exist, which makes it a race-safe sentinel — before the side effect runs, not after. See Activitypub\Scheduler\Statistics::send_monthly_email() for the canonical pattern.

PHP Conventions

Reuse existing functionality instead of hand-rolling it. Before writing any parsing, validation, escaping, or formatting logic, check for a WordPress core function that does the job (e.g. wp_kses_bad_protocol(), wp_parse_url(), wp_http_validate_url()), then for an existing plugin helper (includes/functions*.php, Sanitize, src/shared/). Core primitives are battle-tested against adversarial input in ways a fresh implementation is not, and reviewers can trust them on sight. A custom implementation needs a stated reason why no existing function fits — "shorter" is not one. This applies to refactors too: never replace a core-function-based check with bespoke logic just to save a line.

Backslash-prefix all WordPress functions in namespaced code: \get_option(), \add_action(), \apply_filters(), \__(), \_e(), etc. PHP falls back to global scope, but backslashes are a project standard for consistency and to avoid accidentally shadowing globals.

No inline namespaces. Use use statements at the top of the file instead of inline fully-qualified class names (e.g., use Activitypub\Options; then Options::method(), not \Activitypub\Options::method()).

For new or modified code, use 'unreleased' for all @since, @deprecated, and deprecation function version strings so the release script can replace them. Do not introduce new hardcoded version numbers like '5.1.0'; existing versioned tags in the codebase are fine.

Testing Conventions

Read tests/README.md and the test skill before writing or changing tests. It covers test utilities, data factories, and detailed patterns.

One test file per source file. Tests mirror the file they test: includes/rest/class-seek-controller.php → tests/phpunit/tests/includes/rest/class-test-seek-controller.php. Add tests to that file; never create a test file that spans several source files. The pre-commit hook enforces this.

Skills and Agents

Skills are complex procedures loaded on demand. Canonical files live in .agents/skills/, with stubs in .claude/skills/ for Claude Code discovery.

After reading a skill, check for a local override at ~/.claude/skills/{skill-name}-local/SKILL.md. Local overrides take precedence.