<?php
/**
 * Author Kit Profile Update AJAX Handler.
 *
 * Provides functionality to update author profiles via AJAX,
 * including sanitization and user meta updates.
 *
 * @package Author_Kit
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Handles updating author profile via AJAX for the Author Kit plugin.
 *
 * Registers the AJAX callback and processes profile update requests,
 * sanitizing input and updating user meta accordingly.
 */
class Author_Kit_Update_Author_Profile {


	/**
	 * Constructor.
	 *
	 * Registers AJAX handler for updating author profile.
	 */
	public function __construct() {
		// Hook the function to handle AJAX requests.
		add_action( 'wp_ajax_author_kit_update_author_profile', array( $this, 'author_kit_update_author_profile' ) );
	}

	/**
	 * Handles AJAX request to update author profile.
	 *
	 * Validates nonce, checks login, sanitizes inputs, updates user data and metadata.
	 * Sends a JSON success or error response.
	 *
	 * @return void Outputs JSON response indicating success or failure.
	 */
	public function author_kit_update_author_profile() {

		check_ajax_referer( 'author-kit-update-author-profile-nonce', 'nonce' );

		// Ensure the user is logged in.
		if ( ! is_user_logged_in() ) {
			wp_send_json_error( array( 'message' => __( 'Login Required to Update Profile', 'author-kit' ) ) );
		}

		// Sanitize and retrieve input values.
		$user_id         = isset( $_POST['ID'] ) ? intval( sanitize_text_field( wp_unslash( $_POST['ID'] ) ) ) : 0;
		$display_name    = isset( $_POST['display_name'] ) ? sanitize_text_field( wp_unslash( $_POST['display_name'] ) ) : '';
		$first_name      = isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '';
		$last_name       = isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '';
		$job_title       = isset( $_POST['job_title'] ) ? sanitize_text_field( wp_unslash( $_POST['job_title'] ) ) : '';
		$contact_email   = isset( $_POST['contact_email'] ) ? sanitize_text_field( wp_unslash( $_POST['contact_email'] ) ) : '';
		$company         = isset( $_POST['company'] ) ? sanitize_text_field( wp_unslash( $_POST['company'] ) ) : '';
		$link_preference = isset( $_POST['link_preference'] ) ? sanitize_text_field( wp_unslash( $_POST['link_preference'] ) ) : 'author_archive';
		$user_url        = isset( $_POST['website'] ) ? esc_url_raw( wp_unslash( $_POST['website'] ) ) : '';
		$description     = isset( $_POST['description'] ) ? wp_kses_post( wp_unslash( $_POST['description'] ) ) : '';
		$custom_url      = isset( $_POST['custom_url'] ) ? esc_url_raw( wp_unslash( $_POST['custom_url'] ) ) : '';
		$avatar          = isset( $_POST['avatar'] ) ? esc_url_raw( wp_unslash( $_POST['avatar'] ) ) : '';
		$social_links    = isset( $_POST['social_links'] ) ? (array) json_decode( sanitize_text_field( wp_unslash( $_POST['social_links'] ), true ) ) : array();

		// Sanitize and update user data.
		$update_data  = array(
			'ID'           => $user_id,
			'display_name' => $display_name,
			'first_name'   => $first_name,
			'last_name'    => $last_name,
			'user_url'     => $user_url,
			'description'  => $description,
		);
		$updated_user = wp_update_user( $update_data );

		if ( ! empty( $avatar ) ) {
			update_user_meta( $user_id, 'author_kit_custom_avatar_url', $avatar );
		} elseif ( metadata_exists( 'user', $user_id, 'author_kit_custom_avatar_url' ) ) {
			delete_user_meta( $user_id, 'author_kit_custom_avatar_url' );
		}

		update_user_meta( $user_id, 'author_kit_user_contact_email', $contact_email );
		update_user_meta( $user_id, 'author_kit_user_company', $company );
		update_user_meta( $user_id, 'author_kit_author_job_title', $job_title );
		update_user_meta( $user_id, 'author_kit_name_link_preference', $link_preference );

		// Update custom URL if provided.
		if ( ! empty( $custom_url ) ) {
			update_user_meta( $user_id, 'author_kit_custom_url', $custom_url );
		}

		// Update social links (only save non-empty values).
		if ( ! empty( $social_links ) && is_array( $social_links ) ) {
			$sanitized_social_links = array();
			foreach ( $social_links as $platform => $link ) {
				if ( ! empty( $link ) ) {
					$sanitized_social_links[ $platform ] = esc_url_raw( $link );
				}
			}
			update_user_meta( $user_id, 'author_kit_user_social_profiles', $sanitized_social_links );
		} elseif ( metadata_exists( 'user', $user_id, 'author_kit_user_social_profiles' ) ) {
				// code...
				delete_user_meta( $user_id, 'author_kit_user_social_profiles' );
		}

		if ( is_wp_error( $updated_user ) ) {
			wp_send_json_error( array( 'message' => $updated_user->get_error_message() ) );
		} else {
			// Return success response.
			wp_send_json_success( array( 'message' => __( 'Profile Updated Successfully.', 'author-kit' ) ) );
		}
	}
}


// Initialize the class.
new Author_Kit_Update_Author_Profile();
