Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix old posts not being deleted for ActivityPub #476

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
add_action( 'plugins_loaded', array( __NAMESPACE__ . '\Friends', 'init' ) );
add_action( 'admin_init', array( __NAMESPACE__ . '\Plugin_Installer', 'register_hooks' ) );

if ( is_admin() && FRIENDS_VERSION > get_option( 'friends_plugin_version' ) ) {
if ( is_admin() && version_compare( get_option( 'friends_plugin_version' ), FRIENDS_VERSION, '<' ) ) {
add_action( 'admin_init', array( __NAMESPACE__ . '\Friends', 'upgrade_plugin' ) );
}

Expand Down
44 changes: 40 additions & 4 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3656,9 +3656,13 @@ public function site_status_tests( $tests ) {
'test' => array( $this, 'friend_roles_test' ),
);
$tests['direct']['friends-cron'] = array(
'label' => __( 'Friend cron job is enabled', 'friends' ),
'label' => __( 'Friends cron job is enabled', 'friends' ),
'test' => array( $this, 'friends_cron_test' ),
);
$tests['direct']['friends-delete-cron'] = array(
'label' => __( 'Friends delete old posts cron job is enabled', 'friends' ),
'test' => array( $this, 'friends_cron_delete_test' ),
);
return $tests;
}

Expand Down Expand Up @@ -3727,7 +3731,7 @@ public function friend_roles_test() {

public function friends_cron_test() {
$result = array(
'label' => __( 'The friend cron job is enabled', 'friends' ),
'label' => __( 'The refresh cron job is enabled', 'friends' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Friends', 'friends' ),
Expand All @@ -3739,9 +3743,8 @@ public function friends_cron_test() {
'</p>',
'test' => 'friends-cron',
);

if ( ! wp_next_scheduled( 'cron_friends_refresh_feeds' ) ) {
$result['label'] = __( 'The friends cron job is not enabled', 'friends' );
$result['label'] = __( 'The refresh cron job is not enabled', 'friends' );
$result['badge']['color'] = 'red';
$result['status'] = 'critical';
$result['description'] .= '<p>';
Expand All @@ -3758,6 +3761,39 @@ public function friends_cron_test() {
return $result;
}

public function friends_cron_delete_test() {
$result = array(
'label' => __( 'The cron job to delete old posts is enabled', 'friends' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Friends', 'friends' ),
'color' => 'green',
),
'description' =>
'<p>' .
__( 'The Friends Plugin uses a cron job to delete old posts your friends.', 'friends' ) .
'</p>',
'test' => 'friends-delete-cron',
);

if ( ! wp_next_scheduled( 'cron_friends_delete_old_posts' ) ) {
$result['label'] = __( 'The cron job to delete old posts is not enabled', 'friends' );
$result['badge']['color'] = 'orange';
$result['status'] = 'recommended';
$result['description'] .= '<p>';
$result['description'] .= wp_kses_post(
sprintf(
// translators: %s is a URL.
__( '<strong>To fix this:</strong> <a href="%s">Enable the Friends cron job</a>.', 'friends' ),
esc_url( wp_nonce_url( add_query_arg( '_wp_http_referer', remove_query_arg( '_wp_http_referer' ), self_admin_url( 'admin.php?page=friends-settings&rerun-activate' ) ), 'friends-settings' ) )
)
);
$result['description'] .= '</p>';
}

return $result;
}

public function site_status_test_php_modules( $modules ) {
$modules['mbstring']['required'] = true;
return $modules;
Expand Down
30 changes: 21 additions & 9 deletions includes/class-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private function register_hooks() {

add_action( 'cron_friends_refresh_feeds', array( $this, 'cron_friends_refresh_feeds' ) );
add_action( 'friends_retrieve_user_feeds', array( $this, 'friends_retrieve_user_feeds' ) );
add_action( 'cron_friends_delete_old_posts', array( $this, 'cron_friends_delete_old_posts' ) );

add_action( 'wp_loaded', array( $this, 'friends_add_friend_redirect' ), 100 );
add_action( 'wp_feed_options', array( $this, 'wp_feed_options' ), 90 );
Expand Down Expand Up @@ -120,10 +121,15 @@ public function cron_friends_refresh_feeds() {
$feed->set_polling_now();
$this->retrieve_feed( $feed );
$feed->was_polled();
$friend_user = $feed->get_friend_user();
if ( $friend_user ) {
$friend_user->delete_outdated_posts();
}
}
}

/**
* Cron function to delete old posts.
*/
public function cron_friends_delete_old_posts() {
foreach ( User_Feed::get_all_users() as $friend_user ) {
$friend_user->delete_old_posts();
}
}

Expand All @@ -144,7 +150,6 @@ public function friends_retrieve_user_feeds( $user_id ) {
$feed->set_polling_now();
$this->retrieve_feed( $feed );
$feed->was_polled();
$friend_user->delete_outdated_posts();
}
}
}
Expand Down Expand Up @@ -299,10 +304,6 @@ public function retrieve_friend_posts( $ignore_due_date = false ) {
$feed->set_polling_now();
$this->retrieve_feed( $feed );
$feed->was_polled();
$friend_user = $feed->get_friend_user();
if ( $friend_user ) {
$friend_user->delete_outdated_posts();
}
}
}

Expand Down Expand Up @@ -556,6 +557,14 @@ public function process_incoming_feed_items( array $items, User_Feed $user_feed
$post_formats = get_post_format_strings();
$feed_post_format = $user_feed->get_post_format();

// Sort items by date asc so that older posts will have lower ids than newer posts.
usort(
$items,
function ( $a, $b ) {
return strtotime( $a->date ) - strtotime( $b->date );
}
);

// Limit this as a safety measure.
add_filter( 'wp_revisions_to_keep', array( $this, 'revisions_to_keep' ) );
$new_posts = array();
Expand Down Expand Up @@ -728,6 +737,9 @@ public function process_incoming_feed_items( array $items, User_Feed $user_feed

do_action( 'friends_retrieved_new_posts', $user_feed, $new_posts, $modified_posts, $friend_user );

$deleted_posts = $friend_user->delete_outdated_posts();
$new_posts = array_diff( $new_posts, $deleted_posts );

return $new_posts;
}

Expand Down
4 changes: 4 additions & 0 deletions includes/class-friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,10 @@ private static function setup() {
wp_schedule_event( time(), 'fifteen-minutes', 'cron_friends_refresh_feeds' );
}

if ( ! wp_next_scheduled( 'cron_friends_delete_old_posts' ) ) {
wp_schedule_event( time(), 'daily', 'cron_friends_delete_old_posts' );
}

self::add_default_sidebars_widgets();
flush_rewrite_rules();
}
Expand Down
16 changes: 16 additions & 0 deletions includes/class-user-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,22 @@ public static function get_by_url( $url ) {
return new \WP_Error( 'term_not_found' );
}

public static function get_all_users() {
$term_query = new \WP_Term_Query(
array(
'taxonomy' => self::TAXONOMY,
)
);
$users = array();
foreach ( $term_query->get_terms() as $term ) {
$feed = new self( $term );
$friend_user = $feed->get_friend_user();
$users[ $friend_user->ID ] = $friend_user;
}

return $users;
}

/**
* Get all feeds due.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function test_dont_override_activitypub_mentions() {
'href' => $this->actor,
'name' => '@' . $this->friend_nicename,
),
$object->get_tag()
$object->get_tag() ?? array()
);

$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_cc() );
Expand Down
Loading