| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assets package. |
| 5 |
* |
| 6 |
* (c) Inpsyde GmbH |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Inpsyde\Assets\Handler; |
| 15 |
|
| 16 |
use Inpsyde\Assets\OutputFilter\AsyncScriptOutputFilter; |
| 17 |
use Inpsyde\Assets\OutputFilter\AttributesOutputFilter; |
| 18 |
use Inpsyde\Assets\OutputFilter\DeferScriptOutputFilter; |
| 19 |
use Inpsyde\Assets\OutputFilter\InlineAssetOutputFilter; |
| 20 |
use Inpsyde\Assets\Asset; |
| 21 |
use Inpsyde\Assets\Script; |
| 22 |
use WP_Scripts; |
| 23 |
|
| 24 |
class ScriptHandler implements AssetHandler, OutputFilterAwareAssetHandler |
| 25 |
{ |
| 26 |
use OutputFilterAwareAssetHandlerTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var \WP_Scripts |
| 30 |
*/ |
| 31 |
protected $wpScripts; |
| 32 |
|
| 33 |
/** |
| 34 |
* ScriptHandler constructor. |
| 35 |
* |
| 36 |
* @param \WP_Scripts $wpScripts |
| 37 |
* @param array<string, callable> $outputFilters |
| 38 |
*/ |
| 39 |
public function __construct(WP_Scripts $wpScripts, array $outputFilters = []) |
| 40 |
{ |
| 41 |
$this->withOutputFilter(AsyncScriptOutputFilter::class, new AsyncScriptOutputFilter()); |
| 42 |
$this->withOutputFilter(DeferScriptOutputFilter::class, new DeferScriptOutputFilter()); |
| 43 |
$this->withOutputFilter(InlineAssetOutputFilter::class, new InlineAssetOutputFilter()); |
| 44 |
$this->withOutputFilter(AttributesOutputFilter::class, new AttributesOutputFilter()); |
| 45 |
|
| 46 |
$this->wpScripts = $wpScripts; |
| 47 |
foreach ($outputFilters as $name => $callable) { |
| 48 |
$this->withOutputFilter($name, $callable); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function enqueue(Asset $asset): bool |
| 53 |
{ |
| 54 |
$this->register($asset); |
| 55 |
|
| 56 |
if ($asset->enqueue()) { |
| 57 |
wp_enqueue_script($asset->handle()); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
public function register(Asset $asset): bool |
| 66 |
{ |
| 67 |
/** @var Script $asset */ |
| 68 |
|
| 69 |
$handle = $asset->handle(); |
| 70 |
|
| 71 |
wp_register_script( |
| 72 |
$handle, |
| 73 |
$asset->url(), |
| 74 |
$asset->dependencies(), |
| 75 |
$asset->version(), |
| 76 |
$asset->inFooter() |
| 77 |
); |
| 78 |
|
| 79 |
if (count($asset->localize()) > 0) { |
| 80 |
foreach ($asset->localize() as $name => $args) { |
| 81 |
/** |
| 82 |
* Actually it is possible to use $args as scalar value for |
| 83 |
* \WP_Scripts::localize() - but it will produce a _doing_it_wrong(). |
| 84 |
* |
| 85 |
* @psalm-suppress MixedArgument |
| 86 |
*/ |
| 87 |
wp_localize_script($handle, $name, $args); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
foreach ($asset->inlineScripts() as $location => $data) { |
| 92 |
if (count($data) > 0) { |
| 93 |
wp_add_inline_script($handle, implode("\n", $data), $location); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$translation = $asset->translation(); |
| 98 |
if ($translation['domain'] !== '') { |
| 99 |
/** |
| 100 |
* The $path is allowed to be "null"- or a "string"-value. |
| 101 |
* @psalm-suppress PossiblyNullArgument |
| 102 |
*/ |
| 103 |
wp_set_script_translations($handle, $translation['domain'], $translation['path']); |
| 104 |
} |
| 105 |
|
| 106 |
if (count($asset->data()) > 0) { |
| 107 |
foreach ($asset->data() as $key => $value) { |
| 108 |
$this->wpScripts->add_data($handle, $key, $value); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
public function filterHook(): string |
| 116 |
{ |
| 117 |
return 'script_loader_tag'; |
| 118 |
} |
| 119 |
} |
| 120 |
|