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

StyleAsset::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 6
dl 0
loc 15
rs 9.7666
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 Closure;
13
use YIKES\EasyForms\Plugin;
14
use YIKES\EasyForms\Settings\DisableFrontEndCss;
15
16
/**
17
 * Class StyleAsset.
18
 *
19
 * @since   %VERSION%
20
 *
21
 * @package Yikes\EasyForms\Assets
22
 * @author  Freddie Mixell
23
 */
24
final class StyleAsset extends BaseAsset {
25
26
	const MEDIA_ALL    = 'all';
27
	const MEDIA_PRINT  = 'print';
28
	const MEDIA_SCREEN = 'screen';
29
	const DEPENDENCIES = [];
30
	const VERSION      = Plugin::VERSION;
31
	const DISABLEABLE  = false;
32
33
	const DEFAULT_EXTENSION = 'css';
34
35
	/**
36
	 * Source location of the asset.
37
	 *
38
	 * @since %VERSION%
39
	 *
40
	 * @var string
41
	 */
42
	protected $source;
43
44
	/**
45
	 * Dependencies of the asset.
46
	 *
47
	 * @since %VERSION%
48
	 *
49
	 * @var string[]
50
	 */
51
	protected $dependencies;
52
53
	/**
54
	 * Version of the asset.
55
	 *
56
	 * @since %VERSION%
57
	 *
58
	 * @var string|bool|null
59
	 */
60
	protected $version;
61
62
	/**
63
	 * Media for which the asset is defined.
64
	 *
65
	 * @since %VERSION%
66
	 *
67
	 * @var string
68
	 */
69
	protected $media;
70
71
	/**
72
	 * Whether this asset can be disabled.
73
	 *
74
	 * @since %VERSION%
75
	 *
76
	 * @var string
77
	 */
78
	protected $disableable;
79
80
	/**
81
	 * Instantiate a StyleAsset object.
82
	 *
83
	 * @since %VERSION%
84
	 *
85
	 * @param string           $handle       Handle of the asset.
86
	 * @param string           $source       Source location of the asset.
87
	 * @param array            $dependencies Optional. Dependencies of the asset.
88
	 * @param string|bool|null $version      Optional. Version of the asset.
89
	 * @param string           $media        Media for which the asset is defined.
90
	 * @param bool             $disableable  Whether this script can be disabled.
91
	 */
92
	public function __construct(
93
		$handle,
94
		$source,
95
		$dependencies = self::DEPENDENCIES,
96
		$version = self::VERSION,
97
		$media = self::MEDIA_ALL,
98
		$disableable = self::DISABLEABLE
99
	) {
100
		$this->handle       = $handle;
101
		$this->source       = $this->normalize_source( $source, static::DEFAULT_EXTENSION );
102
		$this->dependencies = (array) $dependencies;
103
		$this->version      = $version;
104
		$this->media        = $media;
105
		$this->disableable  = $disableable;
0 ignored issues
show
Documentation Bug introduced by
The property $disableable was declared of type string, but $disableable is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
106
	}
107
108
	/**
109
	 * Get the enqueue closure to use.
110
	 *
111
	 * @since %VERSION%
112
	 *
113
	 * @return Closure
114
	 */
115 View Code Duplication
	protected function get_register_closure() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
		return function () {
117
			if ( wp_script_is( $this->handle, 'registered' ) ) {
118
				return;
119
			}
120
121
			if ( $this->is_disabled() ) {
122
				return;
123
			}
124
125
			wp_register_style(
126
				$this->handle,
127
				$this->source,
128
				$this->dependencies,
129
				$this->version,
130
				$this->media
131
			);
132
		};
133
	}
134
135
	/**
136
	 * Get the enqueue closure to use.
137
	 *
138
	 * @since %VERSION%
139
	 *
140
	 * @return Closure
141
	 */
142
	protected function get_enqueue_closure() {
143
		return function () {
144
			wp_enqueue_style( $this->handle );
145
		};
146
	}
147
148
	/**
149
	 * Get the dequeue closure to use.
150
	 *
151
	 * @since %VERSION%
152
	 *
153
	 * @return Closure
154
	 */
155
	protected function get_dequeue_closure() {
156
		return function () {
157
			wp_dequeue_style( $this->handle );
158
		};
159
	}
160
161
	/**
162
	 * Whether the current style is disabled.
163
	 *
164
	 * @since %VERSION%
165
	 * @return bool
166
	 */
167
	private function is_disabled() {
168
		if ( ! $this->disableable ) {
169
			return false;
170
		}
171
172
		$setting = ( new DisableFrontEndCss() )->get();
173
		return isset( $setting[ $this->handle ] ) && true === $setting[ $this->handle ];
174
	}
175
}
176