forked from dan-el/esosets-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheso-sets.php
242 lines (200 loc) · 7.73 KB
/
eso-sets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/*
Plugin Name: ESO Sets and Skills
Plugin URI: https://github.com/Woeler/esosets-wordpress
Description: Embed tooltips for sets and skills related to the Elder Scrolls Online into your pages and posts.
Version: 1.3.2
Author: Woeler
Author URI: https://www.github.com/woeler
License: GPL-3
*/
defined('ABSPATH') || exit;
add_action('plugins_loaded', 'EsoSets::setup');
final class EsoSets
{
protected $skillBarItems = [1, 2, 3, 4, 5, 'ult'];
/**
* EsoSets constructor.
*/
public function __construct()
{
}
/**
* Register all the needed functions in Wordpress
*/
public static function setup()
{
$esosets = new EsoSets();
add_shortcode('esoset', [$esosets, 'esoset_func']);
add_shortcode('esoskill', [$esosets, 'esoskill_func']);
add_shortcode('esoskillbar', [$esosets, 'esoskill_skillbar_func']);
add_shortcode('esoskilllist', [$esosets, 'esoskill_skilllist_func']);
add_action('wp_enqueue_scripts', [$esosets, 'addStyle']);
}
/**
* Convert all esoset shortcode elements to tooltips upon post/page save
*
* @param $atts
* @return string
*/
public function esoset_func($atts)
{
if (!isset($atts['id'])) {
return 'Set id not provided';
}
$cache = get_transient(md5('esosets_' . serialize($atts)));
if ($cache) {
return $cache;
}
$result = wp_remote_get('https://beast.pathfindermediagroup.com/api/eso/sets/tooltip/' . $atts['id']);
$result = json_decode(wp_remote_retrieve_body($result), true);
if (!isset($result['name']) || !isset($result['tooltip'])) {
if (isset($atts['setname'])) {
return $atts['setname'];
} else {
return 'An error occurred retrieving the set data';
}
}
$tooltip = str_replace('"', "'", $result['tooltip']);
if (isset($atts['setname'])) {
$name = $atts['setname'];
} else {
$name = $result['name'];
}
$return = '<a class="eso-set" href="https://www.eso-sets.com/set/' . $result['slug'] . '" target="_blank" rel="noopener" ';
if (isset($atts['tooltip']) && $atts['tooltip'] == 'true') {
$return .= 'data-toggle="tooltip" ';
}
$return .= 'data-html="true" title="' . htmlspecialchars($tooltip) . '">' . $name . '</a>';
if (!is_preview()) {
set_transient(md5('esosets_' . serialize($atts)), $return, 3600);
}
return $return;
}
/**
* Convert all esoskill shortcode elements to tooltips upon post/page save
*
* @param $atts
* @return string
*/
public function esoskill_func($atts)
{
if (!isset($atts['id'])) {
return 'Skill id not provided';
}
$cache = get_transient(md5('esoskills_' . serialize($atts)));
if ($cache) {
return $cache;
}
$result = wp_remote_get('https://beast.pathfindermediagroup.com/api/eso/skills/tooltip/' . $atts['id']);
$result = json_decode(wp_remote_retrieve_body($result), true);
if (!isset($result['name']) || !isset($result['tooltip'])) {
if (isset($atts['setname'])) {
return $atts['setname'];
} else {
return 'An error occurred retrieving the skill data';
}
}
$tooltip = str_replace('"', "'", $result['tooltip']);
if (isset($atts['skillname'])) {
$name = $atts['skillname'];
} else {
$name = $result['name'];
}
$return = '<a class="eso-set" href="https://www.eso-skillbook.com/skill/' . $result['slug'] . '" target="_blank" rel="noopener" ';
if (isset($atts['tooltip']) && $atts['tooltip'] == 'true') {
$return .= 'data-toggle="tooltip" ';
}
$return .= 'data-html="true" title="' . htmlspecialchars($tooltip) . '">' . $name . '</a>';
if (!is_preview()) {
set_transient(md5('esoskills_' . serialize($atts)), $return, 3600);
}
return $return;
}
/**
* Convert all esoskillbar shortcode elements to tooltips upon post/page save
*
* @param $atts
* @return string
*/
public function esoskill_skillbar_func($atts)
{
$cache = get_transient(md5('esoskillsbar_' . serialize($atts)));
if ($cache) {
return $cache;
}
$data = [];
foreach ($atts as $key => $skill_id) {
if (false !== strpos($key, 'skill_')) {
$data[$key] = $skill_id;
}
}
$result = wp_remote_get('https://beast.pathfindermediagroup.com/api/eso/skills/skillbar?' . http_build_query($data));
$result = json_decode(wp_remote_retrieve_body($result), true);
$return = '<div class="esoskill-skillbar">';
foreach ($result as $key => $skill) {
$tooltip = str_replace('"', "'", $skill['tooltip']);
$return .= '<a class="eso-set" href="https://www.eso-skillbook.com/skill/' . $skill['slug'] . '" target="_blank" ';
if (isset($atts['tooltip']) && $atts['tooltip'] == 'true') {
$return .= 'data-toggle="tooltip" ';
}
if ($key === 'skill_ult') {
$return .= 'data-html="true" title="' . htmlspecialchars($tooltip) . '"><img class="skill-img" alt="' . $skill['name'] . '" style="margin-left:25px;" src="' . $skill['img'] . '" /></a> ';
} else {
$return .= 'data-html="true" title="' . htmlspecialchars($tooltip) . '"><img class="skill-img" alt="' . $skill['name'] . '" src="' . $skill['img'] . '" /></a> ';
}
}
$return .= '</div>';
if (!is_preview()) {
set_transient(md5('esoskillsbar_' . serialize($atts)), $return, 3600);
}
return $return;
}
/**
* Convert all esoskilllist shortcode elements to tooltips upon post/page save
*
* @param $atts
* @return string
*/
public function esoskill_skilllist_func($atts)
{
$cache = get_transient(md5('esoskillslist_' . serialize($atts)));
if ($cache) {
return $cache;
}
$data = [];
foreach ($atts as $key => $skill_id) {
if (false !== strpos($key, 'skill_')) {
$data[$key] = $skill_id;
}
}
$result = wp_remote_get('https://beast.pathfindermediagroup.com/api/eso/skills/skilllist?' . http_build_query($data));
$result = json_decode(wp_remote_retrieve_body($result), true);
$return = '<div class="esoskill-skillbar">';
foreach ($result as $skill) {
$tooltip = str_replace('"', "'", $skill['tooltip']);
$return .= '<a class="eso-set" href="https://www.eso-skillbook.com/skill/' . $skill['slug'] . '" target="_blank" ';
if (isset($atts['tooltip']) && $atts['tooltip'] == 'true') {
$return .= 'data-toggle="tooltip" ';
}
$return .= 'data-html="true" title="' . htmlspecialchars($tooltip) . '"><img class="skill-img';
if ($skill['type'] === 2) {
$return .= ' passive-skill';
}
$return .= '" src="' . $skill['img'] . '" alt="' . $skill['name'] . '" /></a> ';
}
$return .= '</div>';
if (!is_preview()) {
set_transient(md5('esoskillslist_' . serialize($atts)), $return, 3600);
}
return $return;
}
/**
* Add the tooltip stylesheet to the frontend of Wordpress
*/
public function addStyle()
{
$plugin_url = plugin_dir_url(__FILE__);
wp_enqueue_style('ESO-Sets-Skills-CSS', $plugin_url . '/esosets_tooltips.css');
}
}