Skip to content

Commit

Permalink
Introduce a cronjob to delete old posts
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Feb 21, 2025
1 parent 334c649 commit b75ed3f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,21 @@ public function friends_cron_test() {
$result['description'] .= '</p>';
}

if ( ! wp_next_scheduled( 'cron_friends_delete_old_posts' ) ) {
$result['label'] = __( 'The friends delete old posts cron job is not enabled', 'friends' );
$result['badge']['color'] = 'yellow';
$result['status'] = 'warning';
$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;
}

Expand Down
10 changes: 10 additions & 0 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 @@ -123,6 +124,15 @@ public function cron_friends_refresh_feeds() {
}
}

/**
* 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();
}
}

/**
* Function to fetch a users feeds.
*
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

0 comments on commit b75ed3f

Please sign in to comment.