Here is the code …
I am still changing things around and I stripped the particulars of our server. Please watch out for the parts with the capital letters. Hope this helps somebody! usage: -download {query}
function bils_download_search( $results, $args ) {
// downloadsearch doesn't need paging, so if page arg is greater than one, abort.
// We also don't want to get files for queries less than two characters.
if ( intval( $args['page'] ) > 1 || strlen( $args['s'] ) < 2 ) {
wp_die( 0 );
}
$fileinfos = new RecursiveIteratorIterator(
new FilesOnlyFilter(
new VisibleOnlyFilter(
new RecursiveDirectoryIterator(
$PATH_TO_YOUR_FILEDIRECTORY,
FilesystemIterator::SKIP_DOTS
| FilesystemIterator::UNIX_PATHS
)
)
),
RecursiveIteratorIterator::LEAVES_ONLY,
RecursiveIteratorIterator::CATCH_GET_CHILD
);
foreach ($fileinfos as $pathname => $fileinfo) {
$fpn = $fileinfos->getSubPathname();
if (stripos($fpn , $args['s'])!==false) {
$results[] = array(
'title' => '/'.$fpn,
'permalink' => 'https://PATH-TO-YOUR-SERVER-OR.PROXY?f='.$fpn,
'info' => 'download',
);
}
}
return $results;
}
add_filter( 'better_internal_link_search_modifier-download', 'bils_download_search', 10, 2 );
class VisibleOnlyFilter extends RecursiveFilterIterator
{
public function accept()
{
$fileName = $this->getInnerIterator()->current()->getFileName();
$firstChar = $fileName[0];
return $firstChar !== '.';
}
}
class FilesOnlyFilter extends RecursiveFilterIterator
{
public function accept()
{
$iterator = $this->getInnerIterator();
// allow traversal
if ($iterator->hasChildren()) {
return true;
}
// filter entries, only allow true files
return $iterator->current()->isFile();
}
}