-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrunscope-status.php
More file actions
executable file
·388 lines (307 loc) · 15.2 KB
/
runscope-status.php
File metadata and controls
executable file
·388 lines (307 loc) · 15.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
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
<?php
/*
Plugin Name: Status Page for Runscope
Plugin URI: https://github.com/yllus/status-page-for-runscope
Description: Display a pretty status page at a URL on your WordPress website, with all data pulled from a Runscope bucket.
Author: Sully Syed
Version: 1.0.1
Author URI: http://yllus.com/
*/
class RunscopeStatus {
/**
* A reference to an instance of this class.
*/
private static $instance;
/**
* The array of templates that this plugin tracks.
*/
protected $templates;
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new RunscopeStatus();
}
return self::$instance;
}
/**
* Initializes the plugin by setting filters and administration functions.
*/
private function __construct() {
$this->templates = array();
// Add a filter to the attributes metabox to inject template into the cache.
if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
// Handle WordPress 4.6 and older.
add_filter(
'page_attributes_dropdown_pages_args',
array( $this, 'register_page_template' )
);
}
else {
// Add a filter to WordPress v4.7 version attributes metabox.
add_filter(
'theme_page_templates', array( $this, 'add_new_template' )
);
}
// Add a filter to the save post to inject out template into the page cache
add_filter( 'wp_insert_post_data', array( $this, 'register_page_template' ) );
// Add a filter to the template include to determine if the page has our
// template assigned and return it's path
add_filter( 'template_include', array( $this, 'view_page_template' ) );
// Add your templates to this array.
$this->templates = array(
'page-RUNSCOPESTATUS.php' => 'Status Page for Runscope',
);
// If we're viewing our custom page template, enqueue a specific stylesheet.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
// Add our custom Runscope metabox for anyone editing a Page with this template set.
add_action( 'admin_init', array( $this, 'add_metaboxes' ) );
// Save data from our custom Runscope metabox.
add_action( 'save_post', array( $this, 'save_metabox_data' ), 10, 2 );
// Register a new AJAX responder to display a bucket's test results.
add_action( 'wp_ajax_runscope_display_test_results', array( $this, 'ajax_display_test_results' ) );
add_action( 'wp_ajax_nopriv_runscope_display_test_results', array( $this, 'ajax_display_test_results' ) );
}
public static function metabox_runscope_settings( $post ) {
$str_rsp_access_token = get_post_meta( $post->ID, 'rsp_access_token', true );
$str_rsp_bucket_key = get_post_meta( $post->ID, 'rsp_bucket_key', true );
$str_rsp_environment_name = get_post_meta( $post->ID, 'rsp_environment_name', true );
?>
<label><strong>Access Token</strong></label>
<br>
<input type="password" id="rsp_access_token" name="rsp_access_token" value="<?php echo $str_rsp_access_token; ?>" style="width: 80%;" />
<br>
If you don't already have an OAuth2 access token to use, visit <a target="_blank" href="https://www.runscope.com/applications">https://www.runscope.com/applications</a> and
create one. Once it's been created, grab the <strong>Personal Access Token > Access Token</strong> value and enter it into the field above.
<br><br>
<label><strong>Bucket Key</strong></label>
<br>
<input type="text" id="rsp_bucket_key" name="rsp_bucket_key" value="<?php echo $str_rsp_bucket_key; ?>" style="width: 80%;" />
<br>
Copy and paste in the Bucket Key value containing the Tests you wish to see displayed on this status page. You can grab the Bucket Key value by selecting
the Bucket you wish to use, clicking <strong>API Tests</strong>, then <strong>Bucket Settings</strong> and then looking at the URL. The Bucket Key is the
value found between radar/ and /configure (eg. <strong>r6jaa77r5ltx</strong> in the URL https://www.runscope.com/radar/r6jaa77r5ltx/configure ).
<br><br>
<label><strong>Environment Name</strong></label>
<br>
<input type="text" id="rsp_environment_name" name="rsp_environment_name" value="<?php echo $str_rsp_environment_name; ?>" style="width: 80%;" />
<br>
If you're running a test against multiple Environments and wish to only display results from a particular Environment, enter the <strong>Environment Name</strong>
above. If the field is left blank, the latest test result for each test will be displayed.
<br><br>
<strong>Note:</strong> Need additional status pages for each of your buckets? You can always create additional buckets in Runscope and then create a Page in WordPress to point to each of them!
<?php
}
/**
* ?
*
*/
public function add_metaboxes() {
if ( is_admin() && isset($_GET['post']) ) {
$post_id = $_GET['post'] ? (int) $_GET['post'] : 0;
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if ( $template_file == 'page-RUNSCOPESTATUS.php' ) {
add_meta_box('runscope_metabox_text_box', 'Status Page for Runscope Settings', array( $this, 'metabox_runscope_settings'), 'page', 'normal', 'high');
}
}
}
/**
* ?
*
*/
public static function save_metabox_data() {
global $post;
if ( !isset($_POST['rsp_access_token']) || !isset($_POST['rsp_bucket_key']) || !isset($_POST['rsp_environment_name']) ) {
return;
}
update_post_meta( $post->ID, 'rsp_access_token', $_POST['rsp_access_token'] );
update_post_meta( $post->ID, 'rsp_bucket_key', $_POST['rsp_bucket_key'] );
update_post_meta( $post->ID, 'rsp_environment_name', $_POST['rsp_environment_name'] );
}
/**
* Enqueue a bit of CSS only on our custom page template (and make sure jQuery is enqueued on the page).
*
*/
public function enqueue_styles() {
if ( is_page_template('page-RUNSCOPESTATUS.php') ) {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'moment', plugin_dir_url( __FILE__ ) . '/js/moment.min.js' );
wp_enqueue_script( 'livestamp', plugin_dir_url( __FILE__ ) . '/js/livestamp.min.js' );
wp_enqueue_style( 'open-sans', '//fonts.googleapis.com/css?family=Open+Sans' );
wp_enqueue_style( 'rsp-page-styles', plugin_dir_url( __FILE__ ) . '/runscope-status.css' );
}
}
/**
* Adds our template to the pages cache in order to trick WordPress
* into thinking the template file exists where it doens't really exist.
*
* This is for WordPress versions v4.6 and older.
*
*/
public function register_page_template( $atts ) {
// Create the key used for the themes cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
// Retrieve the cache list.
// If it doesn't exist, or it's empty prepare an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();
}
// New cache, therefore remove the old one
wp_cache_delete( $cache_key , 'themes');
// Now add our template to the list of templates by merging our templates
// with the existing templates array from the cache.
$templates = array_merge( $templates, $this->templates );
// Add the modified cache to allow WordPress to pick it up for listing
// available templates
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
/**
* Adds our template to the pages cache in order to trick WordPress
* into thinking the template file exists where it doens't really exist.
*
* This is for WordPress versions v4.7 and newer.
*
*/
public function add_new_template( $posts_templates ) {
$posts_templates = array_merge( $posts_templates, $this->templates );
return $posts_templates;
}
/**
* Checks if the template is assigned to the page
*/
public function view_page_template( $template ) {
global $post;
if ( is_null($post) ) {
return $template;
}
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( !isset($this->templates[$page_template] ) ) {
return $template;
}
$file = plugin_dir_path(__FILE__) . get_post_meta( $post->ID, '_wp_page_template', true );
// Just to be safe, we check if the file exist first
if ( file_exists( $file ) ) {
return $file;
}
else {
echo $file;
}
return $template;
}
public static function ajax_display_test_results() {
$post_id = $_GET['post_id'];
$obj_response_bucket_details = RunscopeStatus::get_bucket_details($post_id);
if ( $obj_response_bucket_details->rsp_success != '0' ) {
echo "Sorry, there was an issue retrieving your Runscope tests (" . $obj_response_bucket_details->rsp_message . ").";
exit;
}
$obj_response = RunscopeStatus::get_bucket_tests($post_id);
if ( $obj_response->rsp_success != '0' ) {
echo "Sorry, there was an issue retrieving your Runscope tests (" . $obj_response->rsp_message . ").";
exit;
}
$arr_test_results = array();
foreach ( $obj_response->data as $test ) {
$obj_response_test = RunscopeStatus::get_test_results($post_id, $test->id, $test->name, $test->last_run);
$arr_test_results[] = $obj_response_test;
}
RunscopeStatus::theme_test_results($obj_response_bucket_details, $arr_test_results);
exit;
}
public static function theme_test_results( $obj_response_bucket_details, $arr_test_results ) {
$str_test_results = '';
$arr_bucket_status = array('passed' => 0, 'total' => 0);
foreach ( $arr_test_results as $test_result ) {
if ( sizeof($test_result->data) > 0 ) {
// If all of our assertions passed, consider the Test passed.
if ( $test_result->data[0]->assertions_failed == 0 ) {
$arr_bucket_status['passed'] = $arr_bucket_status['passed'] + 1;
}
// Theme the Test results and add it to our output string.
ob_start();
require plugin_dir_path(__FILE__) . 'partials/partial.test-result.php';
$str_test_results = $str_test_results . ob_get_contents();
ob_end_clean();
$arr_bucket_status['total'] = $arr_bucket_status['total'] + 1;
}
}
// Theme the bucket details, placing it at the top of our output string.
ob_start();
require plugin_dir_path(__FILE__) . 'partials/partial.bucket-details.php';
$str_test_results = ob_get_contents() . $str_test_results;
ob_end_clean();
echo $str_test_results;
}
public static function get_test_results( $post_id, $test_id, $test_name, $last_run ) {
$str_rsp_bucket_key = get_post_meta( $post_id, 'rsp_bucket_key', true );
$str_rsp_environment_name = get_post_meta( $post_id, 'rsp_environment_name', true );
$str_url = 'https://api.runscope.com/buckets/' . $str_rsp_bucket_key . '/tests/' . $test_id . '/results';
$arr_test_results = RunscopeStatus::make_api_request( $post_id, $str_url, array('test_name' => $test_name, 'last_run' => $last_run) );
// If an Environment Name has been specified, filter the test results to those that match that environment.
if ( strlen($str_rsp_environment_name) > 0 ) {
$arr_data_temp = array();
for ( $i = 0; $i < sizeof($arr_test_results->data); $i++ ) {
if ( $arr_test_results->data[$i]->environment_name == $str_rsp_environment_name ) {
$arr_data_temp[] = $arr_test_results->data[$i];
}
}
$arr_test_results->data = $arr_data_temp;
}
return $arr_test_results;
}
public static function get_bucket_tests( $post_id ) {
$str_rsp_bucket_key = get_post_meta( $post_id, 'rsp_bucket_key', true );
$str_url = 'https://api.runscope.com/buckets/' . $str_rsp_bucket_key . '/tests?count=1000';
return RunscopeStatus::make_api_request( $post_id, $str_url );
}
public static function get_bucket_details( $post_id ) {
$str_rsp_bucket_key = get_post_meta( $post_id, 'rsp_bucket_key', true );
$str_url = 'https://api.runscope.com/buckets/' . $str_rsp_bucket_key;
return RunscopeStatus::make_api_request( $post_id, $str_url );
}
public static function make_api_request( $post_id, $str_url, $additional_params = array() ) {
$cache_key = 'rsp-api-request-' . $post_id . '-' . md5($str_url . serialize($additional_params));
if ( false === ( $obj_response = get_transient( $cache_key ) ) ) {
$str_rsp_access_token = get_post_meta( $post_id, 'rsp_access_token', true );
$args = $args = array(
'method' => 'GET',
'timeout' => 10,
'sslverify' => false,
'headers' => array(
'Authorization' => 'Bearer ' . $str_rsp_access_token,
)
);
$response = wp_remote_request( $str_url, $args );
if ( is_wp_error($response) ) {
$response_message = $response->get_error_message();
$obj_response = new stdClass();
$obj_response->rsp_success = '-1';
$obj_response->rsp_message = $response_message;
return $obj_response;
}
$response_code = wp_remote_retrieve_response_code( $response );
if ( false === strstr( $response_code, '200' ) ) {
$response_message = wp_remote_retrieve_response_message( $response );
$obj_response = new stdClass();
$obj_response->rsp_success = '-2';
$obj_response->rsp_message = $response_message;
return $obj_response;
}
$obj_response = json_decode( wp_remote_retrieve_body( $response ) );
$obj_response->rsp_success = '0';
$obj_response->rsp_message = 'API successfully contacted and bucket tests retrieved.';
if ( sizeof($additional_params) > 0 ) {
foreach ( $additional_params as $key => $val ) {
$obj_response->$key = $val;
}
}
set_transient($cache_key, $obj_response, 60); // Cache for 1 minute.
}
return $obj_response;
}
}
add_action( 'plugins_loaded', array( 'RunscopeStatus', 'get_instance' ) );
?>