-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalpual_Plugin.php
210 lines (166 loc) · 7.96 KB
/
alpual_Plugin.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
<?php
include_once('alpual_LifeCycle.php');
include_once('alpual_coinvisWidget.php');
class alpual_Plugin extends alpual_LifeCycle {
protected $features = ['graph'=>false, 'table'=>true];
/**
* See: http://plugin.michael-simpson.com/?page_id=31
* @return array of option meta data.
*/
public function getOptionMetaData() {
// http://plugin.michael-simpson.com/?page_id=31
return array(
//'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
'ATextInput' => array(__('Enter in some text', 'my-awesome-plugin')),
'AmAwesome' => array(__('I like this awesome plugin', 'my-awesome-plugin'), 'false', 'true'),
'CanDoSomething' => array(__('Which user role can do something', 'my-awesome-plugin'),
'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone')
);
}
// protected function getOptionValueI18nString($optionValue) {
// $i18nValue = parent::getOptionValueI18nString($optionValue);
// return $i18nValue;
// }
protected function initOptions() {
$options = $this->getOptionMetaData();
if (!empty($options)) {
foreach ($options as $key => $arr) {
if (is_array($arr) && count($arr > 1)) {
$this->addOption($key, $arr[1]);
}
}
}
}
public function getPluginDisplayName() {
return 'CoinVis';
}
protected function getMainPluginFileName() {
return 'coinvis.php';
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Called by install() to create any database tables if needed.
* Best Practice:
* (1) Prefix all table names with $wpdb->prefix
* (2) make table names lower case only
* @return void
*/
protected function installDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` (
// `id` INTEGER NOT NULL");
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Drop plugin-created tables on uninstall.
* @return void
*/
protected function unInstallDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("DROP TABLE IF EXISTS `$tableName`");
}
/**
* Perform actions when upgrading from version X to version Y
* See: http://plugin.michael-simpson.com/?page_id=35
* @return void
*/
public function upgrade() {
}
public function addActionsAndFilters() {
// Add options administration page
// http://plugin.michael-simpson.com/?page_id=47
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
// Example adding a script & style just for the options administration page
// http://plugin.michael-simpson.com/?page_id=47
// if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
// }
// Add Actions & Filters
// http://plugin.michael-simpson.com/?page_id=37
// Adding scripts & styles to all pages
// Examples:
// wp_enqueue_script('jquery');
wp_enqueue_style('my-style', plugins_url('/css/coinvis.css', __FILE__));
if($this->features['graph']) {
wp_enqueue_script( 'd3', 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.8.0/d3.min.js');
wp_enqueue_script('my-script', plugins_url('/js/coinvis.js', __FILE__));
}
// Register short codes
// http://plugin.michael-simpson.com/?page_id=39
// add_shortcode('say-hello-world', array($this, 'doMyShortcode'));
add_shortcode('coin-vis', array($this, 'ap_coin_visualization'));
add_action('widgets_init', create_function('', 'return register_widget("alpual_coinvisWidget");'));
// Register AJAX hooks
// http://plugin.michael-simpson.com/?page_id=41
}
function ap_coin_visualization($atts = [], $content = null, $tag = '')
{
// normalize attribute keys, lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// override default attributes with user attributes
$coin_atts = shortcode_atts([
'coin' => 'bitcoin',
'datapoint' => false,
'graph' => "on"
], $atts, $tag);
// start output
$o = '';
if (!$coin_atts['datapoint']) {
// start box
$o .= '<div class="simple-crypto-box">';
// title
$o .= '<h2 class="coinHeader">' . esc_html__($coin_atts['coin']) . '</h2>';
}
$url = "https://api.coinmarketcap.com/v1/ticker/" . $coin_atts['coin'] . "/";
// Initiate curl
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => false, // Disable SSL verification
CURLOPT_RETURNTRANSFER => true, // Will return the response, if false it print the response
CURLOPT_URL => $url // Set the url
));
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
if (!$result) {
die('Error: "' . curl_error($result) . '" - Code: ' . curl_errno($result));
}
// Will dump a beauty json :3
$coinData = json_decode($result, true)[0];
//$coinData[percent_change_1h] = "-2.5"; // for debugging negatives
/*var_dump($coinData);*/
$content = "";
if ($this->features['table'] && !$coin_atts['datapoint']) {
// get the whole table
$content .= '<ul>
<li class="coinData">Price (USD): <span class="coinDataVal coinPrice">$' . number_format($coinData[price_usd], 2) . ' </span></li>
<li class="coinData">Percent Change Last Hour: <span class="coinDataVal coinPercent">' . number_format($coinData[percent_change_1h], 2) . '<span class="cv-lighter-text">%</span></span></li>
<li class="coinData">Percent Change Last Day: <span class="coinDataVal coinPercent">' . number_format($coinData[percent_change_24h], 2) . '<span class="cv-lighter-text">%</span></span></li>
<li class="coinData">Percent Change Last Week: <span class="coinDataVal coinPercent">' . number_format($coinData[percent_change_7d], 2) . '<span class="cv-lighter-text">%</span></span></li>
<li class="coinData">Market Cap: <span class="coinDataVal coinUSD">$' . number_format($coinData[market_cap_usd], 1) . ' </span></li>
<li class="coinData">24 Hour Volume: <span class="coinDataVal coinUSD">$' . number_format($coinData['24h_volume_usd'], 1) . ' </span></li>
<li class="coinData">Available Coin Supply: <span class="coinDataVal coinAmount">' . number_format($coinData[available_supply], 1) . ' </span></li>
<li class="coinData">Total Supply: <span class="coinDataVal coinAmount">' . number_format($coinData[total_supply], 1) . ' </span></li>
</ul>';
} else {
// get a single piece of data
$dpoint = strtolower($coin_atts['datapoint']);
$content .= number_format($coinData[$dpoint]);
}
if ($this->features['graph'] && strtolower($coin_atts['graph']) != "off" && !$coin_atts['datapoint']) {
// get the graph
$content .='<svg id="' . $coinData[id] . '" data-coin=\'' . json_encode($coinData) . '\' class="simpleCoin"></svg>';
}
$o .= $content;
// end box
if (!$coin_atts['datapoint']) {
$o .= '
</div>';
}
return $o;
}
}