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