From b9d1865186ebc457f4d354c0c41f81f4cfceeeb0 Mon Sep 17 00:00:00 2001 From: Seph Date: Sat, 2 Nov 2024 20:27:01 +0100 Subject: [PATCH] API-Fixes --- tailwind/tailwind.css | 2 +- theme/pages/roster.php | 24 +++-- theme/request.php | 1 + theme/wowpress/Api/Api.php | 73 +++++++------- theme/wowpress/Api/BattleNet.php | 16 ++-- theme/wowpress/Models/Character.php | 142 ++++++++++++++++------------ 6 files changed, 146 insertions(+), 112 deletions(-) diff --git a/tailwind/tailwind.css b/tailwind/tailwind.css index 0520ca5..566f17f 100644 --- a/tailwind/tailwind.css +++ b/tailwind/tailwind.css @@ -57,7 +57,7 @@ --color-deepblue: #000032; --color-alliance: #f7941e; --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 { diff --git a/theme/pages/roster.php b/theme/pages/roster.php index 0b61b59..44e53d1 100644 --- a/theme/pages/roster.php +++ b/theme/pages/roster.php @@ -10,7 +10,7 @@ use WoWPress\Models\User; global $widget_area; set_sidebar_status('top', false); -$characters = Character::orderBy('rank')->get(); +$characters = Character::orderBy('name')->get(); get_header(); ?> @@ -28,7 +28,6 @@ get_header(); Name Klasse - Rang Server Gilde @@ -59,22 +58,21 @@ get_header(); spec) ?> - rank ?> realm ?> guild ?> -
- - - - raidchar) : ?> - - - - -
+
+ + + + raidchar) : ?> + + + + +
diff --git a/theme/request.php b/theme/request.php index 1259ce0..18fe179 100644 --- a/theme/request.php +++ b/theme/request.php @@ -37,6 +37,7 @@ switch ($_POST['action']) { $char = new Character(); $char->name = $_POST['name']; $char->realm = $_POST['realm']; + $char->save(); $char->updateFromAPI(); $char->updateMedia(); } diff --git a/theme/wowpress/Api/Api.php b/theme/wowpress/Api/Api.php index 6e592c2..1841f1d 100644 --- a/theme/wowpress/Api/Api.php +++ b/theme/wowpress/Api/Api.php @@ -71,7 +71,7 @@ abstract class Api curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - + $result = curl_exec($ch); if (curl_errno($ch)) { @@ -103,7 +103,11 @@ abstract class Api $headers = array('Content-Type: application/json', 'Content-Length: ' . strlen($data_json)); $headers[] = 'Accept: application/json'; if ($headerauth) { - $headers[] = 'Authorization: ' . $this->api_key; + if (!is_bool($headerauth)) { + $headers[] = 'Authorization: Bearer ' . $headerauth; + } else { + $headers[] = 'Authorization: ' . $this->api_key; + } } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); @@ -130,10 +134,10 @@ abstract class Api $result = "{}"; if (!str_contains($url, "https:/")) { $url = $this->api_url . $url; - }else{ + } else { $parts = parse_url($url); parse_str($parts['query'], $query); - $parameter = array_merge($parameter,$query); + $parameter = array_merge($parameter, $query); $url = strtok($url, '?'); } @@ -146,39 +150,44 @@ abstract class Api $cache = Cache::where('p_key', $cache_key)->get()->last(); - - if(empty($cache->ID) || $cache->isExpired($expiration)){ - $ch = curl_init(); - if (!empty($parameter)) { - $parameter = http_build_query($parameter); - curl_setopt($ch, CURLOPT_URL, $url . "?" . $parameter); - } else { - curl_setopt($ch, CURLOPT_URL, $url); - } - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HEADER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); + if (empty($cache->ID) || $cache->isExpired($expiration)) { + $ch = curl_init(); + if (!empty($parameter)) { + $parameter = http_build_query($parameter); + curl_setopt($ch, CURLOPT_URL, $url . "?" . $parameter); + } else { + curl_setopt($ch, CURLOPT_URL, $url); + } + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER, 1); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); - $headers = array(); - $headers[] = 'Accept: application/json'; - if ($headerauth) { - $headers[] = 'Authorization: ' . $this->api_key; - } - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $headers = array(); + $headers[] = 'Accept: application/json'; + if ($headerauth) { + if (!is_bool($headerauth)) { + $headers[] = 'Authorization: Bearer ' . $headerauth; + } else { + $headers[] = 'Authorization: ' . $this->api_key; + } + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - $response = curl_exec($ch); - $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($response, 0, $header_size); - $result = substr($response, $header_size); - if (curl_errno($ch)) { - error_log('Error:' . curl_error($ch)); - return false; + $response = curl_exec($ch); + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $header_size); + $result = substr($response, $header_size); + if (curl_errno($ch)) { + error_log('Error:' . curl_error($ch)); + return false; + } + + $cache = Cache::upsert($cache_key, $result); + $from_cache = false; } - $cache = Cache::upsert($cache_key, $result); - $from_cache = false; - } + if (!is_array($cache->value)) { return ['error' => true]; diff --git a/theme/wowpress/Api/BattleNet.php b/theme/wowpress/Api/BattleNet.php index 128a759..7b69ef2 100644 --- a/theme/wowpress/Api/BattleNet.php +++ b/theme/wowpress/Api/BattleNet.php @@ -15,8 +15,8 @@ class BattleNet extends Api{ $data = $this->get("/profile/wow/character/$realm/$name", [ 'namespace' => 'profile-eu', 'locale' => 'en_US', - 'access_token' => $this->bearer() - ], false, $timeout); + # 'access_token' => $this->bearer() + ], $this->bearer(), $timeout); return $data; } @@ -35,13 +35,13 @@ class BattleNet extends Api{ $data = $this->get($guild, [ 'namespace' => 'profile-eu', 'locale' => 'en_US', - 'access_token' => $this->bearer() - ], false, -1); + #'access_token' => $this->bearer() + ], $this->bearer(), -1); $data = $this->get($data['roster']['href'], [ 'namespace' => 'profile-eu', 'locale' => 'en_US', - 'access_token' => $this->bearer() - ], false, -1); + #'access_token' => $this->bearer() + ], $this->bearer(), -1); foreach($data['members'] as $member){ if($member['character']['name'] == $name && $member['character']['realm']['slug'] == $char['realm']['slug']){ $guild = $char['guild']['name']; @@ -62,8 +62,8 @@ class BattleNet extends Api{ $data = $this->get("/profile/wow/character/$realm/$name/character-media", [ 'namespace' => 'profile-eu', 'locale' => 'en_US', - 'access_token' => $this->bearer() - ], false, $timeout); + # 'access_token' => $this->bearer() + ], $this->bearer(), $timeout); if(!empty($data['code']) && $data['code'] == 403){ # var_dump($data); diff --git a/theme/wowpress/Models/Character.php b/theme/wowpress/Models/Character.php index 21479f4..b71c4a9 100644 --- a/theme/wowpress/Models/Character.php +++ b/theme/wowpress/Models/Character.php @@ -19,26 +19,29 @@ class Character extends Model { parent::__construct($attrs); $options = get_option('wowpress_api'); - if(isset($options['bnet'])){ + if (isset($options['bnet'])) { $key = get_option('wowpress_api')['bnet']['key']; $id = get_option('wowpress_api')['bnet']['id']; - }else{ + } else { return false; } - $this->api = new BattleNet($id,$key); + $this->api = new BattleNet($id, $key); } - public function lists(){ - return $this->belongsToMany(CharacterList::class,'wowpress_charlists_item','list_id','character_id')->withPivot('comment','date','id'); + public function lists() + { + 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); } - public function getCanEditAttribute(){ - if(empty($this->user->ID)){ + public function getCanEditAttribute() + { + if (empty($this->user->ID)) { return current_user_can('wowpress_edit_raids'); } return current_user_can('wowpress_edit_raids') || (get_current_user_id() == $this->user->ID); @@ -46,7 +49,7 @@ class Character extends Model - public function sanitize($string,$delimiter_space="") + public function sanitize($string, $delimiter_space = "") { $string = strtolower(str_replace(" ", $delimiter_space, $string)); $string = strtolower(str_replace("'", "", $string)); @@ -54,29 +57,41 @@ class Character extends Model return $string; } - public function getRankAttribute($rank){ - switch($rank){ - case 0: return "Gildenmeister"; - break; - case 1: return "Offizier"; - break; - case 2: return "Offi-Twink"; - break; - case 3: return "Raidlead"; - break; - case 4: return "Ehrenmitglied"; - break; - case 5: return "Raider"; - break; - case 6: return "Raider-Twink"; - break; - case 7: return "F&F"; - break; - case 8: return "Novize"; - break; - case 9: return "Sleeper"; - break; - default: return "Gast"; + public function getRankAttribute($rank) + { + switch ($rank) { + case 0: + return "Gildenmeister"; + break; + case 1: + return "Offizier"; + break; + case 2: + return "Offi-Twink"; + break; + case 3: + return "Raidlead"; + break; + case 4: + return "Ehrenmitglied"; + break; + case 5: + return "Raider"; + break; + case 6: + return "Raider-Twink"; + break; + case 7: + return "F&F"; + break; + case 8: + return "Novize"; + break; + case 9: + return "Sleeper"; + break; + default: + return "Gast"; } } @@ -91,7 +106,8 @@ class Character extends Model 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); 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"; } - public static function specIconLink($class,$spec){ + public static function specIconLink($class, $spec) + { $classname = (new self())->sanitize($class); $specname = (new self())->sanitize($spec); return get_template_directory_uri() . "/icons/class/$classname/$specname.png"; @@ -111,54 +128,63 @@ class Character extends Model public function updateFromAPI() { - $realm = $this->sanitize($this->realm,"-"); - $char_from_api = $this->api->getCharacter($this->name,$realm); - if(!array_key_exists('character_class',$char_from_api)){ + $realm = $this->sanitize($this->realm, "-"); + $char_from_api = $this->api->getCharacter($this->name, $realm); + if (!array_key_exists('character_class', $char_from_api)) { return false; } $this->class = $char_from_api['character_class']['name']; $this->spec = $char_from_api['active_spec']['name']; $this->id_blizz = $char_from_api['id']; - $guild_data = $this->api->getGuildRank($this->name,$realm); - if(!empty($guild_data['rank'])){ - $this->guild = $guild_data['guild']; - $this->rank = $guild_data['rank']; - }else{ + $guild_data = $this->api->getGuildRank($this->name, $realm); + if (!empty($guild_data['rank'])) { + $this->guild = $guild_data['guild']; + $this->rank = $guild_data['rank']; + } else { $this->rank = 99; } $this->save(); return $char_from_api; } - public function updateMedia($force = false){ - if(empty($this->id_blizz)){ + public function updateMedia($force = false) + { + if (empty($this->id_blizz)) { return false; } - $character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/"; - if(!is_dir($character_dir)){ - mkdir($character_dir,0777,true); + $character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/"; + if (!is_dir($character_dir)) { + mkdir($character_dir, 0777, true); } $id = $this->id_blizz; $mod = $id % 256; - $realm = $this->sanitize($this->realm,'-'); - $img_url = "https://render.worldofwarcraft.com/eu/character/$realm/$mod/$id-avatar.jpg"; - file_put_contents($character_dir."avatar.jpg",file_get_contents($img_url)); + $realm = $this->sanitize($this->realm, '-'); + $data = $this->api->getMedia($this->name, $this->realm, 0); + 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(){ - $character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/avatar.jpg"; - $character_dir_uri = wp_upload_dir()['baseurl']."/characters/".$this->ID."/avatar.jpg"; - if(file_exists($character_dir)){ + public function getAvatarAttribute() + { + $character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/avatar.jpg"; + $character_dir_uri = wp_upload_dir()['baseurl'] . "/characters/" . $this->ID . "/avatar.jpg"; + if (file_exists($character_dir)) { return $character_dir_uri; - }else{ + } else { return ""; } - } - public function getClassButtonAttribute(){ - require(get_template_directory()."/template-parts/components/class-button.php"); + public function getClassButtonAttribute() + { + require(get_template_directory() . "/template-parts/components/class-button.php"); } }