WP_Filesystem_Base::find_folder( string $folder ): string|false
- Since
- 2.7.0
- Source
wp-admin/includes/class-wp-filesystem-base.php:177
Description
Assumes that on Windows systems, Stripping off the Drive letter is OK Sanitizes \ to / in Windows filepaths.
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.
Return value
string|false- The location of the remote path, false on failure.
Performance profile
How much work a call to WP_Filesystem_Base::find_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
- Scaling
- Scales with input
- Instructions
- 7–64
- Plugin surface
- None
- Called by
- 5
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Filesystem_Base::find_folder() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($folder) | 7 | none |
!isset($folder) | 21 | stripos(), trailingslashit() |
| always | 26–39 | stripos() |
!isset($folder) && !->exists() | 33–49 | stripos(), ->exists(), ->search_for_folder() |
!isset($folder) && ->exists() | 35–48 | stripos(), ->exists(), trailingslashit() |
!isset($folder) && !$constant_overrides && defined() && $folder === false | 37 | stripos(), defined(), constant(), trailingslashit() |
!isset($folder) && !$constant_overrides && defined() && ->is_dir() | 63–64 | stripos(), defined(), stripos(), preg_quote(), constant(), trailingslashit(), trailingslashit(), ->is_dir() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 127 | 7–64 | 15 | |
| 8.5 | 127 | 7–64 | 15 | |
| 8.4 | 127 | 7–64 | 15 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 139 | 7–67 | 15 | |
| 8.2 | 139 | 7–67 | 15 | |
| 8.1 | 139 | 7–67 | 15 | |
| 7.4 | 139 | 7–67 | 15 |
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 · 5
- stripos()
- trailingslashit()Appends a trailing slash.
- WP_Filesystem_Base::is_dir()Checks if resource is a directory.
- WP_Filesystem_Base::exists()Checks if a file or directory exists.
- WP_Filesystem_Base::search_for_folder()Locates a folder on the remote filesystem.
Used by · 5
- WP_Filesystem_Base::abspath()Returns the path on the remote filesystem of ABSPATH.
- WP_Filesystem_Base::wp_content_dir()Returns the path on the remote filesystem of WP_CONTENT_DIR.
- WP_Filesystem_Base::wp_lang_dir()Returns the path on the remote filesystem of WP_LANG_DIR.
- WP_Filesystem_Base::wp_plugins_dir()Returns the path on the remote filesystem of WP_PLUGIN_DIR.
- WP_Filesystem_Base::wp_themes_dir()Returns the path on the remote filesystem of the Themes Directory.
Source code
public function find_folder( $folder ) { if ( isset( $this->cache[ $folder ] ) ) { return $this->cache[ $folder ]; } if ( stripos( $this->method, 'ftp' ) !== false ) { $constant_overrides = array( 'FTP_BASE' => ABSPATH, 'FTP_CONTENT_DIR' => WP_CONTENT_DIR, 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR, 'FTP_LANG_DIR' => WP_LANG_DIR, ); // Direct matches ( folder = CONSTANT/ ). foreach ( $constant_overrides as $constant => $dir ) { if ( ! defined( $constant ) ) { continue; } if ( $folder === $dir ) { return trailingslashit( constant( $constant ) ); } } // Prefix matches ( folder = CONSTANT/subdir ), foreach ( $constant_overrides as $constant => $dir ) { if ( ! defined( $constant ) ) { continue; } if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir. $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder ); $potential_folder = trailingslashit( $potential_folder ); if ( $this->is_dir( $potential_folder ) ) { $this->cache[ $folder ] = $potential_folder; return $potential_folder; } } } } elseif ( 'direct' === $this->method ) { $folder = str_replace( '\\', '/', $folder ); // Windows path sanitization. return trailingslashit( $folder ); } $folder = preg_replace( '|^([a-z]{1}):|i', '', $folder ); // Strip out Windows drive letter if it's there. $folder = str_replace( '\\', '/', $folder ); // Windows path sanitization. if ( isset( $this->cache[ $folder ] ) ) { return $this->cache[ $folder ]; } if ( $this->exists( $folder ) ) { // Folder exists at that absolute path. $folder = trailingslashit( $folder ); $this->cache[ $folder ] = $folder; return $folder; } $return = $this->search_for_folder( $folder ); if ( $return ) { $this->cache[ $folder ] = $return; } return $return; }Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.