Skip to content

Commit b584bf3

Browse files
authored
Add contributors report, and allow data to be downloaded. (#267)
* Add contributors report, and allow data to be downloaded.
1 parent c880f28 commit b584bf3

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

plugins/wporg-5ftf/includes/reports.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
add_action( 'admin_menu', __NAMESPACE__ . '\add_admin_pages' );
1818
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
1919
add_action( 'admin_init', __NAMESPACE__ . '\export_csv' );
20+
add_action( 'admin_init', __NAMESPACE__ . '\export_contributors_csv' );
2021

2122
/**
2223
* Register admin page.
@@ -30,6 +31,15 @@ function add_admin_pages() {
3031
'5ftf_company_report',
3132
__NAMESPACE__ . '\render_company_report_page'
3233
);
34+
35+
add_submenu_page(
36+
'edit.php?post_type=5ftf_pledge',
37+
'Contributor Report',
38+
'Contributor Report',
39+
'manage_options',
40+
'5ftf_contributor_report',
41+
__NAMESPACE__ . '\render_contributor_report_page'
42+
);
3343
}
3444

3545
/**
@@ -150,6 +160,91 @@ function render_company_report_page() {
150160
set_transient( 'wporg_5ftf_company_report_' . $status, $export_data, 60 );
151161
}
152162

163+
/**
164+
* Render results and download button.
165+
*/
166+
function render_contributor_report_page() {
167+
168+
$status = sanitize_title( $_GET['status'] ?? '' );
169+
$contributor_limit = 1500;
170+
171+
if ( ! in_array( $status, array( 'pending', 'trash', 'publish' ) ) ) {
172+
$status = 'all';
173+
}
174+
175+
$contributors = get_posts( array(
176+
'post_type' => '5ftf_contributor',
177+
'post_status' => $status,
178+
'posts_per_page' => $contributor_limit, // set to avoid unexpected memory overuse.
179+
'orderby' => 'post_title',
180+
'order' => 'ASC',
181+
) );
182+
183+
// Add visible warning on page if we hit the upper limit of the query.
184+
if ( count( $contributors ) === $contributor_limit ) {
185+
echo '<p>WARNING: Contributor limit reached, check the code query.</p>';
186+
}
187+
188+
$all_contributor_data = XProfile\get_all_xprofile_contributors_indexed();
189+
?>
190+
<p>
191+
<b>Total:</b><?php echo count( $contributors ); ?>
192+
<b>Status:</b>
193+
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report">All</a>
194+
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=pending">Pending</a>
195+
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=publish">Publish</a>
196+
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=trash">Trash</a>
197+
</p>
198+
199+
<form action="#" method="post">
200+
<input type="hidden" name="wporg-5ftf-contr" value="1">
201+
<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>">
202+
<input type="submit" value="Export">
203+
<?php wp_nonce_field( '5ftf_download_contributor_report' ); ?>
204+
</form>
205+
<table id="wporg-5ftf-company-report">
206+
<tr>
207+
<th>User id</th>
208+
<th>Username</th>
209+
<th>Company</th>
210+
<th>Hours</th>
211+
<th>Teams</th>
212+
<th>Full Name</th>
213+
<th>Email</th>
214+
<th>Last login</th>
215+
<th>Status</th>
216+
</tr>
217+
<?php
218+
$export_data = array();
219+
foreach ( $contributors as $c ) {
220+
$pledge_company = get_post( $c->post_parent );
221+
$pledge_company_title = get_the_title( $pledge_company ) ?? 'unattached';
222+
$user_id = get_post_meta( $c->ID, 'wporg_user_id', true );
223+
$xprofile = $all_contributor_data[ $user_id ] ?? [
224+
'team_names' => [],
225+
'hours_per_week' => 0,
226+
];
227+
$xprofile_teams = $xprofile['team_names'] ?? [];
228+
$user = get_user_by( 'ID', $user_id );
229+
$last_login = get_user_meta( $user_id, 'last_logged_in', true );
230+
$teams = str_replace( ' Team', '', implode( ',', $xprofile_teams ) );
231+
echo '<tr>';
232+
echo '<td>' . absint( $user_id ) . '</td>';
233+
echo '<td>' . esc_html( $c->post_title ) . '</td>';
234+
echo '<td>' . esc_html( $pledge_company_title ) . '</td>';
235+
echo '<td>' . esc_html( $xprofile['hours_per_week'] ) . '</td>';
236+
echo '<td>' . esc_html( $teams ) . '</td>';
237+
echo '<td>' . esc_html( $user->display_name ) . '</td>';
238+
echo '<td>' . esc_html( $user->user_email ) . '</td>';
239+
echo '<td>' . esc_html( $last_login ) . '</td>';
240+
echo '<td>' . esc_html( $c->post_status ) . '</td>';
241+
echo '</tr>';
242+
$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 );
243+
}
244+
echo '</table>';
245+
246+
set_transient( 'wporg_5ftf_contributor_report_' . $status, $export_data, 2 * MINUTE_IN_SECONDS );
247+
}
153248
/**
154249
* CSV export runner, grabs data lazily from a transient.
155250
*/
@@ -175,3 +270,29 @@ function export_csv() {
175270

176271
$exporter->emit_file();
177272
}
273+
274+
/**
275+
* Export contributors as a CSV, also from transient.
276+
*/
277+
function export_contributors_csv() {
278+
279+
if (
280+
! isset( $_POST['wporg-5ftf-contr'] ) ||
281+
! current_user_can( 'manage_options' ) ||
282+
! wp_verify_nonce( $_POST['_wpnonce'], '5ftf_download_contributor_report' )
283+
) {
284+
return;
285+
}
286+
287+
$status = $_POST['status'];
288+
289+
$data = get_transient( 'wporg_5ftf_contributor_report_' . $status );
290+
291+
$exporter = new Export_CSV( array(
292+
'filename' => 'contributor-report-' . $status,
293+
'headers' => array( 'User id', 'Username', 'Company', 'Hours', 'Teams', 'Full Name', 'Email', 'Last Login', 'Status' ),
294+
'data' => $data,
295+
) );
296+
297+
$exporter->emit_file();
298+
}

plugins/wporg-5ftf/includes/xprofile.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ function get_all_xprofile_contributor_hours_teams(): array {
6969
return $users;
7070
}
7171

72+
/**
73+
*
74+
* Reconfigures xprofile data to be in indexed array.
75+
*
76+
* @return array
77+
*/
78+
function get_all_xprofile_contributors_indexed(): array {
79+
$all_data = get_all_xprofile_contributor_hours_teams();
80+
81+
$newdata = array();
82+
foreach ( $all_data as $contributor ) {
83+
$newdata[ $contributor->user_id ] = [
84+
'hours_per_week' => $contributor->hours_per_week,
85+
'team_names' => $contributor->team_names,
86+
];
87+
}
88+
89+
return $newdata;
90+
}
91+
7292
/**
7393
* Pull relevant data from profiles.wordpress.org.
7494
*

0 commit comments

Comments
 (0)