<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function tilfy_oauth_yonlendirme_uri() {
	return add_query_arg(
		array( 'page' => 'tilfy-ayarlar' ),
		admin_url( 'admin.php' )
	);
}

function tilfy_oauth_tiktok_yonlendirme_uri() {
	return untrailingslashit( rest_url( 'tilfy/v1/oauth/tiktok' ) );
}

function tilfy_tiktok_oauth_durum_olustur() {
	$state = bin2hex( random_bytes( 16 ) );
	set_transient(
		'tilfy_tt_oauth_' . $state,
		array(
			'uid' => get_current_user_id(),
			't'   => time(),
		),
		15 * MINUTE_IN_SECONDS
	);
	return $state;
}

function tilfy_tiktok_oauth_durum_dogrula( $state ) {
	$state = sanitize_text_field( (string) $state );
	if ( $state === '' || ! preg_match( '/^[a-f0-9]{32}$/', $state ) ) {
		return false;
	}
	$data = get_transient( 'tilfy_tt_oauth_' . $state );
	if ( ! is_array( $data ) || empty( $data['uid'] ) ) {
		return false;
	}
	delete_transient( 'tilfy_tt_oauth_' . $state );
	return true;
}

add_action( 'rest_api_init', 'tilfy_oauth_tiktok_rest_kayit' );

function tilfy_oauth_tiktok_rest_kayit() {
	register_rest_route(
		'tilfy/v1',
		'/oauth/tiktok',
		array(
			'methods'             => 'GET',
			'callback'            => 'tilfy_oauth_tiktok_callback',
			'permission_callback' => '__return_true',
		)
	);
}

function tilfy_oauth_tiktok_callback( $istek ) {
	$hata  = sanitize_text_field( (string) $istek->get_param( 'error' ) );
	$kod   = sanitize_text_field( (string) $istek->get_param( 'code' ) );
	$durum = sanitize_text_field( (string) $istek->get_param( 'state' ) );

	if ( $hata !== '' ) {
		$aciklama = sanitize_text_field( (string) $istek->get_param( 'error_description' ) );
		if ( $aciklama === '' ) {
			$aciklama = $hata;
		}
		set_transient( 'tilfy_oauth_hata', $aciklama, 120 );
		tilfy_admin_yonlendir_guvenli(
			array(
				'page'        => 'tilfy-ayarlar',
				'sekme'       => 'tiktok',
				'tilfy_mesaj' => 'oauth_hata',
			)
		);
	}

	if ( $kod === '' && $durum === '' ) {
		return new WP_Error( 'rest_no_route', 'Not found', array( 'status' => 404 ) );
	}

	if ( $kod === '' || $durum === '' || ! tilfy_tiktok_oauth_durum_dogrula( $durum ) ) {
		tilfy_admin_yonlendir_guvenli(
			array(
				'page'        => 'tilfy-ayarlar',
				'sekme'       => 'tiktok',
				'tilfy_mesaj' => 'oauth_hata',
			)
		);
	}

	$sonuc = tilfy_tiktok_oauth_takas( $kod );
	if ( is_wp_error( $sonuc ) ) {
		set_transient( 'tilfy_oauth_hata', $sonuc->get_error_message(), 120 );
	}
	tilfy_admin_yonlendir_guvenli(
		array(
			'page'        => 'tilfy-ayarlar',
			'sekme'       => 'tiktok',
			'tilfy_mesaj' => is_wp_error( $sonuc ) ? 'oauth_hata' : 'tiktok_baglandi',
		)
	);
}

function tilfy_tiktok_oauth_kapsam() {
	$kapsam = 'user.info.basic,video.upload';
	if ( tilfy_ayar_al( 'tiktok_direct_post' ) === '1' ) {
		$kapsam .= ',video.publish';
	}
	return $kapsam;
}

function tilfy_oauth_dis_yonlendir( $url ) {
	if ( ! is_string( $url ) || $url === '' ) {
		tilfy_admin_yonlendir_guvenli( array( 'page' => 'tilfy-ayarlar', 'tilfy_mesaj' => 'oauth_hata' ) );
	}
	if ( strpos( $url, admin_url() ) === 0 ) {
		wp_safe_redirect( esc_url_raw( $url ) );
	} elseif ( preg_match( '#^https://#i', $url ) ) {
		wp_redirect( $url );
	} else {
		tilfy_admin_yonlendir_guvenli( array( 'page' => 'tilfy-ayarlar', 'tilfy_mesaj' => 'oauth_hata' ) );
	}
	exit;
}

function tilfy_oauth_baglan_url( $platform, $sekme = '' ) {
	$platform = sanitize_key( (string) $platform );
	$sekme    = sanitize_key( (string) $sekme );
	if ( $sekme === '' ) {
		$sekme = ( $platform === 'threads' ) ? 'meta' : $platform;
	}
	$url = add_query_arg(
		array(
			'page'         => 'tilfy-ayarlar',
			'sekme'        => $sekme,
			'tilfy_baglan' => $platform,
		),
		admin_url( 'admin.php' )
	);
	return wp_nonce_url( $url, 'tilfy_oauth_baglan' );
}

function tilfy_oauth_state_platform_bul( $durum ) {
	$durum = sanitize_text_field( (string) $durum );
	if ( $durum === '' ) {
		return '';
	}
	foreach ( array( 'youtube', 'meta', 'linkedin', 'threads' ) as $platform ) {
		if ( wp_verify_nonce( $durum, 'tilfy_oauth_' . $platform ) ) {
			return $platform;
		}
	}
	return '';
}

function tilfy_oauth_platform_sekme( $platform ) {
	$platform = sanitize_key( (string) $platform );
	if ( $platform === 'meta' || $platform === 'threads' ) {
		return 'meta';
	}
	if ( $platform === 'youtube' || $platform === 'linkedin' ) {
		return $platform;
	}
	return '';
}

add_action( 'admin_init', 'tilfy_oauth_callback_isle' );

function tilfy_oauth_callback_isle() {
	if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
		return;
	}
	if ( empty( $_GET['page'] ) || sanitize_key( wp_unslash( $_GET['page'] ) ) !== 'tilfy-ayarlar' ) {
		return;
	}

	if ( ! empty( $_GET['tilfy_baglan'] ) ) {
		check_admin_referer( 'tilfy_oauth_baglan' );
		$hedef = sanitize_key( wp_unslash( $_GET['tilfy_baglan'] ) );
		if ( $hedef === 'youtube' ) {
			tilfy_oauth_dis_yonlendir( tilfy_youtube_oauth_url() );
		}
		if ( $hedef === 'meta' ) {
			tilfy_oauth_dis_yonlendir( tilfy_meta_oauth_url() );
		}
		if ( $hedef === 'linkedin' ) {
			tilfy_oauth_dis_yonlendir( tilfy_linkedin_oauth_url() );
		}
		if ( $hedef === 'threads' ) {
			tilfy_oauth_dis_yonlendir( tilfy_threads_oauth_url() );
		}
		if ( $hedef === 'tiktok' ) {
			tilfy_oauth_dis_yonlendir( tilfy_tiktok_oauth_url() );
		}
	}

	if ( ! empty( $_GET['error'] ) && empty( $_GET['tilfy_baglan'] ) ) {
		$platform = tilfy_oauth_state_platform_bul( wp_unslash( $_GET['state'] ?? '' ) );
		$aciklama = sanitize_text_field( wp_unslash( $_GET['error_description'] ?? $_GET['error'] ) );
		if ( $aciklama !== '' ) {
			set_transient( 'tilfy_oauth_hata', $aciklama, 120 );
		}
		$yon_args = array(
			'page'        => 'tilfy-ayarlar',
			'tilfy_mesaj' => 'oauth_hata',
		);
		$sekme = tilfy_oauth_platform_sekme( $platform );
		if ( $sekme !== '' ) {
			$yon_args['sekme'] = $sekme;
		}
		tilfy_admin_yonlendir_guvenli( $yon_args );
	}

	if ( empty( $_GET['code'] ) || empty( $_GET['state'] ) ) {
		return;
	}

	$platform = tilfy_oauth_state_platform_bul( wp_unslash( $_GET['state'] ) );
	if ( $platform === '' ) {
		set_transient( 'tilfy_oauth_hata', 'OAuth durum dogrulanamadi. Sayfayi yenileyip tekrar baglanin.', 120 );
		tilfy_admin_yonlendir_guvenli( array( 'page' => 'tilfy-ayarlar', 'tilfy_mesaj' => 'oauth_hata' ) );
	}

	$kod = sanitize_text_field( wp_unslash( $_GET['code'] ) );
	if ( $platform === 'youtube' ) {
		$sonuc = tilfy_youtube_oauth_takas( $kod );
	} elseif ( $platform === 'meta' ) {
		$sonuc = tilfy_meta_oauth_takas( $kod );
	} elseif ( $platform === 'threads' ) {
		$sonuc = tilfy_threads_oauth_takas( $kod );
	} else {
		$sonuc = tilfy_linkedin_oauth_takas( $kod );
	}
	if ( is_wp_error( $sonuc ) ) {
		set_transient( 'tilfy_oauth_hata', $sonuc->get_error_message(), 120 );
	}
	$yon_args = array(
		'page'        => 'tilfy-ayarlar',
		'tilfy_mesaj' => is_wp_error( $sonuc ) ? 'oauth_hata' : $platform . '_baglandi',
	);
	$sekme = tilfy_oauth_platform_sekme( $platform );
	if ( $sekme !== '' ) {
		$yon_args['sekme'] = $sekme;
	}
	tilfy_admin_yonlendir_guvenli( $yon_args );
}

function tilfy_linkedin_oauth_kapsam() {
	return 'openid profile email w_member_social';
}

function tilfy_linkedin_userinfo_al( $jeton ) {
	$yanit = wp_remote_get(
		'https://api.linkedin.com/v2/userinfo',
		array(
			'timeout' => 20,
			'headers' => array(
				'Authorization' => 'Bearer ' . $jeton,
			),
		)
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$kod = (int) wp_remote_retrieve_response_code( $yanit );
	if ( $kod < 200 || $kod >= 300 ) {
		return new WP_Error( 'tilfy_linkedin', 'LinkedIn userinfo HTTP ' . $kod );
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( ! is_array( $govde ) || empty( $govde['sub'] ) ) {
		return new WP_Error( 'tilfy_linkedin', 'LinkedIn uye ID alinamadi' );
	}
	return $govde;
}

function tilfy_meta_oauth_url() {
	$uygulama = tilfy_ayar_al( 'meta_uygulama_id' );
	if ( $uygulama === '' ) {
		return admin_url( 'admin.php?page=tilfy-ayarlar&tilfy_mesaj=meta_eksik' );
	}
	$durum = wp_create_nonce( 'tilfy_oauth_meta' );
	$args  = array(
		'client_id'       => $uygulama,
		'redirect_uri'    => tilfy_oauth_yonlendirme_uri(),
		'state'           => $durum,
		'response_type'   => 'code',
		'auth_type'       => 'rerequest',
		'return_scopes'   => 'true',
	);
	$konfig = tilfy_ayar_al( 'meta_konfig_id' );
	if ( $konfig !== '' ) {
		$args['config_id'] = $konfig;
	} else {
		$args['scope'] = 'instagram_basic,instagram_content_publish,pages_show_list,pages_read_engagement,pages_manage_posts,pages_manage_metadata,business_management';
	}
	return add_query_arg( $args, tilfy_meta_oauth_dialog_url() );
}

function tilfy_meta_oauth_takas( $kod ) {
	$uygulama = tilfy_ayar_al( 'meta_uygulama_id' );
	$gizli    = tilfy_ayar_al( 'meta_uygulama_gizli' );
	if ( $uygulama === '' || $gizli === '' ) {
		return new WP_Error( 'tilfy_oauth', 'Meta uygulama bilgisi eksik' );
	}
	$yanit = wp_remote_get(
		add_query_arg(
			array(
				'client_id'     => $uygulama,
				'client_secret' => $gizli,
				'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
				'code'          => $kod,
			),
			tilfy_meta_graph_url( 'oauth/access_token' )
		),
		array( 'timeout' => 30 )
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( empty( $govde['access_token'] ) ) {
		return new WP_Error( 'tilfy_oauth', 'Meta jeton alinamadi' );
	}
	tilfy_ayar_kaydet( array( 'meta_erisim_jetonu' => $govde['access_token'] ) );
	$uzun = wp_remote_get(
		add_query_arg(
			array(
				'grant_type'        => 'fb_exchange_token',
				'client_id'         => $uygulama,
				'client_secret'     => $gizli,
				'fb_exchange_token' => $govde['access_token'],
			),
			tilfy_meta_graph_url( 'oauth/access_token' )
		),
		array( 'timeout' => 30 )
	);
	if ( ! is_wp_error( $uzun ) ) {
		$uzun_govde = json_decode( wp_remote_retrieve_body( $uzun ), true );
		if ( ! empty( $uzun_govde['access_token'] ) ) {
			tilfy_ayar_kaydet( array( 'meta_erisim_jetonu' => $uzun_govde['access_token'] ) );
		}
	}
	if ( is_readable( TILFY_YOL . 'includes/api/meta.php' ) ) {
		require_once TILFY_YOL . 'includes/api/meta.php';
		tilfy_meta_ilk_sayfayi_kaydet();
		if ( function_exists( 'tilfy_meta_sayfa_jetonu_al' ) ) {
			tilfy_meta_sayfa_jetonu_al( '', true );
		}
	}
	return true;
}

function tilfy_youtube_oauth_url() {
	$istemci = tilfy_ayar_al( 'youtube_istemci_id' );
	if ( $istemci === '' ) {
		return admin_url( 'admin.php?page=tilfy-ayarlar&tilfy_mesaj=youtube_eksik' );
	}
	$durum = wp_create_nonce( 'tilfy_oauth_youtube' );
	return add_query_arg(
		array(
			'client_id'     => $istemci,
			'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
			'response_type' => 'code',
			'scope'         => 'https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl',
			'access_type'   => 'offline',
			'prompt'        => 'consent',
			'state'         => $durum,
		),
		'https://accounts.google.com/o/oauth2/v2/auth'
	);
}

function tilfy_youtube_oauth_takas( $kod ) {
	$istemci = tilfy_ayar_al( 'youtube_istemci_id' );
	$gizli   = tilfy_ayar_al( 'youtube_istemci_gizli' );
	if ( $istemci === '' || $gizli === '' ) {
		return new WP_Error( 'tilfy_oauth', 'YouTube istemci eksik' );
	}
	$yanit = wp_remote_post(
		'https://oauth2.googleapis.com/token',
		array(
			'timeout' => 30,
			'body'    => array(
				'code'          => $kod,
				'client_id'     => $istemci,
				'client_secret' => $gizli,
				'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
				'grant_type'    => 'authorization_code',
			),
		)
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( empty( $govde['access_token'] ) ) {
		return new WP_Error( 'tilfy_oauth', 'YouTube jeton alinamadi' );
	}
	$kayit = array(
		'youtube_erisim_jetonu' => $govde['access_token'],
		'youtube_erisim_bitis'  => (string) tilfy_jeton_bitis_unix( $govde['expires_in'] ?? 3600, 3600 ),
		'youtube_scope_eksik'   => '',
	);
	if ( ! empty( $govde['refresh_token'] ) ) {
		$kayit['youtube_yenileme_jetonu'] = $govde['refresh_token'];
	}
	tilfy_ayar_kaydet( $kayit );
	return true;
}

function tilfy_linkedin_oauth_url() {
	$istemci = tilfy_ayar_al( 'linkedin_istemci_id' );
	$gizli   = tilfy_ayar_al( 'linkedin_istemci_gizli' );
	if ( $istemci === '' || $gizli === '' ) {
		return admin_url( 'admin.php?page=tilfy-ayarlar&sekme=linkedin' );
	}
	$durum = wp_create_nonce( 'tilfy_oauth_linkedin' );
	return add_query_arg(
		array(
			'response_type' => 'code',
			'client_id'     => $istemci,
			'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
			'state'         => $durum,
			'scope'         => tilfy_linkedin_oauth_kapsam(),
		),
		'https://www.linkedin.com/oauth/v2/authorization'
	);
}

function tilfy_linkedin_oauth_takas( $kod ) {
	$istemci = tilfy_ayar_al( 'linkedin_istemci_id' );
	$gizli   = tilfy_ayar_al( 'linkedin_istemci_gizli' );
	if ( $istemci === '' || $gizli === '' ) {
		return new WP_Error( 'tilfy_oauth', 'LinkedIn istemci bilgisi eksik' );
	}
	$yanit = wp_remote_post(
		'https://www.linkedin.com/oauth/v2/accessToken',
		array(
			'timeout' => 30,
			'headers' => array(
				'Content-Type' => 'application/x-www-form-urlencoded',
			),
			'body'    => array(
				'grant_type'    => 'authorization_code',
				'code'          => $kod,
				'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
				'client_id'     => $istemci,
				'client_secret' => $gizli,
			),
		)
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( empty( $govde['access_token'] ) ) {
		$mesaj = 'LinkedIn jeton alinamadi';
		if ( is_array( $govde ) ) {
			if ( ! empty( $govde['error_description'] ) ) {
				$mesaj = (string) $govde['error_description'];
			} elseif ( ! empty( $govde['error'] ) ) {
				$mesaj = (string) $govde['error'];
			}
		}
		return new WP_Error( 'tilfy_oauth', $mesaj );
	}
	$kayit = array( 'linkedin_erisim_jetonu' => $govde['access_token'] );
	$profil = tilfy_linkedin_userinfo_al( $govde['access_token'] );
	if ( ! is_wp_error( $profil ) ) {
		$kayit['linkedin_uye_id'] = sanitize_text_field( (string) $profil['sub'] );
		if ( ! empty( $profil['name'] ) ) {
			$kayit['linkedin_profil_adi'] = sanitize_text_field( (string) $profil['name'] );
		}
	}
	tilfy_ayar_kaydet( $kayit );
	return true;
}

function tilfy_threads_oauth_url() {
	$uygulama = tilfy_ayar_al( 'threads_uygulama_id' );
	if ( $uygulama === '' || tilfy_ayar_al( 'threads_uygulama_gizli' ) === '' ) {
		return admin_url( 'admin.php?page=tilfy-ayarlar&sekme=meta&tilfy_mesaj=threads_eksik' );
	}
	$durum = wp_create_nonce( 'tilfy_oauth_threads' );
	return add_query_arg(
		array(
			'client_id'     => $uygulama,
			'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
			'state'         => $durum,
			'scope'         => 'threads_basic,threads_content_publish',
			'response_type' => 'code',
		),
		'https://threads.net/oauth/authorize'
	);
}

function tilfy_threads_oauth_takas( $kod ) {
	$uygulama = tilfy_ayar_al( 'threads_uygulama_id' );
	$gizli    = tilfy_ayar_al( 'threads_uygulama_gizli' );
	if ( $uygulama === '' || $gizli === '' ) {
		return new WP_Error( 'tilfy_oauth', 'Threads uygulama bilgisi eksik' );
	}
	$yanit = wp_remote_post(
		'https://graph.threads.net/oauth/access_token',
		array(
			'timeout' => 30,
			'body'    => array(
				'client_id'     => $uygulama,
				'client_secret' => $gizli,
				'grant_type'    => 'authorization_code',
				'redirect_uri'  => tilfy_oauth_yonlendirme_uri(),
				'code'          => $kod,
			),
		)
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( empty( $govde['access_token'] ) ) {
		$mesaj = is_array( $govde ) && isset( $govde['error_message'] ) ? (string) $govde['error_message'] : 'Threads jeton alinamadi';
		return new WP_Error( 'tilfy_oauth', $mesaj );
	}
	$kullanici_id = (string) ( $govde['user_id'] ?? '' );
	$jeton        = (string) $govde['access_token'];
	$uzun         = wp_remote_get(
		add_query_arg(
			array(
				'grant_type'    => 'th_exchange_token',
				'client_secret' => $gizli,
				'access_token'  => $jeton,
			),
			'https://graph.threads.net/access_token'
		),
		array( 'timeout' => 30 )
	);
	if ( ! is_wp_error( $uzun ) ) {
		$uzun_govde = json_decode( wp_remote_retrieve_body( $uzun ), true );
		if ( ! empty( $uzun_govde['access_token'] ) ) {
			$jeton = (string) $uzun_govde['access_token'];
		}
	}
	$kayit = array( 'threads_erisim_jetonu' => $jeton );
	if ( $kullanici_id !== '' ) {
		$kayit['threads_kullanici_id'] = $kullanici_id;
	}
	tilfy_ayar_kaydet( $kayit );
	return true;
}

function tilfy_tiktok_oauth_url() {
	$anahtar = trim( (string) tilfy_ayar_al( 'tiktok_istemci_anahtar' ) );
	$gizli   = trim( (string) tilfy_ayar_al( 'tiktok_istemci_gizli' ) );
	if ( $anahtar === '' || $gizli === '' ) {
		return add_query_arg(
			array(
				'page'        => 'tilfy-ayarlar',
				'sekme'       => 'tiktok',
				'tilfy_mesaj' => 'tiktok_eksik',
			),
			admin_url( 'admin.php' )
		);
	}
	$durum    = tilfy_tiktok_oauth_durum_olustur();
	$redirect = tilfy_oauth_tiktok_yonlendirme_uri();
	$args     = array(
		'client_key'    => $anahtar,
		'scope'         => tilfy_tiktok_oauth_kapsam(),
		'response_type' => 'code',
		'redirect_uri'  => $redirect,
		'state'         => $durum,
	);
	return add_query_arg( $args, 'https://www.tiktok.com/v2/auth/authorize/' );
}

function tilfy_tiktok_oauth_takas( $kod ) {
	$anahtar = tilfy_ayar_al( 'tiktok_istemci_anahtar' );
	$gizli   = tilfy_ayar_al( 'tiktok_istemci_gizli' );
	if ( $anahtar === '' || $gizli === '' ) {
		return new WP_Error( 'tilfy_oauth', 'TikTok istemci bilgisi eksik' );
	}
	$redirect = tilfy_oauth_tiktok_yonlendirme_uri();
	$yanit    = wp_remote_post(
		'https://open.tiktokapis.com/v2/oauth/token/',
		array(
			'timeout' => 30,
			'headers' => array(
				'Content-Type'  => 'application/x-www-form-urlencoded',
				'Cache-Control' => 'no-cache',
			),
			'body'    => array(
				'client_key'    => $anahtar,
				'client_secret' => $gizli,
				'code'          => $kod,
				'grant_type'    => 'authorization_code',
				'redirect_uri'  => $redirect,
			),
		)
	);
	if ( is_wp_error( $yanit ) ) {
		return $yanit;
	}
	$govde = json_decode( wp_remote_retrieve_body( $yanit ), true );
	if ( empty( $govde['access_token'] ) ) {
		$mesaj = is_array( $govde ) ? ( $govde['error_description'] ?? $govde['error'] ?? 'TikTok jeton alinamadi' ) : 'TikTok jeton alinamadi';
		return new WP_Error( 'tilfy_oauth', (string) $mesaj );
	}
	$kayit = array(
		'tiktok_erisim_jetonu'   => sanitize_text_field( (string) $govde['access_token'] ),
		'tiktok_erisim_bitis'    => (string) tilfy_jeton_bitis_unix( $govde['expires_in'] ?? 86400, 86400 ),
		'tiktok_yenileme_jetonu' => sanitize_text_field( (string) ( $govde['refresh_token'] ?? '' ) ),
		'tiktok_open_id'         => sanitize_text_field( (string) ( $govde['open_id'] ?? '' ) ),
	);
	tilfy_ayar_kaydet( $kayit );
	return true;
}
