Skip to content

Instantly share code, notes, and snippets.

@devbeno
Created April 6, 2022 18:48
Show Gist options
  • Save devbeno/0362b8ccf1c525c27581650b9b4a3451 to your computer and use it in GitHub Desktop.
Save devbeno/0362b8ccf1c525c27581650b9b4a3451 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Illuminate\View\Factory;
use Illuminate\Events\Dispatcher;
use Illuminate\View\FileViewFinder;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Compilers\BladeCompiler;
$GLOBALS['filesystem'] = new Filesystem;
$GLOBALS['compiler'] = new BladeCompiler($GLOBALS['filesystem'], getCompiledTemplateDirectory());
add_action('init', function() {
$GLOBALS['compiler']->component('partials.the_loop', 'loop');
});
add_action('wp_enqueue_scripts', function () {
if (!function_exists('enablejQuery') || !enablejQuery()) {
wp_deregister_script('jquery');
}
wp_enqueue_style('site', get_stylesheet_uri());
wp_enqueue_script('site', get_stylesheet_directory_uri() . '/public/js/site.js');
});
add_filter('template_include', function ($template) {
$templateName = wp_basename(wp_basename($template, '.php'), '.blade');
if (getBladeViewFactory()->exists($templateName)) {
$GLOBALS['template_name'] = $templateName;
$template = __DIR__ . '/../core/index.php';
}
return $template;
});
collect([
'index',
'404',
'archive',
'author',
'category',
'tag',
'taxonomy',
'date',
'embed',
'home',
'frontpage',
'privacypolicy',
'page',
'paged',
'search',
'single',
'singular',
'attachment'
])->each(function($type) {
add_filter("{$type}_template_hierarchy", function($templates) {
return collect($templates)->map(function($template) {
$filename = wp_basename($template, '.php');
return ["templates/$filename.blade.php", $template];
})->flatten()
->toArray();
});
});
function getBladeViewFactory()
{
$viewResolver = new EngineResolver;
$viewResolver->register('blade', function () {
return new CompilerEngine($GLOBALS['compiler']);
});
$viewResolver->register('php', function () {
return new PhpEngine;
});
return new Factory(
$viewResolver,
new FileViewFinder($GLOBALS['filesystem'], getTemplateDirectory()),
new Dispatcher()
);
}
function getTemplateDirectory()
{
return [__DIR__ . '/../templates/'];
}
function getCompiledTemplateDirectory()
{
return __DIR__ . '/../compiled/';
}
function enablejQuery()
{
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment