From d16014911bffb0527a2597909d5005f713c5e7b5 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 11 May 2023 19:53:53 +0200 Subject: [PATCH 1/2] Compat: introduce a conditional to detect ActivityPub requests This conditional could be used within the plugin, but also by third-party plugins, to detect whether a request is an ActivityPub request, without having to manually check for query vars and headers every time. --- includes/functions.php | 44 ++++++++++++++++++++++++++++++++++++++++++ readme.txt | 1 + 2 files changed, 45 insertions(+) diff --git a/includes/functions.php b/includes/functions.php index 19a3512..f1d6c65 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -228,3 +228,47 @@ function is_tombstone( $wp_error ) { return false; } + +/** + * Check if a request is for an ActivityPub request. + * + * @return bool False by default. + */ +function is_activitypub_request() { + global $wp_query; + + /* + * ActivityPub requests are currently only made for + * author archives, singular posts, and the homepage. + */ + if ( ! \is_author() && ! \is_singular() && ! \is_home() ) { + return false; + } + + // One can trigger an ActivityPub request by adding ?activitypub to the URL. + global $wp_query; + if ( isset( $wp_query->query_vars['activitypub'] ) ) { + return true; + } + + /* + * The other (more common) option to make an ActivityPub request + * is to send an Accept header. + */ + if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { + $accept = $_SERVER['HTTP_ACCEPT']; + + /* + * $accept can be a single value, or a comma separated list of values. + * We want to support both scenarios, + * and return true when the header includes at least one of the following: + * - application/activity+json + * - application/ld+json + */ + if ( preg_match( '/(application\/(ld\+json|activity\+json))/', $accept ) ) { + return true; + } + } + + return false; +} diff --git a/readme.txt b/readme.txt index 0004235..c08f455 100644 --- a/readme.txt +++ b/readme.txt @@ -115,6 +115,7 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu = Next = +* Compatibility: add a new conditional, `\Activitypub\is_activitypub_request()`, to allow third-party plugins to detect ActivityPub requests. * Compatibility: add hooks to allow modifying images returned in ActivityPub requests. * Compatibility: indicate that the plugin is compatible and has been tested with the latest version of WordPress, 6.2. From 0b60944f93a947bbb458e82ad919338166ec087c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 11 May 2023 03:05:09 +0000 Subject: [PATCH 2/2] Update dealerdirect/phpcodesniffer-composer-installer requirement Updates the requirements on [dealerdirect/phpcodesniffer-composer-installer](https://github.com/PHPCSStandards/composer-installer) to permit the latest version. - [Release notes](https://github.com/PHPCSStandards/composer-installer/releases) - [Changelog](https://github.com/PHPCSStandards/composer-installer/blob/main/.github_changelog_generator) - [Commits](https://github.com/PHPCSStandards/composer-installer/compare/v0.7.1...v1.0.0) --- updated-dependencies: - dependency-name: dealerdirect/phpcodesniffer-composer-installer dependency-type: direct:development ... Signed-off-by: dependabot[bot] --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 67bd2d3..b0117c4 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "squizlabs/php_codesniffer": "3.*", "wp-coding-standards/wpcs": "*", "yoast/phpunit-polyfills": "^1.0", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1" + "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0" }, "config": { "allow-plugins": true