WowPress-Tailwind/theme/wowpress/Frontend/Widgets/BossKills.php

82 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2024-04-23 22:43:54 +02:00
<?php
namespace WoWPress\Frontend\Widgets;
class BossKills extends Widget
{
protected $template_path = "boss_kills.php";
protected $title = "Boss-Kills";
protected $base_id = "wowpress_boss_kills";
public $name = "Boss-Kills";
protected $restricted = false;
2024-04-27 21:21:33 +02:00
public function getBossList($instance){
$out = [];
2024-08-05 20:19:44 +02:00
if(empty($instance['bosses'])){
$instance['bosses'] = [];
}
2024-04-27 21:21:33 +02:00
foreach($instance['bosses'] as $boss){
if(!empty($boss['name'])){
$out[] = $boss;
}
}
return $out;
}
public function getCompletePercent($instance){
2024-08-05 20:19:44 +02:00
$bosses = $this->getBossList($instance)?:[];
if(empty($bosses)){
return 0;
}
2024-04-27 21:21:33 +02:00
$done = 0;
foreach($bosses as $boss){
if(!empty($boss['date'])){
$done++;
}
}
2024-05-02 21:29:31 +02:00
return ($done/count($bosses))*100;
2024-04-27 21:21:33 +02:00
}
2024-04-23 22:43:54 +02:00
public function form($instance)
{
$raid_name = !empty($instance['raid_name']) ? $instance['raid_name'] : ""; ?>
<p>
<label for="<?php echo esc_attr($this->get_field_id("raid_name")); ?>"><?php echo esc_html__('Raid-Name', 'wowpress'); ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id("raid_name")); ?>" name="<?php echo esc_attr($this->get_field_name("raid_name")); ?>" value="<?php echo esc_attr($raid_name); ?>">
</p>
<?php
for ($i = 0; $i <= 15; $i++) {
$boss_name = !empty($instance['bosses'][$i]['name']) ? $instance['bosses'][$i]['name'] : "";
$boss_kill = !empty($instance['bosses'][$i]['date']) ? $instance['bosses'][$i]['date'] : "";
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id("bosses[$i][name]")); ?>"><?php echo esc_html__('Boss-Name', 'wowpress'); ?></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id("bosses[$i][name]")); ?>" name="<?php echo esc_attr($this->get_field_name("bosses[$i][name]")); ?>" value="<?php echo esc_attr($boss_name); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id("bosses[$i][date]")); ?>"><?php echo esc_html__('Firstkill', 'wowpress'); ?></label>
<input type="date" class="widefat" id="<?php echo esc_attr($this->get_field_id("bosses[$i][date]")); ?>" name="<?php echo esc_attr($this->get_field_name("bosses[$i][date]")); ?>" value="<?php echo esc_attr($boss_kill); ?>">
</p>
<?php
}
}
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['raid_name'] = (!empty($new_instance['raid_name'])) ? strip_tags($new_instance['raid_name']) : '';
for ($i = 0; $i <= 15; $i++) {
$instance['bosses'][$i]['name'] = (!empty($new_instance['bosses'][$i]['name'])) ? strip_tags($new_instance['bosses'][$i]['name']) : '';
$instance['bosses'][$i]['date'] = (!empty($new_instance['bosses'][$i]['date'])) ? strip_tags($new_instance['bosses'][$i]['date']) : '';
}
return $instance;
}
}