| 1 |
<?php |
| 2 |
require_once(dirname(__FILE__) . '/admin-ui.php'); |
| 3 |
|
| 4 |
class FeedWordPressDiagnosticsPage extends FeedWordPressAdminPage { |
| 5 |
function FeedWordPressDiagnosticsPage () { |
| 6 |
// Set meta-box context name |
| 7 |
FeedWordPressAdminPage::FeedWordPressAdminPage('feedwordpressdiagnosticspage'); |
| 8 |
$this->dispatch = 'feedwordpress_diagnostics'; |
| 9 |
$this->filename = __FILE__; |
| 10 |
|
| 11 |
$this->test_html = array(); |
| 12 |
add_action('feedwordpress_diagnostics_do_http_test', array($this, 'do_http_test'), 10, 1); |
| 13 |
} |
| 14 |
|
| 15 |
function has_link () { return false; } |
| 16 |
|
| 17 |
function display () { |
| 18 |
global $wpdb, $wp_db_version, $fwp_path; |
| 19 |
global $fwp_post; |
| 20 |
|
| 21 |
if (FeedWordPress::needs_upgrade()) : |
| 22 |
fwp_upgrade_page(); |
| 23 |
return; |
| 24 |
endif; |
| 25 |
|
| 26 |
// If this is a POST, validate source and user credentials |
| 27 |
FeedWordPressCompatibility::validate_http_request(/*action=*/ 'feedwordpress_diagnostics', /*capability=*/ 'manage_options'); |
| 28 |
|
| 29 |
if (strtoupper($_SERVER['REQUEST_METHOD'])=='POST') : |
| 30 |
$this->accept_POST($fwp_post); |
| 31 |
do_action('feedwordpress_admin_page_diagnostics_save', $fwp_post, $this); |
| 32 |
endif; |
| 33 |
|
| 34 |
//////////////////////////////////////////////// |
| 35 |
// Prepare settings page /////////////////////// |
| 36 |
//////////////////////////////////////////////// |
| 37 |
|
| 38 |
$this->display_update_notice_if_updated('Diagnostics'); |
| 39 |
|
| 40 |
$this->open_sheet('FeedWordPress Diagnostics'); |
| 41 |
?> |
| 42 |
<div id="post-body"> |
| 43 |
<?php |
| 44 |
$boxes_by_methods = array( |
| 45 |
'info_box' => __('Diagnostic Information'), |
| 46 |
'diagnostics_box' => __('Display Diagnostics'), |
| 47 |
'updates_box' => __('Updates'), |
| 48 |
'tests_box' => __('Diagnostic Tests'), |
| 49 |
); |
| 50 |
|
| 51 |
foreach ($boxes_by_methods as $method => $title) : |
| 52 |
add_meta_box( |
| 53 |
/*id=*/ 'feedwordpress_'.$method, |
| 54 |
/*title=*/ $title, |
| 55 |
/*callback=*/ array('FeedWordPressDiagnosticsPage', $method), |
| 56 |
/*page=*/ $this->meta_box_context(), |
| 57 |
/*context=*/ $this->meta_box_context() |
| 58 |
); |
| 59 |
endforeach; |
| 60 |
do_action('feedwordpress_admin_page_diagnostics_meta_boxes', $this); |
| 61 |
?> |
| 62 |
<div class="metabox-holder"> |
| 63 |
<?php |
| 64 |
fwp_do_meta_boxes($this->meta_box_context(), $this->meta_box_context(), $this); |
| 65 |
?> |
| 66 |
</div> <!-- class="metabox-holder" --> |
| 67 |
</div> <!-- id="post-body" --> |
| 68 |
|
| 69 |
<?php |
| 70 |
$this->close_sheet(); |
| 71 |
} /* FeedWordPressDiagnosticsPage::display () */ |
| 72 |
|
| 73 |
function accept_POST ($post) { |
| 74 |
if (isset($post['submit']) |
| 75 |
or isset($post['save']) |
| 76 |
or isset($post['feedwordpress_diagnostics_do'])) : |
| 77 |
update_option('feedwordpress_debug', $post['feedwordpress_debug']); |
| 78 |
update_option('feedwordpress_secret_key', $post['feedwordpress_secret_key']); |
| 79 |
|
| 80 |
if (!isset($post['diagnostics_output']) |
| 81 |
or !is_array($post['diagnostics_output'])) : |
| 82 |
$post['diagnostics_output'] = array(); |
| 83 |
endif; |
| 84 |
update_option('feedwordpress_diagnostics_output', $post['diagnostics_output']); |
| 85 |
|
| 86 |
if (!isset($post['diagnostics_show']) |
| 87 |
or !is_array($post['diagnostics_show'])) : |
| 88 |
$post['diagnostics_show'] = array(); |
| 89 |
endif; |
| 90 |
update_option('feedwordpress_diagnostics_show', $post['diagnostics_show']); |
| 91 |
|
| 92 |
if ($post['diagnostics_show'] |
| 93 |
and in_array('updated_feeds:errors:persistent', $post['diagnostics_show'])) : |
| 94 |
update_option('feedwordpress_diagnostics_persistent_errors_hours', (int) $post['diagnostics_persistent_error_hours']); |
| 95 |
else : |
| 96 |
delete_option('feedwordpress_diagnostics_persistent_errors_hours'); |
| 97 |
endif; |
| 98 |
|
| 99 |
if (in_array('email', $post['diagnostics_output'])) : |
| 100 |
$ded = $post['diagnostics_email_destination']; |
| 101 |
if ('mailto'==$ded) : |
| 102 |
$ded .= ':'.$post['diagnostics_email_destination_address']; |
| 103 |
endif; |
| 104 |
|
| 105 |
update_option('feedwordpress_diagnostics_email_destination', $ded); |
| 106 |
else : |
| 107 |
delete_option('feedwordpress_diagnostics_email_destination'); |
| 108 |
endif; |
| 109 |
|
| 110 |
if (isset($post['feedwordpress_diagnostics_do'])) : |
| 111 |
foreach ($post['feedwordpress_diagnostics_do'] as $do => $value) : |
| 112 |
do_action('feedwordpress_diagnostics_do_'.$do, $post); |
| 113 |
endforeach; |
| 114 |
endif; |
| 115 |
|
| 116 |
$this->updated = true; // Default update message |
| 117 |
endif; |
| 118 |
} /* FeedWordPressDiagnosticsPage::accept_POST () */ |
| 119 |
|
| 120 |
static function info_box ($page, $box = NULL) { |
| 121 |
global $feedwordpress; |
| 122 |
global $wp_version; |
| 123 |
$link_category_id = FeedWordPress::link_category_id(); |
| 124 |
?> |
| 125 |
<table class="edit-form narrow"> |
| 126 |
<thead style="display: none"> |
| 127 |
<th scope="col">Topic</th> |
| 128 |
<th scope="col">Information</th> |
| 129 |
</thead> |
| 130 |
|
| 131 |
<tbody> |
| 132 |
<tr> |
| 133 |
<th scope="row">Version:</th> |
| 134 |
<td>You are using FeedWordPress version <strong><?php print FEEDWORDPRESS_VERSION; ?></strong>.</td> |
| 135 |
</tr> |
| 136 |
|
| 137 |
<tr> |
| 138 |
<th scope="row">Hosting Environment:</th> |
| 139 |
<td><ul style="margin-top: 0; padding-top: 0;"> |
| 140 |
<li><em>WordPress:</em> version <?php print $wp_version; ?></li> |
| 141 |
<?php if (function_exists('phpversion')) : ?> |
| 142 |
<li><em>PHP:</em> version <?php print phpversion(); ?></li> |
| 143 |
<?php endif; ?> |
| 144 |
<?php if (function_exists('apache_get_version')) : ?> |
| 145 |
<li><sem>Web Server:</em> <?php print apache_get_version(); ?></li> |
| 146 |
<?php endif; ?> |
| 147 |
</ul> |
| 148 |
</td> |
| 149 |
</tr> |
| 150 |
|
| 151 |
<tr> |
| 152 |
<th scope="row">Link Category:</th> |
| 153 |
<td><?php if (!is_wp_error($link_category_id)) : |
| 154 |
$term = get_term($link_category_id, 'link_category'); |
| 155 |
?><p>Syndicated feeds are |
| 156 |
kept in link category #<?php print $term->term_id; ?>, <strong><?php print $term->name; ?></strong>.</p> |
| 157 |
<?php else : ?> |
| 158 |
<p><strong>FeedWordPress has been unable to set up a valid Link Category |
| 159 |
for syndicated feeds.</strong> Attempting to set one up returned an |
| 160 |
<code><?php $link_category_id->get_error_code(); ?></code> error with this |
| 161 |
additional data:</p> |
| 162 |
<table> |
| 163 |
<tbody> |
| 164 |
<tr> |
| 165 |
<th scope="row">Message:</th> |
| 166 |
<td><?php print $link_category_id->get_error_message(); ?></td> |
| 167 |
</tr> |
| 168 |
<?php $data = $link_category_id->get_error_data(); if (!empty($data)) : ?> |
| 169 |
<tr> |
| 170 |
<th scope="row">Auxiliary Data:</th> |
| 171 |
<td><pre><?php print esc_html(MyPHP::val($link_category_id->get_error_data())); ?></pre></td> |
| 172 |
</tr> |
| 173 |
<?php endif; ?> |
| 174 |
</table> |
| 175 |
<?php endif; ?></td> |
| 176 |
</tr> |
| 177 |
|
| 178 |
<tr> |
| 179 |
<th scope="row"><?php _e('Secret Key:'); ?></th> |
| 180 |
<td><input type="text" name="feedwordpress_secret_key" value="<?php print esc_attr($feedwordpress->secret_key()); ?>" /> |
| 181 |
<p class="setting-description">This is used to control access to some diagnostic testing functions. You can change it to any string you want, |
| 182 |
but only tell it to people you trust to help you troubleshoot your |
| 183 |
FeedWordPress installation. Keep it secret—keep it safe.</p></td> |
| 184 |
</tr> |
| 185 |
</table> |
| 186 |
|
| 187 |
<?php |
| 188 |
} /* FeedWordPressDiagnosticsPage::info_box () */ |
| 189 |
|
| 190 |
static function diagnostics_box ($page, $box = NULL) { |
| 191 |
$settings = array(); |
| 192 |
$settings['debug'] = (get_option('feedwordpress_debug')=='yes'); |
| 193 |
|
| 194 |
$diagnostics_output = get_option('feedwordpress_diagnostics_output', array()); |
| 195 |
|
| 196 |
$users = fwp_author_list(); |
| 197 |
|
| 198 |
$ded = get_option('feedwordpress_diagnostics_email_destination', 'admins'); |
| 199 |
|
| 200 |
if (preg_match('/^mailto:(.*)$/', $ded, $ref)) : |
| 201 |
$ded_addy = $ref[1]; |
| 202 |
else : |
| 203 |
$ded_addy = NULL; |
| 204 |
endif; |
| 205 |
|
| 206 |
// Hey ho, let's go... |
| 207 |
?> |
| 208 |
<table class="edit-form"> |
| 209 |
<tr style="vertical-align: top"> |
| 210 |
<th scope="row">Debugging mode:</th> |
| 211 |
<td><select name="feedwordpress_debug" size="1"> |
| 212 |
<option value="yes"<?php echo ($settings['debug'] ? ' selected="selected"' : ''); ?>>on</option> |
| 213 |
<option value="no"<?php echo ($settings['debug'] ? '' : ' selected="selected"'); ?>>off</option> |
| 214 |
</select> |
| 215 |
|
| 216 |
<p>When debugging mode is <strong>ON</strong>, FeedWordPress displays many |
| 217 |
diagnostic error messages, warnings, and notices that are ordinarily suppressed, |
| 218 |
and turns off all caching of feeds. Use with caution: this setting is useful for |
| 219 |
testing but absolutely inappropriate for a production server.</p> |
| 220 |
|
| 221 |
</td> |
| 222 |
</tr> |
| 223 |
<tr> |
| 224 |
<th scope="row">Diagnostics output:</th> |
| 225 |
<td><ul class="options"> |
| 226 |
<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> |
| 227 |
<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> |
| 228 |
<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> |
| 229 |
<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> |
| 230 |
<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"> |
| 231 |
<option value="admins"<?php if ('admins'==$ded) : ?> selected="selected"<?php endif; ?>>the site administrators</option> |
| 232 |
<?php foreach ($users as $id => $name) : ?> |
| 233 |
<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> |
| 234 |
<?php endforeach; ?> |
| 235 |
<option value="mailto"<?php if (!is_null($ded_addy)) : ?> selected="selected"<?php endif; ?>>another e-mail address...</option> |
| 236 |
</select> |
| 237 |
<input type="email" id="diagnostics-email-destination-address" name="diagnostics_email_destination_address" value="<?php print $ded_addy; ?>" placeholder="email address" /></li> |
| 238 |
</ul></td> |
| 239 |
</tr> |
| 240 |
</table> |
| 241 |
|
| 242 |
<script type="text/javascript"> |
| 243 |
contextual_appearance( |
| 244 |
'diagnostics-email-destination', |
| 245 |
'diagnostics-email-destination-address', |
| 246 |
'diagnostics-email-destination-default', |
| 247 |
'mailto', |
| 248 |
'inline' |
| 249 |
); |
| 250 |
jQuery('#diagnostics-email-destination').change ( function () { |
| 251 |
contextual_appearance( |
| 252 |
'diagnostics-email-destination', |
| 253 |
'diagnostics-email-destination-address', |
| 254 |
'diagnostics-email-destination-default', |
| 255 |
'mailto', |
| 256 |
'inline' |
| 257 |
); |
| 258 |
} ); |
| 259 |
</script> |
| 260 |
<?php |
| 261 |
} /* FeedWordPressDiagnosticsPage::diagnostics_box () */ |
| 262 |
|
| 263 |
static function updates_box ($page, $box = NULL) { |
| 264 |
$hours = get_option('feedwordpress_diagnostics_persistent_errors_hours', 2); |
| 265 |
$fields = apply_filters('feedwordpress_diagnostics', array( |
| 266 |
'Update Diagnostics' => array( |
| 267 |
'update_schedule:check' => 'whenever a FeedWordPress checks in on the update schedule', |
| 268 |
'updated_feeds' => 'as each feed is checked for updates', |
| 269 |
'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', |
| 270 |
'updated_feeds:errors' => 'any time FeedWordPress encounters any errors while checking a feed for updates', |
| 271 |
'updated_feeds:http' => "displaying the raw HTTP data passed to and from the feed being checked for updates", |
| 272 |
'syndicated_posts' => 'as each syndicated post is added to the database', |
| 273 |
'feed_items' => 'as each syndicated item is considered on the feed', |
| 274 |
'memory_usage' => 'indicating how much memory was used', |
| 275 |
), |
| 276 |
'Syndicated Post Details' => array( |
| 277 |
'feed_items:freshness' => 'as FeedWordPress decides whether to treat an item as a new post, an update, or a duplicate of an existing post', |
| 278 |
'feed_items:rejected' => 'when FeedWordPress rejects a post without syndicating it', |
| 279 |
'syndicated_posts:categories' => 'as categories, tags, and other terms are added on the post', |
| 280 |
'syndicated_posts:meta_data' => 'as syndication meta-data is added on the post', |
| 281 |
), |
| 282 |
'Advanced Diagnostics' => array( |
| 283 |
'feed_items:freshness:reasons' => 'explaining the reason that a post was treated as an update to an existing post', |
| 284 |
'feed_items:freshness:sql' => 'when FeedWordPress issues the SQL query it uses to decide whether to treat items as new, updates, or duplicates', |
| 285 |
'syndicated_posts:categories:test' => 'as FeedWordPress checks for the familiarity of feed categories and tags', |
| 286 |
'syndicated_posts:static_meta_data' => 'providing meta-data about syndicated posts in the Edit Posts interface', |
| 287 |
), |
| 288 |
), $page); |
| 289 |
|
| 290 |
foreach ($fields as $section => $items) : |
| 291 |
foreach ($items as $key => $label) : |
| 292 |
$checked[$key] = ''; |
| 293 |
endforeach; |
| 294 |
endforeach; |
| 295 |
|
| 296 |
$diagnostics_show = get_option('feedwordpress_diagnostics_show', array()); |
| 297 |
if (is_array($diagnostics_show)) : foreach ($diagnostics_show as $thingy) : |
| 298 |
$checked[$thingy] = ' checked="checked"'; |
| 299 |
endforeach; endif; |
| 300 |
|
| 301 |
// Hey ho, let's go... |
| 302 |
?> |
| 303 |
<table class="edit-form"> |
| 304 |
<?php foreach ($fields as $section => $ul) : ?> |
| 305 |
<tr> |
| 306 |
<th scope="row"><?php print esc_html($section); ?>:</th> |
| 307 |
<td><p>Show a diagnostic message...</p> |
| 308 |
<ul class="options"> |
| 309 |
<?php foreach ($ul as $key => $label) : ?> |
| 310 |
<li><label><input |
| 311 |
type="checkbox" name="diagnostics_show[]" |
| 312 |
value="<?php print esc_html($key); ?>" |
| 313 |
<?php print $checked[$key]; ?> /> |
| 314 |
<?php print $label; ?></label></li> |
| 315 |
<?php endforeach; ?> |
| 316 |
</ul></td> |
| 317 |
</tr> |
| 318 |
<?php endforeach; ?> |
| 319 |
</table> |
| 320 |
<?php |
| 321 |
} /* FeedWordPressDiagnosticsPage::updates_box () */ |
| 322 |
|
| 323 |
static function tests_box ($page, $box = NULL) { |
| 324 |
?> |
| 325 |
<script type="text/javascript"> |
| 326 |
function clone_http_test_args_keyvalue_prototype () { |
| 327 |
var next = jQuery('#http-test-args').find('.http-test-args-keyvalue').length; |
| 328 |
var newRow = jQuery('#http-test-args-keyvalue-prototype').clone().attr('id', 'http-test-args-keyvalue-' + next); |
| 329 |
newRow.find('.http_test_args_key').attr('name', 'http_test_args_key['+next+']').val(''); |
| 330 |
newRow.find('.http_test_args_value').attr('name', 'http_test_args_value['+next+']').val(''); |
| 331 |
|
| 332 |
newRow.appendTo('#http-test-args'); |
| 333 |
return false; |
| 334 |
} |
| 335 |
</script> |
| 336 |
|
| 337 |
<table class="edit-form"> |
| 338 |
<tr> |
| 339 |
<th scope="row">HTTP:</th> |
| 340 |
<td><div><input type="url" name="http_test_url" value="" placeholder="http://www.example.com/" size="127" style="width: 80%; max-width: 80.0em;" /> |
| 341 |
<input type="submit" name="feedwordpress_diagnostics_do[http_test]" value="Test »" /></div> |
| 342 |
<div><select name="http_test_method" size="1"> |
| 343 |
<option value="wp_remote_request">wp_remote_request</option> |
| 344 |
<option value="FeedWordPress_File">FeedWordPress_File</option> |
| 345 |
</select></div> |
| 346 |
<table> |
| 347 |
<tr> |
| 348 |
<td> |
| 349 |
<div id="http-test-args"> |
| 350 |
<div id="http-test-args-keyvalue-prototype" class="http-test-args-keyvalue"><label>Args: |
| 351 |
<input type="text" class='http_test_args_key' name="http_test_args_key[0]" value="" placeholder="key" /></label> |
| 352 |
<label>= <input type="text" class='http_test_args_value' name="http_test_args_value[0]" value="" placeholder="value" /></label> |
| 353 |
</div> |
| 354 |
</div> |
| 355 |
</td> |
| 356 |
<td><a href="#http-test-args" onclick="return clone_http_test_args_keyvalue_prototype();">+ Add</a></td> |
| 357 |
</tr> |
| 358 |
</table> |
| 359 |
|
| 360 |
<?php if (isset($page->test_html['http_test'])) : ?> |
| 361 |
<div style="position: relative"> |
| 362 |
<div style="width: 100%; overflow: scroll; background-color: #eed"> |
| 363 |
<pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -o-pre-wrap;"><?php print $page->test_html['http_test']; ?></pre> |
| 364 |
</div> |
| 365 |
</div> |
| 366 |
<?php endif; ?> |
| 367 |
</td> |
| 368 |
</tr> |
| 369 |
</table> |
| 370 |
|
| 371 |
<?php |
| 372 |
} /* FeedWordPressDiagnosticsPage::tests_box () */ |
| 373 |
|
| 374 |
var $test_html; |
| 375 |
static function do_http_test ($post) { |
| 376 |
if (isset($post['http_test_url']) and isset($post['http_test_method'])) : |
| 377 |
$url = $post['http_test_url']; |
| 378 |
|
| 379 |
$args = array(); |
| 380 |
if (isset($post['http_test_args_key'])) : |
| 381 |
foreach ($post['http_test_args_key'] as $idx => $name) : |
| 382 |
$name = trim($name); |
| 383 |
if (strlen($name) > 0) : |
| 384 |
$value = NULL; |
| 385 |
if (isset($post['http_test_args_value']) |
| 386 |
and isset($post['http_test_args_value'][$idx])) : |
| 387 |
$value = $post['http_test_args_value'][$idx]; |
| 388 |
endif; |
| 389 |
|
| 390 |
if (preg_match('/^javascript:(.*)$/i', $value, $refs)) : |
| 391 |
if (function_exists('json_decode')) : |
| 392 |
$json_value = json_decode($refs[1]); |
| 393 |
if (!is_null($json_value)) : |
| 394 |
$value = $json_value; |
| 395 |
endif; |
| 396 |
endif; |
| 397 |
endif; |
| 398 |
|
| 399 |
$args[$name] = $value; |
| 400 |
endif; |
| 401 |
endforeach; |
| 402 |
endif; |
| 403 |
|
| 404 |
switch ($post['http_test_method']) : |
| 405 |
case 'wp_remote_request' : |
| 406 |
$out = wp_remote_request($url, $args); |
| 407 |
break; |
| 408 |
case 'FeedWordPress_File' : |
| 409 |
$out = new FeedWordPress_File($url); |
| 410 |
break; |
| 411 |
endswitch; |
| 412 |
|
| 413 |
$this->test_html['http_test'] = esc_html(MyPHP::val($out)); |
| 414 |
endif; |
| 415 |
} /* FeedWordPressDiagnosticsPage::do_http_test () */ |
| 416 |
|
| 417 |
} /* class FeedWordPressDiagnosticsPage */ |
| 418 |
|
| 419 |
$diagnosticsPage = new FeedWordPressDiagnosticsPage; |
| 420 |
$diagnosticsPage->display(); |
| 421 |
|
| 422 |
|