get( 'mail_from_email', '' );
if ( $from_email && is_email( $from_email ) ) {
return; // already configured
}
$is_adsd_rcc_page = str_contains( $screen->id, 'smart-referral' ) || str_contains( $screen->id, 'srr' );
$is_dashboard = ( $screen->id === 'dashboard' );
if ( ! $is_adsd_rcc_page && ! $is_dashboard ) {
return;
}
$settings_url = admin_url( 'admin.php?page=adsd-referral-couponcore' );
echo '
';
}
// ─── Menu ─────────────────────────────────────────────────────────────────
public function register_menu(): void {
add_menu_page(
__( 'AD-SD Referral CouponCore EcoSystem', 'adsd-referral-couponcore' ),
__( 'Referral Rewards', 'adsd-referral-couponcore' ),
'manage_options',
'adsd-referral-couponcore',
[ $this, 'page_settings' ],
'dashicons-share',
58
);
add_submenu_page( 'adsd-referral-couponcore', __( 'Settings', 'adsd-referral-couponcore' ), __( 'Settings', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-referral-couponcore', [ $this, 'page_settings' ] );
add_submenu_page( 'adsd-referral-couponcore', __( 'Email Templates', 'adsd-referral-couponcore' ), __( 'Email Templates', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-rcc-email-templates', [ $this, 'page_email_templates' ] );
add_submenu_page( 'adsd-referral-couponcore', __( 'Reward Codes', 'adsd-referral-couponcore' ), __( 'Reward Codes', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-rcc-codes', [ $this, 'page_codes' ] );
add_submenu_page( 'adsd-referral-couponcore', __( 'Users', 'adsd-referral-couponcore' ), __( 'Users', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-rcc-users', [ $this, 'page_users' ] );
add_submenu_page( 'adsd-referral-couponcore', __( 'Logs', 'adsd-referral-couponcore' ), __( 'Logs', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-rcc-logs', [ $this, 'page_logs' ] );
add_submenu_page( 'adsd-referral-couponcore', __( 'Passkey', 'adsd-referral-couponcore' ), __( 'Passkey', 'adsd-referral-couponcore' ), 'manage_options', 'adsd-rcc-passkey', [ $this, 'page_passkey' ] );
}
// ─── Settings Page ────────────────────────────────────────────────────────
public function page_settings(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$s = ADSD_RCC_Settings::instance();
$woo = ADSD_RCC_Plugin::is_woocommerce_active();
?>
admin_header( __( 'Settings', 'adsd-referral-couponcore' ) ); ?>
show_notices(); ?>
get_all_email_templates();
$tpl_keys = [ 'verification', 'reward_generated', 'reward_used', 'reward_expired' ];
$current = ADSD_RCC_DB::instance()->get_email_template( $active_tpl );
?>
admin_header( __( 'Email Templates', 'adsd-referral-couponcore' ) ); ?>
show_notices(); ?>
prefix . ADSD_RCC_TABLE_PREFIX . 'rewards';
$t = esc_sql( $table );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only pagination, no state change
$page = max( 1, (int) sanitize_text_field( wp_unslash( $_GET['paged'] ?? '1' ) ) );
$limit = 25;
$offset = ( $page - 1 ) * $limit;
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.Security.NonceVerification.Recommended
$cache_key_total = 'adsd_rcc_codes_total';
$total = wp_cache_get( $cache_key_total, 'adsd_rcc' );
if ( false === $total ) {
$total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$t}`" );
wp_cache_set( $cache_key_total, $total, 'adsd_rcc', MINUTE_IN_SECONDS );
}
$total = (int) $total;
$cache_key_rows = 'adsd_rcc_codes_p' . $page;
$rows = wp_cache_get( $cache_key_rows, 'adsd_rcc' );
if ( false === $rows ) {
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM `{$t}` ORDER BY created_at DESC LIMIT %d OFFSET %d",
$limit,
$offset
)
);
wp_cache_set( $cache_key_rows, $rows, 'adsd_rcc', MINUTE_IN_SECONDS );
}
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.Security.NonceVerification.Recommended
?>
admin_header( __( 'Reward Codes', 'adsd-referral-couponcore' ) ); ?>
|
|
|
|
|
|
|
|
user_id );
?>
| id; ?> |
user_email ) : esc_html__( 'Unknown', 'adsd-referral-couponcore' ); ?> |
reward_code ); ?> |
|
status ) ); ?> |
expires_at ? esc_html( date_i18n( get_option( 'date_format' ), strtotime( $r->expires_at ) ) ) : esc_html( '—' ); ?> |
used_by ? esc_html( ucfirst( str_replace( '_', ' ', $r->used_by ) ) ) : esc_html( '—' ); ?> |
woo_coupon_code ? '' . esc_html( $r->woo_coupon_code ) . '' : esc_html( '—' ); ?> |
pagination( $total, $limit, $page, 'adsd-rcc-codes' ); ?>
prefix . ADSD_RCC_TABLE_PREFIX . 'referrals';
$rwd_table = $wpdb->prefix . ADSD_RCC_TABLE_PREFIX . 'rewards';
$ref_t = esc_sql( $ref_table );
$rwd_t = esc_sql( $rwd_table );
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter
$rows = wp_cache_get( 'adsd_rcc_users_summary', 'adsd_rcc' );
if ( false === $rows ) {
$rows = $wpdb->get_results(
"SELECT referrer_id, COUNT(*) AS total_referrals, SUM(status = 'rewarded') AS successful FROM `{$ref_t}` GROUP BY referrer_id ORDER BY successful DESC LIMIT 50"
);
wp_cache_set( 'adsd_rcc_users_summary', $rows, 'adsd_rcc', 5 * MINUTE_IN_SECONDS );
}
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter
?>
admin_header( __( 'Users', 'adsd-referral-couponcore' ) ); ?>
|
|
|
|
referrer_id );
$rwd_cache = wp_cache_get( 'adsd_rcc_rwd_count_' . (int) $row->referrer_id, 'adsd_rcc' );
if ( false === $rwd_cache ) {
$rwd_cache = (int) $wpdb->get_var(
$wpdb->prepare( "SELECT COUNT(*) FROM `{$rwd_t}` WHERE user_id = %d", (int) $row->referrer_id )
);
wp_cache_set( 'adsd_rcc_rwd_count_' . (int) $row->referrer_id, $rwd_cache, 'adsd_rcc', 5 * MINUTE_IN_SECONDS );
}
$rewards = (int) $rwd_cache;
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter
?>
| user_email ) : esc_html( '#' . $row->referrer_id ); ?> |
total_referrals; ?> |
successful; ?> |
|
count_logs();
$logs = $db->get_logs( [ 'limit' => $limit, 'offset' => ( $page - 1 ) * $limit ] );
?>
admin_header( __( 'Logs', 'adsd-referral-couponcore' ) ); ?>
|
|
|
|
|
user_id ? get_user_by( 'id', $log->user_id ) : null;
?>
| created_at ) ) ); ?> |
event_type ); ?> |
user_email ) : ( $log->user_id ? '#' . (int) $log->user_id : '—' ); ?> |
message ); ?> |
ip_address ); ?> |
pagination( $total, $limit, $page, 'adsd-rcc-logs' ); ?>
get_active_passkey();
?>
admin_header( __( 'Shop Passkey', 'adsd-referral-couponcore' ) ); ?>
show_notices(); ?>
save_batch( $data );
ADSD_RCC_Settings::instance()->bust_cache();
$this->redirect_with_notice( 'adsd-referral-couponcore', 'Settings saved.' );
}
public function save_email_template(): void {
if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adsd_rcc_nonce'] ?? '' ) ), 'adsd_rcc_save_email_template' ) ) {
wp_die( esc_html__( 'Security check failed.', 'adsd-referral-couponcore' ) );
}
$key = sanitize_key( $_POST['template_key'] ?? '' );
ADSD_RCC_DB::instance()->save_email_template( $key, [
'subject' => sanitize_text_field( wp_unslash( $_POST['subject'] ?? '' ) ),
'heading' => sanitize_text_field( wp_unslash( $_POST['heading'] ?? '' ) ),
'body' => wp_kses_post( wp_unslash( $_POST['body'] ?? '' ) ),
'button_text' => sanitize_text_field( wp_unslash( $_POST['button_text'] ?? '' ) ),
'footer' => sanitize_text_field( wp_unslash( $_POST['footer'] ?? '' ) ),
] );
$this->redirect_with_notice( 'adsd-rcc-email-templates&tpl=' . $key, 'Template saved.' );
}
public function save_passkey(): void {
if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['adsd_rcc_nonce'] ?? '' ) ), 'adsd_rcc_save_passkey' ) ) {
wp_die( esc_html__( 'Security check failed.', 'adsd-referral-couponcore' ) );
}
$admin_pass = sanitize_text_field( wp_unslash( $_POST['admin_password'] ?? '' ) );
$user = wp_get_current_user();
if ( ! wp_check_password( $admin_pass, $user->user_pass, $user->ID ) ) {
$this->redirect_with_notice( 'adsd-rcc-passkey', 'Admin password is incorrect.', 'error' );
return;
}
$new_key = sanitize_text_field( wp_unslash( $_POST['new_passkey'] ?? '' ) );
if ( ! $new_key ) {
$new_key = ADSD_RCC_Crypto::token( 16 );
}
ADSD_RCC_DB::instance()->insert_passkey( wp_hash_password( $new_key ), $user->ID );
// Show the key once in the redirect
set_transient( 'adsd_rcc_new_passkey_' . $user->ID, $new_key, 60 );
$this->redirect_with_notice( 'adsd-rcc-passkey', 'Passkey generated. Copy it now — it will not be shown again.' );
}
// ─── Helpers ─────────────────────────────────────────────────────────────
private function admin_header( string $title ): void {
echo ' ' . esc_html__( 'AD-SD Referral CouponCore EcoSystem', 'adsd-referral-couponcore' ) . ' — ' . esc_html( $title ) . '
';
}
private function show_notices(): void {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only admin notice from redirect, no state change
if ( ! empty( $_GET['adsd_rcc_saved'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
echo '' . esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['adsd_rcc_saved'] ) ) ) ) . '
';
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['adsd_rcc_error'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
echo '' . esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['adsd_rcc_error'] ) ) ) ) . '
';
}
$user_id = get_current_user_id();
$key = 'adsd_rcc_new_passkey_' . $user_id;
$passkey = get_transient( $key );
if ( $passkey ) {
delete_transient( $key );
echo '' . esc_html__( 'Your new passkey (copy it now — shown only once):', 'adsd-referral-couponcore' ) . ' ' . esc_html( $passkey ) . '
';
}
}
private function redirect_with_notice( string $page, string $msg, string $type = 'saved' ): void {
$key = $type === 'error' ? 'adsd_rcc_error' : 'adsd_rcc_saved';
wp_safe_redirect( admin_url( 'admin.php?page=' . $page . '&' . $key . '=' . urlencode( $msg ) ) );
exit;
}
private function pagination( int $total, int $limit, int $page, string $slug ): void {
$pages = ceil( $total / $limit );
if ( $pages <= 1 ) {
return;
}
echo '';
echo wp_kses_post( paginate_links( [ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_kses_post() is an escaping function
'base' => add_query_arg( [ 'page' => $slug, 'paged' => '%#%' ], admin_url( 'admin.php' ) ),
'format' => '',
'current' => $page,
'total' => $pages,
] ) );
echo '
';
}
public function ajax_expire_reward(): void {
if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'adsd_rcc_admin_nonce' ) ) {
wp_send_json_error();
}
$count = ADSD_RCC_DB::instance()->expire_overdue_rewards();
wp_send_json_success( [ 'expired' => $count ] );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
}