-
Notifications
You must be signed in to change notification settings - Fork 1
/
wow-armory.php
572 lines (467 loc) · 22.4 KB
/
wow-armory.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
/*
Plugin Name: WoW Armory
Plugin URI: http://www.marenkay.com/wordpress/wow-armory/
Description: WoW Armory is a plugin which allows to embed item and character information from the World of Warcraft armory.
Version: 1.0
Author: Daniel S. Reichenbach
Author URI: http://www.marenkay.com/
*/
/* Copyright 2008 Daniel S. Reichenbach (email : daniel.s.reichenbach@mac.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
load_plugin_textdomain('wow_armory_plugin',PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)) . '/lang');
/**
* Require the phpArmory class to retrieve the XML from the Armory
*/
require_once(dirname(__FILE__) . '/phparmory/phpArmory.class.php');
if (!class_exists("WordPressArmoryCache")) {
class WordPressArmoryCache extends phpArmory5 {
/**
* The mysql cache table
*
* @var string
*/
var $dataTable = "armory_cache";
/**
* The time between cache updates in seconds
*
* @todo Make the interval able to be set for each thing (ie. update items once a week, update guilds once a day, etc..)
*
* @var integer
*/
var $updateInterval = 14400;
/**
* Internal cache id of the current item
*
* @var integer
*/
var $cacheID = 0;
/**
* WordPressArmoryCache class constructor.
* @access public
* @param string $areaName
* @param int $downloadRetries
* @return mixed $result Returns TRUE if the class could be instantiated properly. Returns FALSE and an error string, if the class could not be instantiated.
*/
public function __construct($areaName = NULL, $downloadRetries = NULL) {
global $wpdb;
$table_name = $wpdb->prefix . $this->dataTable;
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE IF NOT EXISTS `" . $table_name . "` (
`cache_id` VARCHAR(100) NOT NULL DEFAULT '',
`cache_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`cache_xml` TEXT,
PRIMARY KEY `cache_id` (`cache_id`))";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
if ($areaName){
$this->phpArmory5->setArea($areaName);
}
if ($downloadRetries){
$this->phpArmory5($downloadRetries);
}
}
/**
* getCharacterData
*
* Attempts to fetch a cached version of the requested
* character. Otherwise, it calls the parent function.
*
* @return string[] An associative array
* @param string $character The name of the character
* @param string $realm The character's realm
*/
public function getCharacterData($characterName = NULL, $realmName = NULL) {
if(($characterName==NULL)&&($this->characterName)) $characterName = $this->characterName;
if(($realmName==NULL)&&($this->realmName)) $realmName = $this->realmName;
$this->cacheID = "c".md5($characterName.$realmName);
$cached = $this->cacheFetch($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getCharacterData($characterName, $realmName);
if ( $this->cacheID ) {
$scached = serialize($cached);
$this->cacheSave($this->cacheID, $scached);
unset($this->cacheID);
}
return $cached;
}else{
return $cached;
}
}
/**
* getGuildData
*
* Attempts to fetch a cached version of the requested
* guild. Otherwise, it calls the parent function.
*
* @return string[] An associative array
* @param string $guild The name of the guild
* @param string $realm The guild's realm
*/
public function getGuildData($guildName = NULL, $realmName = NULL) {
if(($guildName==NULL)&&($this->guildName)) $guildName = $this->guildName;
if(($realmName==NULL)&&($this->realmName)) $realm = $this->realmName;
$this->cacheID = "g".md5($guildName.$realmName);
$cached = $this->cacheFetch($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getGuildData($guildName, $realmName);
if ( $this->cacheID ) {
$scached = serialize($cached);
$this->cacheSave($this->cacheID, $scached);
unset($this->cacheID);
}
return $cached;
} else {
return $cached;
}
}
/**
* getItemData
*
* Attempts to fetch a cached version of the requested
* item. Otherwise, it calls the parent function.
*
* @return string[] An associative array
* @param integer $itemID The ID of the item
*/
public function getItemData($itemID) {
$this->cacheID = "i".md5($itemID);
$cached = $this->cacheFetch($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getItemData($itemID);
if ( $this->cacheID ) {
$scached = serialize($cached);
$this->cacheSave($this->cacheID, $scached);
unset($this->cacheID);
}
return $cached;
}else{
return $cached;
}
}
/**
* getItemDataByName
*
* Attempts to fetch a cached version of the requested
* item search. Otherwise, it calls the parent function.
*
* @return string[] An associative array
* @param string $item The name of the item
* @param string[] $filter Associative array of search parameters
*/
public function getItemDataByName($itemName, $filter = NULL) {
if ($filter&&is_array($filter)) {
$this->cacheID = "s".md5($itemName.implode('', $filter));
} else {
$this->cacheID = "s".md5($itemName);
}
$cached = $this->cacheFetch($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getItemDataByName($itemName, $filter);
if ( $this->cacheID ) {
$scached = serialize($cached);
$this->cacheSave($this->cacheID, $scached);
unset($this->cacheID);
}
return $cached;
}else{
return $cached;
}
}
/**
* cacheFetch
*
* This function returns the unserialized XML data
* for the requested cache id from the cache. It
* will also remove old cached files/rows that have
* not been updated since the update interval.
*
* @return string[] An associative array
* @param string $cacheID The ID of the cached thing
*/
public function cacheFetch($cacheID) {
global $wpdb;
$table_name = $wpdb->prefix . $this->dataTable;
$query = "SELECT cache_xml, UNIX_TIMESTAMP(cache_time) AS cache_time FROM `".$table_name."` WHERE cache_id = '".$wpdb->escape($cacheID)."'";
$results = $wpdb->get_results( $query );
if ($result) {
if (time()-mysql_result($result, 0, 'cache_time') > $this->updateInterval) {
$query = "DELETE FROM `".$table_name."` WHERE cache_id = '".$wpdb->escape($cacheID)."'";
$results = $wpdb->query( $query );
} else {
// Return the cached XML as an array
return $this->convertXmlToArray(mysql_result($result, 0, 'cache_xml'));
}
}
}
/**
* cacheSave
*
* This function saves the given XML data to the
* cache by its cache id.
*
* @param string $cacheID The ID of the cached thing
* @param string $xml The XML info to be saved
*/
public function cacheSave($cacheID, $xml) {
global $wpdb;
$table_name = $wpdb->prefix . $this->dataTable;
if (get_magic_quotes_gpc()) $xml = stripslashes($xml);
$query = "REPLACE INTO `".$table_name."` (cache_id, cache_xml) VALUES('".$wpdb->escape($cacheID)."','".$wpdb->escape(xml)."')";
$results = $wpdb->query( $query );
}
}
}
if (!class_exists("WoWArmoryPlugin")) {
class WoWArmoryPlugin {
var $adminOptionsName = "WoWArmoryPluginAdminOptions";
function WoWArmoryPlugin() { //constructor
}
/**
* addHeaderCode()
*
* Add the scripts and styles used for WoW Armory to the WordPress
* header.
*
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function addHeaderCode() {
$url = get_bloginfo('wpurl');
?>
<script type="text/javascript" src="<?php echo $url; ?>/wp-content/plugins/wow-armory/js/overlib.js"></script>
<link rel="stylesheet" href="<?php echo $url; ?>/wp-content/plugins/wow-armory/css/wow-armory.css" type="text/css" />
<?php
}
/**
* getAdminOptions()
*
* Returns an array of admin options.
*
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function getAdminOptions() {
$wowarmoryAdminOptions = array(
'wowarmory_area' => 'en',
'wowarmory_modify_content' => 'true',
'wowarmory_modify_comment' => 'true');
$wowarmoryOptions = get_option($this->adminOptionsName);
if (!empty($wowarmoryOptions)) {
foreach ($wowarmoryOptions as $key => $option)
$wowarmoryAdminOptions[$key] = $option;
}
update_option($this->adminOptionsName, $wowarmoryAdminOptions);
return $wowarmoryAdminOptions;
}
/**
* init()
*
* Plugin initialization.
*
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function init() {
$this->getAdminOptions();
}
/**
* printAdminPage()
*
* Prints out the admin page
*
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function printAdminPage() {
$wowarmoryOptions = $this->getAdminOptions();
if (isset($_POST['update_WoWArmoryPluginSettings'])) {
if (isset($_POST['wowarmory_area'])) {
$wowarmoryOptions['wowarmory_area'] = $_POST['wowarmory_area'];
}
if (isset($_POST['wowarmory_modify_content'])) {
$wowarmoryOptions['wowarmory_modify_content'] = $_POST['wowarmory_modify_content'];
}
if (isset($_POST['wowarmory_modify_comment'])) {
$wowarmoryOptions['wowarmory_modify_comment'] = $_POST['wowarmory_modify_comment'];
}
update_option($this->adminOptionsName, $wowarmoryOptions);
if ( !empty($_POST ) ) : ?>
<div id="message" class="updated fade"><p><?php _e('Options saved.', 'wow_armory_plugin') ?></p></div>
<?php
endif;
} ?>
<div class="wrap">
<h2 id="write-post"><?php _e("WoW Armory Options…",'wow_armory_plugin');?></h2>
<p><?php _e('WoW Armory plugin will retrieve character, guild, and item data from the <a href="http://www.wowarmory.com/">World of Warcraft armory</a>.','wow_armory_plugin');?></p>
<?php
if (!function_exists("curl_init")) {
?>
<p style="padding: .5em; background-color: #d22; color: #fff; font-weight: bold;"><?php _e("WordPress armory plugin can not be used. You will need to install CURL support for your PHP installation.",'wow_armory_plugin');?></p>
<?
} else {
?>
<p style="padding: .5em; background-color: #2d2; color: #fff; font-weight: bold;"><?php _e("WordPress armory plugin is configured and ready to grab data.",'wow_armory_plugin');?></p>
<?
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo basename(__FILE__); ?>">
<?php wp_nonce_field('update-options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e("Region:",'wow_armory_plugin'); ?></th>
<td>
<select name="wowarmory_area" id="wowarmory_area">
<option <?php if ($wowarmoryOptions['wowarmory_area'] == "us") { echo 'selected'; } ?> value="us"><?php _e("US armory",'wow_armory_plugin'); ?></option>
<option <?php if ($wowarmoryOptions['wowarmory_area'] == "eu") { echo 'selected'; } ?> value="eu"><?php _e("EU armory",'wow_armory_plugin'); ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e("Pages and Posts:",'wow_armory_plugin'); ?></th>
<td>
<p><label for="wowarmory_modify_content_yes"><input type="radio" id="wowarmory_modify_content_yes" name="wowarmory_modify_content" value="true" <?php if ($wowarmoryOptions['wowarmory_modify_content'] == "true") { echo 'checked="checked"'; } ?> /> <?php _e("Yes",'wow_armory_plugin'); ?></label> <label for="wowarmory_modify_content_no"><input type="radio" id="wowarmory_modify_content_no" name="wowarmory_modify_content" value="false" <?php if ($wowarmoryOptions['wowarmory_modify_content'] == "false") { echo 'checked="checked"'; } ?>/> <?php _e("No",'wow_armory_plugin'); ?></label></p>
<p><?php _e('Selecting <em>No</em> will disable the display of character and item links for posts and pages.','wow_armory_plugin'); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e("Comments:",'wow_armory_plugin'); ?></th>
<td>
<p><label for="wowarmory_modify_comment_yes"><input type="radio" id="wowarmory_modify_comment_yes" name="wowarmory_modify_comment" value="true" <?php if ($wowarmoryOptions['wowarmory_modify_comment'] == "true") { echo 'checked="checked"'; } ?> /> <?php _e("Yes",'wow_armory_plugin'); ?></label> <label for="wowarmory_modify_comment_no"><input type="radio" id="wowarmory_modify_comment_no" name="wowarmory_modify_comment" value="false" <?php if ($wowarmoryOptions['wowarmory_modify_comment'] == "false") { echo 'checked="checked"'; } ?>/> <?php _e("No",'wow_armory_plugin'); ?></label></p>
<p><?php _e('Selecting <em>No</em> will disable the display of character and item links for comments.','wow_armory_plugin'); ?></p>
</td>
</tr>
</table>
<div class="submit">
<input type="submit" name="update_WoWArmoryPluginSettings" value="<?php _e('Update Options »') ?>" />
</div>
</form>
</div>
<?php
} // End function printAdminPage()
/**
* modifyContent()
*
* Parse the supplied WordPress content and insert character and item
* links pointing to the armory. The actual parsing is relayed to
* doParse().
*
* @param string $content WordPress page/post conent
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function modifyContent($content = '') {
$content = $this->doParse($content);
return $content;
}
/**
* modifyComment()
*
* Parse the supplied WordPress comment and insert character and item
* links pointing to the armory. The actual parsing is relayed to
* doParse().
*
* @param string $content WordPress page/post content
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function modifyComment($content = '') {
$content = $this->doParse($content);
return $content;
}
/**
* doParse()
*
* Parse the supplied content variable and insert character and item
* links pointing to the armory.
*
* @param string $content WordPress page/post content
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function doParse($content = '') {
$wowarmoryOptions = $this->getAdminOptions();
$armoryFetch = new WordPressArmoryCache();
$armoryFetch->setArea($wowarmoryOptions['wowarmory_area;']);
$itemRarity = array (
0 => "#5b5b5b",
1 => "#ffffff",
2 => "#007200",
3 => "#004385",
4 => "#5d1f88",
5 => "#c24e00",
6 => "#9c884d"
);
$content = utf8_decode($content);
// parse the content variable for all [item]...[/item] occurrences
while (preg_match('#\[(item)(=[0-5])?\](.+?)\[/item\]#s', $content, $match)) {
$itemName = $match[3];
$iconSize = $match[2];
$itemType = $match[1];
$itemName = html_entity_decode($itemName, ENT_QUOTES);
$armoryAreaData = $armoryFetch->getArea();
$item = $armoryFetch->getItemDataByName($itemName);
if ($item) { // armory supplied a valid item xml object
$itemQuality = $item['itemtooltips']['itemtooltip']['overallqualityid'];
$itemHtml = '<a style="font-weight: bold; color: '.$itemRarity[$itemQuality].';" href="'.$armoryAreaData[1].'item-info.xml?i='.$item['itemtooltips']['itemtooltip']['id'].'">';
$itemHtml = $itemHtml . '<span onmouseover="return overlib(\'<table cellpadding=\\\'0\\\' border=\\\'0\\\' class=\\\'tooltip_new\\\'><tr><td><center><img src=\\\''.get_bloginfo('wpurl').'/'.PLUGINDIR.'/'.dirname(plugin_basename (__FILE__)).'/images/ajax-loader.gif\\\' border=\\\'0\\\' align=\\\'Loading...\\\' /><br />Searching...please wait.</center></td></tr></table>\',VAUTO,HAUTO,FULLHTML);" onmouseout="return nd();">';
$itemHtml = $itemHtml . $item['itemtooltips']['itemtooltip']['name'];
$itemHtml = $itemHtml . '</span>';
$itemHtml = $itemHtml . '</a>';
} else { // armory did not supply a valid xml object
$itemHtml = '<a style="font-weight: bold; color: '.$itemRarity[0].';" href="'.$armoryAreaData[1].'search.xml?searchQuery='.$itemName.'&searchType=items">';
$itemHtml = $itemHtml . '<span onmouseover="return overlib(\'<table cellpadding=\\\'0\\\' border=\\\'0\\\' class=\\\'tooltip_new\\\'><tr><td><center><img src=\\\''.get_bloginfo('wpurl').'/'.PLUGINDIR.'/'.dirname(plugin_basename (__FILE__)).'/images/ajax-loader.gif\\\' border=\\\'0\\\' align=\\\'Loading...\\\' /><br />Item not found. Click to search the armory.</center></td></tr></table>\',VAUTO,HAUTO,FULLHTML);" onmouseout="return nd();">';
$itemHtml = $itemHtml . $itemName;
$itemHtml = $itemHtml . '</span>';
$itemHtml = $itemHtml . '</a>';
}
// replace the itemname with an armory link
$content = str_replace($match[0], $itemHtml, $content);
}
return $content;
}
/**
* itemTooltip()
*
* Parse the supplied item variable and generate a HTML tooltip.
*
* @param array $item WordPress page/post conent
* @return string $itemHtml Generated HTML tooltip
* @author Daniel S. Reichenbach <daniel.s.reichenbach@mac.com>
*/
function itemTooltip($item = array()) {
return;
}
}
} //End Class WoWArmoryPlugin
if (class_exists("WoWArmoryPlugin")) {
$wowarmory_plugin = new WoWArmoryPlugin();
}
//Initialize the admin panel
if (!function_exists("WoWArmoryPlugin_ap")) {
function WoWArmoryPlugin_ap() {
global $wowarmory_plugin;
if (!isset($wowarmory_plugin)) {
return;
}
if (function_exists('add_submenu_page')) {
add_submenu_page('plugins.php', __('WoW Armory Settings'), __('WoW Armory'), 'manage_options', basename(__FILE__), array(&$wowarmory_plugin, 'printAdminPage'));
}
}
}
//Actions and Filters
if (isset($wowarmory_plugin)) {
//Actions
add_action('admin_menu', 'WoWArmoryPlugin_ap');
add_action('wp_head', array(&$wowarmory_plugin, 'addHeaderCode'), 1);
add_action('activate_wow-armory/wow-armory.php', array(&$wowarmory_plugin, 'init'));
//Filters
add_filter('the_content', array(&$wowarmory_plugin, 'modifyContent'));
add_filter('comment_text',array(&$wowarmory_plugin, 'modifyComment'), 35);
}
?>