Skip to content

Commit

Permalink
Merge branch 'trunk' into add/user-error-notice
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle authored Jan 29, 2025
2 parents ccdad5c + 39dfabc commit 3a1b447
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins

### Fixed

* Handle deletes from remote servers that leave behind an accessible Tombstone object.
* No longer parses tags for post types that don't support Activitypub.

## [4.7.3] - 2025-01-21

### Fixed
Expand Down
13 changes: 12 additions & 1 deletion bin/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,22 @@ async function createRelease(version) {

updateVersionInFile('includes/class-migration.php', version, [
{
search: /version_compare\([^,]+,\s*['"]unreleased['"]/gi,
search: /(?<!\*[\s\S]{0,50})(?<=version_compare\s*\(\s*\$version_from_db,\s*')unreleased(?=',\s*['<=>])/g,
replace: (match) => match.replace(/unreleased/i, version)
}
]);

const phpFiles = execWithOutput('find . -name "*.php"').split('\n');

phpFiles.forEach((filePath) => {
updateVersionInFile(filePath, version, [
{
search: /@since unreleased/g,
replace: `@since ${version}`
}
]);
});

// Update CHANGELOG.md
updateChangelog(version);

Expand Down
11 changes: 11 additions & 0 deletions includes/class-hashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static function filter_activity_object( $activity ) {
* @param \WP_Post $post Post object.
*/
public static function insert_post( $post_id, $post ) {
// Check if the post supports ActivityPub.
if ( ! \post_type_supports( \get_post_type( $post ), 'activitypub' ) ) {
return;
}

// Check if the (custom) post supports tags.
$taxonomies = \get_object_taxonomies( $post );
if ( ! in_array( 'post_tag', $taxonomies, true ) ) {
return;
}

$tags = array();

// Skip hashtags in HTML attributes, like hex colors.
Expand Down
8 changes: 7 additions & 1 deletion includes/class-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,19 @@ public static function is_tombstone( $url ) {
*/
\do_action( 'activitypub_pre_http_is_tombstone', $url );

$response = \wp_safe_remote_get( $url );
$response = \wp_safe_remote_get( $url, array( 'headers' => array( 'Accept' => 'application/activity+json' ) ) );
$code = \wp_remote_retrieve_response_code( $response );

if ( in_array( (int) $code, array( 404, 410 ), true ) ) {
return true;
}

$data = \wp_remote_retrieve_body( $response );
$data = \json_decode( $data, true );
if ( $data && isset( $data['type'] ) && 'Tombstone' === $data['type'] ) {
return true;
}

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ For reasons of data protection, it is not possible to see the followers of other

* Added: Error notice when trying to enable a user for ActivityPub that doesn't have the `publish_posts` capability
* Changed: Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins
* Fixed: Handle deletes from remote servers that leave behind an accessible Tombstone object.
* Fixed: No longer parses tags for post types that don't support Activitypub.

= 4.7.3 =

Expand Down
23 changes: 22 additions & 1 deletion tests/includes/class-test-hashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Activitypub\Tests;

use Activitypub\Collection\Followers;

/**
* Test class for Activitypub Hashtag.
*
Expand Down Expand Up @@ -89,7 +91,7 @@ public function the_content_provider() {
* @param string $message The error message.
*/
public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $message ) {
$post_id = $this->factory->post->create(
$post_id = self::factory()->post->create(
array(
'post_content' => $content,
'post_excerpt' => $excerpt,
Expand All @@ -104,6 +106,25 @@ public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $me
}
}

/**
* Test no hashtags for unsupported post types.
*
* @covers ::insert_post
*/
public function test_no_hashtags_for_unsupported_post_types() {
$post_id = self::factory()->post->create(
array(
'post_content' => 'Testing #php and #programming',
'post_type' => Followers::POST_TYPE,
)
);

\Activitypub\Hashtag::insert_post( $post_id, get_post( $post_id ) );
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );

$this->assertEmpty( $tags, 'Should not add hashtags to unsupported post types' );
}

/**
* Data provider for hashtag tests.
*
Expand Down
85 changes: 85 additions & 0 deletions tests/includes/class-test-http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Test file for Activitypub HTTP Class
*
* @package Activitypub
*/

namespace Activitypub\Tests;

use Activitypub\Http;

/**
* Test class for ActivityPub HTTP Class
*
* @coversDefaultClass \Activitypub\Http
*/
class Test_Http extends \WP_UnitTestCase {

/**
* Response code is 404 -> is_tombstone returns true
*
* @covers ::is_tombstone
*
* @dataProvider data_is_tombstone
*
* @param array $request The request array.
* @param bool $result The expected result.
*/
public function test_is_tombstone( $request, $result ) {
$fake_request = function () use ( $request ) {
return $request;
};
add_filter( 'pre_http_request', $fake_request, 10, 3 );
$response = Http::is_tombstone( 'https://fake.test/object/123' );
$this->assertEquals( $result, $response );
remove_filter( 'pre_http_request', $fake_request, 10 );
}

/**
* Data provider for test_is_tombstone.
*
* @return array
*/
public function data_is_tombstone() {
return array(
array( array( 'response' => array( 'code' => 404 ) ), true ),
array( array( 'response' => array( 'code' => 410 ) ), true ),
array(
array(
'response' => array( 'code' => 200 ),
'body' => '',
),
false,
),
array(
array(
'response' => array( 'code' => 200 ),
'body' => '{}',
),
false,
),
array(
array(
'response' => array( 'code' => 200 ),
'body' => '{"type": "Note"}',
),
false,
),
array(
array(
'response' => array( 'code' => 200 ),
'body' => '{"type": "Tombstone"}',
),
true,
),
array(
array(
'response' => array( 'code' => 200 ),
'body' => '{"foo": "bar"}',
),
false,
),
);
}
}

0 comments on commit 3a1b447

Please sign in to comment.