From 3f96573795377cf8ce0467d81f1ae4a05eb3656b Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 2 Jun 2022 13:56:59 -0700 Subject: [PATCH] Bin: Add script for stopgap stats --- bin/stats-without-spam.php | 130 +++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 bin/stats-without-spam.php diff --git a/bin/stats-without-spam.php b/bin/stats-without-spam.php new file mode 100644 index 0000000..77c40f8 --- /dev/null +++ b/bin/stats-without-spam.php @@ -0,0 +1,130 @@ + Pledge\CPT_ID, + 'post_status' => 'any', + 'posts_per_page' => -1, + ) ); + + foreach ( $all_companies as $company ) { + $log = PledgeLog\get_pledge_log( $company->ID ); + $creation_entry = get_creation_entry( $log ); + $creation_date = $creation_entry[0]['timestamp'] ?? null; + + if ( 1 !== count( $creation_entry ) || empty( $creation_entry[0]['timestamp'] ) ) { + WP_CLI::error( new WP_Error( + 'log_mismatch', + "log doesn't match expectations", + array( $company, $creation_entry ) + ) ); + } + + // These companies haven't been manually verified, and many look just as inaccurate as the ones that were cleaned. + // ⚠️ This is only meaningful because it just happened a few months before this script was written. It will become + // less meaningful as time passes. + if ( $creation_date >= strtotime( DATE_CLEANED_PLEDGES ) ) { + continue; + } + + // These were manually determined to be spam/dormant. + if ( Pledge\DEACTIVE_STATUS === $company->post_status ) { + $messages = implode( ' ', wp_list_pluck( $log, 'message' ) ); + + if ( str_contains( $messages, 'Manually removing spam/dormant pledges' ) ) { + continue; + } + } + + // Don't remove companies that don't currently have any confirmed contributors, because they may have had them in the past. + + $valid_pledges[] = $company; + } + + return $valid_pledges; +} + +function get_creation_entry( array $log ): array { + $creation_entry = array_filter( $log, function( $entry ) { + return 'pledge_created' === $entry['type']; + } ); + + return array_values( $creation_entry ); // remove index gaps +} + +function get_month_counts( array $valid_pledges ) : array { + $month_counts = array(); + + foreach ( $valid_pledges as $pledge ) { + $log = PledgeLog\get_pledge_log( $pledge->ID ); + $creation_entry = get_creation_entry( $log ); + $month_key = date( 'Y-m', $creation_entry[0]['timestamp'] ); + + $month_counts[ $month_key ]++; + } + + return $month_counts; +} + +function filter_active_pledges( array $pledges ): array { + return array_filter( $pledges, function( $pledge ) { + $active_contributors = get_post_meta( $pledge->ID, PledgeMeta\META_PREFIX . 'pledge-confirmed-contributors', true ); + + return 'publish' === $pledge->post_status && count( $active_contributors ) > 0; + } ); +}