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-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 {

View File

@ -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();
<tr>
<td class="p-auto">Name</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">Gilde</td>
@ -59,22 +58,21 @@ get_header();
<?= translate_string($char->spec) ?></span>
</div>
</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->guild ?></td>
<?php if (current_user_can('wowpress_edit_characters')) : ?>
<td class="p-auto hidden lg:table-cell">
<form action="/request" method="POST">
<?php wp_nonce_field('toggleRaidchar', 'toggleRaidchar_nonce'); ?>
<input type="hidden" name="action" value="toggleRaidchar">
<input type="hidden" name="id" value="<?= $char->ID ?>">
<?php if ($char->raidchar) : ?>
<button type="submit" class="btn btn-<?=$char->color?>"><?=Icon::get('o-check-circle')?> Ja</button>
<?php else : ?>
<button type="submit" class="btn btn-gray"><?=Icon::get('o-no-symbol')?> Nein</button>
<?php endif; ?>
</form>
<form action="/request" method="POST">
<?php wp_nonce_field('toggleRaidchar', 'toggleRaidchar_nonce'); ?>
<input type="hidden" name="action" value="toggleRaidchar">
<input type="hidden" name="id" value="<?= $char->ID ?>">
<?php if ($char->raidchar) : ?>
<button type="submit" class="btn btn-<?= $char->color ?>"><?= Icon::get('o-check-circle') ?> Ja</button>
<?php else : ?>
<button type="submit" class="btn btn-gray"><?= Icon::get('o-no-symbol') ?> Nein</button>
<?php endif; ?>
</form>
</td>
<td class="p-auto hidden lg:table-cell">

View File

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

View File

@ -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];

View File

@ -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);

View File

@ -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");
}
}