| 1 |
<?php |
| 2 |
/** |
| 3 |
* class FeedWordPressDiagnostic: help to organize some diagnostics output functions. |
| 4 |
* |
| 5 |
* @uses FeedWordPress::diagnostic() |
| 6 |
*/ |
| 7 |
class FeedWordPressDiagnostic { |
| 8 |
/** |
| 9 |
* Log error on a feed. |
| 10 |
* |
| 11 |
* @param array $error A structured array of error fields. |
| 12 |
* @param mixex $old Unused. |
| 13 |
* @param object $link A link object, containing an URI. * |
| 14 |
*/ |
| 15 |
public static function feed_error( $error, $old, $link ) { |
| 16 |
$wpError = $error['object']; |
| 17 |
$url = $link->uri(); |
| 18 |
|
| 19 |
// check for effects of an effective-url filter |
| 20 |
$effectiveUrl = $link->uri( array( 'fetch' => true ) ); |
| 21 |
if ( $url != $effectiveUrl ) : $url .= ' | ' . $effectiveUrl; endif; |
| 22 |
|
| 23 |
$mesgs = $wpError->get_error_messages(); |
| 24 |
foreach ( $mesgs as $mesg ) : |
| 25 |
$mesg = esc_html( $mesg ); |
| 26 |
FeedWordPress::diagnostic( |
| 27 |
'updated_feeds:errors', |
| 28 |
"Feed Error: [{$url}] update returned error: $mesg" |
| 29 |
); |
| 30 |
|
| 31 |
$hours = get_option( 'feedwordpress_diagnostics_persistent_errors_hours', 2 ); |
| 32 |
$span = ( $error['ts'] - $error['since'] ); |
| 33 |
|
| 34 |
if ( $span >= ( $hours * 60 * 60 ) ) : |
| 35 |
$since = date( 'r', $error['since'] ); |
| 36 |
/** @var string Never used. */ |
| 37 |
$mostRecent = date( 'r', $error['ts'] ); // never used?... (gwyneth 20230919) |
| 38 |
FeedWordPress::diagnostic( |
| 39 |
'updated_feeds:errors:persistent', |
| 40 |
"Feed Update Error: [{$url}] returning errors" |
| 41 |
." since {$since}:<br/><code>$mesg</code>", |
| 42 |
$url, |
| 43 |
$error['since'], |
| 44 |
$error['ts'] |
| 45 |
); |
| 46 |
endif; |
| 47 |
endforeach; |
| 48 |
} /* FeedWordPressDiagnostic::feed_error() */ |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns an array with the list of administrator emails for this site. |
| 52 |
* |
| 53 |
* @param int|string $id Current blog ID (for multisite installations). |
| 54 |
* |
| 55 |
* @return array Array with all email addresses for this blog. |
| 56 |
* |
| 57 |
* @uses get_users_of_blog() |
| 58 |
* |
| 59 |
* @note This uses the deprecated WP function `get_users_of_blog()`, which |
| 60 |
* should be replaced with `get_users()`, which, however, has quite more |
| 61 |
* intricate syntax (and customisation!). |
| 62 |
* |
| 63 |
* It might be even possible to retrieve everything in a single call: |
| 64 |
* ``` |
| 65 |
* return get_users( |
| 66 |
* array( |
| 67 |
* 'role__in' => 'administrator', |
| 68 |
* 'capability__in' => 'administrator', |
| 69 |
* 'fields' => 'user_email', |
| 70 |
* 'count_total' => false // no need to count them; improves performance. |
| 71 |
* ) |
| 72 |
* ); |
| 73 |
* ``` |
| 74 |
* |
| 75 |
* Alternatively, the function `admin_emails()` may simply be marked as deprecated and |
| 76 |
* `get_users()` used instead. This requires debugging! (gwyneth 20230919) |
| 77 |
*/ |
| 78 |
public static function admin_emails( $id = '' ) { |
| 79 |
// deprecated, see comment on the function description! (gwyneth 20230919) |
| 80 |
$users = get_users_of_blog( $id ); |
| 81 |
$recipients = array(); |
| 82 |
foreach ( $users as $user ) : |
| 83 |
$user_id = ( isset( $user->user_id ) ? $user->user_id : $user->ID ); |
| 84 |
$dude = new WP_User( $user_id ); |
| 85 |
if ( $dude->has_cap('administrator') ) : |
| 86 |
if ( $dude->user_email ) : |
| 87 |
$recipients[] = $dude->user_email; |
| 88 |
endif; |
| 89 |
endif; |
| 90 |
endforeach; |
| 91 |
return $recipients; |
| 92 |
} /* FeedWordPressDiagnostic::admin_emails() */ |
| 93 |
|
| 94 |
public static function noncritical_bug ($varname, $var, $line, $file = NULL) { |
| 95 |
if ( FEEDWORDPRESS_DEBUG ) : // halt only when we are doing debugging |
| 96 |
self::critical_bug($varname, $var, $line, $file); |
| 97 |
endif; |
| 98 |
} /* FeedWordPressDiagnostic::noncritical_bug () */ |
| 99 |
|
| 100 |
public static function critical_bug ($varname, $var, $line, $file = NULL) { |
| 101 |
global $wp_version; |
| 102 |
|
| 103 |
if ( !is_null($file)) : |
| 104 |
$location = "line # {$line} of ".basename($file); |
| 105 |
else : |
| 106 |
$location = "line # {$line}"; |
| 107 |
endif; |
| 108 |
|
| 109 |
print '<p><strong>Critical error:</strong> There may be a bug in FeedWordPress. Please <a href="'.esc_url( FEEDWORDPRESS_AUTHOR_CONTACT ) .'">contact the author</a> and paste the following information into your e-mail:</p>'; |
| 110 |
print "\n<pre>"; |
| 111 |
print "Triggered at " . esc_html($location) . "\n"; |
| 112 |
print "FeedWordPress: " . esc_html( FEEDWORDPRESS_VERSION ) . "\n"; |
| 113 |
print "WordPress: " . esc_html( $wp_version ) . "\n"; |
| 114 |
print "PHP: " . esc_html( phpversion() ) . "\n"; |
| 115 |
print "Error data: "; |
| 116 |
print esc_html($varname) . ": " . esc_html( MyPHP::val( $var ) ) . "\n"; |
| 117 |
print "\n</pre>"; |
| 118 |
die; |
| 119 |
} /* FeedWordPressDiagnostic::critical_bug () */ |
| 120 |
|
| 121 |
public static function is_on ($level) { |
| 122 |
$show = get_option('feedwordpress_diagnostics_show', array()); |
| 123 |
if ( ! is_array( $show ) ) { |
| 124 |
$show = array( $show ); |
| 125 |
} |
| 126 |
|
| 127 |
return ( in_array( $level, $show ) ); |
| 128 |
} /* FeedWordPressDiagnostic::is_on () */ |
| 129 |
|
| 130 |
} /* class FeedWordPressDiagnostic */ |
| 131 |
|