search_theme_directories() │ WP 2.9.0
Знаходить усі зареєстровані директорії тем з каталогу themes.
Працює на основі глобальної змінної $wp_theme_directories .
Повертає
Массив|false
. Масив даних папок тем.
Використання
search_theme_directories( $force );
-
$force
(логічний) -
Ставимо true, якщо необхідно запустити нове сканування директорій.
Типово: false
Приклади
#1 Отримаємо дані папок всіх тем
$theme_dirs = search_theme_directories();
print_r ($ theme_dirs);
нотатки
- Global. Масив. $wp_theme_directories
список змін
Код search_theme_directories() search theme directories WP 6.0.2
function search_theme_directories( $force = false ) {
Global $wp_theme_directories;
static $found_themes = null;
if ( empty( $wp_theme_directories ) ) {
return false;
}
if ( ! $force && isset( $found_themes ) ) {
return $found_themes;
}
$found_themes = array();
$wp_theme_directories = (array) $wp_theme_directories;
$ relative_theme_roots = array();
foreach ( $wp_theme_directories as $theme_root ) {
if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
$relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
} else {
$relative_theme_roots[ $theme_root ] = $theme_root;
}
}
$cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' );
if ($ cache_expiration) {
$cached_roots = get_site_transient( 'theme_roots');
if ( is_array ( $ cached_roots ) ) {
foreach ( $cached_roots as $theme_dir => $theme_root ) {
if ( ! isset ( $ relative_theme_roots [ $ theme_root ] ) ) {
continue;
}
$found_themes[ $theme_dir ] = array(
'theme_file' => $theme_dir . '/style.css',
'theme_root' => $ relative_theme_roots [ $ theme_root ],
);
}
return $found_themes;
}
if ( ! is_int( $cache_expiration ) ) {
$cache_expiration = 30 * MINUTE_IN_SECONDS;
}
} else {
$cache_expiration = 30 * MINUTE_IN_SECONDS;
}
foreach ( $wp_theme_directories as $theme_root ) {
$ dirs = @ scandir ($ theme_root);
if ( ! $dirs ) {
trigger_error( "$theme_root is not readable", E_USER_NOTICE );
continue;
}
foreach ($dirs as $dir) {
if ( ! is_dir( $theme_root . '/' . $dir ) || '.' === $dir[0] || 'CVS' === $dir ) {
continue;
}
if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
$found_themes[ $dir ] = array(
'theme_file' => $dir . '/style.css',
'theme_root' => $theme_root,
);
} else {
$found_theme = false;
$sub_dirs = @ scandir( $theme_root . '/' . $dir );
if (! $sub_dirs) {
trigger_error( "$theme_root/$dir не readable", E_USER_NOTICE );
continue;
}
foreach ( $sub_dirs as $sub_dir ) {
if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || '.' === $dir[0] || 'CVS' === $dir ) {
continue;
}
if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) {
continue;
}
$found_themes[ $dir . '/'. $sub_dir ] = array(
'theme_file' => $dir . '/'. $ sub_dir . '/style.css',
'theme_root' => $theme_root,
);
$found_theme = true;
}
if ( ! $found_theme ) {
$found_themes[ $dir ] = array(
'theme_file' => $dir . '/style.css',
'theme_root' => $theme_root,
);
}
}
}
}
asort($ found_themes);
$theme_roots = array();
$relative_theme_roots = array_flip( $relative_theme_roots );
foreach ( $found_themes as $theme_dir => $theme_data ) {
$theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ];
}
if ( get_site_transient( 'theme_roots' ) != $theme_roots ) {
set_site_transient( 'theme_roots', $theme_roots, $cache_expiration);
}
return $found_themes;
}
Зв’язані функції