' . esc_html__( 'You must be logged in to request verification.', '6arshid-social-community' ) . '
'; } $loader = \Arshid6Social\Template_Loader::instance(); return $loader->get_template( 'verification/request.php', array( 'verification' => $this, 'user_id' => get_current_user_id(), 'types' => $this->get_types(), 'pending' => $this->get_pending_request( get_current_user_id() ), 'is_verified' => $this->is_verified( get_current_user_id() ), ), true ); } // ── AJAX ───────────────────────────────────────────────────────────────── private function nonce_check( bool $admin = false ): void { // Nonce must be present and valid — checked first and separately. if ( ! check_ajax_referer( 'arshid6social_ajax_nonce', 'nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', '6arshid-social-community' ) ), 403 ); } if ( ! is_user_logged_in() ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', '6arshid-social-community' ) ), 401 ); } if ( $admin && ! current_user_can( 'arshid6social_manage_members' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', '6arshid-social-community' ) ), 403 ); } } public function ajax_submit_request(): void { $this->nonce_check(); $user_id = get_current_user_id(); if ( ! arshid6social_check_rate_limit( 'arshid6social_rl_verify', $user_id, (int) get_option( 'arshid6social_verification_rate_limit', 3 ) ) ) { wp_send_json_error( array( 'message' => __( 'Too many verification requests. Please try later.', '6arshid-social-community' ) ), 429 ); } // Nonce already verified above via $this->nonce_check(). // phpcs:disable WordPress.Security.NonceVerification $type = sanitize_key( wp_unslash( $_POST['type'] ?? 'general' ) ); $name = sanitize_text_field( wp_unslash( $_POST['full_name'] ?? '' ) ); $cat = sanitize_text_field( wp_unslash( $_POST['category'] ?? '' ) ); $links = sanitize_textarea_field( wp_unslash( $_POST['links'] ?? '' ) ); $fields = array( 'full_name' => $name, 'category' => $cat, 'links' => $links, ); $doc_paths = array(); $require = get_option( 'arshid6social_verification_require_doc', false ); if ( ! empty( $_FILES['document']['tmp_name'] ) ) { $result = \Arshid6Social\Media_Handler::handle( $_FILES['document'], 'verification_doc', $user_id ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- $_FILES array validated/sanitized by Media_Handler::handle() (MIME, size, re-encode). if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } $doc_paths[] = $result['path']; \Arshid6Social\Media_Handler::register_to_media_library( $result['path'], $result['url'], $result['mime'], sanitize_file_name( (string) ( $_FILES['document']['name'] ?? '' ) ), $user_id ); } elseif ( $require ) { wp_send_json_error( array( 'message' => __( 'A document upload is required for verification.', '6arshid-social-community' ) ) ); } // phpcs:enable $id = $this->submit_request( $user_id, $type, $fields, $doc_paths ); if ( ! $id ) { wp_send_json_error( array( 'message' => __( 'You already have a pending verification request.', '6arshid-social-community' ) ) ); } $this->notify_admin_new_request( $user_id ); wp_send_json_success( array( 'message' => __( 'Your verification request has been submitted. We will review it shortly.', '6arshid-social-community' ) ) ); } public function ajax_get_status(): void { check_ajax_referer( 'arshid6social_ajax_nonce', 'nonce' ); $user_id = get_current_user_id(); $pending = $this->get_pending_request( $user_id ); wp_send_json_success( array( 'verified' => $this->is_verified( $user_id ), 'badge' => $this->get_badge_html( $user_id ), 'pending' => $pending ? array( 'id' => (int) $pending->id, 'status' => $pending->status, 'reason' => $pending->reason ?? '', ) : null, ) ); } public function ajax_admin_grant(): void { $this->nonce_check( true ); $user_id = absint( $_POST['user_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification $type = sanitize_key( wp_unslash( $_POST['type'] ?? 'general' ) ); // phpcs:ignore WordPress.Security.NonceVerification $ok = $this->grant( $user_id, $type ); $ok ? wp_send_json_success() : wp_send_json_error(); } public function ajax_admin_revoke(): void { $this->nonce_check( true ); $user_id = absint( $_POST['user_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification $ok = $this->revoke( $user_id ); $ok ? wp_send_json_success() : wp_send_json_error(); } public function ajax_admin_approve(): void { $this->nonce_check( true ); $req_id = absint( $_POST['request_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification $type = sanitize_key( wp_unslash( $_POST['type'] ?? 'general' ) ); // phpcs:ignore WordPress.Security.NonceVerification $ok = $this->approve_request( $req_id, $type ); $ok ? wp_send_json_success() : wp_send_json_error(); } public function ajax_admin_reject(): void { $this->nonce_check( true ); $req_id = absint( $_POST['request_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification $reason = sanitize_textarea_field( wp_unslash( $_POST['reason'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification $ok = $this->reject_request( $req_id, $reason ); $ok ? wp_send_json_success() : wp_send_json_error(); } public function ajax_admin_more_info(): void { $this->nonce_check( true ); $req_id = absint( $_POST['request_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification $message = sanitize_textarea_field( wp_unslash( $_POST['message'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification $ok = $this->request_more_info( $req_id, $message ); $ok ? wp_send_json_success() : wp_send_json_error(); } public function ajax_resubmit_request(): void { $this->nonce_check(); $user_id = get_current_user_id(); $pending = $this->get_pending_request( $user_id ); if ( ! $pending || 'more_info' !== $pending->status ) { wp_send_json_error( array( 'message' => __( 'No request awaiting additional information.', '6arshid-social-community' ) ) ); } // Nonce already verified above via $this->nonce_check(). // phpcs:disable WordPress.Security.NonceVerification $name = sanitize_text_field( wp_unslash( $_POST['full_name'] ?? '' ) ); $cat = sanitize_text_field( wp_unslash( $_POST['category'] ?? '' ) ); $links = sanitize_textarea_field( wp_unslash( $_POST['links'] ?? '' ) ); $fields = array( 'full_name' => $name, 'category' => $cat, 'links' => $links, ); $doc_paths = json_decode( $pending->document_paths ?? '[]', true ); if ( ! empty( $_FILES['document']['tmp_name'] ) ) { $result = \Arshid6Social\Media_Handler::handle( $_FILES['document'], 'verification_doc', $user_id ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- $_FILES array validated/sanitized by Media_Handler::handle() (MIME, size, re-encode). if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } $doc_paths[] = $result['path']; } // phpcs:enable global $wpdb; $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prefix . 'arshid6social_verification_requests', array( 'fields_json' => wp_json_encode( $fields ), 'document_paths' => wp_json_encode( $doc_paths ), 'status' => 'pending', 'reason' => null, 'created_at' => current_time( 'mysql' ), ), array( 'id' => (int) $pending->id ), array( '%s', '%s', '%s', '%s', '%s' ), array( '%d' ) ); $this->notify_admin_new_request( $user_id ); wp_send_json_success( array( 'message' => __( 'Your additional information has been submitted. We will review it shortly.', '6arshid-social-community' ) ) ); } public function serve_doc(): void { check_ajax_referer( 'arshid6social_ajax_nonce', 'nonce' ); if ( ! current_user_can( 'arshid6social_manage_members' ) ) { status_header( 403 ); exit; } // phpcs:ignore WordPress.Security.NonceVerification $req_id = absint( $_GET['request_id'] ?? 0 ); $idx = absint( $_GET['idx'] ?? 0 ); global $wpdb; $request = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT document_paths FROM {$wpdb->prefix}arshid6social_verification_requests WHERE id = %d", $req_id ) ); if ( ! $request ) { status_header( 404 ); exit; } $paths = json_decode( $request->document_paths ?? '[]', true ); $path = $paths[ $idx ] ?? ''; $mime = mime_content_type( $path ) ?: 'application/octet-stream'; \Arshid6Social\Media_Handler::serve_protected_file( $path, $mime ); } // ── Notifications ───────────────────────────────────────────────────────── private function notify_user( int $user_id, string $action, int $secondary_id ): void { $notifications = ARSHID6SOCIAL()->component( 'notifications' ); if ( ! $notifications ) { return; } $notifications->add( array( 'user_id' => $user_id, 'item_id' => get_current_user_id() ?: $user_id, 'secondary_item_id' => $secondary_id, 'component_name' => 'verification', 'component_action' => $action, ) ); } // ── Helpers ─────────────────────────────────────────────────────────────── private function notify_admin_new_request( int $user_id ): void { $admin_email = get_option( 'admin_email' ); if ( ! $admin_email ) { return; } $user = get_userdata( $user_id ); $name = $user ? $user->display_name : "#{$user_id}"; $email = $user ? $user->user_email : ''; $subject = sprintf( /* translators: 1: site name, 2: user display name */ __( '[%1$s] New verification request from %2$s', '6arshid-social-community' ), get_bloginfo( 'name' ), $name ); $body = sprintf( /* translators: 1: name, 2: email, 3: admin URL */ __( "User %1\$s (%2\$s) has submitted a verification request.\n\nReview it in the admin panel:\n%3\$s", '6arshid-social-community' ), $name, $email, admin_url( 'admin.php?page=arshid6social-verification' ) ); wp_mail( $admin_email, $subject, $body ); } private function maybe_purge_docs( object $request ): void { if ( ! get_option( 'arshid6social_verification_doc_purge', true ) ) { return; } $paths = json_decode( $request->document_paths ?? '[]', true ); foreach ( (array) $paths as $path ) { \Arshid6Social\Media_Handler::delete_file( $path ); } } // ── REST ───────────────────────────────────────────────────────────────── public function register_rest_routes(): void { ( new Verification_REST( $this ) )->register_routes(); } }