API-Fixes

master
Seph 2024-11-02 20:27:01 +01:00
parent b1f5904b91
commit b9d1865186
6 changed files with 146 additions and 112 deletions

View File

@ -57,7 +57,7 @@
--color-deepblue: #000032; --color-deepblue: #000032;
--color-alliance: #f7941e; --color-alliance: #f7941e;
--color-background: var(--color-deepblue); --color-background: var(--color-deepblue);
--color-glass: color-mix(in lch, transparent 75%, var(--color-background)); --color-glass: color-mix(in lch, transparent 42%, var(--color-background));
} }
.p-auto { .p-auto {

View File

@ -10,7 +10,7 @@ use WoWPress\Models\User;
global $widget_area; global $widget_area;
set_sidebar_status('top', false); set_sidebar_status('top', false);
$characters = Character::orderBy('rank')->get(); $characters = Character::orderBy('name')->get();
get_header(); get_header();
?> ?>
@ -28,7 +28,6 @@ get_header();
<tr> <tr>
<td class="p-auto">Name</td> <td class="p-auto">Name</td>
<td class="p-auto hidden lg:table-cell">Klasse</td> <td class="p-auto hidden lg:table-cell">Klasse</td>
<td class="p-auto hidden lg:table-cell">Rang</td>
<td class="p-auto hidden lg:table-cell">Server</td> <td class="p-auto hidden lg:table-cell">Server</td>
<td class="p-auto hidden lg:table-cell">Gilde</td> <td class="p-auto hidden lg:table-cell">Gilde</td>
@ -59,7 +58,6 @@ get_header();
<?= translate_string($char->spec) ?></span> <?= translate_string($char->spec) ?></span>
</div> </div>
</td> </td>
<td class="p-auto hidden lg:table-cell"><?= $char->rank ?></td>
<td class="p-auto hidden lg:table-cell"><?= $char->realm ?></td> <td class="p-auto hidden lg:table-cell"><?= $char->realm ?></td>
<td class="p-auto hidden lg:table-cell"><?= $char->guild ?></td> <td class="p-auto hidden lg:table-cell"><?= $char->guild ?></td>

View File

@ -37,6 +37,7 @@ switch ($_POST['action']) {
$char = new Character(); $char = new Character();
$char->name = $_POST['name']; $char->name = $_POST['name'];
$char->realm = $_POST['realm']; $char->realm = $_POST['realm'];
$char->save();
$char->updateFromAPI(); $char->updateFromAPI();
$char->updateMedia(); $char->updateMedia();
} }

View File

@ -103,8 +103,12 @@ abstract class Api
$headers = array('Content-Type: application/json', 'Content-Length: ' . strlen($data_json)); $headers = array('Content-Type: application/json', 'Content-Length: ' . strlen($data_json));
$headers[] = 'Accept: application/json'; $headers[] = 'Accept: application/json';
if ($headerauth) { if ($headerauth) {
if (!is_bool($headerauth)) {
$headers[] = 'Authorization: Bearer ' . $headerauth;
} else {
$headers[] = 'Authorization: ' . $this->api_key; $headers[] = 'Authorization: ' . $this->api_key;
} }
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@ -146,7 +150,6 @@ abstract class Api
$cache = Cache::where('p_key', $cache_key)->get()->last(); $cache = Cache::where('p_key', $cache_key)->get()->last();
if (empty($cache->ID) || $cache->isExpired($expiration)) { if (empty($cache->ID) || $cache->isExpired($expiration)) {
$ch = curl_init(); $ch = curl_init();
if (!empty($parameter)) { if (!empty($parameter)) {
@ -163,8 +166,12 @@ abstract class Api
$headers = array(); $headers = array();
$headers[] = 'Accept: application/json'; $headers[] = 'Accept: application/json';
if ($headerauth) { if ($headerauth) {
if (!is_bool($headerauth)) {
$headers[] = 'Authorization: Bearer ' . $headerauth;
} else {
$headers[] = 'Authorization: ' . $this->api_key; $headers[] = 'Authorization: ' . $this->api_key;
} }
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch); $response = curl_exec($ch);
@ -180,6 +187,8 @@ abstract class Api
$from_cache = false; $from_cache = false;
} }
if (!is_array($cache->value)) { if (!is_array($cache->value)) {
return ['error' => true]; return ['error' => true];
} }

View File

@ -15,8 +15,8 @@ class BattleNet extends Api{
$data = $this->get("/profile/wow/character/$realm/$name", [ $data = $this->get("/profile/wow/character/$realm/$name", [
'namespace' => 'profile-eu', 'namespace' => 'profile-eu',
'locale' => 'en_US', 'locale' => 'en_US',
'access_token' => $this->bearer() # 'access_token' => $this->bearer()
], false, $timeout); ], $this->bearer(), $timeout);
return $data; return $data;
} }
@ -35,13 +35,13 @@ class BattleNet extends Api{
$data = $this->get($guild, [ $data = $this->get($guild, [
'namespace' => 'profile-eu', 'namespace' => 'profile-eu',
'locale' => 'en_US', 'locale' => 'en_US',
'access_token' => $this->bearer() #'access_token' => $this->bearer()
], false, -1); ], $this->bearer(), -1);
$data = $this->get($data['roster']['href'], [ $data = $this->get($data['roster']['href'], [
'namespace' => 'profile-eu', 'namespace' => 'profile-eu',
'locale' => 'en_US', 'locale' => 'en_US',
'access_token' => $this->bearer() #'access_token' => $this->bearer()
], false, -1); ], $this->bearer(), -1);
foreach($data['members'] as $member){ foreach($data['members'] as $member){
if($member['character']['name'] == $name && $member['character']['realm']['slug'] == $char['realm']['slug']){ if($member['character']['name'] == $name && $member['character']['realm']['slug'] == $char['realm']['slug']){
$guild = $char['guild']['name']; $guild = $char['guild']['name'];
@ -62,8 +62,8 @@ class BattleNet extends Api{
$data = $this->get("/profile/wow/character/$realm/$name/character-media", [ $data = $this->get("/profile/wow/character/$realm/$name/character-media", [
'namespace' => 'profile-eu', 'namespace' => 'profile-eu',
'locale' => 'en_US', 'locale' => 'en_US',
'access_token' => $this->bearer() # 'access_token' => $this->bearer()
], false, $timeout); ], $this->bearer(), $timeout);
if(!empty($data['code']) && $data['code'] == 403){ if(!empty($data['code']) && $data['code'] == 403){
# var_dump($data); # var_dump($data);

View File

@ -29,15 +29,18 @@ class Character extends Model
$this->api = new BattleNet($id, $key); $this->api = new BattleNet($id, $key);
} }
public function lists(){ public function lists()
{
return $this->belongsToMany(CharacterList::class, 'wowpress_charlists_item', 'list_id', 'character_id')->withPivot('comment', 'date', 'id'); return $this->belongsToMany(CharacterList::class, 'wowpress_charlists_item', 'list_id', 'character_id')->withPivot('comment', 'date', 'id');
} }
public function user(){ public function user()
{
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function getCanEditAttribute(){ public function getCanEditAttribute()
{
if (empty($this->user->ID)) { if (empty($this->user->ID)) {
return current_user_can('wowpress_edit_raids'); return current_user_can('wowpress_edit_raids');
} }
@ -54,29 +57,41 @@ class Character extends Model
return $string; return $string;
} }
public function getRankAttribute($rank){ public function getRankAttribute($rank)
{
switch ($rank) { switch ($rank) {
case 0: return "Gildenmeister"; case 0:
return "Gildenmeister";
break; break;
case 1: return "Offizier"; case 1:
return "Offizier";
break; break;
case 2: return "Offi-Twink"; case 2:
return "Offi-Twink";
break; break;
case 3: return "Raidlead"; case 3:
return "Raidlead";
break; break;
case 4: return "Ehrenmitglied"; case 4:
return "Ehrenmitglied";
break; break;
case 5: return "Raider"; case 5:
return "Raider";
break; break;
case 6: return "Raider-Twink"; case 6:
return "Raider-Twink";
break; break;
case 7: return "F&F"; case 7:
return "F&F";
break; break;
case 8: return "Novize"; case 8:
return "Novize";
break; break;
case 9: return "Sleeper"; case 9:
return "Sleeper";
break; break;
default: return "Gast"; default:
return "Gast";
} }
} }
@ -91,7 +106,8 @@ class Character extends Model
return get_template_directory_uri() . "/icons/class/$classname/$classname.png"; return get_template_directory_uri() . "/icons/class/$classname/$classname.png";
} }
public static function classIconLink($class){ public static function classIconLink($class)
{
$classname = (new self())->sanitize($class); $classname = (new self())->sanitize($class);
return get_template_directory_uri() . "/icons/class/$classname/$classname.png"; return get_template_directory_uri() . "/icons/class/$classname/$classname.png";
} }
@ -103,7 +119,8 @@ class Character extends Model
return get_template_directory_uri() . "/icons/class/$classname/$specname.png"; return get_template_directory_uri() . "/icons/class/$classname/$specname.png";
} }
public static function specIconLink($class,$spec){ public static function specIconLink($class, $spec)
{
$classname = (new self())->sanitize($class); $classname = (new self())->sanitize($class);
$specname = (new self())->sanitize($spec); $specname = (new self())->sanitize($spec);
return get_template_directory_uri() . "/icons/class/$classname/$specname.png"; return get_template_directory_uri() . "/icons/class/$classname/$specname.png";
@ -130,7 +147,8 @@ class Character extends Model
return $char_from_api; return $char_from_api;
} }
public function updateMedia($force = false){ public function updateMedia($force = false)
{
if (empty($this->id_blizz)) { if (empty($this->id_blizz)) {
return false; return false;
} }
@ -142,11 +160,19 @@ class Character extends Model
$id = $this->id_blizz; $id = $this->id_blizz;
$mod = $id % 256; $mod = $id % 256;
$realm = $this->sanitize($this->realm, '-'); $realm = $this->sanitize($this->realm, '-');
$img_url = "https://render.worldofwarcraft.com/eu/character/$realm/$mod/$id-avatar.jpg"; $data = $this->api->getMedia($this->name, $this->realm, 0);
file_put_contents($character_dir."avatar.jpg",file_get_contents($img_url)); if (key_exists("assets", $data)) {
foreach ($data['assets'] as $asset) {
if ($asset['key'] == 'avatar') {
file_put_contents($character_dir . "avatar.jpg", file_get_contents($asset['value']));
return true;
}
}
}
} }
public function getAvatarAttribute(){ public function getAvatarAttribute()
{
$character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/avatar.jpg"; $character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/avatar.jpg";
$character_dir_uri = wp_upload_dir()['baseurl'] . "/characters/" . $this->ID . "/avatar.jpg"; $character_dir_uri = wp_upload_dir()['baseurl'] . "/characters/" . $this->ID . "/avatar.jpg";
if (file_exists($character_dir)) { if (file_exists($character_dir)) {
@ -154,11 +180,11 @@ class Character extends Model
} else { } else {
return ""; return "";
} }
} }
public function getClassButtonAttribute(){ public function getClassButtonAttribute()
{
require(get_template_directory() . "/template-parts/components/class-button.php"); require(get_template_directory() . "/template-parts/components/class-button.php");
} }
} }