Skip to content

Commit dc11f5b

Browse files
committed
Some fixes about transient
1 parent ffda0b0 commit dc11f5b

1 file changed

Lines changed: 86 additions & 5 deletions

File tree

src/Theme/Infrastructure/Providers/ThemeServiceProvider.php

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public function register(): void
6464
$this->initializeUtilityClasses();
6565
$this->registerThemeServices();
6666
$this->registerCommands();
67+
68+
69+
6770
}
6871

6972
/**
@@ -76,6 +79,12 @@ public function boot(Filter $filter): void
7679
$this->filter = $filter;
7780
$this->registerThemeDirectories();
7881
$this->setupThemeBoot();
82+
83+
84+
add_action('after_setup_theme', function () {
85+
// dd(get_theme_roots());
86+
});
87+
7988
}
8089

8190
/**
@@ -244,12 +253,87 @@ private function registerThemeDirectories(): void
244253

245254
// Hook into WordPress option system to reset theme root when needed
246255
$this->filter->add('option_stylesheet_root', $this->resetThemeRootOption(...), PHP_INT_MAX);
256+
$this->filter->add('site_transient_theme_roots', $this->handleThemeRootsTransient(...), PHP_INT_MAX);
247257

248258
if ($this->isValidThemeDirectory($baseThemePath)) {
249259
$this->addToGlobalThemeDirectories($baseThemePath);
250260
}
251261
}
252262

263+
/**
264+
* Handle the site_transient_theme_roots filter.
265+
*
266+
* Normalizes theme roots by fixing invalid paths and updates
267+
* the transient only when necessary.
268+
*
269+
* @param array|bool $roots Theme roots array from WordPress
270+
* @return array Normalized theme roots
271+
*/
272+
private function handleThemeRootsTransient(array|bool $roots): array|bool
273+
{
274+
if (!$roots) {
275+
return $roots;
276+
}
277+
278+
$updatedRoots = $this->normalizeThemeRoots($roots);
279+
280+
if ($updatedRoots !== $roots) {
281+
set_site_transient('theme_roots', $updatedRoots);
282+
}
283+
284+
return $updatedRoots;
285+
}
286+
287+
/**
288+
* Normalize theme roots by fixing invalid paths.
289+
*
290+
* @param array $roots Theme roots array from WordPress
291+
* @return array Normalized theme roots
292+
*/
293+
private function normalizeThemeRoots(array $roots): array
294+
{
295+
$normalizedRoots = [];
296+
297+
foreach ($roots as $themeSlug => $themePath) {
298+
$normalizedRoots[$themeSlug] = $this->resolveThemePath($themePath);
299+
}
300+
301+
return $normalizedRoots;
302+
}
303+
304+
/**
305+
* Resolve theme path to a valid directory.
306+
*
307+
* @param string $themePath Original theme path
308+
* @return string Valid theme directory path
309+
*/
310+
private function resolveThemePath(string $themePath): string
311+
{
312+
// Si le chemin existe déjà, on le garde
313+
if (file_exists($themePath)) {
314+
return $themePath;
315+
}
316+
317+
// Si c'est un chemin WordPress standard, utiliser le répertoire par défaut
318+
if ($this->isWordPressThemePath($themePath)) {
319+
return WP_CONTENT_DIR . '/themes';
320+
}
321+
322+
// Sinon, utiliser notre répertoire de base
323+
return $this->getBaseThemePath();
324+
}
325+
326+
/**
327+
* Check if the theme path is a WordPress standard theme path.
328+
*
329+
* @param string $themePath Path to check
330+
* @return bool True if it's a WordPress theme path
331+
*/
332+
private function isWordPressThemePath(string $themePath): bool
333+
{
334+
return str_contains($themePath, '/content/themes/');
335+
}
336+
253337
/**
254338
* Get the base theme path from configuration.
255339
*
@@ -259,11 +343,8 @@ private function registerThemeDirectories(): void
259343
*/
260344
private function getBaseThemePath(): string
261345
{
262-
try {
263-
return ThemeConfig::get('path', base_path('themes'));
264-
} catch (\RuntimeException) {
265-
return base_path('themes');
266-
}
346+
return base_path('themes');
347+
267348
}
268349

269350
/**

0 commit comments

Comments
 (0)