| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function affwp_network_stats_shortcode( $atts ) {
|
| if ( ! function_exists( 'affwp_get_affiliate_id' ) ) {
|
| return '';
|
| }
|
|
|
| if ( ! function_exists( '\AffiliateWP\MTC\affiliate_wp_mtc' ) ) {
|
| return '';
|
| }
|
|
|
| $affiliate_id = affwp_get_affiliate_id();
|
| if ( ! $affiliate_id ) {
|
| return '';
|
| }
|
|
|
| $atts = shortcode_atts(
|
| array(
|
| 'type' => 'total_earnings',
|
| 'status' => 'all',
|
| ),
|
| $atts,
|
| 'affwp_network_stats'
|
| );
|
|
|
| $type = sanitize_key( $atts['type'] );
|
| $status = sanitize_key( $atts['status'] );
|
|
|
|
|
|
|
| if ( 'own_earnings' === $type ) {
|
| $total = 0.0;
|
| if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
|
| $total += (float) affwp_get_affiliate_earnings( $affiliate_id, false );
|
| }
|
| if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
|
| $total += (float) affwp_get_affiliate_unpaid_earnings( $affiliate_id, false );
|
| }
|
| return affwp_currency_filter( affwp_format_amount( $total ) );
|
| }
|
|
|
| if ( 'own_referrals' === $type ) {
|
| $count = 0;
|
| if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
|
| $count += (int) affwp_count_referrals( $affiliate_id, 'paid' );
|
| }
|
| if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
|
| $count += (int) affwp_count_referrals( $affiliate_id, 'unpaid' );
|
| }
|
| return $count;
|
| }
|
|
|
|
|
|
|
| $all_ids = array();
|
| $queue = array( $affiliate_id );
|
| $visited = array();
|
|
|
| while ( ! empty( $queue ) ) {
|
| $current = array_shift( $queue );
|
| if ( in_array( $current, $visited, true ) ) {
|
| continue;
|
| }
|
| $visited[] = $current;
|
| $all_ids[] = $current;
|
| $sub_ids = \AffiliateWP\MTC\affiliate_wp_mtc()->network->get_sub_affiliates( $current );
|
| foreach ( $sub_ids as $sub_id ) {
|
| $queue[] = $sub_id;
|
| }
|
| }
|
|
|
| $downline_ids = array_diff( $all_ids, array( $affiliate_id ) );
|
|
|
| if ( 'network_count' === $type ) {
|
| return count( $downline_ids );
|
| }
|
|
|
| $ids_to_sum = ( 'downline_earnings' === $type ) ? $downline_ids : $all_ids;
|
|
|
| if ( 'downline_referrals' === $type ) {
|
| $count = 0;
|
| foreach ( $downline_ids as $id ) {
|
| if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
|
| $count += (int) affwp_count_referrals( $id, 'paid' );
|
| }
|
| if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
|
| $count += (int) affwp_count_referrals( $id, 'unpaid' );
|
| }
|
| }
|
| return $count;
|
| }
|
|
|
|
|
| $total = 0.0;
|
| foreach ( $ids_to_sum as $id ) {
|
| if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
|
| $total += (float) affwp_get_affiliate_earnings( $id, false );
|
| }
|
| if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
|
| $total += (float) affwp_get_affiliate_unpaid_earnings( $id, false );
|
| }
|
| }
|
|
|
| return affwp_currency_filter( affwp_format_amount( $total ) );
|
| }
|
| add_shortcode( 'affwp_network_stats', 'affwp_network_stats_shortcode' );
|
|
|
| |
| |
Comments