API-Fixes
parent
b1f5904b91
commit
b9d1865186
|
@ -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 {
|
||||||
|
|
|
@ -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,22 +58,21 @@ 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>
|
||||||
|
|
||||||
<?php if (current_user_can('wowpress_edit_characters')) : ?>
|
<?php if (current_user_can('wowpress_edit_characters')) : ?>
|
||||||
<td class="p-auto hidden lg:table-cell">
|
<td class="p-auto hidden lg:table-cell">
|
||||||
<form action="/request" method="POST">
|
<form action="/request" method="POST">
|
||||||
<?php wp_nonce_field('toggleRaidchar', 'toggleRaidchar_nonce'); ?>
|
<?php wp_nonce_field('toggleRaidchar', 'toggleRaidchar_nonce'); ?>
|
||||||
<input type="hidden" name="action" value="toggleRaidchar">
|
<input type="hidden" name="action" value="toggleRaidchar">
|
||||||
<input type="hidden" name="id" value="<?= $char->ID ?>">
|
<input type="hidden" name="id" value="<?= $char->ID ?>">
|
||||||
<?php if ($char->raidchar) : ?>
|
<?php if ($char->raidchar) : ?>
|
||||||
<button type="submit" class="btn btn-<?=$char->color?>"><?=Icon::get('o-check-circle')?> Ja</button>
|
<button type="submit" class="btn btn-<?= $char->color ?>"><?= Icon::get('o-check-circle') ?> Ja</button>
|
||||||
<?php else : ?>
|
<?php else : ?>
|
||||||
<button type="submit" class="btn btn-gray"><?=Icon::get('o-no-symbol')?> Nein</button>
|
<button type="submit" class="btn btn-gray"><?= Icon::get('o-no-symbol') ?> Nein</button>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</form>
|
</form>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td class="p-auto hidden lg:table-cell">
|
<td class="p-auto hidden lg:table-cell">
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ abstract class Api
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$result = curl_exec($ch);
|
$result = curl_exec($ch);
|
||||||
if (curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
|
@ -103,7 +103,11 @@ 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) {
|
||||||
$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);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
|
@ -130,10 +134,10 @@ abstract class Api
|
||||||
$result = "{}";
|
$result = "{}";
|
||||||
if (!str_contains($url, "https:/")) {
|
if (!str_contains($url, "https:/")) {
|
||||||
$url = $this->api_url . $url;
|
$url = $this->api_url . $url;
|
||||||
}else{
|
} else {
|
||||||
$parts = parse_url($url);
|
$parts = parse_url($url);
|
||||||
parse_str($parts['query'], $query);
|
parse_str($parts['query'], $query);
|
||||||
$parameter = array_merge($parameter,$query);
|
$parameter = array_merge($parameter, $query);
|
||||||
$url = strtok($url, '?');
|
$url = strtok($url, '?');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,39 +150,44 @@ 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)) {
|
$parameter = http_build_query($parameter);
|
||||||
$parameter = http_build_query($parameter);
|
curl_setopt($ch, CURLOPT_URL, $url . "?" . $parameter);
|
||||||
curl_setopt($ch, CURLOPT_URL, $url . "?" . $parameter);
|
} else {
|
||||||
} else {
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
}
|
||||||
}
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($ch, CURLOPT_HEADER, 1);
|
||||||
curl_setopt($ch, CURLOPT_HEADER, 1);
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
|
||||||
|
|
||||||
|
|
||||||
$headers = array();
|
$headers = array();
|
||||||
$headers[] = 'Accept: application/json';
|
$headers[] = 'Accept: application/json';
|
||||||
if ($headerauth) {
|
if ($headerauth) {
|
||||||
$headers[] = 'Authorization: ' . $this->api_key;
|
if (!is_bool($headerauth)) {
|
||||||
}
|
$headers[] = 'Authorization: Bearer ' . $headerauth;
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
} else {
|
||||||
|
$headers[] = 'Authorization: ' . $this->api_key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||||
$header = substr($response, 0, $header_size);
|
$header = substr($response, 0, $header_size);
|
||||||
$result = substr($response, $header_size);
|
$result = substr($response, $header_size);
|
||||||
if (curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
error_log('Error:' . curl_error($ch));
|
error_log('Error:' . curl_error($ch));
|
||||||
return false;
|
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)) {
|
if (!is_array($cache->value)) {
|
||||||
return ['error' => true];
|
return ['error' => true];
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -19,26 +19,29 @@ class Character extends Model
|
||||||
{
|
{
|
||||||
parent::__construct($attrs);
|
parent::__construct($attrs);
|
||||||
$options = get_option('wowpress_api');
|
$options = get_option('wowpress_api');
|
||||||
if(isset($options['bnet'])){
|
if (isset($options['bnet'])) {
|
||||||
$key = get_option('wowpress_api')['bnet']['key'];
|
$key = get_option('wowpress_api')['bnet']['key'];
|
||||||
$id = get_option('wowpress_api')['bnet']['id'];
|
$id = get_option('wowpress_api')['bnet']['id'];
|
||||||
}else{
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$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');
|
||||||
}
|
}
|
||||||
return current_user_can('wowpress_edit_raids') || (get_current_user_id() == $this->user->ID);
|
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(" ", $delimiter_space, $string));
|
||||||
$string = strtolower(str_replace("'", "", $string));
|
$string = strtolower(str_replace("'", "", $string));
|
||||||
|
@ -54,29 +57,41 @@ class Character extends Model
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRankAttribute($rank){
|
public function getRankAttribute($rank)
|
||||||
switch($rank){
|
{
|
||||||
case 0: return "Gildenmeister";
|
switch ($rank) {
|
||||||
break;
|
case 0:
|
||||||
case 1: return "Offizier";
|
return "Gildenmeister";
|
||||||
break;
|
break;
|
||||||
case 2: return "Offi-Twink";
|
case 1:
|
||||||
break;
|
return "Offizier";
|
||||||
case 3: return "Raidlead";
|
break;
|
||||||
break;
|
case 2:
|
||||||
case 4: return "Ehrenmitglied";
|
return "Offi-Twink";
|
||||||
break;
|
break;
|
||||||
case 5: return "Raider";
|
case 3:
|
||||||
break;
|
return "Raidlead";
|
||||||
case 6: return "Raider-Twink";
|
break;
|
||||||
break;
|
case 4:
|
||||||
case 7: return "F&F";
|
return "Ehrenmitglied";
|
||||||
break;
|
break;
|
||||||
case 8: return "Novize";
|
case 5:
|
||||||
break;
|
return "Raider";
|
||||||
case 9: return "Sleeper";
|
break;
|
||||||
break;
|
case 6:
|
||||||
default: return "Gast";
|
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";
|
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";
|
||||||
|
@ -111,54 +128,63 @@ class Character extends Model
|
||||||
|
|
||||||
public function updateFromAPI()
|
public function updateFromAPI()
|
||||||
{
|
{
|
||||||
$realm = $this->sanitize($this->realm,"-");
|
$realm = $this->sanitize($this->realm, "-");
|
||||||
$char_from_api = $this->api->getCharacter($this->name,$realm);
|
$char_from_api = $this->api->getCharacter($this->name, $realm);
|
||||||
if(!array_key_exists('character_class',$char_from_api)){
|
if (!array_key_exists('character_class', $char_from_api)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->class = $char_from_api['character_class']['name'];
|
$this->class = $char_from_api['character_class']['name'];
|
||||||
$this->spec = $char_from_api['active_spec']['name'];
|
$this->spec = $char_from_api['active_spec']['name'];
|
||||||
$this->id_blizz = $char_from_api['id'];
|
$this->id_blizz = $char_from_api['id'];
|
||||||
$guild_data = $this->api->getGuildRank($this->name,$realm);
|
$guild_data = $this->api->getGuildRank($this->name, $realm);
|
||||||
if(!empty($guild_data['rank'])){
|
if (!empty($guild_data['rank'])) {
|
||||||
$this->guild = $guild_data['guild'];
|
$this->guild = $guild_data['guild'];
|
||||||
$this->rank = $guild_data['rank'];
|
$this->rank = $guild_data['rank'];
|
||||||
}else{
|
} else {
|
||||||
$this->rank = 99;
|
$this->rank = 99;
|
||||||
}
|
}
|
||||||
$this->save();
|
$this->save();
|
||||||
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;
|
||||||
}
|
}
|
||||||
$character_dir = wp_upload_dir()['basedir']."/characters/".$this->ID."/";
|
$character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/";
|
||||||
if(!is_dir($character_dir)){
|
if (!is_dir($character_dir)) {
|
||||||
mkdir($character_dir,0777,true);
|
mkdir($character_dir, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$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_uri = wp_upload_dir()['baseurl']."/characters/".$this->ID."/avatar.jpg";
|
$character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/avatar.jpg";
|
||||||
if(file_exists($character_dir)){
|
$character_dir_uri = wp_upload_dir()['baseurl'] . "/characters/" . $this->ID . "/avatar.jpg";
|
||||||
|
if (file_exists($character_dir)) {
|
||||||
return $character_dir_uri;
|
return $character_dir_uri;
|
||||||
}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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue