Completed
Branch BUG/chicken-is-a-required-ques... (f96ad9)
by
unknown
42:30 queued 28:14
created

JavascriptAsset::requiresTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\values\assets;
4
5
use Closure;
6
use EventEspresso\core\domain\DomainInterface;
7
use EventEspresso\core\exceptions\InvalidDataTypeException;
8
9
/**
10
 * Class JavascriptAsset
11
 * Details for a Javascript asset
12
 *
13
 * @package EventEspresso\core\domain\values\assets
14
 * @author  Brent Christensen
15
 * @since   $VID:$
16
 */
17
class JavascriptAsset extends BrowserAsset
18
{
19
20
    /**
21
     * @var boolean $load_in_footer
22
     */
23
    private $load_in_footer = false;
24
25
    /**
26
     * @var boolean $requires_translation
27
     */
28
    private $requires_translation = false;
29
30
    /**
31
     * @var boolean $has_localized_data
32
     */
33
    private $has_localized_data = false;
34
35
    /**
36
     * @var Closure $localization_callback
37
     */
38
    private $localization_callback;
39
40
41
    /**
42
     * Asset constructor.
43
     *
44
     * @param string          $handle
45
     * @param string          $source
46
     * @param array           $dependencies
47
     * @param bool            $load_in_footer
48
     * @param DomainInterface $domain
49
     * @throws InvalidDataTypeException
50
     */
51
    public function __construct(
52
        $handle,
53
        $source,
54
        array $dependencies,
55
        $load_in_footer,
56
        DomainInterface $domain
57
    ) {
58
        parent::__construct(Asset::TYPE_JS, $handle, $source, $dependencies, $domain);
59
        $this->setLoadInFooter($load_in_footer);
60
    }
61
62
63
    /**
64
     * @return bool
65
     */
66
    public function loadInFooter()
67
    {
68
        return $this->load_in_footer;
69
    }
70
71
72
    /**
73
     * @param bool $load_in_footer
74
     */
75
    private function setLoadInFooter($load_in_footer = true)
76
    {
77
        $this->load_in_footer = filter_var($load_in_footer, FILTER_VALIDATE_BOOLEAN);
78
    }
79
80
81
    /**
82
     * @return bool
83
     */
84
    public function requiresTranslation()
85
    {
86
        return $this->requires_translation;
87
    }
88
89
90
    /**
91
     * @param bool $requires_translation
92
     * @return JavascriptAsset
93
     */
94
    public function setRequiresTranslation($requires_translation = true)
95
    {
96
        $this->requires_translation = filter_var($requires_translation, FILTER_VALIDATE_BOOLEAN);
97
        return $this;
98
    }
99
100
101
    /**
102
     * @return bool
103
     */
104
    public function hasLocalizedData()
105
    {
106
        return $this->has_localized_data;
107
    }
108
109
110
    /**
111
     * @param bool $has_localized_data
112
     * @return JavascriptAsset
113
     */
114
    public function setHasLocalizedData($has_localized_data = true)
115
    {
116
        $this->has_localized_data = filter_var($has_localized_data, FILTER_VALIDATE_BOOLEAN);
117
        return $this;
118
    }
119
120
121
    /**
122
     * @return Closure
123
     */
124
    public function localizationCallback()
125
    {
126
        return $this->localization_callback;
127
    }
128
129
130
    /**
131
     * @return bool
132
     */
133
    public function hasLocalizationCallback()
134
    {
135
        return $this->localization_callback instanceof Closure;
136
    }
137
138
139
    /**
140
     * @param Closure $localization_callback
141
     * @return JavascriptAsset
142
     */
143
    public function setLocalizationCallback(Closure $localization_callback)
144
    {
145
        $this->localization_callback = $localization_callback;
146
        $this->setHasLocalizedData();
147
        return $this;
148
    }
149
150
151
    /**
152
     * @since $VID:$
153
     */
154
    public function enqueueAsset()
155
    {
156
        wp_enqueue_script($this->handle());
157
    }
158
}
159