Skip to content

Commit

Permalink
Don't parse files everytime I need a directory list.
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Dec 20, 2022
1 parent 30581fe commit a898fd6
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/my-calendar-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,33 @@ function mc_directory_list( $directory ) {
if ( ! file_exists( $directory ) ) {
return array();
}
$results = array();
$handler = opendir( $directory );
// keep going until all files in directory have been read.
while ( false !== ( $file = readdir( $handler ) ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
// if $file isn't this directory or its parent add it to the results array.
if ( strlen( $file ) < 5 ) {
continue;
}
if ( filesize( $directory . '/' . $file ) > 11 ) {
if ( '.' !== $file && '..' !== $file && ! is_dir( $directory . $file ) && (
'image/svg_xml' === mime_content_type( $directory . $file ) ||
'image/svg' === mime_content_type( $directory . $file ) ||
'image/svg+xml' === mime_content_type( $directory . $file ) ||
exif_imagetype( $directory . $file ) === IMAGETYPE_GIF ||
exif_imagetype( $directory . $file ) === IMAGETYPE_PNG ||
exif_imagetype( $directory . $file ) === IMAGETYPE_JPEG )
) {
$results[] = $file;
$results = get_transient( 'mc_icon_list' );
if ( empty( $results ) ) {
$results = array();
$handler = opendir( $directory );
// keep going until all files in directory have been read.
while ( false !== ( $file = readdir( $handler ) ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
// if $file isn't this directory or its parent add it to the results array.
if ( strlen( $file ) < 5 ) {
continue;
}
if ( filesize( $directory . '/' . $file ) > 11 ) {
if ( '.' !== $file && '..' !== $file && ! is_dir( $directory . $file ) && (
'image/svg_xml' === mime_content_type( $directory . $file ) ||
'image/svg' === mime_content_type( $directory . $file ) ||
'image/svg+xml' === mime_content_type( $directory . $file ) ||
exif_imagetype( $directory . $file ) === IMAGETYPE_GIF ||
exif_imagetype( $directory . $file ) === IMAGETYPE_PNG ||
exif_imagetype( $directory . $file ) === IMAGETYPE_JPEG )
) {
$results[] = $file;
}
}
}
closedir( $handler );
sort( $results, SORT_STRING );
set_transient( 'mc_icon_list', $results, HOUR_IN_SECONDS );
}
closedir( $handler );
sort( $results, SORT_STRING );

return $results;
}
Expand Down

0 comments on commit a898fd6

Please sign in to comment.