add_action( 'wp_ajax_academiccrew_update_member', $this, 'handle_ajax' ); } public function render(): string { if ( ! is_user_logged_in() ) { return '

' . esc_html__( 'You must be logged in to edit your profile.', 'academiccrew' ) . '

'; } $user_id = get_current_user_id(); $member_id = $this->get_member_id_for_user( $user_id ); if ( ! $member_id ) { return '

' . esc_html__( 'No member profile found for your account.', 'academiccrew' ) . '

'; } wp_enqueue_style( 'academiccrew-edit-form', ACADEMICCREW_URL . 'assets/css/edit-form.css', [], ACADEMICCREW_VERSION ); wp_enqueue_script( 'academiccrew-edit-form', ACADEMICCREW_URL . 'assets/js/edit-form.js', [], ACADEMICCREW_VERSION, true ); wp_localize_script( 'academiccrew-edit-form', 'ACADEMICCREW_EDIT', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'academiccrew_member_edit' ), 'memberId' => $member_id, 'i18n' => [ 'saving' => __( 'Saving...', 'academiccrew' ), 'saved' => __( 'Profile updated successfully.', 'academiccrew' ), 'error' => __( 'An error occurred. Please try again.', 'academiccrew' ), ], ] ); $bio = get_post_meta( $member_id, '_acc_bio', true ); $avatar = get_post_meta( $member_id, '_acc_avatar_url', true ); $website = get_post_meta( $member_id, '_acc_personal_website', true ); $linkedin = get_post_meta( $member_id, '_acc_linkedin_url', true ); $github = get_post_meta( $member_id, '_acc_github_url', true ); $twitter = get_post_meta( $member_id, '_acc_twitter_url', true ); ob_start(); ?>
/400
user_owns_member( $user_id, $member_id ) ) { wp_send_json_error( [ 'message' => __( 'Permission denied.', 'academiccrew' ) ], 403 ); } foreach ( self::ALLOWED_FIELDS as $meta_key ) { if ( ! isset( $_POST[ $meta_key ] ) ) { continue; } $value = match ( true ) { str_ends_with( $meta_key, '_url' ) => esc_url_raw( wp_unslash( $_POST[ $meta_key ] ) ), $meta_key === '_acc_bio' => mb_substr( sanitize_textarea_field( wp_unslash( $_POST[ $meta_key ] ) ), 0, 400 ), default => sanitize_text_field( wp_unslash( $_POST[ $meta_key ] ) ), }; update_post_meta( $member_id, $meta_key, $value ); } wp_send_json_success( [ 'message' => __( 'Profile updated successfully.', 'academiccrew' ) ] ); } private function get_member_id_for_user( int $user_id ): int { $posts = get_posts( [ 'post_type' => 'academiccrew_member', 'post_status' => [ 'publish', 'draft', 'private' ], 'posts_per_page' => 1, 'fields' => 'ids', 'meta_query' => [ [ 'key' => '_academiccrew_wp_user_id', 'value' => $user_id, ] ], ] ); return (int) ( $posts[0] ?? 0 ); } private function user_owns_member( int $user_id, int $member_id ): bool { if ( current_user_can( 'manage_options' ) ) { return true; } $owner = (int) get_post_meta( $member_id, '_academiccrew_wp_user_id', true ); return $owner === $user_id; } }