From b584bf354ecca0e39c26245f18bb1674346a7ed0 Mon Sep 17 00:00:00 2001 From: Paul Kevan <2290623+pkevan@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:03:23 +0100 Subject: [PATCH] Add contributors report, and allow data to be downloaded. (#267) * Add contributors report, and allow data to be downloaded. --- plugins/wporg-5ftf/includes/reports.php | 121 +++++++++++++++++++++++ plugins/wporg-5ftf/includes/xprofile.php | 20 ++++ 2 files changed, 141 insertions(+) diff --git a/plugins/wporg-5ftf/includes/reports.php b/plugins/wporg-5ftf/includes/reports.php index 44853402..cd68c652 100644 --- a/plugins/wporg-5ftf/includes/reports.php +++ b/plugins/wporg-5ftf/includes/reports.php @@ -17,6 +17,7 @@ add_action( 'admin_menu', __NAMESPACE__ . '\add_admin_pages' ); add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' ); add_action( 'admin_init', __NAMESPACE__ . '\export_csv' ); +add_action( 'admin_init', __NAMESPACE__ . '\export_contributors_csv' ); /** * Register admin page. @@ -30,6 +31,15 @@ function add_admin_pages() { '5ftf_company_report', __NAMESPACE__ . '\render_company_report_page' ); + + add_submenu_page( + 'edit.php?post_type=5ftf_pledge', + 'Contributor Report', + 'Contributor Report', + 'manage_options', + '5ftf_contributor_report', + __NAMESPACE__ . '\render_contributor_report_page' + ); } /** @@ -150,6 +160,91 @@ function render_company_report_page() { set_transient( 'wporg_5ftf_company_report_' . $status, $export_data, 60 ); } +/** + * Render results and download button. + */ +function render_contributor_report_page() { + + $status = sanitize_title( $_GET['status'] ?? '' ); + $contributor_limit = 1500; + + if ( ! in_array( $status, array( 'pending', 'trash', 'publish' ) ) ) { + $status = 'all'; + } + + $contributors = get_posts( array( + 'post_type' => '5ftf_contributor', + 'post_status' => $status, + 'posts_per_page' => $contributor_limit, // set to avoid unexpected memory overuse. + 'orderby' => 'post_title', + 'order' => 'ASC', + ) ); + + // Add visible warning on page if we hit the upper limit of the query. + if ( count( $contributors ) === $contributor_limit ) { + echo '

WARNING: Contributor limit reached, check the code query.

'; + } + + $all_contributor_data = XProfile\get_all_xprofile_contributors_indexed(); + ?> +

+ Total: + Status: + All + Pending + Publish + Trash +

+ +
+ + + + +
+ + + + + + + + + + + + + post_parent ); + $pledge_company_title = get_the_title( $pledge_company ) ?? 'unattached'; + $user_id = get_post_meta( $c->ID, 'wporg_user_id', true ); + $xprofile = $all_contributor_data[ $user_id ] ?? [ + 'team_names' => [], + 'hours_per_week' => 0, + ]; + $xprofile_teams = $xprofile['team_names'] ?? []; + $user = get_user_by( 'ID', $user_id ); + $last_login = get_user_meta( $user_id, 'last_logged_in', true ); + $teams = str_replace( ' Team', '', implode( ',', $xprofile_teams ) ); + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + $export_data[] = array( $user_id, $c->post_title, $pledge_company_title, $xprofile['hours_per_week'], $teams, $user->display_name, $user->user_email, $last_login, $c->post_status ); + } + echo '
User idUsernameCompanyHoursTeamsFull NameEmailLast loginStatus
' . absint( $user_id ) . '' . esc_html( $c->post_title ) . '' . esc_html( $pledge_company_title ) . '' . esc_html( $xprofile['hours_per_week'] ) . '' . esc_html( $teams ) . '' . esc_html( $user->display_name ) . '' . esc_html( $user->user_email ) . '' . esc_html( $last_login ) . '' . esc_html( $c->post_status ) . '
'; + + set_transient( 'wporg_5ftf_contributor_report_' . $status, $export_data, 2 * MINUTE_IN_SECONDS ); +} /** * CSV export runner, grabs data lazily from a transient. */ @@ -175,3 +270,29 @@ function export_csv() { $exporter->emit_file(); } + +/** + * Export contributors as a CSV, also from transient. + */ +function export_contributors_csv() { + + if ( + ! isset( $_POST['wporg-5ftf-contr'] ) || + ! current_user_can( 'manage_options' ) || + ! wp_verify_nonce( $_POST['_wpnonce'], '5ftf_download_contributor_report' ) + ) { + return; + } + + $status = $_POST['status']; + + $data = get_transient( 'wporg_5ftf_contributor_report_' . $status ); + + $exporter = new Export_CSV( array( + 'filename' => 'contributor-report-' . $status, + 'headers' => array( 'User id', 'Username', 'Company', 'Hours', 'Teams', 'Full Name', 'Email', 'Last Login', 'Status' ), + 'data' => $data, + ) ); + + $exporter->emit_file(); +} diff --git a/plugins/wporg-5ftf/includes/xprofile.php b/plugins/wporg-5ftf/includes/xprofile.php index 90587094..59595bce 100644 --- a/plugins/wporg-5ftf/includes/xprofile.php +++ b/plugins/wporg-5ftf/includes/xprofile.php @@ -69,6 +69,26 @@ function get_all_xprofile_contributor_hours_teams(): array { return $users; } +/** + * + * Reconfigures xprofile data to be in indexed array. + * + * @return array + */ +function get_all_xprofile_contributors_indexed(): array { + $all_data = get_all_xprofile_contributor_hours_teams(); + + $newdata = array(); + foreach ( $all_data as $contributor ) { + $newdata[ $contributor->user_id ] = [ + 'hours_per_week' => $contributor->hours_per_week, + 'team_names' => $contributor->team_names, + ]; + } + + return $newdata; +} + /** * Pull relevant data from profiles.wordpress.org. *