Skip to content

Commit

Permalink
Rewrite FilesHelper and tests to be more functional and testable
Browse files Browse the repository at this point in the history
  • Loading branch information
john-shaffer committed Nov 6, 2021
1 parent 1db11c3 commit 3e637a7
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 130 deletions.
27 changes: 27 additions & 0 deletions src/CoreOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,33 @@ public static function getLineDelimitedBlobValue( string $name ) : array {
return $vals;
}

/**
* Get option default BLOB value
*
* @throws WP2StaticException
* @return string option default BLOB value
*/
public static function getDefaultBlobValue( string $name ) : string {
$val = self::optionSpecs()[ $name ]['default_blob_value'];
return $val ? $val : '';
}

/**
* @return array<string>
*/
public static function getDefaultLineDelimitedBlobValue( string $name ) : array {
$vals = preg_split(
'/\r\n|\r|\n/',
self::getDefaultBlobValue( $name )
);

if ( ! $vals ) {
return [];
}

return $vals;
}

/**
* Get option (value, description, label, etc)
*
Expand Down
74 changes: 53 additions & 21 deletions src/FilesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ public static function deleteDirWithFiles( string $dir ) : void {
/**
* Get public URLs for all files in a local directory.
*
* @param string $dir
* @param array<string> $filenames_to_ignore
* @param array<string> $file_extensions_to_ignore
* @return string[] list of relative, urlencoded URLs
*/
public static function getListOfLocalFilesByDir( string $dir ) : array {
public static function getListOfLocalFilesByDir(
string $dir,
array $filenames_to_ignore,
array $file_extensions_to_ignore
) : array {
$files = [];

$site_path = SiteInfo::getPath( 'site' );
Expand All @@ -54,7 +61,11 @@ public static function getListOfLocalFilesByDir( string $dir ) : array {
);

foreach ( $iterator as $filename => $file_object ) {
$path_crawlable = self::filePathLooksCrawlable( $filename );
$path_crawlable = self::pathLooksCrawlable(
$filename,
$filenames_to_ignore,
$file_extensions_to_ignore
);

if ( $path_crawlable ) {
if ( is_string( $site_path ) ) {
Expand All @@ -74,18 +85,17 @@ public static function getListOfLocalFilesByDir( string $dir ) : array {
/**
* Ensure a given filepath has an allowed filename and extension.
*
* @param string $file_name
* @param array<string> $filenames_to_ignore
* @param array<string> $file_extensions_to_ignore
* @return bool True if the given file does not have a disallowed filename
* or extension.
*/
public static function filePathLooksCrawlable( string $file_name ) : bool {
$filenames_to_ignore = CoreOptions::getLineDelimitedBlobValue( 'filenamesToIgnore' );

$filenames_to_ignore =
apply_filters(
'wp2static_filenames_to_ignore',
$filenames_to_ignore
);

public static function pathLooksCrawlable(
string $file_name,
array $filenames_to_ignore,
array $file_extensions_to_ignore
) : bool {
$filename_matches = 0;

str_ireplace( $filenames_to_ignore, '', $file_name, $filename_matches );
Expand All @@ -95,16 +105,6 @@ public static function filePathLooksCrawlable( string $file_name ) : bool {
return false;
}

$file_extensions_to_ignore = CoreOptions::getLineDelimitedBlobValue(
'fileExtensionsToIgnore'
);

$file_extensions_to_ignore =
apply_filters(
'wp2static_file_extensions_to_ignore',
$file_extensions_to_ignore
);

/*
Prepare the file extension list for regex:
- Add prepending (escaped) \ for a literal . at the start of
Expand All @@ -121,6 +121,38 @@ public static function filePathLooksCrawlable( string $file_name ) : bool {
return true;
}

/**
* Ensure a given filepath has an allowed filename and extension.
*
* @return bool True if the given file does not have a disallowed filename
* or extension.
*/
public static function filePathLooksCrawlable( string $file_name ) : bool {
$filenames_to_ignore = CoreOptions::getLineDelimitedBlobValue( 'filenamesToIgnore' );

$filenames_to_ignore =
apply_filters(
'wp2static_filenames_to_ignore',
$filenames_to_ignore
);

$file_extensions_to_ignore = CoreOptions::getLineDelimitedBlobValue(
'fileExtensionsToIgnore'
);

$file_extensions_to_ignore =
apply_filters(
'wp2static_file_extensions_to_ignore',
$file_extensions_to_ignore
);

return self::pathLooksCrawlable(
$file_name,
$filenames_to_ignore,
$file_extensions_to_ignore
);
}

/**
* Clean all detected URLs before use. Accepts relative and absolute URLs
* both with and without starting or trailing slashes.
Expand Down
24 changes: 23 additions & 1 deletion src/URLDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,30 @@ public static function detectURLs() : string {
}

if ( CoreOptions::getValue( 'detectUploads' ) ) {
$filenames_to_ignore = CoreOptions::getLineDelimitedBlobValue( 'filenamesToIgnore' );

$filenames_to_ignore =
apply_filters(
'wp2static_filenames_to_ignore',
$filenames_to_ignore
);

$file_extensions_to_ignore = CoreOptions::getLineDelimitedBlobValue(
'fileExtensionsToIgnore'
);

$file_extensions_to_ignore =
apply_filters(
'wp2static_file_extensions_to_ignore',
$file_extensions_to_ignore
);

$arrays_to_merge[] =
FilesHelper::getListOfLocalFilesByDir( SiteInfo::getPath( 'uploads' ) );
FilesHelper::getListOfLocalFilesByDir(
SiteInfo::getPath( 'uploads' ),
$filenames_to_ignore,
$file_extensions_to_ignore
);
}

$detect_sitemaps = apply_filters( 'wp2static_detect_sitemaps', 1 );
Expand Down
Loading

0 comments on commit 3e637a7

Please sign in to comment.