79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
|
|
class WPI_API_Importer
|
|
{
|
|
|
|
private static function api_url($url)
|
|
{
|
|
if (empty($url)) {
|
|
return $url;
|
|
}
|
|
|
|
// Nếu đã là absolute URL (http / https)
|
|
$parsed = parse_url($url);
|
|
if (isset($parsed['scheme']) && in_array($parsed['scheme'], ['http', 'https'], true)) {
|
|
return $url;
|
|
}
|
|
|
|
$base_url = rtrim(WPI_Config_Model::get_config('base_api'), '/');
|
|
|
|
return $base_url . ltrim($url, '/');
|
|
}
|
|
|
|
|
|
public static function products(array $args = [])
|
|
{
|
|
$page = isset($args['page'])
|
|
? (int) $args['page']
|
|
: (int) WPI_Config_Model::get_config('current_page', 1);
|
|
|
|
$limit = isset($args['limit'])
|
|
? (int) $args['limit']
|
|
: (int) WPI_Config_Model::get_config('limit_per_time', 20);
|
|
|
|
$import_api = WPI_Config_Model::get_config('import_api');
|
|
$authen_key = WPI_Config_Model::get_config('authen_key');
|
|
|
|
if (!$import_api || !$authen_key) {
|
|
WPI_Logger::log('Missing API config');
|
|
return;
|
|
}
|
|
|
|
$url = add_query_arg([
|
|
'page' => $page,
|
|
'perPage' => $limit,
|
|
], $import_api);
|
|
|
|
WPI_Logger::log("Calling API: {$url}");
|
|
|
|
return wp_remote_get(self::api_url($url), [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $authen_key,
|
|
],
|
|
]);
|
|
}
|
|
|
|
|
|
public static function categories()
|
|
{
|
|
|
|
$url = WPI_Config_Model::get_config('categories_api');
|
|
$authen_key = WPI_Config_Model::get_config('authen_key');
|
|
|
|
if (!$url || !$authen_key) {
|
|
WPI_Logger::log('Missing API config');
|
|
return;
|
|
}
|
|
|
|
return wp_remote_get(self::api_url($url), [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $authen_key,
|
|
],
|
|
]);
|
|
}
|
|
}
|