diff --git a/plugins/wporg-5ftf/includes/reports.php b/plugins/wporg-5ftf/includes/reports.php
index 4485340..43451cc 100644
--- a/plugins/wporg-5ftf/includes/reports.php
+++ b/plugins/wporg-5ftf/includes/reports.php
@@ -17,6 +17,7 @@ defined( 'WPINC' ) || die();
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,89 @@ 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
+
+
+
+
+
+ User id |
+ Username |
+ Company |
+ Hours |
+ Teams |
+ Full Name |
+ Email |
+ Last login |
+ Status |
+
+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 '' . absint( $user_id ) . ' | ';
+ echo '' . esc_html( $c->post_title ) . ' | ';
+ echo '' . esc_html( $pledge_company_title ) . ' | ';
+ echo '' . esc_html( $xprofile['hours_per_week'] ) . ' | ';
+ echo '' . esc_html( $teams ) . ' | ';
+ echo '' . esc_html( $user->display_name ) . ' | ';
+ echo '' . esc_html( $user->user_email ) . ' | ';
+ echo '' . esc_html( $last_login ) . ' | ';
+ echo '' . esc_html( $c->post_status ) . ' | ';
+ 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 '
';
+
+ set_transient( 'wporg_5ftf_contributor_report_' . $status, $export_data, 2 * MINUTE_IN_SECONDS );
+
+}
/**
* CSV export runner, grabs data lazily from a transient.
*/
@@ -175,3 +268,30 @@ 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 9058709..298eef8 100644
--- a/plugins/wporg-5ftf/includes/xprofile.php
+++ b/plugins/wporg-5ftf/includes/xprofile.php
@@ -69,6 +69,25 @@ 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.
*