Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiled views path in wp-admin context #1

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ define(
],
'path_to_compiled_views' => __DIR__ . '/wp-content/themes/<your-theme>/dist/blade', // Where you want Blade to save compiled files.
'never_expire_cache' => false, // Use `true` on production environments.
'base_path' => __DIR__, // The base path that should be removed from paths before hashing.
]
);
```
Expand Down
5 changes: 3 additions & 2 deletions bin/compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
// Check if a config path was set.
if ( empty( $config_file ) || ! file_exists( $config_file ) ) {
echo "\033[31m✗ Path to config file missing!\n";
exit( 0 );
exit( 1 );
}

// Check if vendor autoload file was set.
if ( empty( $vendor_autoload_file ) || ! file_exists( $vendor_autoload_file ) ) {
echo "\033[31m✗ Path to config file missing!\n";
exit( 0 );
exit( 1 );
}

// Load files.
Expand All @@ -37,6 +37,7 @@
$blade = new Blade();
$blade->paths_to_views = $blade_config['paths_to_views'] ?? [];
$blade->path_to_compiled_views = $blade_config['path_to_compiled_views'] ?? '';
$blade->base_path = $blade_config['base_path'] ?? '';
$blade->initialize();

// Build Blade cache.
Expand Down
9 changes: 8 additions & 1 deletion inc/class-blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class Blade {
*/
public string $path_to_compiled_views = '';

/**
* Store the base path.
*
* @var string Base path.
*/
public string $base_path = '';

/**
* Store view factory.
*
Expand Down Expand Up @@ -93,7 +100,7 @@ public function initialize(): void {

// Create View Factory capable of rendering PHP and Blade templates.
$view_resolver = new EngineResolver();
$this->blade_compiler = new BladeCompiler( $filesystem, $this->path_to_compiled_views );
$this->blade_compiler = new BladeCompiler( $filesystem, $this->path_to_compiled_views, $this->base_path );

$this->blade_compiler->never_expire_cache = $this->never_expire_cache;

Expand Down
15 changes: 0 additions & 15 deletions inc/class-compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Travelopia\Blade;

use ErrorException;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;

/**
Expand All @@ -26,20 +25,6 @@ class Compiler extends BladeCompiler {
*/
public bool $never_expire_cache = false;

/**
* Get the path to the compiled version of a view.
*
* @param string $path Path to view.
*
* @return string
*/
public function getCompiledPath( $path ): string { // phpcs:ignore
// Get path.
$path = str_replace( getcwd(), '', $path );

return $this->cachePath . '/' . sha1( 'v2' . Str::after( $path, $this->basePath ) ) . '.' . $this->compiledExtension; // phpcs:ignore
}

/**
* Determine if the view at the given path is expired.
*
Expand Down
1 change: 1 addition & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function get_blade(): Blade {
$blade->paths_to_views = apply_filters( 'wordpress_blade_view_paths', $blade_config['paths_to_views'] );
$blade->path_to_compiled_views = apply_filters( 'wordpress_blade_compiled_path', $blade_config['path_to_compiled_views'] );
$blade->never_expire_cache = apply_filters( 'wordpress_blade_never_expire_cache', $blade_config['never_expire_cache'] );
$blade->base_path = apply_filters( 'wordpress_blade_base_path', $blade_config['base_path'] );
$blade->view_callback = __NAMESPACE__ . '\\view_callback';
$blade->initialize();

Expand Down