Improve wp acorn compability
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace AcornPlugin\Acorn;
|
||||
|
||||
/**
|
||||
* Elects a single Acorn booter per request across all Acorn plugins.
|
||||
*
|
||||
* did_action() is a read-only probe of WordPress' action bookkeeping — unlike
|
||||
* Roots\Acorn\Application::getInstance(), which force-creates and installs an
|
||||
* empty container as the global singleton. The action name is a cross-plugin
|
||||
* contract: every Acorn plugin in the installation must use the same.
|
||||
*/
|
||||
final class BootCoordinator
|
||||
{
|
||||
public const BOOTED_ACTION = 'acorn/booted';
|
||||
|
||||
public function shouldBoot(): bool
|
||||
{
|
||||
return ! did_action(self::BOOTED_ACTION);
|
||||
}
|
||||
|
||||
public function markBooted(): void
|
||||
{
|
||||
// Deliberately unprefixed: BOOTED_ACTION is a shared cross-plugin
|
||||
// contract, so every cooperating Acorn plugin fires the same hook.
|
||||
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
|
||||
do_action(self::BOOTED_ACTION);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace AcornPlugin\Acorn;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
|
||||
/**
|
||||
* Emits Livewire's frontend assets exactly once, from the plugin that owns the
|
||||
* Acorn boot. Livewire v4's @livewireScripts self-initialises from the data-*
|
||||
* attributes on the injected script.
|
||||
* WordPress never dispatches Laravel's RequestHandled event, so the
|
||||
* directives must be echoed into wp_head/wp_footer by hand.
|
||||
*/
|
||||
final class LivewireAssets
|
||||
{
|
||||
public static function registerOnce(): void
|
||||
{
|
||||
// Livewire renders its own trusted <style>/<script> markup — escaping it
|
||||
// would corrupt the output, so the EscapeOutput sniff is suppressed here.
|
||||
add_action('wp_head', static function (): void {
|
||||
echo Blade::render('@livewireStyles'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
});
|
||||
|
||||
add_action('wp_footer', static function (): void {
|
||||
echo Blade::render('@livewireScripts'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace AcornPlugin\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
/**
|
||||
* Registers this plugin's views, Livewire component and shortcode against the
|
||||
* shared Acorn container. Auto-discovered via composer.json extra.acorn.providers,
|
||||
* so it is registered by whichever plugin boots Acorn. Everything is namespaced,
|
||||
* so the plugin coexists with other Acorn plugins in the same container.
|
||||
*
|
||||
* Extends Illuminate\Support\ServiceProvider (not Roots\Acorn\ServiceProvider,
|
||||
* which is `final` in Acorn v6 and exists only to expose defaultProviders()).
|
||||
* Acorn's own providers (Sage, Assets, View …) all extend the Illuminate base,
|
||||
* which carries the protected loadViewsFrom() helper this provider relies on.
|
||||
*/
|
||||
class PluginServiceProvider extends ServiceProvider
|
||||
{
|
||||
/** Shared kebab prefix for this plugin's Blade + Livewire namespaces. */
|
||||
private const NAMESPACE = 'acorn-starter';
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadViewsFrom($this->resourcePath('views'), self::NAMESPACE);
|
||||
$this->registerLivewireComponents();
|
||||
$this->registerShortcodes();
|
||||
}
|
||||
|
||||
private function registerLivewireComponents(): void
|
||||
{
|
||||
if (! class_exists(Livewire::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Livewire::addNamespace(self::NAMESPACE, $this->resourcePath('views/components'));
|
||||
}
|
||||
|
||||
private function registerShortcodes(): void
|
||||
{
|
||||
add_shortcode('acorn_hello', fn () => view(self::NAMESPACE . '::hello-world')->render());
|
||||
}
|
||||
|
||||
private function resourcePath(string $path = ''): string
|
||||
{
|
||||
return dirname(__DIR__, 2) . '/resources/' . ltrim($path, '/');
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
per aspera ad astra.
|
||||
Reference in New Issue
Block a user