|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\ValidationStringsDefaults; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
|
9
|
|
|
|
|
10
|
|
|
class EnqueuePublicAssets |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
public function handle() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->enqueueAssets(); |
|
18
|
|
|
$this->enqueuePolyfillService(); |
|
19
|
|
|
$this->enqueueRecaptchaScript(); |
|
20
|
|
|
$this->inlineScript(); |
|
21
|
|
|
$this->inlineStyles(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function enqueueAssets() |
|
28
|
|
|
{ |
|
29
|
|
|
if (apply_filters('site-reviews/assets/css', true)) { |
|
30
|
|
|
wp_enqueue_style( |
|
31
|
|
|
Application::ID, |
|
32
|
|
|
$this->getStylesheet(), |
|
33
|
|
|
[], |
|
34
|
|
|
glsr()->version |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
if (apply_filters('site-reviews/assets/js', true)) { |
|
38
|
|
|
$dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39
|
|
|
? [Application::ID.'/polyfill'] |
|
40
|
|
|
: []; |
|
41
|
|
|
$dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42
|
|
|
wp_enqueue_script( |
|
43
|
|
|
Application::ID, |
|
44
|
|
|
glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45
|
|
|
$dependencies, |
|
46
|
|
|
glsr()->version, |
|
47
|
|
|
true |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function enqueuePolyfillService() |
|
56
|
|
|
{ |
|
57
|
|
|
if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61
|
|
|
'features' => 'CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62
|
|
|
'flags' => 'gated', |
|
63
|
|
|
], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function enqueueRecaptchaScript() |
|
70
|
|
|
{ |
|
71
|
|
|
// wpforms-recaptcha |
|
72
|
|
|
// google-recaptcha |
|
73
|
|
|
// nf-google-recaptcha |
|
74
|
|
|
if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
$language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78
|
|
|
wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79
|
|
|
'hl' => $language, |
|
80
|
|
|
'render' => 'explicit', |
|
81
|
|
|
], 'https://www.google.com/recaptcha/api.js')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function inlineScript() |
|
88
|
|
|
{ |
|
89
|
|
|
$variables = [ |
|
90
|
|
|
'action' => Application::PREFIX.'action', |
|
91
|
|
|
'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92
|
|
|
'ajaxurl' => admin_url('admin-ajax.php'), |
|
93
|
|
|
'nameprefix' => Application::ID, |
|
94
|
|
|
'validationconfig' => glsr(Style::class)->validation, |
|
95
|
|
|
'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
96
|
|
|
]; |
|
97
|
|
|
$variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98
|
|
|
wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
public function inlineStyles() |
|
105
|
|
|
{ |
|
106
|
|
|
$inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107
|
|
|
if (!apply_filters('site-reviews/assets/css', true)) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
if (!file_exists($inlineStylesheetPath)) { |
|
111
|
|
|
glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
$inlineStylesheetValues = glsr()->config('inline-styles'); |
|
115
|
|
|
$stylesheet = str_replace( |
|
116
|
|
|
array_keys($inlineStylesheetValues), |
|
117
|
|
|
array_values($inlineStylesheetValues), |
|
118
|
|
|
file_get_contents($inlineStylesheetPath) |
|
119
|
|
|
); |
|
120
|
|
|
wp_add_inline_style(Application::ID, $stylesheet); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function buildInlineScript(array $variables) |
|
127
|
|
|
{ |
|
128
|
|
|
$script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
129
|
|
|
foreach ($variables as $key => $value) { |
|
130
|
|
|
$script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
131
|
|
|
} |
|
132
|
|
|
$pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
133
|
|
|
$optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134
|
|
|
return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function getFixedSelectorsForPagination() |
|
141
|
|
|
{ |
|
142
|
|
|
$selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
143
|
|
|
return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getStylesheet() |
|
150
|
|
|
{ |
|
151
|
|
|
$currentStyle = glsr(Style::class)->style; |
|
152
|
|
|
return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153
|
|
|
? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154
|
|
|
: glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|