ID ) { return; } if ( ! get_option( 'arshid6social_monetization_enabled' ) ) { return; } $this->render_payout_account_card( $profile_user ); $this->render_earnings_card( $profile_user->ID ); } // ------------------------------------------------------------------------- // Payout account card — IBAN form // ------------------------------------------------------------------------- private function render_payout_account_card( \WP_User $user ): void { $encrypted_iban = (string) get_user_meta( $user->ID, self::IBAN_META, true ); $has_iban = '' !== $encrypted_iban; $address = array( 'line1' => (string) get_user_meta( $user->ID, 'arshid6social_monetization_address_line1', true ), 'city' => (string) get_user_meta( $user->ID, 'arshid6social_monetization_address_city', true ), 'postal' => (string) get_user_meta( $user->ID, 'arshid6social_monetization_address_postal', true ), 'country' => (string) get_user_meta( $user->ID, 'arshid6social_monetization_address_country', true ), ); $iban_nonce = wp_create_nonce( 'arshid6social_monetization_save_iban_' . $user->ID ); $address_nonce = wp_create_nonce( 'arshid6social_monetization_save_address_' . $user->ID ); ?>

✓


prefix . 'arshid6social_monetization_transactions'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table}'" ) !== $table ) { return; } // Total income: completed PPV + subscription receipts where user is creator. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- interpolated identifiers are $wpdb->prefix table names / whitelisted clauses; values bound via prepare() placeholders. $total_income = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount - platform_fee ), 0 ) FROM {$table} WHERE creator_id = %d AND type IN ('ppv','subscription') AND status = 'completed'", $user_id ) ); // Total paid out: completed payout transactions. $total_paid_out = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount ), 0 ) FROM {$table} WHERE creator_id = %d AND type = 'payout' AND status = 'completed'", $user_id ) ); // Pending cashout requests (payout in pending state). $pending_cashout = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount ), 0 ) FROM {$table} WHERE creator_id = %d AND type = 'payout' AND status = 'pending'", $user_id ) ); $available = max( 0.0, $total_income - $total_paid_out - $pending_cashout ); $currency = strtoupper( (string) get_option( 'arshid6social_monetization_currency', 'USD' ) ); $has_iban = '' !== (string) get_user_meta( $user_id, self::IBAN_META, true ); $can_cashout = $available > 0 && $has_iban; $cashout_nonce = wp_create_nonce( 'arshid6social_monetization_request_cashout_' . $user_id ); // All transactions visible to this user (as creator or payer). $rows = (array) $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT type, amount, platform_fee, currency, status, created_at FROM {$table} WHERE creator_id = %d OR payer_id = %d ORDER BY created_at DESC LIMIT 50", $user_id, $user_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter ?>
0 && ! $has_iban ) : ?>

0 ) : ?>

type_label( $row->type ); $net = 'payout' === $row->type ? -( (float) $row->amount ) : (float) $row->amount - (float) $row->platform_fee; $status_css = $this->status_class( $row->status ); ?>
created_at ) ) ); ?> amount, 2 ) . ' ' . strtoupper( $row->currency ) ); ?> platform_fee, 2 ) ); ?> type ? '−' : '' ) . number_format( abs( $net ), 2 ) ); ?> status ) ); ?>
prefix . 'arshid6social_monetization_transactions'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$transactions_table}'" ) === $transactions_table; // phpcs:disable WordPress.Security.NonceVerification.Recommended $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $paged = max( 1, isset( $_GET['paged'] ) ? (int) $_GET['paged'] : 1 ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $per_page = (int) get_user_option( 'arshid6social_payouts_per_page' ); if ( $per_page < 1 ) { $per_page = 20; } $offset = ( $paged - 1 ) * $per_page; // Build query with optional search join. $meta_key = esc_sql( self::IBAN_META ); $where_sql = "um.meta_key = '{$meta_key}' AND um.meta_value != ''"; $join_sql = "LEFT JOIN {$wpdb->users} u ON u.ID = um.user_id"; $vals = array(); if ( '' !== $search ) { $like = '%' . $wpdb->esc_like( $search ) . '%'; $where_sql .= ' AND ( u.display_name LIKE %s OR u.user_email LIKE %s OR u.user_login LIKE %s )'; array_push( $vals, $like, $like, $like ); } // Count. // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter $count_sql = "SELECT COUNT(*) FROM {$wpdb->usermeta} um {$join_sql} WHERE {$where_sql}"; $total = (int) ( empty( $vals ) ? $wpdb->get_var( $count_sql ) // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared : $wpdb->get_var( $wpdb->prepare( $count_sql, ...$vals ) ) ); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared // Fetch page. // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter $rows_sql = "SELECT um.user_id, um.meta_value AS iban_enc FROM {$wpdb->usermeta} um {$join_sql} WHERE {$where_sql} ORDER BY um.user_id ASC LIMIT %d OFFSET %d"; $fetch_vals = array_merge( $vals, array( $per_page, $offset ) ); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared $iban_users = $wpdb->get_results( $wpdb->prepare( $rows_sql, ...$fetch_vals ) ); $total_pages = (int) ceil( $total / $per_page ); $admin_nonce = wp_create_nonce( 'arshid6social_monetization_admin_payout' ); $currency = strtoupper( (string) get_option( 'arshid6social_monetization_currency', 'USD' ) ); $base_url = add_query_arg( array_filter( array( 'page' => 'arshid6social-monetization', 'tab' => 'payouts', 's' => $search ?: null, ) ), admin_url( 'admin.php' ) ); ?>

'padding:4px 12px;' ) ); ?>

user_id; $user = get_userdata( $uid ); if ( ! $user ) { continue; } $iban_plain = Monetization_Crypto::decrypt( (string) $row->iban_enc ); $line1 = (string) get_user_meta( $uid, 'arshid6social_monetization_address_line1', true ); $city = (string) get_user_meta( $uid, 'arshid6social_monetization_address_city', true ); $postal = (string) get_user_meta( $uid, 'arshid6social_monetization_address_postal', true ); $country = (string) get_user_meta( $uid, 'arshid6social_monetization_address_country', true ); $address_parts = array_filter( array( $line1, $city, $postal, $country ) ); $available = 0.0; $pending_cashout = 0.0; $pending_tx_id = 0; if ( $table_exists ) { // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- interpolated identifiers are $wpdb->prefix table names / whitelisted clauses; values bound via prepare() placeholders. $income = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount - platform_fee ), 0 ) FROM {$transactions_table} WHERE creator_id = %d AND type IN ('ppv','subscription') AND status = 'completed'", $uid ) ); $paid_out = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount ), 0 ) FROM {$transactions_table} WHERE creator_id = %d AND type = 'payout' AND status = 'completed'", $uid ) ); $pending_row = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT id, amount FROM {$transactions_table} WHERE creator_id = %d AND type = 'payout' AND status = 'pending' ORDER BY created_at DESC LIMIT 1", $uid ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter $pending_cashout = $pending_row ? (float) $pending_row->amount : 0.0; $pending_tx_id = $pending_row ? (int) $pending_row->id : 0; $available = max( 0.0, $income - $paid_out - $pending_cashout ); } ?>
display_name ); ?>
user_email ); ?>
0 ? number_format( $pending_cashout, 2 ) . ' ' . $currency : '—' ); ?> 0 ) : ?>
1 ) : ?>
add_query_arg( 'paged', '%#%', $base_url ), 'format' => '', 'current' => $paged, 'total' => $total_pages, 'prev_text' => '«', 'next_text' => '»', ) ) ); ?>
__( 'Not logged in.', '6arshid-social-community' ) ), 401 ); } $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'arshid6social_monetization_save_iban_' . $user_id ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed.', '6arshid-social-community' ) ), 403 ); } $raw_iban = isset( $_POST['iban'] ) ? strtoupper( preg_replace( '/\s+/', '', sanitize_text_field( wp_unslash( $_POST['iban'] ) ) ) ) : ''; if ( '' === $raw_iban ) { wp_send_json_error( array( 'message' => __( 'IBAN cannot be empty.', '6arshid-social-community' ) ) ); } // Basic IBAN format check: 2 letters + 2 digits + up to 30 alphanumeric chars. if ( ! preg_match( '/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/', $raw_iban ) ) { wp_send_json_error( array( 'message' => __( 'Invalid IBAN format. Please check and try again.', '6arshid-social-community' ) ) ); } $encrypted = Monetization_Crypto::encrypt( $raw_iban ); if ( '' === $encrypted ) { wp_send_json_error( array( 'message' => __( 'Could not save IBAN securely. Please contact support.', '6arshid-social-community' ) ) ); } update_user_meta( $user_id, self::IBAN_META, $encrypted ); wp_send_json_success( array( 'message' => __( 'IBAN saved.', '6arshid-social-community' ) ) ); } public function ajax_save_address(): void { $user_id = get_current_user_id(); if ( ! $user_id ) { wp_send_json_error( array( 'message' => __( 'Not logged in.', '6arshid-social-community' ) ), 401 ); } $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'arshid6social_monetization_save_address_' . $user_id ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed.', '6arshid-social-community' ) ), 403 ); } $fields = array( 'arshid6social_monetization_address_line1' => 'line1', 'arshid6social_monetization_address_city' => 'city', 'arshid6social_monetization_address_postal' => 'postal', 'arshid6social_monetization_address_country' => 'country', ); foreach ( $fields as $meta_key => $post_key ) { $value = isset( $_POST[ $post_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) : ''; update_user_meta( $user_id, $meta_key, $value ); } wp_send_json_success( array( 'message' => __( 'Address saved.', '6arshid-social-community' ) ) ); } /** Creator requests a cashout of their available balance. */ public function ajax_request_cashout(): void { $user_id = get_current_user_id(); if ( ! $user_id ) { wp_send_json_error( array( 'message' => __( 'Not logged in.', '6arshid-social-community' ) ), 401 ); } $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'arshid6social_monetization_request_cashout_' . $user_id ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed.', '6arshid-social-community' ) ), 403 ); } $has_iban = '' !== (string) get_user_meta( $user_id, self::IBAN_META, true ); if ( ! $has_iban ) { wp_send_json_error( array( 'message' => __( 'Please save your IBAN before requesting a cashout.', '6arshid-social-community' ) ) ); } global $wpdb; $table = $wpdb->prefix . 'arshid6social_monetization_transactions'; // Check there's no pending cashout already. // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL string built from $wpdb->prefix/whitelist identifiers and static clauses; all values bound via $wpdb->prepare() placeholders (never raw user input). $pending = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- table/identifier from $wpdb->prefix or in-code whitelist (never user input); dynamic clauses use bound %d/%s placeholders. "SELECT COALESCE( SUM( amount ), 0 ) FROM {$table} WHERE creator_id = %d AND type = 'payout' AND status = 'pending'", $user_id ) ); if ( $pending > 0 ) { wp_send_json_error( array( 'message' => __( 'You already have a pending cashout request.', '6arshid-social-community' ) ) ); } // Recalculate available balance server-side. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- interpolated identifiers are $wpdb->prefix table names / whitelisted clauses; values bound via prepare() placeholders. $total_income = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount - platform_fee ), 0 ) FROM {$table} WHERE creator_id = %d AND type IN ('ppv','subscription') AND status = 'completed'", $user_id ) ); $paid_out = (float) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT COALESCE( SUM( amount ), 0 ) FROM {$table} WHERE creator_id = %d AND type = 'payout' AND status = 'completed'", $user_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter $available = $total_income - $paid_out; if ( $available <= 0 ) { wp_send_json_error( array( 'message' => __( 'No balance available for cashout.', '6arshid-social-community' ) ) ); } $currency = strtoupper( (string) get_option( 'arshid6social_monetization_currency', 'USD' ) ); $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $table, array( 'type' => 'payout', 'payer_id' => 0, 'creator_id' => $user_id, 'amount' => $available, 'platform_fee' => 0.00, 'currency' => $currency, 'gateway' => 'manual', 'gateway_ref' => 'cashout_request_' . $user_id . '_' . time(), 'status' => 'pending', 'created_at' => current_time( 'mysql' ), ), array( '%s', '%d', '%d', '%f', '%f', '%s', '%s', '%s', '%s', '%s' ) ); wp_send_json_success( array( 'message' => sprintf( /* translators: %1$s = amount, %2$s = currency */ __( 'Cashout of %1$s %2$s requested! The admin will transfer it to your IBAN.', '6arshid-social-community' ), number_format( $available, 2 ), $currency ), ) ); } /** Admin-only: returns decrypted IBAN for a given user (used via AJAX if needed). */ public function ajax_admin_get_iban(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } check_ajax_referer( 'arshid6social_monetization_admin_payout' ); $uid = absint( $_POST['user_id'] ?? 0 ); $enc = (string) get_user_meta( $uid, self::IBAN_META, true ); $plain = Monetization_Crypto::decrypt( $enc ); wp_send_json_success( array( 'iban' => $plain ) ); } /** Admin-only: marks a specific pending payout transaction as completed (payout sent). */ public function ajax_admin_mark_paid(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } check_ajax_referer( 'arshid6social_monetization_admin_payout' ); global $wpdb; $uid = absint( $_POST['user_id'] ?? 0 ); $tx_id = absint( $_POST['tx_id'] ?? 0 ); if ( ! $uid ) { wp_send_json_error( array( 'message' => 'Invalid user.' ) ); } $table = $wpdb->prefix . 'arshid6social_monetization_transactions'; if ( $tx_id ) { // Mark specific payout transaction as completed. $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $table, array( 'status' => 'completed' ), array( 'id' => $tx_id, 'creator_id' => $uid, 'type' => 'payout', 'status' => 'pending', ), array( '%s' ), array( '%d', '%d', '%s', '%s' ) ); } else { // Fallback: mark all pending payouts for this creator. $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $table, array( 'status' => 'completed' ), array( 'creator_id' => $uid, 'type' => 'payout', 'status' => 'pending', ), array( '%s' ), array( '%d', '%s', '%s' ) ); } wp_send_json_success( array( 'message' => 'Marked as paid.' ) ); } // ------------------------------------------------------------------------- // Helpers // ------------------------------------------------------------------------- private function type_label( string $type ): string { $map = array( 'subscription' => __( 'Subscription', '6arshid-social-community' ), 'ppv' => __( 'Pay-per-view', '6arshid-social-community' ), 'refund' => __( 'Refund', '6arshid-social-community' ), 'payout' => __( 'Payout', '6arshid-social-community' ), ); return $map[ $type ] ?? ucfirst( $type ); } private function status_class( string $status ): string { $map = array( 'completed' => 'color:#16a34a;font-weight:600;', 'pending' => 'color:#d97706;', 'failed' => 'color:#dc2626;', 'refunded' => 'color:#6b7280;', ); return $map[ $status ] ?? ''; } }