get_option( 'dashboard_days_back', self::DEFAULT_DAYS_BACK ); if ( isset( $_POST['affiliates-dashboard-widget-submitted'] ) ) { if ( wp_verify_nonce( $_POST[self::NONCE], 'admin' ) ) { if ( !empty( $_POST['days_back'] ) ) { $days_back = abs( intval( $_POST['days_back'] ) ); if ( $days_back < self::MIN_DAYS_BACK ) { $days_back = self::MIN_DAYS_BACK; } $affiliates_options->update_option( 'dashboard_days_back', $days_back ); } else { $days_back = null; $affiliates_options->delete_option( 'dashboard_days_back' ); } } } $from_date = null; $thru_date = null; if ( $days_back > self::MIN_DAYS_BACK ) { $thru_date = date( 'Y-m-d', time() ); // today $from_date = date( 'Y-m-d', strtotime( $thru_date ) - $days_back * 3600 * 24 ); } $totals = array(); $totals[AFFILIATES_REFERRAL_STATUS_CLOSED] = self::get_totals( $from_date, $thru_date, AFFILIATES_REFERRAL_STATUS_CLOSED ); $totals[AFFILIATES_REFERRAL_STATUS_ACCEPTED] = self::get_totals( $from_date, $thru_date, AFFILIATES_REFERRAL_STATUS_ACCEPTED ); $totals[AFFILIATES_REFERRAL_STATUS_PENDING] = self::get_totals( $from_date, $thru_date, AFFILIATES_REFERRAL_STATUS_PENDING ); $totals[AFFILIATES_REFERRAL_STATUS_REJECTED] = self::get_totals( $from_date, $thru_date, AFFILIATES_REFERRAL_STATUS_REJECTED ); $output = ''; $output .= '
| '; $output .= ''; switch( $status ) { case AFFILIATES_REFERRAL_STATUS_CLOSED : $output .= sprintf( __( 'Closed', 'affiliates' ), esc_attr( __( 'Accumulated total for closed referrals (commissions paid).', 'affiliates' ) ) ); break; case AFFILIATES_REFERRAL_STATUS_ACCEPTED : $output .= sprintf( __( 'Accepted', 'affiliates' ), esc_attr( __( 'Accumulated total for accepted referrals (commissions unpaid).', 'affiliates' ) ) ); break; case AFFILIATES_REFERRAL_STATUS_PENDING : $output .= sprintf( __( 'Pending', 'affiliates' ), esc_attr( __( 'Accumulated total for pending referrals.', 'affiliates' ) ) ); break; case AFFILIATES_REFERRAL_STATUS_REJECTED : $output .= sprintf( __( 'Rejected', 'affiliates' ), esc_attr( __( 'Accumulated total for rejected referrals.', 'affiliates' ) ) ); break; } $output .= ''; $output .= ' | '; } $output .= '
|---|
', 'vertical-align:top;' . !$is_rtl ? 'text-align:right;padding-left:1.62em;padding-right:1em;' : 'padding-left:1em;padding-right:1.62em;' );
$output .= '
| ';
}
$output .= '
'; $output .= __( 'Shows accumulated referral totals for all time when left empty, or for the last number of days set.', 'affiliates' ); $output .= '
'; echo $output; } /** * Returns totals for the given period or for all time. * @param string $from_date * @param string $thru_date * @param string $status */ private static function get_totals( $from_date = null , $thru_date = null, $status = AFFILIATES_REFERRAL_STATUS_ACCEPTED ) { global $wpdb; $referrals_table = _affiliates_get_tablename( 'referrals' ); $where = " WHERE TRUE "; $values = array(); if ( $from_date ) { $from_date = date( 'Y-m-d', strtotime( $from_date ) ); } if ( $thru_date ) { $thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 ); } if ( $from_date && $thru_date ) { $where .= " AND datetime >= %s AND datetime < %s "; $values[] = $from_date; $values[] = $thru_date; } else if ( $from_date ) { $where .= " AND datetime >= %s "; $values[] = $from_date; } else if ( $thru_date ) { $where .= " AND datetime < %s "; $values[] = $thru_date; } if ( !empty( $status ) ) { $where .= " AND status = %s "; $values[] = $status; } $totals = $wpdb->get_results( $wpdb->prepare( "SELECT SUM(amount) total, currency_id FROM $referrals_table $where GROUP BY currency_id ", $values ) ); if ( $totals ) { $result = array(); foreach( $totals as $total ) { if ( ( $total->currency_id !== null ) && ( $total->total !== null ) ) { $result[$total->currency_id] = $total->total; } } return $result; } else { return false; } } } Affiliates_Dashboard_Widget::init();