Skip to content

Commit

Permalink
Add contributors report, and allow data to be downloaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkevan committed Jul 3, 2024
1 parent 554347a commit a26287c
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
120 changes: 120 additions & 0 deletions plugins/wporg-5ftf/includes/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'
);
}

/**
Expand Down Expand Up @@ -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 '<p>WARNING: Contributor limit reached, check the code query.</p>';
}

$all_contributor_data = XProfile\get_all_xprofile_contributors_indexed();
?>

Check failure on line 189 in plugins/wporg-5ftf/includes/reports.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

Line indented incorrectly; expected 1 tabs, found 0
<p>
<b>Total:</b><?php echo count( $contributors ); ?>
<b>Status:</b>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report">All</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=pending">Pending</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=publish">Publish</a>
<a href="edit.php?post_type=5ftf_pledge&page=5ftf_contributor_report&status=trash">Trash</a>
</p>

<form action="#" method="post">
<input type="hidden" name="wporg-5ftf-contr" value="1">
<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>">
<input type="submit" value="Export">
<?php wp_nonce_field( '5ftf_download_contributor_report' ); ?>
</form>
<table id="wporg-5ftf-company-report">
<tr>
<th>User id</th>
<th>Username</th>
<th>Company</th>
<th>Hours</th>
<th>Teams</th>
<th>Full Name</th>
<th>Email</th>
<th>Last login</th>
<th>Status</th>
</tr>
<?php

Check failure on line 217 in plugins/wporg-5ftf/includes/reports.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

Line indented incorrectly; expected at least 1 tabs, found 0
$export_data = array();
foreach ( $contributors as $c ) {
$pledge_company = get_post( $c->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 ];

Check failure on line 223 in plugins/wporg-5ftf/includes/reports.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

When a multi-item array uses associative keys, each value should start on a new line.
$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 '<tr>';
echo '<td>' . absint( $user_id ) . '</td>';
echo '<td>' . esc_html( $c->post_title ) . '</td>';
echo '<td>' . esc_html( $pledge_company_title ) . '</td>';
echo '<td>' . esc_html( $xprofile['hours_per_week'] ) . '</td>';
echo '<td>' . esc_html( $teams ) . '</td>';
echo '<td>' . esc_html( $user->display_name ) . '</td>';
echo '<td>' . esc_html( $user->user_email ) . '</td>';
echo '<td>' . esc_html( $last_login ) . '</td>';
echo '<td>' . esc_html( $c->post_status ) . '</td>';
echo '</tr>';
$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 '</table>';

set_transient( 'wporg_5ftf_contributor_report_' . $status, $export_data, 2 * MINUTE_IN_SECONDS );

}

Check failure on line 245 in plugins/wporg-5ftf/includes/reports.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

Function closing brace must go on the next line following the body; found 1 blank lines before brace
/**
* CSV export runner, grabs data lazily from a transient.
*/
Expand All @@ -175,3 +268,30 @@ function export_csv() {

$exporter->emit_file();
}

/**
* Export contributors as a CSV, also from transient.
*
*/

Check failure on line 275 in plugins/wporg-5ftf/includes/reports.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

Additional blank lines found at end of doc comment
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();
}
19 changes: 19 additions & 0 deletions plugins/wporg-5ftf/includes/xprofile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 78 in plugins/wporg-5ftf/includes/xprofile.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

There must not be a space before the colon in a return type declaration

$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 ];

Check failure on line 84 in plugins/wporg-5ftf/includes/xprofile.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

When a multi-item array uses associative keys, each value should start on a new line.
}

return $newdata;

}

Check failure on line 89 in plugins/wporg-5ftf/includes/xprofile.php

View workflow job for this annotation

GitHub Actions / PHP CodeSniffer

Function closing brace must go on the next line following the body; found 1 blank lines before brace

/**
* Pull relevant data from profiles.wordpress.org.
*
Expand Down

0 comments on commit a26287c

Please sign in to comment.