Completed
Pull Request — staging (#840)
by
unknown
18:46
created

AssetsAwareness::get_assets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Assets;
11
12
use YIKES\EasyForms\Exception\InvalidAssetHandle;
13
14
/**
15
 * Trait AssetsAwareness
16
 *
17
 * @since   %VERSION%
18
 *
19
 * @package YIKES\EasyForms
20
 * @author  Freddie Mixell
21
 */
22
trait AssetsAwareness {
23
24
	/**
25
	 * Assets handler instance to use.
26
	 *
27
	 * @since %VERSION%
28
	 *
29
	 * @var AssetsHandler
30
	 */
31
	protected $assets_handler;
32
33
	/**
34
	 * Array of asset objects.
35
	 *
36
	 * @since %VERSION%
37
	 * @var Asset[]
38
	 */
39
	protected $assets = [];
40
41
	/**
42
	 * Get the array of known assets.
43
	 *
44
	 * @since %VERSION%
45
	 *
46
	 * @return Asset[]
47
	 */
48
	protected function get_assets() {
49
		if ( empty( $this->assets ) ) {
50
			$this->load_assets();
51
		}
52
53
		return $this->assets;
54
	}
55
56
	/**
57
	 * Register the known assets.
58
	 *
59
	 * @since %VERSION%
60
	 */
61
	protected function register_assets() {
62
		foreach ( $this->get_assets() as $asset ) {
63
			$this->assets_handler->add( $asset );
64
		}
65
	}
66
67
	/**
68
	 * Enqueue the known assets.
69
	 *
70
	 * @since %VERSION%
71
	 *
72
	 * @throws InvalidAssetHandle If the passed-in asset handle is not valid.
73
	 */
74
	protected function enqueue_assets() {
75
		foreach ( $this->get_assets() as $asset ) {
76
			$this->assets_handler->enqueue( $asset );
77
		}
78
	}
79
80
	/**
81
	 * Enqueue a single asset.
82
	 *
83
	 * @since %VERSION%
84
	 *
85
	 * @param string $handle Handle of the asset to enqueue.
86
	 *
87
	 * @throws InvalidAssetHandle If the passed-in asset handle is not valid.
88
	 */
89
	protected function enqueue_asset( $handle ) {
90
		$this->assets_handler->enqueue_handle( $handle );
91
	}
92
93
	/**
94
	 * Set the assets handler to use within this object.
95
	 *
96
	 * @since %VERSION%
97
	 *
98
	 * @param AssetsHandler $assets Assets handler to use.
99
	 */
100
	public function with_assets_handler( AssetsHandler $assets ) {
101
		$this->assets_handler = $assets;
102
	}
103
104
	/**
105
	 * Load asset objects for use.
106
	 *
107
	 * @since %VERSION%
108
	 */
109
	protected function load_assets() {
110
		$this->assets = [];
111
	}
112
}
113