forked from radgeek/feedwordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics-page.php
432 lines (379 loc) · 16.4 KB
/
diagnostics-page.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
<?php
require_once(dirname(__FILE__) . '/admin-ui.php');
class FeedWordPressDiagnosticsPage extends FeedWordPressAdminPage {
public function __construct() {
// Set meta-box context name
parent::__construct('feedwordpressdiagnosticspage');
$this->dispatch = 'feedwordpress_diagnostics';
$this->filename = __FILE__;
$this->test_html = array();
add_action('feedwordpress_diagnostics_do_http_test', array($this, 'do_http_test'), 10, 1);
}
function has_link () { return false; }
function display () {
if (FeedWordPress::needs_upgrade()) :
fwp_upgrade_page();
return;
endif;
// If this is a POST, validate source and user credentials
FeedWordPressCompatibility::validate_http_request(/*action=*/ 'feedwordpress_diagnostics', /*capability=*/ 'manage_options');
if (strtoupper($_SERVER['REQUEST_METHOD'])=='POST') :
$this->accept_POST($_POST);
do_action('feedwordpress_admin_page_diagnostics_save', $_POST, $this);
endif;
////////////////////////////////////////////////
// Prepare settings page ///////////////////////
////////////////////////////////////////////////
$this->display_update_notice_if_updated('Diagnostics');
$this->open_sheet('FeedWordPress Diagnostics');
?>
<div id="post-body">
<?php
$boxes_by_methods = array(
'info_box' => __('Diagnostic Information'),
'diagnostics_box' => __('Display Diagnostics'),
'updates_box' => __('Updates'),
'tests_box' => __('Diagnostic Tests'),
);
foreach ($boxes_by_methods as $method => $title) :
add_meta_box(
/*id=*/ 'feedwordpress_'.$method,
/*title=*/ $title,
/*callback=*/ array('FeedWordPressDiagnosticsPage', $method),
/*page=*/ $this->meta_box_context(),
/*context=*/ $this->meta_box_context()
);
endforeach;
do_action('feedwordpress_admin_page_diagnostics_meta_boxes', $this);
?>
<div class="metabox-holder">
<?php
do_meta_boxes($this->meta_box_context(), $this->meta_box_context(), $this);
?>
</div> <!-- class="metabox-holder" -->
</div> <!-- id="post-body" -->
<?php
$this->close_sheet();
} /* FeedWordPressDiagnosticsPage::display () */
function accept_POST ($post) {
if (isset($post['submit'])
or isset($post['save'])
or isset($post['feedwordpress_diagnostics_do'])) :
update_option('feedwordpress_debug', $post['feedwordpress_debug']);
update_option('feedwordpress_secret_key', $post['feedwordpress_secret_key']);
if (!isset($post['diagnostics_output'])
or !is_array($post['diagnostics_output'])) :
$post['diagnostics_output'] = array();
endif;
update_option('feedwordpress_diagnostics_output', $post['diagnostics_output']);
if (!isset($post['diagnostics_show'])
or !is_array($post['diagnostics_show'])) :
$post['diagnostics_show'] = array();
endif;
update_option('feedwordpress_diagnostics_show', $post['diagnostics_show']);
if ($post['diagnostics_show']
and in_array('updated_feeds:errors:persistent', $post['diagnostics_show'])) :
update_option('feedwordpress_diagnostics_persistent_errors_hours', (int) $post['diagnostics_persistent_error_hours']);
else :
delete_option('feedwordpress_diagnostics_persistent_errors_hours');
endif;
if (in_array('email', $post['diagnostics_output'])) :
$ded = $post['diagnostics_email_destination'];
if ('mailto'==$ded) :
$ded .= ':'.$post['diagnostics_email_destination_address'];
endif;
update_option('feedwordpress_diagnostics_email_destination', $ded);
else :
delete_option('feedwordpress_diagnostics_email_destination');
endif;
if (isset($post['feedwordpress_diagnostics_do'])) :
foreach ($post['feedwordpress_diagnostics_do'] as $do => $value) :
do_action('feedwordpress_diagnostics_do_'.$do, $post);
endforeach;
endif;
$this->updated = true; // Default update message
endif;
} /* FeedWordPressDiagnosticsPage::accept_POST () */
static function info_box ($page, $box = NULL) {
global $feedwordpress;
global $wp_version;
$link_category_id = FeedWordPress::link_category_id();
?>
<table class="edit-form narrow">
<thead style="display: none">
<th scope="col">Topic</th>
<th scope="col">Information</th>
</thead>
<tbody>
<tr>
<th scope="row">Version:</th>
<td>You are using FeedWordPress version <strong><?php print FEEDWORDPRESS_VERSION; ?></strong>.</td>
</tr>
<tr>
<th scope="row">Hosting Environment:</th>
<td><ul style="margin-top: 0; padding-top: 0;">
<li><em>WordPress:</em> version <?php print $wp_version; ?></li>
<?php if (function_exists('phpversion')) : ?>
<li><em>PHP:</em> version <?php print phpversion(); ?></li>
<?php endif; ?>
<?php if (function_exists('apache_get_version')) : ?>
<li><sem>Web Server:</em> <?php print apache_get_version(); ?></li>
<?php endif; ?>
</ul>
</td>
</tr>
<tr>
<th scope="row">Link Category:</th>
<td><?php if (!is_wp_error($link_category_id)) :
$term = get_term($link_category_id, 'link_category');
?><p>Syndicated feeds are
kept in link category #<?php print $term->term_id; ?>, <strong><?php print $term->name; ?></strong>.</p>
<?php else : ?>
<p><strong>FeedWordPress has been unable to set up a valid Link Category
for syndicated feeds.</strong> Attempting to set one up returned an
<code><?php $link_category_id->get_error_code(); ?></code> error with this
additional data:</p>
<table>
<tbody>
<tr>
<th scope="row">Message:</th>
<td><?php print $link_category_id->get_error_message(); ?></td>
</tr>
<?php $data = $link_category_id->get_error_data(); if (!empty($data)) : ?>
<tr>
<th scope="row">Auxiliary Data:</th>
<td><pre><?php print esc_html(MyPHP::val($link_category_id->get_error_data())); ?></pre></td>
</tr>
<?php endif; ?>
</table>
<?php endif; ?></td>
</tr>
<tr>
<th scope="row"><?php _e('Secret Key:'); ?></th>
<td><input type="text" name="feedwordpress_secret_key" value="<?php print esc_attr($feedwordpress->secret_key()); ?>" />
<p class="setting-description">This is used to control access to some diagnostic testing functions. You can change it to any string you want,
but only tell it to people you trust to help you troubleshoot your
FeedWordPress installation. Keep it secret—keep it safe.</p></td>
</tr>
</table>
<?php
} /* FeedWordPressDiagnosticsPage::info_box () */
static function diagnostics_box ($page, $box = NULL) {
$settings = array();
$settings['debug'] = (get_option('feedwordpress_debug')=='yes');
$diagnostics_output = get_option('feedwordpress_diagnostics_output', array());
$users = fwp_author_list();
$ded = get_option('feedwordpress_diagnostics_email_destination', 'admins');
if (preg_match('/^mailto:(.*)$/', $ded, $ref)) :
$ded_addy = $ref[1];
else :
$ded_addy = NULL;
endif;
// Hey ho, let's go...
?>
<table class="edit-form">
<tr style="vertical-align: top">
<th scope="row">Debugging mode:</th>
<td><select name="feedwordpress_debug" size="1">
<option value="yes"<?php echo ($settings['debug'] ? ' selected="selected"' : ''); ?>>on</option>
<option value="no"<?php echo ($settings['debug'] ? '' : ' selected="selected"'); ?>>off</option>
</select>
<p>When debugging mode is <strong>ON</strong>, FeedWordPress displays many
diagnostic error messages, warnings, and notices that are ordinarily suppressed,
and turns off all caching of feeds. Use with caution: this setting is useful for
testing but absolutely inappropriate for a production server.</p>
</td>
</tr>
<tr>
<th scope="row">Diagnostics output:</th>
<td><ul class="options">
<li><input type="checkbox" name="diagnostics_output[]" value="error_log" <?php print (in_array('error_log', $diagnostics_output) ? ' checked="checked"' : ''); ?> /> Log in PHP error logs</label></li>
<li><input type="checkbox" name="diagnostics_output[]" value="admin_footer" <?php print (in_array('admin_footer', $diagnostics_output) ? ' checked="checked"' : ''); ?> /> Display in WordPress admin footer</label></li>
<li><input type="checkbox" name="diagnostics_output[]" value="echo" <?php print (in_array('echo', $diagnostics_output) ? ' checked="checked"' : ''); ?> /> Echo in web browser as they are issued</label></li>
<li><input type="checkbox" name="diagnostics_output[]" value="echo_in_cronjob" <?php print (in_array('echo_in_cronjob', $diagnostics_output) ? ' checked="checked"' : ''); ?> /> Echo to output when they are issued during an update cron job</label></li>
<li><input type="checkbox" name="diagnostics_output[]" value="email" <?php print (in_array('email', $diagnostics_output) ? ' checked="checked"' : ''); ?> /> Send a daily email digest to:</label> <select name="diagnostics_email_destination" id="diagnostics-email-destination" size="1">
<option value="admins"<?php if ('admins'==$ded) : ?> selected="selected"<?php endif; ?>>the site administrators</option>
<?php foreach ($users as $id => $name) : ?>
<option value="user:<?php print (int) $id; ?>"<?php if (sprintf('user:%d', (int) $id)==$ded) : ?> selected="selected"<?php endif; ?>><?php print esc_html($name); ?></option>
<?php endforeach; ?>
<option value="mailto"<?php if (!is_null($ded_addy)) : ?> selected="selected"<?php endif; ?>>another e-mail address...</option>
</select>
<input type="email" id="diagnostics-email-destination-address" name="diagnostics_email_destination_address" value="<?php print $ded_addy; ?>" placeholder="email address" /></li>
</ul></td>
</tr>
</table>
<script type="text/javascript">
contextual_appearance(
'diagnostics-email-destination',
'diagnostics-email-destination-address',
'diagnostics-email-destination-default',
'mailto',
'inline'
);
jQuery('#diagnostics-email-destination').change ( function () {
contextual_appearance(
'diagnostics-email-destination',
'diagnostics-email-destination-address',
'diagnostics-email-destination-default',
'mailto',
'inline'
);
} );
</script>
<?php
} /* FeedWordPressDiagnosticsPage::diagnostics_box () */
static function updates_box ($page, $box = NULL) {
$hours = get_option('feedwordpress_diagnostics_persistent_errors_hours', 2);
$fields = apply_filters('feedwordpress_diagnostics', array(
'Update Diagnostics' => array(
'update_schedule:check' => 'whenever a FeedWordPress checks in on the update schedule',
'updated_feeds' => 'as each feed is checked for updates',
'updated_feeds:errors:persistent' => 'when attempts to update a feed have resulted in errors</label> <label>for at least <input type="number" min="1" max="360" step="1" name="diagnostics_persistent_error_hours" value="'.$hours.'" /> hours',
'updated_feeds:errors' => 'any time FeedWordPress encounters any errors while checking a feed for updates',
'updated_feeds:http' => "displaying the raw HTTP data passed to and from the feed being checked for updates",
'syndicated_posts' => 'as each syndicated post is added to the database',
'feed_items' => 'as each syndicated item is considered on the feed',
'memory_usage' => 'indicating how much memory was used',
),
'Syndicated Post Details' => array(
'feed_items:freshness' => 'as FeedWordPress decides whether to treat an item as a new post, an update, or a duplicate of an existing post',
'feed_items:rejected' => 'when FeedWordPress rejects a post without syndicating it',
'syndicated_posts:categories' => 'as categories, tags, and other terms are added on the post',
'syndicated_posts:meta_data' => 'as syndication meta-data is added on the post',
),
'Advanced Diagnostics' => array(
'feed_items:freshness:reasons' => 'explaining the reason that a post was treated as an update to an existing post',
'feed_items:freshness:sql' => 'when FeedWordPress issues the SQL query it uses to decide whether to treat items as new, updates, or duplicates',
'syndicated_posts:categories:test' => 'as FeedWordPress checks for the familiarity of feed categories and tags',
'syndicated_posts:static_meta_data' => 'providing meta-data about syndicated posts in the Edit Posts interface',
),
), $page);
foreach ($fields as $section => $items) :
foreach ($items as $key => $label) :
$checked[$key] = '';
endforeach;
endforeach;
$diagnostics_show = get_option('feedwordpress_diagnostics_show', array());
if (is_array($diagnostics_show)) : foreach ($diagnostics_show as $thingy) :
$checked[$thingy] = ' checked="checked"';
endforeach; endif;
// Hey ho, let's go...
?>
<table class="edit-form">
<?php foreach ($fields as $section => $ul) : ?>
<tr>
<th scope="row"><?php print esc_html($section); ?>:</th>
<td><p>Show a diagnostic message...</p>
<ul class="options">
<?php foreach ($ul as $key => $label) : ?>
<li><label><input
type="checkbox" name="diagnostics_show[]"
value="<?php print esc_html($key); ?>"
<?php print $checked[$key]; ?> />
<?php print $label; ?></label></li>
<?php endforeach; ?>
</ul></td>
</tr>
<?php endforeach; ?>
</table>
<?php
} /* FeedWordPressDiagnosticsPage::updates_box () */
static function tests_box ($page, $box = NULL) {
?>
<script type="text/javascript">
function clone_http_test_args_keyvalue_prototype () {
var next = jQuery('#http-test-args').find('.http-test-args-keyvalue').length;
var newRow = jQuery('#http-test-args-keyvalue-prototype').clone().attr('id', 'http-test-args-keyvalue-' + next);
newRow.find('.http_test_args_key').attr('name', 'http_test_args_key['+next+']').val('');
newRow.find('.http_test_args_value').attr('name', 'http_test_args_value['+next+']').val('');
newRow.appendTo('#http-test-args');
return false;
}
</script>
<table class="edit-form">
<tr>
<th scope="row">HTTP:</th>
<td><div><input type="url" name="http_test_url" value="" placeholder="http://www.example.com/" size="127" style="width: 80%; max-width: 80.0em;" />
<input type="submit" name="feedwordpress_diagnostics_do[http_test]" value="Test »" /></div>
<div><select name="http_test_method" size="1">
<option value="wp_remote_request">wp_remote_request</option>
<option value="FeedWordPress_File">FeedWordPress_File</option>
</select></div>
<table>
<tr>
<td>
<div id="http-test-args">
<div id="http-test-args-keyvalue-prototype" class="http-test-args-keyvalue"><label>Args:
<input type="text" class='http_test_args_key' name="http_test_args_key[0]" value="" placeholder="key" /></label>
<label>= <input type="text" class='http_test_args_value' name="http_test_args_value[0]" value="" placeholder="value" /></label>
</div>
</div>
</td>
<td><a href="#http-test-args" onclick="return clone_http_test_args_keyvalue_prototype();">+ Add</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<th>XPath:</th>
<td><div><input type="text" name="http_test_xpath" value="" placeholder="xpath-like query" /></div>
<div><p>Leave blank to test HTTP, fill in to test a query.</p></div>
</td>
</tr>
<?php if (isset($page->test_html['http_test'])) : ?>
<tr>
<th scope="row">RESULTS:</th>
<td>
<div>URL: <code><?php print esc_html($page->test_html['url']); ?></code></div>
<div style="position: relative">
<div style="width: 100%; overflow: scroll; background-color: #eed">
<pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -o-pre-wrap;"><?php print $page->test_html['http_test']; ?></pre>
</div>
</div>
</td>
</tr>
<?php endif; ?>
</table>
<?php
} /* FeedWordPressDiagnosticsPage::tests_box () */
private $test_html;
public function do_http_test ($post) {
if (isset($post['http_test_url']) and isset($post['http_test_method'])) :
$url = $post['http_test_url'];
$args = array();
if (isset($post['http_test_args_key'])) :
foreach ($post['http_test_args_key'] as $idx => $name) :
$name = trim($name);
if (strlen($name) > 0) :
$value = NULL;
if (isset($post['http_test_args_value'])
and isset($post['http_test_args_value'][$idx])) :
$value = $post['http_test_args_value'][$idx];
endif;
if (preg_match('/^javascript:(.*)$/i', $value, $refs)) :
if (function_exists('json_decode')) :
$json_value = json_decode($refs[1]);
if (!is_null($json_value)) :
$value = $json_value;
endif;
endif;
endif;
$args[$name] = $value;
endif;
endforeach;
endif;
switch ($post['http_test_method']) :
case 'wp_remote_request' :
$out = wp_remote_request($url, $args);
break;
case 'FeedWordPress_File' :
$out = new FeedWordPress_File($url);
break;
endswitch;
$this->test_html['url'] = $url;
$this->test_html['http_test'] = esc_html(MyPHP::val($out));
endif;
} /* FeedWordPressDiagnosticsPage::do_http_test () */
} /* class FeedWordPressDiagnosticsPage */
$diagnosticsPage = new FeedWordPressDiagnosticsPage;
$diagnosticsPage->display();