Skip to content

Commit

Permalink
Merge branch 'trunk' into add/post-meta-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland authored Jan 10, 2025
2 parents 6566e1a + bca6fd7 commit 79398df
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 60 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10
strategy:
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
include:
- wp-version: latest
- wp-version: '6.5'
php-versions: '7.0'
- wp-version: '6.5'
php-versions: '7.1'
- wp-version: '5.9'
php-versions: '7.2'
steps:
- name: Install svn
run: |
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Comment counts get updated when the plugin is activated/deactivated/deleted
* Added a filter to make custom comment types manageable in WP.com Calypso
* Cleanup of options, post meta, and user meta when the plugin is uninstalled

### Changed

* Hide ActivityPub post meta keys from the custom Fields UI
* Bumped minimum required PHP version to 7.2

### Fixed

* Undefined array key warnings in various places
* @-mentions in federated comments being displayed with a line break
* Fetching replies from the same instance for Enable Mastodon Apps
* Image captions not being included in the ActivityPub representation when the image is attached to the post

Expand Down
2 changes: 1 addition & 1 deletion activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Author URI: https://automattic.com/
* License: MIT
* License URI: http://opensource.org/licenses/MIT
* Requires PHP: 7.0
* Requires PHP: 7.2
* Text Domain: activitypub
* Domain Path: /languages
*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"description": "The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.",
"type": "wordpress-plugin",
"require": {
"php": ">=7.0",
"php": ">=7.2",
"composer/installers": "^1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.21 || ^6.5 || ^7.5 || ^8",
"phpunit/phpunit": "^8 || ^9",
"phpcompatibility/php-compatibility": "*",
"phpcompatibility/phpcompatibility-wp": "*",
"squizlabs/php_codesniffer": "3.*",
Expand Down
9 changes: 9 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static function init() {
public static function activate() {
self::flush_rewrite_rules();
Scheduler::register_schedules();

\add_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
Migration::update_comment_counts();
}

/**
Expand All @@ -70,13 +73,19 @@ public static function activate() {
public static function deactivate() {
self::flush_rewrite_rules();
Scheduler::deregister_schedules();

\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
Migration::update_comment_counts( 2000 );
}

/**
* Uninstall Hook.
*/
public static function uninstall() {
Scheduler::deregister_schedules();

\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
Migration::update_comment_counts( 2000 );

delete_metadata( 'user', 0, '_activitypub_user_identifier', '', true );
delete_metadata( 'user', 0, 'activitypub_default_extra_fields', '', true );
Expand Down
67 changes: 25 additions & 42 deletions includes/transformer/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,6 @@ public function change_wp_user_id( $user_id ) {
$this->wp_object->user_id = $user_id;
}

/**
* Transforms the WP_Comment object to an ActivityPub Object.
*
* @see \Activitypub\Activity\Base_Object
*
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
*/
public function to_object() {
$object = parent::to_object();

$content = $this->get_content();
$at_replies = '';
$reply_context = $this->extract_reply_context( array() );

foreach ( $reply_context as $acct => $url ) {
$at_replies .= sprintf(
'<a class="u-mention mention" href="%s">%s</a> ',
esc_url( $url ),
esc_html( $acct )
);
}

$at_replies = trim( $at_replies );

if ( $at_replies ) {
$content = sprintf( '<p>%s</p>%s', $at_replies, $content );
}

$object->set_content( $content );
$object->set_content_map(
array(
$this->get_locale() => $content,
)
);

return $object;
}

/**
* Returns the User-URL of the Author of the Post.
*
Expand All @@ -107,8 +69,18 @@ protected function get_attributed_to() {
* @return string The content.
*/
protected function get_content() {
$comment = $this->wp_object;
$content = $comment->comment_content;
$comment = $this->wp_object;
$content = $comment->comment_content;
$mentions = '';

foreach ( $this->extract_reply_context() as $acct => $url ) {
$mentions .= sprintf(
'<a rel="mention" class="u-url mention" href="%s">%s</a> ',
esc_url( $url ),
esc_html( $acct )
);
}
$content = $mentions . $content;

/**
* Filter the content of the comment.
Expand Down Expand Up @@ -258,11 +230,11 @@ function ( $comment_id ) {
* Collect all other Users that participated in this comment-thread
* to send them a notification about the new reply.
*
* @param array $mentions The already mentioned ActivityPub users.
* @param array $mentions Optional. The already mentioned ActivityPub users. Default empty array.
*
* @return array The list of all Repliers.
*/
public function extract_reply_context( $mentions ) {
public function extract_reply_context( $mentions = array() ) {
// Check if `$this->wp_object` is a WP_Comment.
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
return $mentions;
Expand Down Expand Up @@ -364,4 +336,15 @@ public function get_to() {
get_rest_url_by_path( $path ),
);
}

/**
* Returns the content map for the comment.
*
* @return array The content map for the comment.
*/
public function get_content_map() {
return array(
$this->get_locale() => $this->get_content(),
);
}
}
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<arg name="parallel" value="50"/>

<rule ref="PHPCompatibility"/>
<config name="testVersion" value="7.0-"/>
<config name="testVersion" value="7.2-"/>
<rule ref="PHPCompatibilityWP"/>
<config name="minimum_supported_wp_version" value="4.7"/>
<config name="minimum_supported_wp_version" value="5.5"/>

<config name="text_domain" value="activitypub,default"/>

Expand Down
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<directory prefix="class-test-" suffix=".php">./tests</directory>
</testsuite>
</testsuites>
<!--
<listeners>
<listener class="Activitypub\Tests\Activitypub_Testcase_Timer" file="tests/class-activitypub-testcase-timer.php" />
</listeners>
-->
</phpunit>
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: OStatus, fediverse, activitypub, activitystream
Requires at least: 5.5
Tested up to: 6.7
Stable tag: 4.6.0
Requires PHP: 7.0
Requires PHP: 7.2
License: MIT
License URI: http://opensource.org/licenses/MIT

Expand Down Expand Up @@ -134,10 +134,13 @@ For reasons of data protection, it is not possible to see the followers of other

= Unreleased =

* Added: Comment counts get updated when the plugin is activated/deactivated/deleted
* Added: A filter to make custom comment types manageable in WP.com Calypso
* Added: Cleanup of options, post meta, and user meta when the plugin is uninstalled
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
* Changed: Bumped minimum required PHP version to 7.2
* Fixed: Undefined array key warnings in various places
* Fixed: @-mentions in federated comments being displayed with a line break
* Fixed: Fetching replies from the same instance for Enable Mastodon Apps
* Fixed: Image captions not being included in the ActivityPub representation when the image is attached to the post

Expand Down
6 changes: 3 additions & 3 deletions tests/includes/collection/class-test-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function test_add_follower() {
$db_followers = Followers::get_followers( 1 );
$db_followers2 = Followers::get_followers( 2 );

$this->assertContains( $follower, $db_followers );
$this->assertContains( $follower2, $db_followers2 );
$this->assertStringContainsString( $follower, serialize( $db_followers ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$this->assertStringContainsString( $follower2, serialize( $db_followers2 ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
}

/**
Expand Down Expand Up @@ -329,7 +329,7 @@ public function test_add_duplicate_follower() {

$db_followers = Followers::get_followers( 1 );

$this->assertContains( $follower, $db_followers );
$this->assertStringContainsString( $follower, serialize( $db_followers ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize

$follower = current( $db_followers );
$meta = get_post_meta( $follower->get__id(), '_activitypub_user_id', false );
Expand Down
2 changes: 1 addition & 1 deletion tests/includes/rest/class-test-signature-verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test_activity_signature() {
$date = gmdate( 'D, d M Y H:i:s T' );
$signature = Signature::generate_signature( 1, 'POST', $remote_actor, $date, $digest );

$this->assertRegExp( '/keyId="http:\/\/example\.org\/\?author=1#main-key",algorithm="rsa-sha256",headers="\(request-target\) host date digest",signature="[^"]*"/', $signature );
$this->assertMatchesRegularExpression( '/keyId="http:\/\/example\.org\/\?author=1#main-key",algorithm="rsa-sha256",headers="\(request-target\) host date digest",signature="[^"]*"/', $signature );

// Signed headers.
$url_parts = wp_parse_url( $remote_actor );
Expand Down
2 changes: 1 addition & 1 deletion tests/includes/transformer/class-test-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function test_content_with_reply_context() {
$content = $object->get_content();

// Test that reply context is added.
$this->assertEquals( '<p><a class="u-mention mention" href="https://example.net/@remote">@[email protected]</a> <a class="u-mention mention" href="https://remote.example/@author">@[email protected]</a></p><p>This is a comment</p>', $content );
$this->assertSame( '<p><a rel="mention" class="u-url mention" href="https://example.net/@remote">@[email protected]</a> <a rel="mention" class="u-url mention" href="https://remote.example/@author">@[email protected]</a> This is a comment</p>', $content );

// Clean up.
wp_delete_comment( $reply_comment_id, true );
Expand Down
10 changes: 10 additions & 0 deletions tests/includes/transformer/class-test-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ public function test_block_attachments_with_fallback() {
)
);

// For WP versions 6.1 and prior, we only look for attached images.
if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
wp_update_post(
array(
'ID' => $attachment_id,
'post_parent' => $post_id,
)
);
}

$object = Post::transform( get_post( $post_id ) )->to_object();

$this->assertEquals(
Expand Down

0 comments on commit 79398df

Please sign in to comment.