Error_Log_Dashboard_Widget::on_load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
Plugin Name: Error Log Dashboard Widget
5
Plugin URI: https://github.com/Rarst/error-log-dashboard-widget
6
Description: Robust zero-configuration and low-memory WordPress plugin to keep an eye on error log.
7
Author: Andrey "Rarst" Savchenko
8
Author URI: http://www.rarst.net/
9
Version: 1.0.5
10
License: GPLv2 or later
11
12
Includes last_lines() function by phant0m, licensed under cc-wiki and GPLv2+
13
*/
14
15
Error_Log_Dashboard_Widget::on_load();
16
17
/**
18
 * Main plugin's class.
19
 */
20
class Error_Log_Dashboard_Widget {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
21
22
	/**
23
	 * Set up logic on load.
24
	 */
25
	public static function on_load() {
26
27
		add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
28
	}
29
30
	/**
31
	 * Set up logic on admin init.
32
	 */
33
	public static function admin_init() {
34
35
		add_action( 'wp_dashboard_setup', array( __CLASS__, 'wp_dashboard_setup' ) );
36
	}
37
38
	/**
39
	 * Add dashboard widget.
40
	 */
41
	public static function wp_dashboard_setup() {
42
43
		if ( current_user_can( apply_filters( 'error_log_widget_capability', 'manage_options' ) ) ) {
44
			wp_add_dashboard_widget( 'error-log-widget', __( 'Error Log', 'error-log-widget' ), array( __CLASS__, 'widget_callback' ) );
45
		}
46
	}
47
48
	/**
49
	 * Read log and render widget output.
50
	 */
51
	public static function widget_callback() {
52
53
		$log_errors = ini_get( 'log_errors' );
54
55
		if ( ! $log_errors ) {
56
			echo '<p>' . __( 'Error logging disabled.', 'error-log-widget' ) . ' <a href="https://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Logging">' . __( 'Configure error log', 'error-log-widget' ) . '</a></p>';
57
		}
58
59
		$error_log = ini_get( 'error_log' );
60
		$logs      = apply_filters( 'error_log_widget_logs', array( $error_log ) );
61
		$count     = apply_filters( 'error_log_widget_lines', 10 );
62
		$lines     = array();
63
64
		foreach ( $logs as $log ) {
65
66
			if ( is_readable( $log ) ) {
67
				$lines = array_merge( $lines, self::last_lines( $log, $count ) );
68
			}
69
		}
70
71
		$lines = array_map( 'trim', $lines );
72
		$lines = array_filter( $lines );
73
74
		if ( empty( $lines ) ) {
75
76
			echo '<p>' . __( 'No errors found... Yet.', 'error-log-widget' ) . '</p>';
77
78
			return;
79
		}
80
81
		foreach ( $lines as $key => $line ) {
82
83
			if ( false !== strpos( $line, ']' ) ) {
84
				list( $time, $error ) = explode( ']', $line, 2 );
85
			} else {
86
				list( $time, $error ) = array( '', $line );
87
			}
88
89
			$time          = trim( $time, '[]' );
90
			$error         = trim( $error );
91
			$lines[ $key ] = compact( 'time', 'error' );
92
		}
93
94
		if ( count( $logs ) > 1 ) {
95
96
			uasort( $lines, array( __CLASS__, 'time_field_compare' ) );
97
			$lines = array_slice( $lines, 0, $count );
98
		}
99
100
		echo '<table class="widefat">';
101
102
		foreach ( $lines as $line ) {
103
104
			$error = esc_html( $line['error'] );
105
			$time  = esc_html( $line['time'] );
106
107
			if ( ! empty( $error ) ) {
108
				echo( "<tr><td>{$time}</td><td>{$error}</td></tr>" );
109
			}
110
		}
111
112
		echo '</table>';
113
	}
114
115
	/**
116
	 * Compare callback for freeform date/time strings in line items.
117
	 *
118
	 * @param array $a First value.
119
	 * @param array $b Second value.
120
	 *
121
	 * @return int
122
	 */
123
	public static function time_field_compare( $a, $b ) {
124
125
		if ( $a['time'] === $b['time'] ) {
126
			return 0;
127
		}
128
129
		return ( strtotime( $a['time'] ) > strtotime( $b['time'] ) ) ? - 1 : 1;
130
	}
131
132
	/**
133
	 * Reads lines from end of file. Memory-safe.
134
	 *
135
	 * @link http://stackoverflow.com/questions/6451232/php-reading-large-files-from-end/6451391#6451391
136
	 *
137
	 * @param string  $path       Filesystem path to the file.
138
	 * @param integer $line_count How many lines to read.
139
	 * @param integer $block_size Size of block to use for read.
140
	 *
141
	 * @return array
142
	 */
143
	public static function last_lines( $path, $line_count, $block_size = 512 ) {
144
		$lines = array();
145
146
		// we will always have a fragment of a non-complete line
147
		// keep this in here till we have our next entire line.
148
		$leftover = '';
149
150
		$fh = fopen( $path, 'r' );
151
		// go to the end of the file.
152
		fseek( $fh, 0, SEEK_END );
153
154
		do {
155
			// need to know whether we can actually go back
156
			// $block_size bytes.
157
			$can_read = $block_size;
158
159
			if ( ftell( $fh ) <= $block_size ) {
160
				$can_read = ftell( $fh );
161
			}
162
163
			if ( empty( $can_read ) ) {
164
				break;
165
			}
166
167
			// go back as many bytes as we can
168
			// read them to $data and then move the file pointer
169
			// back to where we were.
170
			fseek( $fh, - $can_read, SEEK_CUR );
171
			$data  = fread( $fh, $can_read );
172
			$data .= $leftover;
173
			fseek( $fh, - $can_read, SEEK_CUR );
174
175
			// split lines by \n. Then reverse them,
176
			// now the last line is most likely not a complete
177
			// line which is why we do not directly add it, but
178
			// append it to the data read the next time.
179
			$split_data = array_reverse( explode( "\n", $data ) );
180
			$new_lines  = array_slice( $split_data, 0, - 1 );
181
			$lines      = array_merge( $lines, $new_lines );
182
			$leftover   = $split_data[ count( $split_data ) - 1 ];
183
		} while ( count( $lines ) < $line_count && ftell( $fh ) != 0 );
184
185
		if ( ftell( $fh ) == 0 ) {
186
			$lines[] = $leftover;
187
		}
188
189
		fclose( $fh );
190
		// Usually, we will read too many lines, correct that here.
191
		return array_slice( $lines, 0, $line_count );
192
	}
193
}
194