-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphite-graphs.php
More file actions
285 lines (244 loc) · 11.2 KB
/
Copy pathgraphite-graphs.php
File metadata and controls
285 lines (244 loc) · 11.2 KB
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
<?php
/*
Plugin Name: Graphite Graphs
Plugin URI: http://rcollier.me/software/graphite-graphs/
Description: Display externally hosted graphite graphs in admin or on your theme
Version: 1.0
Author: Rich Collier
Author URI: http://rcollier.me
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Prevent execution of this file directly
if ( ! defined( 'ABSPATH' ) ) die( 'No cheating allowed :-)' );
// Singleton class for plugin functions
final class WP_Graphite_Graphs {
// Singleton init
public static function init() {
static $instance = null;
if ( $instance === null )
$instance = new WP_Graphite_Graphs();
return $instance;
}
// PHP5 Constructor
private function __construct() {
// Activation and deactivation hooks, for creating and destroying WP options
register_activation_hook( __FILE__, array( 'WP_Graphite_Graphs', 'plugin_activate' ) );
register_deactivation_hook( __FILE__, array( 'WP_Graphite_Graphs', 'plugin_deactivate' ) );
// Choreograph the show
add_action( 'wp_dashboard_setup', array( 'WP_Graphite_Graphs', 'action__wp_dashboard_setup' ) );
add_action( 'wp_ajax_wpggSaveDashOptions', array( 'WP_Graphite_Graphs', 'action__save_dash_options' ) );
add_action( 'widgets_init', array( 'WP_Graphite_Graphs', 'action__widgets_init' ) );
add_action( 'admin_enqueue_scripts', array( 'WP_Graphite_Graphs', 'action__admin_enqueue_scripts' ) );
}
// Register our sidebar widget
function action__widgets_init() {
register_widget( 'Graphite_Graph_Sidebar_Widget' );
}
// Activation hook
function plugin_activate() {
wpgg_update_option( 'install_date', date( 'Y-m-d' ) );
wpgg_update_option( 'install_version', '1.0' );
}
// Deactivation hook
function plugin_deactivate() {
delete_option( 'wpgg_options' );
}
// Enqueue admin scripts
function action__admin_enqueue_scripts( $hook ) {
// No need to load javascript anywhere except the dashboard
if ( 'index.php' == $hook )
wp_enqueue_script( 'graphite_graphs_javascript', plugin_dir_url( __FILE__ ) . 'graphite-graphs-admin.js' );
}
// Add the dashboard widget
function action__wp_dashboard_setup() {
$widget_title = wpgg_get_option( 'dash_title' );
// Title the widget if blank
if ( empty( $widget_title ) )
$widget_title = 'Untitled Graph';
// Add the dashboard widget
wp_add_dashboard_widget( 'graphite_dashboard_graph', $widget_title, array( 'WP_Graphite_Graphs', 'ui__dashboard_graph' ) );
}
// Save dashboard options sent from AJAX
function action__save_dash_options() {
// Sanitize and store the saved fields
wpgg_update_option( 'dash_title', sanitize_text_field( $_POST['wpgg_dash_title'] ) );
wpgg_update_option( 'dash_server', sanitize_text_field( $_POST['wpgg_dash_server'] ) );
wpgg_update_option( 'dash_metrics', sanitize_text_field( $_POST['wpgg_dash_metrics'] ) );
wpgg_update_option( 'dash_duration', intval( $_POST['wpgg_dash_duration'] ) );
// Default to "Dark" color scheme
if ( 'Light' == $_POST['wpgg_dash_colorscheme'] )
wpgg_update_option( 'dash_colorscheme', 'Light' );
else
wpgg_update_option( 'dash_colorscheme', 'Dark' );
// All done
die( 'success' );
}
// Create the dashboard widget UI
function ui__dashboard_graph() {
?>
<img width="100%" height="100%" id="dash_graph_img" />
<div style="display:none;margin-bottom:10px;" id="dash_graph_message">Configure a new graph by clicking "Settings" below.</div>
<img id="graph_loading_overlay" style="display:none;position:absolute;bottom:10px;right:12px;" src="<?php echo get_admin_url( '', 'images/loading.gif', 'admin' ); ?>" />
<a style="cursor:pointer;" id="wpgg_dashboard_settings_button">Settings</a> | <a style="cursor:pointer;" id="wpgg_dashboard_refresh_button">Refresh</a>
<div id="wpgg_dashboard_settings" style="display:none;">
<table class="form-table">
<tr>
<td>Graph Title</td>
<td><input type="text" name="wpgg_dash_title" id="wpgg_dash_title" value="<?php echo esc_html( wpgg_get_option( 'dash_title' ) ); ?>" /><br /><span class="description">Title at the top of this widget</span></td>
<tr>
<td>Graphite Server URL</td>
<td><input type="text" name="wpgg_dash_server" id="wpgg_dash_server" value="<?php echo esc_url( wpgg_get_option( 'dash_server' ) ); ?>" /><br /><span class="description">Example: http://graphite.somedomain.com:8080</span></td>
</tr>
<tr>
<td>Display Metrics</td>
<td><input type="text" name="wpgg_dash_metrics" id="wpgg_dash_metrics" value="<?php echo esc_html( wpgg_get_option( 'dash_metrics' ) ); ?>" /><br /><span class="description">Example: system.test.pageloadspeed or system.test.*</span></td>
</tr>
<tr>
<td>Duration</td>
<td>
<select name="wpgg_dash_duration" id="wpgg_dash_duration" />
<option value="<?php echo intval( wpgg_get_option( 'dash_duration' ) ); ?>"><?php echo intval( wpgg_get_option( 'dash_duration' ) ); ?></option>
<option value="-1">-1</option>
<option value="-2">-2</option>
<option value="-4">-4</option>
<option value="-8">-8</option>
<option value="-12">-12</option>
<option value="-24">-24</option>
</select> hours
</td>
</tr>
<tr>
<td>Color Scheme</td>
<td>
<select name="wpgg_dash_colorscheme" id="wpgg_dash_colorscheme" />
<?php
// Show current color scheme as option 1
$current_scheme = esc_html( wpgg_get_option( 'dash_colorscheme' ) );
if ( empty( $current_scheme ) )
$current_scheme = 'Dark';
?>
<option value="<?php echo $current_scheme; ?>"><?php echo $current_scheme; ?></option>
<option value="Dark">Dark</option>
<option value="Light">Light</option>
<option value="Aqua">Aqua</option>
<option value="Fire">Fire</option>
</select>
</td>
</tr>
</table>
<br />
<input type="button" id="graphite_dash_cancel" value="Cancel" class="button button-primary" />
<input type="submit" id="graphite_dash_submit" value="Save Changes" class="button button-primary" />
</div>
<?php
}
}
// Widget class for WP_Widget
class Graphite_Graph_Sidebar_Widget extends WP_Widget {
// Widget constructor
public function __construct() {
parent::__construct( 'graphite_graph_widget', 'Graphite Graph Widget' );
}
// Widget output
public function widget( $args, $instance ) {
// Do we have the necessary clearance to continue?
if ( $instance['graph_private'] && ! is_user_logged_in() )
return false;
// Get our baggage
extract( $args );
// Give the widget some styling and display the title
echo $before_widget . $before_title . esc_attr( $instance['graph_title'] ) . $after_title;
// Create the query string for the color scheme
if ( 'Light' == $instance['graph_colorscheme'] )
$colorscheme = '&bgcolor=white&fgcolor=black';
elseif ( 'Aqua' == $instance['graph_colorscheme'] )
$colorscheme = '&bgcolor=03aeff&fgcolor=0000aa';
elseif ( 'Fire' == $instance['graph_colorscheme'] )
$colorscheme = '&bgcolor=ffb84d&fgcolor=a32900';
else
$colorscheme = '&bgcolor=black&fgcolor=white';
// Dump out a safe happy image
echo '<img class="graphite_graph_image" width="100%" height="100%" src="' . trailingslashit( esc_url( $instance['graph_server_url'] ) ) . 'render?target=' . esc_attr( $instance['graph_metrics'] ) . '&from=' . intval( $instance['graph_duration'] ) . 'hours&width=480&height=270&lineMode=connected&lineWidth=4&hideLegend=true&fontSize=18' . $colorscheme . '" />';
// Close up the styling
echo $after_widget;
}
// Widget setup
public function form( $instance ) {
?>
<p>
<label>Graph Title<br />
<input class="widefat" id="<?php echo $this->get_field_id( 'graph_title' ); ?>" name="<?php echo $this->get_field_name( 'graph_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['graph_title'] ); ?>" />
</label>
</p>
<p>
<label>Graphite Server URL<br />
<input class="widefat" id="<?php echo $this->get_field_id( 'graph_server_url' ); ?>" name="<?php echo $this->get_field_name( 'graph_server_url' ); ?>" type="text" value="<?php echo esc_url( $instance['graph_server_url'] ); ?>" />
</label>
</p>
<p>
<label>Display Metrics<br />
<input class="widefat" id="<?php echo $this->get_field_id( 'graph_metrics' ); ?>" name="<?php echo $this->get_field_name( 'graph_metrics' ); ?>" type="text" value="<?php echo esc_attr( $instance['graph_metrics'] ); ?>" />
</label>
</p>
<p>
<label>Duration<br />
<select id="<?php echo $this->get_field_id( 'graph_duration' ); ?>" name="<?php echo $this->get_field_name( 'graph_duration' ); ?>">
<?php if ( 0 !== intval( $instance['graph_duration'] ) ) : ?><option value="<?php echo intval( $instance['graph_duration'] ); ?>"><?php echo intval( $instance['graph_duration'] ); ?></option><?php endif; ?>
<option value="-1">-1</option>
<option value="-2">-2</option>
<option value="-4">-4</option>
<option value="-8">-8</option>
<option value="-12">-12</option>
<option value="-24">-24</option>
</select> hours
</p>
<p>
<label>Color Scheme<br />
<select id="<?php echo $this->get_field_id( 'graph_colorscheme' ); ?>" name="<?php echo $this->get_field_name( 'graph_colorscheme' ); ?>">
<option value="<?php echo esc_attr( $instance['graph_colorscheme'] ); ?>"><?php echo esc_attr( $instance['graph_colorscheme'] ); ?></option>
<option value="Dark">Dark</option>
<option value="Light">Light</option>
<option value="Aqua">Aqua</option>
<option value="Fire">Fire</option>
</select>
</label>
</p>
<p>
<label>
<input type="checkbox" id="<?php echo $this->get_field_id( 'graph_private' ); ?>" name="<?php echo $this->get_field_name( 'graph_private' ); ?>" <?php checked( $instance['graph_private'] ); ?> />
Make this graph private
</label>
</p>
<?php
}
// Sanitize function
public function update( $new_instance ) {
$safe_instance = array();
// Sanitize our values
$safe_instance['graph_title'] = ( ! empty( $new_instance['graph_title'] ) ) ? sanitize_text_field( $new_instance['graph_title'] ) : '';
$safe_instance['graph_server_url'] = ( ! empty( $new_instance['graph_server_url'] ) ) ? sanitize_text_field( $new_instance['graph_server_url'] ) : '';
$safe_instance['graph_metrics'] = ( ! empty( $new_instance['graph_metrics'] ) ) ? sanitize_text_field( $new_instance['graph_metrics'] ) : '';
$safe_instance['graph_duration'] = ( ! empty( $new_instance['graph_duration'] ) ) ? intval( $new_instance['graph_duration'] ) : '';
// Make sure our color scheme is whitelisted
$allowed_colors = array( 'Light', 'Dark', 'Aqua', 'Fire' );
if ( in_array( $new_instance['graph_colorscheme'], $allowed_colors ) )
$safe_instance['graph_colorscheme'] = $new_instance['graph_colorscheme'];
$safe_instance['graph_private'] = ( isset( $new_instance['graph_private'] ) ) ? true : false;
return $safe_instance;
}
}
// Get an option from our array
function wpgg_get_option( $option_key ) {
$wpgg_options = get_option( 'wpgg_options' );
return $wpgg_options[$option_key];
}
// Save an option to our array
function wpgg_update_option( $option_key, $option_value ) {
$wpgg_options = get_option( 'wpgg_options' );
$wpgg_options[$option_key] = $option_value;
update_option( 'wpgg_options', $wpgg_options );
}
// Let the fun and games begin ...
WP_Graphite_Graphs::init();
// omit