$roots Schluessel plugins, mu_plugins, themes, abspath. * @return array{type:string,name:string,key:string} */ public static function classify( $file, array $roots ) { $file = self::normalize( $file ); if ( '' === $file ) { return self::result( self::TYPE_OTHER, '' ); } $rest = self::relative( $file, isset( $roots['plugins'] ) ? $roots['plugins'] : '' ); if ( null !== $rest ) { return self::result( self::TYPE_PLUGIN, self::first_segment( $rest ) ); } $rest = self::relative( $file, isset( $roots['mu_plugins'] ) ? $roots['mu_plugins'] : '' ); if ( null !== $rest ) { return self::result( self::TYPE_MU_PLUGIN, self::first_segment( $rest ) ); } $rest = self::relative( $file, isset( $roots['themes'] ) ? $roots['themes'] : '' ); if ( null !== $rest ) { return self::result( self::TYPE_THEME, self::first_segment( $rest ) ); } $rest = self::relative( $file, isset( $roots['abspath'] ) ? $roots['abspath'] : '' ); if ( null !== $rest && preg_match( '#^(wp-includes|wp-admin)/#', $rest ) ) { return self::result( self::TYPE_CORE, '' ); } return self::result( self::TYPE_OTHER, '' ); } /** * Vereinheitlicht Trennzeichen und doppelte Schraegstriche. */ public static function normalize( $path ) { if ( ! is_string( $path ) || '' === $path ) { return ''; } $path = str_replace( '\\', '/', $path ); return (string) preg_replace( '#/+#', '/', $path ); } /** * Teil des Pfads unterhalb des Wurzelordners, sonst null. */ private static function relative( $file, $root ) { $root = self::normalize( $root ); if ( '' === $root ) { return null; } $root = rtrim( $root, '/' ) . '/'; if ( strlen( $file ) <= strlen( $root ) ) { return null; } $head = substr( $file, 0, strlen( $root ) ); // Windows-Laufwerksbuchstaben ohne Beachtung der Gross-/Kleinschreibung. $same = preg_match( '#^[A-Za-z]:/#', $root ) ? 0 === strcasecmp( $head, $root ) : $head === $root; return $same ? substr( $file, strlen( $root ) ) : null; } private static function first_segment( $rest ) { $pos = strpos( $rest, '/' ); return false === $pos ? $rest : substr( $rest, 0, $pos ); } private static function result( $type, $name ) { return array( 'type' => $type, 'name' => $name, 'key' => '' === $name ? $type : $type . ':' . $name, ); } }