17
17
add_action ( 'admin_menu ' , __NAMESPACE__ . '\add_admin_pages ' );
18
18
add_action ( 'admin_enqueue_scripts ' , __NAMESPACE__ . '\enqueue_assets ' );
19
19
add_action ( 'admin_init ' , __NAMESPACE__ . '\export_csv ' );
20
+ add_action ( 'admin_init ' , __NAMESPACE__ . '\export_contributors_csv ' );
20
21
21
22
/**
22
23
* Register admin page.
@@ -30,6 +31,15 @@ function add_admin_pages() {
30
31
'5ftf_company_report ' ,
31
32
__NAMESPACE__ . '\render_company_report_page '
32
33
);
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
+ );
33
43
}
34
44
35
45
/**
@@ -150,6 +160,91 @@ function render_company_report_page() {
150
160
set_transient ( 'wporg_5ftf_company_report_ ' . $ status , $ export_data , 60 );
151
161
}
152
162
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
+ }
153
248
/**
154
249
* CSV export runner, grabs data lazily from a transient.
155
250
*/
@@ -175,3 +270,29 @@ function export_csv() {
175
270
176
271
$ exporter ->emit_file ();
177
272
}
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
+ }
0 commit comments