, totals: array} */ public static function summarize( array $opens, int $doors ): array { $perDoor = []; for ( $n = 1; $n <= $doors; $n++ ) { $perDoor[ $n ] = [ 'door' => $n, 'opens' => 0, 'coupons' => 0, ]; } $people = []; foreach ( $opens as $row ) { $n = (int) $row['door']; if ( ! isset( $perDoor[ $n ] ) ) { continue; } ++$perDoor[ $n ]['opens']; if ( ! empty( $row['coupon_id'] ) ) { ++$perDoor[ $n ]['coupons']; } if ( ! empty( $row['subscriber_id'] ) ) { $people[ (int) $row['subscriber_id'] ] = true; } } return [ 'doors' => $perDoor, 'totals' => [ 'opens' => array_sum( array_column( $perDoor, 'opens' ) ), 'people' => count( $people ), 'coupons' => array_sum( array_column( $perDoor, 'coupons' ) ), ], ]; } /** * The report for a saved calendar, plus subscriber counts. */ public static function build( array $config ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, admin report. $opens = $wpdb->get_results( $wpdb->prepare( "SELECT door, subscriber_id, coupon_id FROM {$wpdb->prefix}advently_opens WHERE calendar_start = %s", $config['start'] ), ARRAY_A ); $report = self::summarize( is_array( $opens ) ? $opens : [], $config['doors'] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, admin report. $report['totals']['subscribers'] = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}advently_subscribers" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, admin report. $report['totals']['optin'] = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}advently_subscribers WHERE consent_at IS NOT NULL" ); $report['currency'] = html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ); /** * Filters the Results report before the admin app receives it. * * @param array $report Report: doors (per door) and totals. * @param array $config Calendar config. */ return (array) apply_filters( 'advently_report', $report, $config ); } }