login_prompt();
}
ob_start();
$user_id = get_current_user_id();
$engine = ADSD_RCC_Referral_Engine::instance();
$settings = ADSD_RCC_Settings::instance();
$data = $engine->get_dashboard_data( $user_id );
$links = $data['links'];
$referrals = $data['referrals'];
$rewards = $data['rewards'];
$limit = $data['daily_limit'];
$today = $data['links_today'];
?>
code_expiry_days();
/* translators: %d: number of days reward codes remain valid */
echo esc_html( sprintf( __( 'Rewards are valid for %d days.', 'adsd-referral-couponcore' ), $expiry_days ) ); ?>
token, ADSD_RCC_Settings::instance()->page_url( 'register' ) );
?>
token ); ?>
⎘
created_at ) ) ); ?>
click_count ); ?>
expires_at ? esc_html( date_i18n( get_option( 'date_format' ), strtotime( $link->expires_at ) ) ) : '—'; ?>
status ); ?>
$r->status === 'active' );
if ( $active ) : ?>
login_prompt();
}
ob_start();
$user_id = get_current_user_id();
$rewards = ADSD_RCC_DB::instance()->get_rewards_for_user( $user_id );
?>
reward_code ); ?>
status ) ); ?>
expires_at ? esc_html( date_i18n( get_option( 'date_format' ), strtotime( $reward->expires_at ) ) ) : '—'; ?>
used_by ? esc_html( ucfirst( str_replace( '_', ' ', $reward->used_by ) ) ) : '—'; ?>
' . esc_html__( 'You are already logged in.', 'adsd-referral-couponcore' ) . '';
}
$ref_token = ADSD_RCC_Referral_Engine::instance()->get_session_token();
ob_start();
?>
page_url( 'dashboard' );
wp_safe_redirect( $redirect );
exit;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display parameters set by our own redirects
$notice = sanitize_text_field( wp_unslash( $_GET['adsd_rcc_notice'] ?? '' ) );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$message = sanitize_text_field( urldecode( wp_unslash( $_GET['adsd_rcc_message'] ?? '' ) ) );
ob_start();
?>
ADSD_RCC_Settings::instance()->page_url( 'dashboard' ),
'label_username' => __( 'Email Address', 'adsd-referral-couponcore' ),
'label_password' => __( 'Password', 'adsd-referral-couponcore' ),
'label_log_in' => __( 'Log In', 'adsd-referral-couponcore' ),
'remember' => true,
] );
?>
__( 'You must be logged in.', 'adsd-referral-couponcore' ) ] );
return;
}
$nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) );
if ( ! wp_verify_nonce( $nonce, 'adsd_rcc_public_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Security check failed.', 'adsd-referral-couponcore' ) ] );
return;
}
$result = ADSD_RCC_Referral_Engine::instance()->generate_link( get_current_user_id() );
if ( is_wp_error( $result ) ) {
wp_send_json_error( [ 'message' => $result->get_error_message() ] );
return;
}
wp_send_json_success( $result );
}
// ─── AJAX: Checker Validate ───────────────────────────────────────────────
public function ajax_checker_validate(): void {
$nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) );
$passkey = sanitize_text_field( wp_unslash( $_POST['passkey'] ?? '' ) );
$code = sanitize_text_field( wp_unslash( $_POST['reward_code'] ?? '' ) );
if ( ! wp_verify_nonce( $nonce, 'adsd_rcc_public_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Security check failed.', 'adsd-referral-couponcore' ) ] );
return;
}
if ( ! ADSD_RCC_DB::instance()->validate_passkey( $passkey ) ) {
wp_send_json_error( [ 'message' => __( 'Invalid passkey.', 'adsd-referral-couponcore' ) ] );
return;
}
$reward = ADSD_RCC_DB::instance()->get_reward_by_code( strtoupper( $code ) );
if ( ! $reward ) {
wp_send_json_error( [ 'message' => __( 'Reward code not found.', 'adsd-referral-couponcore' ) ] );
return;
}
$user = get_user_by( 'id', (int) $reward->user_id );
wp_send_json_success( [
'reward_code' => $reward->reward_code,
'reward_value' => ADSD_RCC_Reward_Engine::format_value( $reward ),
'status' => $reward->status,
'expires_at' => $reward->expires_at
? date_i18n( get_option( 'date_format' ), strtotime( $reward->expires_at ) )
: __( 'No expiry', 'adsd-referral-couponcore' ),
'user_email' => $user ? $user->user_email : '',
'can_mark_used' => $reward->status === 'active',
] );
}
// ─── AJAX: Checker Mark Used ──────────────────────────────────────────────
public function ajax_checker_mark_used(): void {
$nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) );
$passkey = sanitize_text_field( wp_unslash( $_POST['passkey'] ?? '' ) );
$code = sanitize_text_field( wp_unslash( $_POST['reward_code'] ?? '' ) );
if ( ! wp_verify_nonce( $nonce, 'adsd_rcc_public_nonce' ) ) {
wp_send_json_error( [ 'message' => __( 'Security check failed.', 'adsd-referral-couponcore' ) ] );
return;
}
if ( ! ADSD_RCC_DB::instance()->validate_passkey( $passkey ) ) {
wp_send_json_error( [ 'message' => __( 'Invalid passkey.', 'adsd-referral-couponcore' ) ] );
return;
}
$db = ADSD_RCC_DB::instance();
$reward = $db->get_reward_by_code( strtoupper( $code ) );
if ( ! $reward ) {
wp_send_json_error( [ 'message' => __( 'Reward not found.', 'adsd-referral-couponcore' ) ] );
return;
}
if ( $reward->status !== 'active' ) {
wp_send_json_error( [
'message' => sprintf(
/* translators: %s: current status of the reward (e.g. expired, used) */
__( 'This reward is already %s.', 'adsd-referral-couponcore' ),
$reward->status
),
] );
return;
}
$db->update_reward( (int) $reward->id, [
'status' => 'used',
'used_at' => current_time( 'mysql', true ),
'used_by' => 'local_shop',
'shopkeeper_used' => 1,
] );
$user = get_user_by( 'id', (int) $reward->user_id );
if ( $user ) {
ADSD_RCC_Reward_Engine::instance()->send_used_email( $user, $reward );
}
$db->log( 'reward_used_local', (int) $reward->user_id, (int) $reward->id, 'Marked as used via Checker.' );
wp_send_json_success( [ 'message' => __( 'Reward successfully marked as used.', 'adsd-referral-couponcore' ) ] );
}
// ─── Helpers ─────────────────────────────────────────────────────────────
private function login_prompt(): string {
return '';
}
// ─── [adsd_rcc_set_password] ───────────────────────────────────────────────────
/**
* Renders the set-password form. Accepts ?adsd_rcc_key=KEY&adsd_rcc_login=LOGIN in the URL.
* The link is sent to new users via the verification email so they can set their own password.
*/
public function set_password_form( array $atts = [] ): string {
// These GET params come from the password-reset link emailed by process_verification().
// The cryptographically random token IS the security credential — no nonce needed.
// phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$key = sanitize_text_field( rawurldecode( wp_unslash( $_GET['adsd_rcc_key'] ?? '' ) ) );
$login = sanitize_text_field( rawurldecode( wp_unslash( $_GET['adsd_rcc_login'] ?? '' ) ) );
// phpcs:enable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// If no key/login, show an invalid-link message
if ( ! $key || ! $login ) {
return ''
. esc_html__( 'This link is invalid or has expired. Please register again.', 'adsd-referral-couponcore' )
. '
';
}
// Validate the key right now so we show an error early if it's bad
$user = get_user_by( 'login', $login );
if ( ! $user ) {
return ''
. esc_html__( 'This link is invalid or has expired.', 'adsd-referral-couponcore' )
. '
';
}
$check = check_password_reset_key( $key, $login );
if ( is_wp_error( $check ) ) {
return ''
. esc_html__( 'This password-setup link has expired or already been used. Please contact support.', 'adsd-referral-couponcore' )
. '
';
}
ob_start();
?>
tag is ever printed.
*/
wp_enqueue_script( 'adsd-rcc-set-password' );
wp_localize_script(
'adsd-rcc-set-password',
'ADSD_RCC_PwData',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'adsd_rcc_set_password_nonce' ),
'key' => $key,
'login' => $login,
'redirectUrl' => ADSD_RCC_Settings::instance()->page_url( 'login' ),
'i18n' => array(
'weak' => __( 'Weak', 'adsd-referral-couponcore' ),
'moderate' => __( 'Moderate', 'adsd-referral-couponcore' ),
'strong' => __( 'Strong', 'adsd-referral-couponcore' ),
'min_length' => __( 'Password must be at least 8 characters.', 'adsd-referral-couponcore' ),
'mismatch' => __( 'Passwords do not match.', 'adsd-referral-couponcore' ),
'saving' => __( 'Saving\u2026', 'adsd-referral-couponcore' ),
'success' => __( 'Password set! Redirecting\u2026', 'adsd-referral-couponcore' ),
'error' => __( 'Something went wrong.', 'adsd-referral-couponcore' ),
'network_error' => __( 'Network error. Please try again.', 'adsd-referral-couponcore' ),
),
)
);
return ob_get_clean();
}
/**
* AJAX: validate the reset key and save the new password for the user.
* Accessible by both logged-in and logged-out users (new user, not yet logged in).
*/
public function ajax_save_new_password(): void {
// Nonce check
if ( ! wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ),
'adsd_rcc_set_password_nonce'
) ) {
wp_send_json_error( [ 'message' => __( 'Security check failed.', 'adsd-referral-couponcore' ) ] );
return;
}
$key = sanitize_text_field( wp_unslash( $_POST['adsd_rcc_key'] ?? '' ) );
$login = sanitize_text_field( wp_unslash( $_POST['adsd_rcc_login'] ?? '' ) );
$password = sanitize_text_field( wp_unslash( $_POST['adsd_rcc_password'] ?? '' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- password validated by length check below, not sanitized to preserve special chars
if ( ! $key || ! $login || ! $password ) {
wp_send_json_error( [ 'message' => __( 'Missing required fields.', 'adsd-referral-couponcore' ) ] );
return;
}
if ( strlen( $password ) < 8 ) {
wp_send_json_error( [ 'message' => __( 'Password must be at least 8 characters.', 'adsd-referral-couponcore' ) ] );
return;
}
// Validate reset key using WordPress's own function
$user = check_password_reset_key( $key, $login );
if ( is_wp_error( $user ) ) {
wp_send_json_error( [ 'message' => __( 'This link has expired or already been used. Please contact support.', 'adsd-referral-couponcore' ) ] );
return;
}
// Save the new password and invalidate the reset key
reset_password( $user, $password );
ADSD_RCC_DB::instance()->log( 'password_set', $user->ID, null, 'User set their password via adsd_rcc_set_password form.' );
wp_send_json_success( [
'message' => __( 'Password saved! Redirecting you to login…', 'adsd-referral-couponcore' ),
] );
}
}