wppaste
WordPress

WP_Filesystem_Base::search_for_folder( string $folder, string $base = '.', bool $loop = false ): string|false

Since
2.7.0
Source
wp-admin/includes/class-wp-filesystem-base.php:259
Locates a folder on the remote filesystem.

Description

Expects Windows sanitized path.

Compatibility

WordPress
since 2.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$folderstring
The folder to locate.
$basestringoptional
The folder to start searching from.Default: '.'
$loopbooloptional
If the function has recursed. Internal use only.Default: false

Return value

string|false
The location of the remote path, false to cease looping.

Performance profile

How much work a call to WP_Filesystem_Base::search_for_folder() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
37–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 118.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 24 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Filesystem_Base::search_for_folder() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($base) && $base !== "." && !isset($files[$last_path])37–40untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist()
!isset($files[$last_path])41–46->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist()
!empty($base) && $base !== "." && isset($files[$last_path])42–43untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), trailingslashit()
!empty($base) && $base !== "." && !isset($files[$last_path]) && $base !== "/"44–45untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), ->search_for_folder()
isset($files[$last_path])46–49->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), trailingslashit()
!empty($base) && $base !== "." && !isset($files[$last_path])47–50untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist()
!isset($files[$last_path]) && $base !== "/"48–51->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), ->search_for_folder()
!isset($files[$last_path])51–56->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist()
!empty($base) && $base !== "." && isset($files[$last_path])52–53untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), __(), printf(), trailingslashit()
!empty($base) && $base !== "." && isset($files[$last_path])52–53untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), trailingslashit()
!empty($base) && $base !== "." && !isset($files[$last_path]) && $base !== "/"54–55untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), ->search_for_folder()
isset($files[$last_path])56–59->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), __(), printf(), trailingslashit()
12 further outcomes, up to 88 instructions
isset($files[$last_path])56–59->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), trailingslashit()
!isset($files[$last_path]) && $base !== "/"58–61->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), ->search_for_folder()
!empty($base) && $base !== "." && isset($files[$last_path])62–63untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), __(), printf(), trailingslashit()
!empty($base) && $base !== "." && !$folder_parts && $index !== false && isset($files[$key])63untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), array_slice(), ->search_for_folder()
isset($files[$last_path])66–69->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), __(), printf(), trailingslashit()
!$folder_parts && $index !== false && isset($files[$key])67–69->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), array_slice(), ->search_for_folder()
!empty($base) && $base !== "." && !$folder_parts && $index !== false && isset($files[$key])72untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), __(), printf(), array_slice(), ->search_for_folder()
!empty($base) && $base !== "." && !$folder_parts && $index !== false && isset($files[$key])73untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), array_slice(), ->search_for_folder()
!$folder_parts && $index !== false && isset($files[$key])76–78->cwd(), trailingslashit(), untrailingslashit(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), __(), printf(), array_slice(), ->search_for_folder()
!$folder_parts && $index !== false && isset($files[$key])77–79->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), array_slice(), ->search_for_folder()
!empty($base) && $base !== "." && !$folder_parts && $index !== false && isset($files[$key])82untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), __(), printf(), array_slice(), ->search_for_folder()
!$folder_parts && $index !== false && isset($files[$key])86–88->cwd(), trailingslashit(), untrailingslashit(), __(), printf(), explode(), array_keys(), array_pop(), ->dirlist(), path_join(), trailingslashit(), __(), printf(), array_slice(), ->search_for_folder()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11837–8813
8.511837–8813
8.411837–88133 fewer instructions than PHP 8.3
8.312137–9113
8.212137–9113
8.112137–9113
7.412137–9113

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 2

Source code

	public function search_for_folder( $folder, $base = '.', $loop = false ) {		if ( empty( $base ) || '.' === $base ) {			$base = trailingslashit( $this->cwd() );		} 		$folder = untrailingslashit( $folder ); 		if ( $this->verbose ) {			/* translators: 1: Folder to locate, 2: Folder to start searching from. */			printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br />\n", $folder, $base );		} 		$folder_parts     = explode( '/', $folder );		$folder_part_keys = array_keys( $folder_parts );		$last_index       = array_pop( $folder_part_keys );		$last_path        = $folder_parts[ $last_index ]; 		$files = $this->dirlist( $base ); 		foreach ( $folder_parts as $index => $key ) {			if ( $index === $last_index ) {				continue; // We want this to be caught by the next code block.			} 			/*			 * Working from /home/ to /user/ to /wordpress/ see if that file exists within			 * the current folder, If it's found, change into it and follow through looking			 * for it. If it can't find WordPress down that route, it'll continue onto the next			 * folder level, and see if that matches, and so on. If it reaches the end, and still			 * can't find it, it'll return false for the entire function.			 */			if ( isset( $files[ $key ] ) ) { 				// Let's try that folder:				$newdir = trailingslashit( path_join( $base, $key ) ); 				if ( $this->verbose ) {					/* translators: %s: Directory name. */					printf( "\n" . __( 'Changing to %s' ) . "<br />\n", $newdir );				} 				// Only search for the remaining path tokens in the directory, not the full path again.				$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );				$ret       = $this->search_for_folder( $newfolder, $newdir, $loop ); 				if ( $ret ) {					return $ret;				}			}		} 		/*		 * Only check this as a last resort, to prevent locating the incorrect install.		 * All above procedures will fail quickly if this is the right branch to take.		 */		if ( isset( $files[ $last_path ] ) ) {			if ( $this->verbose ) {				/* translators: %s: Directory name. */				printf( "\n" . __( 'Found %s' ) . "<br />\n", $base . $last_path );			} 			return trailingslashit( $base . $last_path );		} 		/*		 * Prevent this function from looping again.		 * No need to proceed if we've just searched in `/`.		 */		if ( $loop || '/' === $base ) {			return false;		} 		/*		 * As an extra last resort, Change back to / if the folder wasn't found.		 * This comes into effect when the CWD is /home/user/ but WP is at /var/www/....		 */		return $this->search_for_folder( $folder, '/', true );	}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/class-wp-filesystem-base.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.