@@ -1123,6 +1123,50 @@ public static function get_database_size() {
1123
1123
return (int ) $ size ;
1124
1124
}
1125
1125
1126
+ public static function ajax_get_sizes () {
1127
+ check_ajax_referer ( 'health-check-site-status-result ' );
1128
+
1129
+ if ( ! current_user_can ( 'install_plugins ' ) || is_multisite () ) {
1130
+ wp_send_json_error ();
1131
+ }
1132
+
1133
+ $ sizes_data = Health_Check_Debug_Data::get_sizes ();
1134
+ $ all_sizes = array ( 'raw ' => 0 );
1135
+
1136
+ foreach ( $ sizes_data as $ name => $ value ) {
1137
+ $ name = sanitize_text_field ( $ name );
1138
+ $ data = array ();
1139
+
1140
+ if ( isset ( $ value ['size ' ] ) ) {
1141
+ if ( is_string ( $ value ['size ' ] ) ) {
1142
+ $ data ['size ' ] = sanitize_text_field ( $ value ['size ' ] );
1143
+ } else {
1144
+ $ data ['size ' ] = (int ) $ value ['size ' ];
1145
+ }
1146
+ }
1147
+
1148
+ if ( isset ( $ value ['debug ' ] ) ) {
1149
+ if ( is_string ( $ value ['debug ' ] ) ) {
1150
+ $ data ['debug ' ] = sanitize_text_field ( $ value ['debug ' ] );
1151
+ } else {
1152
+ $ data ['debug ' ] = (int ) $ value ['debug ' ];
1153
+ }
1154
+ }
1155
+
1156
+ if ( ! empty ( $ value ['raw ' ] ) ) {
1157
+ $ data ['raw ' ] = (int ) $ value ['raw ' ];
1158
+ }
1159
+
1160
+ $ all_sizes [ $ name ] = $ data ;
1161
+ }
1162
+
1163
+ if ( isset ( $ all_sizes ['total_size ' ]['debug ' ] ) && 'not available ' === $ all_sizes ['total_size ' ]['debug ' ] ) {
1164
+ wp_send_json_error ( $ all_sizes );
1165
+ }
1166
+
1167
+ wp_send_json_success ( $ all_sizes );
1168
+ }
1169
+
1126
1170
/**
1127
1171
* Fetch the sizes of the WordPress directories: `wordpress` (ABSPATH), `plugins`, `themes`, and `uploads`.
1128
1172
* Intended to supplement the array returned by `WP_Debug_Data::debug_data()`.
@@ -1156,6 +1200,15 @@ public static function get_sizes() {
1156
1200
$ max_execution_time -= 2 ;
1157
1201
}
1158
1202
1203
+ if ( ! defined ( 'WP_START_TIMESTAMP ' ) ) {
1204
+ global $ timestart ;
1205
+ if ( version_compare ( phpversion (), '5.4.0 ' , '>= ' ) && isset ( $ _SERVER ['REQUEST_TIME_FLOAT ' ] ) ) {
1206
+ define ( 'WP_START_TIMESTAMP ' , $ _SERVER ['REQUEST_TIME_FLOAT ' ] );
1207
+ } else {
1208
+ define ( 'WP_START_TIMESTAMP ' , $ timestart );
1209
+ }
1210
+ }
1211
+
1159
1212
// Go through the various installation directories and calculate their sizes.
1160
1213
// No trailing slashes.
1161
1214
$ paths = array (
@@ -1182,9 +1235,17 @@ public static function get_sizes() {
1182
1235
1183
1236
if ( microtime ( true ) - WP_START_TIMESTAMP < $ max_execution_time ) {
1184
1237
if ( 'wordpress_size ' === $ name ) {
1185
- $ dir_size = recurse_dirsize ( $ path , $ exclude , $ max_execution_time );
1238
+ if ( version_compare ( get_bloginfo ( 'version ' ), '5.2.0 ' , '< ' ) ) {
1239
+ $ dir_size = Health_Check_Debug_Data::recurse_dirsize ( $ path , $ exclude , $ max_execution_time );
1240
+ } else {
1241
+ $ dir_size = recurse_dirsize ( $ path , $ exclude , $ max_execution_time );
1242
+ }
1186
1243
} else {
1187
- $ dir_size = recurse_dirsize ( $ path , null , $ max_execution_time );
1244
+ if ( version_compare ( get_bloginfo ( 'version ' ), '5.2.0 ' , '< ' ) ) {
1245
+ $ dir_size = Health_Check_Debug_Data::recurse_dirsize ( $ path , null , $ max_execution_time );
1246
+ } else {
1247
+ $ dir_size = recurse_dirsize ( $ path , null , $ max_execution_time );
1248
+ }
1188
1249
}
1189
1250
}
1190
1251
@@ -1248,4 +1309,71 @@ public static function get_sizes() {
1248
1309
1249
1310
return $ all_sizes ;
1250
1311
}
1312
+
1313
+ /**
1314
+ * Fallback function for directory size calculation on sites running WordPress <5.2
1315
+ *
1316
+ * @param string $directory Full path of a directory.
1317
+ * @param string|array $exclude Optional. Full path of a subdirectory to exclude from the total, or array of paths.
1318
+ * Expected without trailing slash(es).
1319
+ * @param int $max_execution_time Maximum time to run before giving up. In seconds.
1320
+ * The timeout is global and is measured from the moment WordPress started to load.
1321
+ * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
1322
+ */
1323
+ static function recurse_dirsize ( $ directory , $ exclude = null , $ max_execution_time = null ) {
1324
+ $ size = 0 ;
1325
+
1326
+ $ directory = untrailingslashit ( $ directory );
1327
+
1328
+ if ( ! file_exists ( $ directory ) || ! is_dir ( $ directory ) || ! is_readable ( $ directory ) ) {
1329
+ return false ;
1330
+ }
1331
+
1332
+ if (
1333
+ ( is_string ( $ exclude ) && $ directory === $ exclude ) ||
1334
+ ( is_array ( $ exclude ) && in_array ( $ directory , $ exclude , true ) )
1335
+ ) {
1336
+ return false ;
1337
+ }
1338
+
1339
+ if ( null === $ max_execution_time ) {
1340
+ // Keep the previous behavior but attempt to prevent fatal errors from timeout if possible.
1341
+ if ( function_exists ( 'ini_get ' ) ) {
1342
+ $ max_execution_time = ini_get ( 'max_execution_time ' );
1343
+ } else {
1344
+ // Disable...
1345
+ $ max_execution_time = 0 ;
1346
+ }
1347
+
1348
+ // Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value.
1349
+ if ( $ max_execution_time > 10 ) {
1350
+ $ max_execution_time -= 1 ;
1351
+ }
1352
+ }
1353
+
1354
+ $ handle = opendir ( $ directory );
1355
+ if ( $ handle ) {
1356
+ while ( ( $ file = readdir ( $ handle ) ) !== false ) {
1357
+ $ path = $ directory . '/ ' . $ file ;
1358
+ if ( '. ' != $ file && '.. ' != $ file ) {
1359
+ if ( is_file ( $ path ) ) {
1360
+ $ size += filesize ( $ path );
1361
+ } elseif ( is_dir ( $ path ) ) {
1362
+ $ handlesize = Health_Check_Debug_Data::recurse_dirsize ( $ path , $ exclude , $ max_execution_time );
1363
+ if ( $ handlesize > 0 ) {
1364
+ $ size += $ handlesize ;
1365
+ }
1366
+ }
1367
+
1368
+ if ( $ max_execution_time > 0 && microtime ( true ) - WP_START_TIMESTAMP > $ max_execution_time ) {
1369
+ // Time exceeded. Give up instead of risking a fatal timeout.
1370
+ $ size = null ;
1371
+ break ;
1372
+ }
1373
+ }
1374
+ }
1375
+ closedir ( $ handle );
1376
+ }
1377
+ return $ size ;
1378
+ }
1251
1379
}
0 commit comments