Home / eCommerce / AffiliateWP – Admin Notification for Vanity Coupon Code Requests
Duplicate Snippet

Embed Snippet on Your Site

AffiliateWP – Admin Notification for Vanity Coupon Code Requests

Sends an admin email notification whenever an affiliate submits a valid vanity coupon code change request via the AffiliateWP Vanity Coupon Codes add-on. The notification includes the affiliate's name, ID, current code, requested code, and a direct link to the review page. Only fires for requests that pass validation (unique code, no pending request already in queue, no invalid characters) -- so no false-alarm emails for failed submissions. Requires the Vanity Coupon Codes add-on to be active.

Code Preview
php
<?php
add_action( 'wp_ajax_validate_vanity_code', 'send_vanity_coupon_request_notification', 9 );
function send_vanity_coupon_request_notification() {
    if ( ! isset( $_REQUEST['vccNonce'] ) || ! wp_verify_nonce( $_REQUEST['vccNonce'], 'request_vanity_coupon_code' ) ) {
        return;
    }
    $affiliate_id = isset( $_REQUEST['affiliateID'] ) ? sanitize_text_field( $_REQUEST['affiliateID'] ) : false;
    $coupon_id    = isset( $_REQUEST['couponID'] )    ? sanitize_text_field( $_REQUEST['couponID'] )    : false;
    $old_code     = isset( $_REQUEST['oldCode'] )     ? sanitize_text_field( $_REQUEST['oldCode'] )     : false;
    $new_code     = isset( $_REQUEST['newCode'] )     ? sanitize_text_field( $_REQUEST['newCode'] )     : false;
    $integration  = isset( $_REQUEST['integration'] ) ? sanitize_text_field( $_REQUEST['integration'] ) : false;
    $type         = isset( $_REQUEST['type'] )        ? sanitize_text_field( $_REQUEST['type'] )        : false;
    // Mirror the main handler's bail conditions so we only notify on requests that will succeed.
    if ( ! $affiliate_id || ! $coupon_id || ! $old_code || ! $new_code || ! $integration || ! $type ) {
        return;
    }
    if ( affiliatewp_vanity_coupon_codes()->db->is_pending( $coupon_id ) ) {
        return;
    }
    $sanitized_new_code = affiliatewp_vanity_coupon_codes()->db->sanitize_vanity_code( $new_code );
    if ( strtoupper( $new_code ) !== $sanitized_new_code || empty( $sanitized_new_code ) ) {
        return;
    }
    if ( ! affiliatewp_vanity_coupon_codes()->db->is_unique( $sanitized_new_code, $integration ) ) {
        return;
    }
    $affiliate = affwp_get_affiliate( $affiliate_id );
    if ( ! $affiliate ) {
        return;
    }
    $user = get_userdata( affwp_get_affiliate_user_id( $affiliate->affiliate_id ) );
    if ( ! $user ) {
        return;
    }
    $admin_email = get_option( 'admin_email' );
    $subject     = sprintf( '[%s] New Vanity Coupon Code Request', get_bloginfo( 'name' ) );
    $message     = sprintf(
        'Affiliate: %s' . "\n\n" .
        'Affiliate ID: %d' . "\n\n" .
        'Current Code: %s' . "\n\n" .
        'Requested Code: %s' . "\n\n" .
        'To review this request, please visit: %s',
        $user->display_name,
        $affiliate->affiliate_id,
        $old_code,
        $sanitized_new_code,
        admin_url( 'admin.php?page=affiliate-wp-vanity-coupon-codes' )
    );
    $headers = array(
        'Content-Type: text/plain; charset=UTF-8',
        'From: ' . get_bloginfo( 'name' ) . ' <' . $admin_email . '>',
    );
    wp_mail( $admin_email, $subject, $message, $headers );
}

Comments

Add a Comment