@@ -19,6 +19,13 @@ class Debug {
19
19
*/
20
20
private static $ log_file = 'debug-aspire-update.log ' ;
21
21
22
+ /**
23
+ * The filesystem.
24
+ *
25
+ * @var Filesystem_Direct
26
+ */
27
+ private static $ filesystem ;
28
+
22
29
/**
23
30
* Get the Log file path.
24
31
*
@@ -31,79 +38,41 @@ private static function get_file_path() {
31
38
/**
32
39
* Initializes the WordPress Filesystem.
33
40
*
34
- * @return WP_Filesystem_Base|false The filesystem object or false on failure .
41
+ * @return Filesystem_Direct The filesystem object.
35
42
*/
36
43
private static function init_filesystem () {
37
- global $ wp_filesystem ;
38
-
39
- if ( ! $ wp_filesystem ) {
44
+ if ( ! self ::$ filesystem instanceof Filesystem_Direct ) {
40
45
require_once ABSPATH . 'wp-admin/includes/file.php ' ;
46
+ require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php ' ;
47
+ require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php ' ;
41
48
WP_Filesystem ();
49
+ self ::$ filesystem = new Filesystem_Direct ( false );
42
50
}
43
-
44
- return $ wp_filesystem ;
45
- }
46
-
47
- /**
48
- * Checks the filesystem status and logs error to debug log.
49
- *
50
- * @param WP_Filesystem_Base $wp_filesystem The filesystem object.
51
- *
52
- * @return boolean true on success and false on failure.
53
- */
54
- private static function verify_filesystem ( $ wp_filesystem ) {
55
- if ( ! $ wp_filesystem ) {
56
- if (
57
- defined ( 'WP_DEBUG ' ) &&
58
- ( true === WP_DEBUG ) &&
59
- defined ( 'WP_DEBUG_LOG ' ) &&
60
- ( true === WP_DEBUG_LOG )
61
- ) {
62
- // phpcs:disable WordPress.PHP.DevelopmentFunctions
63
- /**
64
- * Log error in file write fails only if debug is set to true. This is a valid use case.
65
- */
66
- error_log ( 'AspireUpdate - Could not open or write to the file system. Check file system permissions to debug log directory. ' ); // @codeCoverageIgnore
67
- // phpcs:enable
68
- }
69
- return false ;
70
- }
71
- return true ;
51
+ return self ::$ filesystem ;
72
52
}
73
53
74
54
/**
75
55
* Get the content of the log file truncated upto N number of lines.
76
56
*
77
57
* @param integer $limit Max no of lines to return. Defaults to a 1000 lines.
78
58
*
79
- * @return string |WP_Error The File content truncate upto the number of lines set in the limit parameter .
59
+ * @return array |WP_Error An array of lines in the file, limited to $limit, or a WP_Error object on failure .
80
60
*/
81
61
public static function read ( $ limit = 1000 ) {
82
62
$ wp_filesystem = self ::init_filesystem ();
83
63
$ file_path = self ::get_file_path ();
84
- if ( ! self ::verify_filesystem ( $ wp_filesystem ) || ! $ wp_filesystem ->exists ( $ file_path ) || ! $ wp_filesystem ->is_readable ( $ file_path ) ) {
64
+
65
+ if ( ! $ wp_filesystem ->exists ( $ file_path ) || ! $ wp_filesystem ->is_readable ( $ file_path ) ) {
85
66
return new \WP_Error ( 'not_readable ' , __ ( 'Error: Unable to read the log file. ' , 'aspireupdate ' ) );
86
67
}
87
68
88
- $ file_content = $ wp_filesystem ->get_contents_array ( $ file_path );
89
- $ content = '' ;
90
- $ index = 0 ;
91
- foreach ( $ file_content as $ file_content_lines ) {
92
- if ( ( $ index < $ limit ) ) {
93
- $ content .= $ file_content_lines . PHP_EOL ;
94
- ++$ index ;
95
- }
96
- }
97
- if ( '' === trim ( $ content ) ) {
98
- $ content = esc_html__ ( '*****Log file is empty.***** ' , 'aspireupdate ' );
99
- } elseif ( $ limit < count ( $ file_content ) ) {
100
- $ content .= PHP_EOL . sprintf (
101
- /* translators: 1: The number of lines at which the content was truncated. */
102
- esc_html__ ( '*****Log truncated at %s lines.***** ' , 'aspireupdate ' ),
103
- $ limit
104
- );
69
+ $ file_content = $ wp_filesystem ->get_contents_array ( $ file_path , $ limit , true );
70
+
71
+ if ( ( false === $ file_content ) || ( 0 === count ( array_filter ( $ file_content ) ) ) ) {
72
+ $ file_content = [ esc_html__ ( '*****Log file is empty.***** ' , 'aspireupdate ' ) ];
105
73
}
106
- return $ content ;
74
+
75
+ return $ file_content ;
107
76
}
108
77
109
78
/**
@@ -114,7 +83,8 @@ public static function read( $limit = 1000 ) {
114
83
public static function clear () {
115
84
$ wp_filesystem = self ::init_filesystem ();
116
85
$ file_path = self ::get_file_path ();
117
- if ( ! self ::verify_filesystem ( $ wp_filesystem ) || ! $ wp_filesystem ->exists ( $ file_path ) || ! $ wp_filesystem ->is_writable ( $ file_path ) ) {
86
+
87
+ if ( ! $ wp_filesystem ->exists ( $ file_path ) || ! $ wp_filesystem ->is_writable ( $ file_path ) ) {
118
88
return new \WP_Error ( 'not_accessible ' , __ ( 'Error: Unable to access the log file. ' , 'aspireupdate ' ) );
119
89
}
120
90
@@ -133,30 +103,22 @@ public static function clear() {
133
103
* @param string $type The log level ('string', 'request', 'response').
134
104
*/
135
105
public static function log ( $ message , $ type = 'string ' ) {
136
- $ wp_filesystem = self ::init_filesystem ();
137
- if ( self ::verify_filesystem ( $ wp_filesystem ) ) {
138
- $ timestamp = gmdate ( 'Y-m-d H:i:s ' );
139
- $ formatted_message = sprintf (
140
- '[%s] [%s]: %s ' ,
141
- $ timestamp ,
142
- strtoupper ( $ type ),
143
- self ::format_message ( $ message )
144
- ) . PHP_EOL ;
145
-
146
- $ file_path = self ::get_file_path ();
147
-
148
- $ content = '' ;
149
- if ( $ wp_filesystem ->exists ( $ file_path ) ) {
150
- if ( $ wp_filesystem ->is_readable ( $ file_path ) ) {
151
- $ content = $ wp_filesystem ->get_contents ( $ file_path );
152
- }
153
- }
154
- $ wp_filesystem ->put_contents (
155
- $ file_path ,
156
- $ formatted_message . $ content ,
157
- FS_CHMOD_FILE
158
- );
159
- }
106
+ $ wp_filesystem = self ::init_filesystem ();
107
+ $ timestamp = gmdate ( 'Y-m-d H:i:s ' );
108
+ $ formatted_message = sprintf (
109
+ '[%s] [%s]: %s ' ,
110
+ $ timestamp ,
111
+ strtoupper ( $ type ),
112
+ self ::format_message ( $ message )
113
+ ) . PHP_EOL ;
114
+
115
+ $ file_path = self ::get_file_path ();
116
+ $ wp_filesystem ->put_contents (
117
+ $ file_path ,
118
+ $ formatted_message ,
119
+ FS_CHMOD_FILE ,
120
+ 'a '
121
+ );
160
122
}
161
123
162
124
/**
0 commit comments