From b1d65a64a8523d84a68bef2f5985fe81dba5ea71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 27 Dec 2023 22:41:16 +0100 Subject: [PATCH] move static function that retrieves all follows and pending follow requests to the table that displays it --- includes/collection/class-follow-requests.php | 77 ------------------- includes/table/class-follow-requests.php | 61 ++++++++++++++- 2 files changed, 60 insertions(+), 78 deletions(-) delete mode 100644 includes/collection/class-follow-requests.php diff --git a/includes/collection/class-follow-requests.php b/includes/collection/class-follow-requests.php deleted file mode 100644 index ddbd41f..0000000 --- a/includes/collection/class-follow-requests.php +++ /dev/null @@ -1,77 +0,0 @@ -get_results( - $wpdb->prepare( - "SELECT SQL_CALC_FOUND_ROWS follow_request.ID AS id, follow_request.post_date AS published, follow_request.guid, follow_request.post_status AS 'status', follower.post_title AS 'post_title', follower.guid AS follower_guid, follower.id AS follower_id, follower.post_modified AS follower_modified - FROM {$wpdb->posts} AS follow_request - LEFT JOIN {$wpdb->posts} AS follower ON follow_request.post_parent = follower.ID - LEFT JOIN {$wpdb->postmeta} AS meta ON follow_request.ID = meta.post_id - WHERE follow_request.post_type = 'ap_follow_request' - AND (follower.post_title LIKE %s OR follower.guid LIKE %s) - AND meta.meta_key = 'activitypub_user_id' - AND meta.meta_value = %s - ORDER BY %s %s - LIMIT %d OFFSET %d", - '%' . $wpdb->esc_like( $search ) . '%', - '%' . $wpdb->esc_like( $search ) . '%', - $user_id, - $orderby, - $order, - $per_page, - $offset - ) - ); - $current_total_items = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); - - // Second step: Get the total rows without the LIMIT - $total_items = $wpdb->get_var( - $wpdb->prepare( - "SELECT COUNT(follow_request.ID) - FROM {$wpdb->posts} AS follow_request - LEFT JOIN {$wpdb->posts} AS follower ON follow_request.post_parent = follower.ID - LEFT JOIN {$wpdb->postmeta} AS meta ON follow_request.ID = meta.post_id - WHERE follow_request.post_type = 'ap_follow_request' - AND meta.meta_key = 'activitypub_user_id' - AND meta.meta_value = %s", - $user_id - ) - ); - - return compact( 'follow_requests', 'current_total_items', 'total_items' ); - } -} diff --git a/includes/table/class-follow-requests.php b/includes/table/class-follow-requests.php index 8c0f831..079e03d 100644 --- a/includes/table/class-follow-requests.php +++ b/includes/table/class-follow-requests.php @@ -60,6 +60,65 @@ class Follow_Requests extends WP_List_Table { return $sortable_columns; } + + /** + * Get follow requests together with information from the follower. + * + * @param int $user_id The ID of the WordPress User, which may be 0 for the blog and -1 for the application user + * @param int $per_page Number of items per page + * @param int $page_num The current page + * @param int $args May contain custom ordering or search terms. + * + * @return array Containing an array of all follow requests and the total numbers. + */ + public static function get_follow_requests_for_user( $user_id, $per_page, $page_num, $args ) { + $order = isset( $args['order'] ) && strtolower( $args['order'] ) === 'asc' ? 'ASC' : 'DESC'; + $orderby = isset( $args['orderby'] ) ? sanitize_text_field( $args['orderby'] ) : 'published'; + $search = isset( $args['s'] ) ? sanitize_text_field( $args['s'] ) : ''; + + $offset = (int) $per_page * ( (int) $page_num - 1 ); + + global $wpdb; + $follow_requests = $wpdb->get_results( + $wpdb->prepare( + "SELECT SQL_CALC_FOUND_ROWS follow_request.ID AS id, follow_request.post_date AS published, follow_request.guid, follow_request.post_status AS 'status', follower.post_title AS 'post_title', follower.guid AS follower_guid, follower.id AS follower_id, follower.post_modified AS follower_modified + FROM {$wpdb->posts} AS follow_request + LEFT JOIN {$wpdb->posts} AS follower ON follow_request.post_parent = follower.ID + LEFT JOIN {$wpdb->postmeta} AS meta ON follow_request.ID = meta.post_id + WHERE follow_request.post_type = 'ap_follow_request' + AND (follower.post_title LIKE %s OR follower.guid LIKE %s) + AND meta.meta_key = 'activitypub_user_id' + AND meta.meta_value = %s + ORDER BY %s %s + LIMIT %d OFFSET %d", + '%' . $wpdb->esc_like( $search ) . '%', + '%' . $wpdb->esc_like( $search ) . '%', + $user_id, + $orderby, + $order, + $per_page, + $offset + ) + ); + $current_total_items = $wpdb->get_var( 'SELECT FOUND_ROWS()' ); + + // Second step: Get the total rows without the LIMIT + $total_items = $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT(follow_request.ID) + FROM {$wpdb->posts} AS follow_request + LEFT JOIN {$wpdb->posts} AS follower ON follow_request.post_parent = follower.ID + LEFT JOIN {$wpdb->postmeta} AS meta ON follow_request.ID = meta.post_id + WHERE follow_request.post_type = 'ap_follow_request' + AND meta.meta_key = 'activitypub_user_id' + AND meta.meta_value = %s", + $user_id + ) + ); + + return compact( 'follow_requests', 'current_total_items', 'total_items' ); + } + public function prepare_items() { $columns = $this->get_columns(); $hidden = array(); @@ -93,7 +152,7 @@ class Follow_Requests extends WP_List_Table { } } - $follow_requests_with_count = FollowerRequestCollection::get_follow_requests_for_user( $this->user_id, $per_page, $page_num, $args ); + $follow_requests_with_count = self::get_follow_requests_for_user( $this->user_id, $per_page, $page_num, $args ); $follow_requests = $follow_requests_with_count['follow_requests']; $counter = $follow_requests_with_count['total_items'];