Completed
Branch EDTR/barista (ea23a2)
by
unknown
09:50 queued 34s
created

Barista::addInlineData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
/**
6
 * @package EventEspresso\core\services\assets
7
 * @author  Manzoor Wani, Brent Christensen
8
 * @since   $VID:$
9
 */
10
class Barista
11
{
12
13
    const DEPENDENCY_LIST_REGISTERED = 'registered';
14
15
16
    /**
17
     * @var AssetManifest
18
     */
19
    private $asset_manifest;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $initialized = false;
25
26
27
    /**
28
     * Barista constructor.
29
     *
30
     * @param AssetManifest   $asset_manifest
31
     */
32
    public function __construct(AssetManifest $asset_manifest)
33
    {
34
        $this->asset_manifest = $asset_manifest;
35
    }
36
37
38
    /**
39
     * @return void
40
     */
41
    public function initialize()
42
    {
43
        if (! $this->initialized) {
44
            add_action('wp_enqueue_scripts', [$this, 'registerScripts'], 0);
45
            add_action('admin_enqueue_scripts', [$this, 'registerScripts'], 0);
46
            add_action('wp_enqueue_scripts', [$this, 'registerPackagesStyles'], 0);
47
            add_action('admin_enqueue_scripts', [$this, 'registerPackagesStyles'], 0);
48
            add_action('wp_enqueue_scripts', [$this, 'addInlineData'], 10);
49
            add_action('admin_enqueue_scripts', [$this, 'addInlineData'], 10);
50
            $this->initialized = true;
51
        }
52
    }
53
54
55
    /**
56
     * Registers all the WordPress packages scripts that are in the standardized
57
     * `build/` location.
58
     *
59
     * @return void
60
     */
61
    public function registerScripts()
62
    {
63
        $entry_points = $this->asset_manifest->getEntryPoints();
64
        foreach ($entry_points as $entry_point) {
65
            if ($this->asset_manifest->hasAsset($entry_point)) {
66
                $handle = $this->asset_manifest->getAssetHandle($entry_point);
67
                $source = $this->asset_manifest->getAssetUrl($entry_point);
68
                $dependencies = $this->asset_manifest->getAssetDependencies($entry_point);
69
                $version = $this->asset_manifest->getAssetVersion($entry_point);
70
                wp_register_script($handle, $source, $dependencies, $version, true);
71
            }
72
        }
73
    }
74
75
76
    /**
77
     * Registers all the packages and domain styles that are in the build folder.
78
     *
79
     * @return void
80
     */
81
    public function registerPackagesStyles()
82
    {
83
        $entry_points = $this->asset_manifest->getEntryPoints();
84
        foreach ($entry_points as $entry_point) {
85
            if ($this->asset_manifest->hasAsset($entry_point, AssetManifest::ASSET_EXT_CSS)) {
86
                $handle = $this->asset_manifest->getAssetHandle($entry_point);
87
                $source = $this->asset_manifest->getAssetUrl($entry_point, AssetManifest::ASSET_EXT_CSS);
88
                $dependencies = $this->asset_manifest->getAssetDependencies($entry_point, AssetManifest::ASSET_EXT_CSS);
89
                $version = $this->asset_manifest->getAssetVersion($entry_point, AssetManifest::ASSET_EXT_CSS);
90
                wp_register_style($handle, $source, $dependencies, $version, 'all');
91
            }
92
        }
93
    }
94
}
95