/** * Calculates the contents of a directory recursively. Warning: this might be a long process. * * @author Nevma (info@nevma.gr) * * @param string $dir The directory whose contents to calculate recursively. * * @return int The total size of the directory and its children recursively in bytes. */ function adaptive_images_plugin_dir_size($dir) { // var_dump( $dir ); // echo '<hr />'; // $result = exec( 'du -hcs ' . $dir, $output, $code ); // var_dump( $result ); // ==> empty when not done, echoes "3.5M Total" when done // echo '<hr />'; // var_dump( $output ); // ==> empty when not done, holds lines of command output when done // echo '<hr />'; // var_dump( $code ); // ==> 0 when command executed successfully, 1 when not // echo '<hr />'; // return; // Keep count of recursively accessed files. static $total_files = 0; static $total_size = 0; static $total_dirs = 0; // Do not take into acount files and symbolic links. if (is_dir($dir) && !is_link($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { $file = $dir . "/" . $object; if (filetype($file) != "dir") { // Add file size in total. $total_files++; $total_size += filesize($file); } else { // Descend into directory children. $total_dirs++; adaptive_images_plugin_dir_size($file); } } } } return array('files' => $total_files, 'size' => $total_size, 'dirs' => $total_dirs); }
/** * Takes care of the plugin settings page actions. * * @author Nevma (info@nevma.gr) * * @return void Nothing really!. */ function adaptive_images_admin_settings_actions() { if (!isset($_GET['action'])) { return; } // Cleanup image cache action. if ($_GET['action'] == 'cleanup-image-cache' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-cleanup-image-cache')) { $cache_path = adaptive_images_plugin_get_cahe_directory_path(); $result = adaptive_images_actions_rmdir_recursive($cache_path); add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Cleanup image cache <hr />' . '<p>Total files deleted from the adaptive images cache: ' . $result['files'] . '</p>' . '<p>Total directories deleted from the adaptive images cache: ' . $result['dirs'] . '</p>' . '<p>' . 'Total size deleted from the adaptive images cache: ' . adaptive_images_plugin_file_size_human($result['size']) . '</p>', 'updated'); } // Calculate image cache size action. if ($_GET['action'] == 'calculate-cache-size' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-calculate-cache-size')) { $cache_path = adaptive_images_plugin_get_cahe_directory_path(); $cache_size = adaptive_images_plugin_dir_size($cache_path); add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Calculate cache size <hr />' . '<p>Total files in the adaptive images cache: ' . $cache_size['files'] . '</p>' . '<p>Total directories in the adaptive images cache: ' . $cache_size['dirs'] . '</p>' . '<p>' . 'Total size of the adaptive images cache: ' . adaptive_images_plugin_file_size_human($cache_size['size']) . '</p>', 'updated'); } // Print plugin info action. if ($_GET['action'] == 'print-debug-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-debug-info')) { add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Debug info <hr />' . adaptive_images_debug_general_info(FALSE), 'updated'); } // Print system info action. if ($_GET['action'] == 'print-diagnostic-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-diagnostic-info')) { add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'System information <hr />' . adaptive_images_debug_diagnostic_info(FALSE), 'updated'); } }