| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if ( ! defined( 'ABSPATH' ) ) {
|
| exit;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action( 'affwp_referrals_dashboard_before_table', 'affwp_custom_referrals_export_button' );
|
| function affwp_custom_referrals_export_button( $affiliate_id ) {
|
| $export_url = wp_nonce_url(
|
| add_query_arg( 'affwp_export_referrals', '1' ),
|
| 'affwp_export_referrals_' . absint( $affiliate_id ),
|
| '_affwp_export_nonce'
|
| );
|
| ?>
|
| <p style="margin-bottom: 12px;">
|
| <a href="<?php echo esc_url( $export_url ); ?>" class="affwp-button">
|
| <?php esc_html_e( 'Download Referrals CSV', 'affiliate-wp' ); ?>
|
| </a>
|
| </p>
|
| <?php
|
| }
|
|
|
| /**
|
| * Handles the CSV export request.
|
| *
|
| * Security: verifies the affiliate is logged in, validates the nonce,
|
| * and only returns referrals belonging to the currently logged-in affiliate.
|
| * Affiliates cannot export another affiliate's data.
|
| */
|
| add_action( 'init', 'affwp_custom_handle_referrals_export' );
|
| function affwp_custom_handle_referrals_export() {
|
|
|
| if ( ! isset( $_GET['affwp_export_referrals'] ) ) {
|
| return;
|
| }
|
|
|
|
|
| if ( ! affwp_is_affiliate() ) {
|
| wp_die( esc_html__( 'You must be an active affiliate to export referrals.', 'affiliate-wp' ) );
|
| }
|
|
|
| $affiliate_id = affwp_get_affiliate_id();
|
|
|
|
|
| $nonce = isset( $_GET['_affwp_export_nonce'] )
|
| ? sanitize_text_field( wp_unslash( $_GET['_affwp_export_nonce'] ) )
|
| : '';
|
|
|
| if ( ! wp_verify_nonce( $nonce, 'affwp_export_referrals_' . $affiliate_id ) ) {
|
| wp_die( esc_html__( 'Security check failed. Please return to your affiliate area and try again.', 'affiliate-wp' ) );
|
| }
|
|
|
|
|
|
|
|
|
| $referrals = affiliate_wp()->referrals->get_referrals(
|
| [
|
| 'affiliate_id' => $affiliate_id,
|
| 'number' => -1,
|
| 'status' => [ 'paid', 'unpaid', 'rejected' ],
|
| 'orderby' => 'date',
|
| 'order' => 'DESC',
|
| ]
|
| );
|
|
|
|
|
| nocache_headers();
|
| header( 'Content-Type: text/csv; charset=utf-8' );
|
| header( 'Content-Disposition: attachment; filename="referrals-' . gmdate( 'Y-m-d' ) . '.csv"' );
|
|
|
| $output = fopen( 'php://output', 'w' );
|
|
|
| $is_woocommerce = class_exists( 'WooCommerce' );
|
|
|
|
|
| if ( $is_woocommerce ) {
|
| fputcsv(
|
| $output,
|
| [ 'Date', 'Order ID', 'Customer Name', 'Customer Email', 'Items Ordered', 'Order Total', 'Commission Earned', 'Status' ]
|
| );
|
| } else {
|
| fputcsv(
|
| $output,
|
| [ 'Date', 'Reference', 'Description', 'Amount', 'Status' ]
|
| );
|
| }
|
|
|
| foreach ( $referrals as $referral ) {
|
|
|
|
|
| if ( $is_woocommerce && ! empty( $referral->reference ) ) {
|
| $order = wc_get_order( absint( $referral->reference ) );
|
|
|
| if ( $order instanceof WC_Abstract_Order ) {
|
|
|
| $items = [];
|
| foreach ( $order->get_items() as $item ) {
|
| $items[] = $item->get_name() . ' x' . $item->get_quantity();
|
| }
|
|
|
| fputcsv(
|
| $output,
|
| [
|
| $referral->date_i18n( 'datetime' ),
|
| $referral->reference,
|
| $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
|
| $order->get_billing_email(),
|
| implode( ' | ', $items ),
|
| $order->get_total(),
|
| $referral->amount,
|
| affwp_get_referral_status_label( $referral ),
|
| ]
|
| );
|
| continue;
|
| }
|
| }
|
|
|
|
|
| fputcsv(
|
| $output,
|
| [
|
| $referral->date_i18n( 'datetime' ),
|
| $referral->reference,
|
| $referral->description,
|
| $referral->amount,
|
| affwp_get_referral_status_label( $referral ),
|
| ]
|
| );
|
| }
|
|
|
| fclose( $output );
|
| exit;
|
| }
|
|
|
| |
| |
Comments