Passed
Push — master ( 3b6fab...7d2cd0 )
by Paul
04:26
created

EnqueuePublicAssets::enqueuePolyfillService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Modules\Html;
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->inlineStyles();
21
		$this->localizeAssets();
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 ))return;
58
		wp_enqueue_script(
59
			Application::ID.'/polyfill',
60
			'https://cdn.polyfill.io/v2/polyfill.js?features=Element.prototype.closest,Element.prototype.dataset,Event&flags=gated',
61
			[],
62
			glsr()->version
63
		);
64
	}
65
66
	/**
67
	 * @return void
68
	 */
69
	public function enqueueRecaptchaScript()
70
	{
71
		if( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' ) != 'custom' )return;
72
		$language = apply_filters( 'site-reviews/recaptcha/language', get_locale() );
73
		wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg([
74
			'hl' => $language,
75
			'onload' => 'glsr_render_recaptcha',
76
			'render' => 'explicit',
77
		], 'https://www.google.com/recaptcha/api.js' ));
78
		$inlineScript = file_get_contents( glsr()->path( 'assets/scripts/recaptcha.js' ));
79
		wp_add_inline_script( Application::ID.'/google-recaptcha', $inlineScript, 'before' );
80
	}
81
82
	/**
83
	 * @param
84
	 * @return
85
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
86
	public function inlineStyles()
87
	{
88
		$inlineStylesheetPath = glsr()->path( 'assets/styles/inline-styles.css' );
89
		if( !apply_filters( 'site-reviews/assets/css', true ))return;
90
		if( !file_exists( $inlineStylesheetPath )) {
91
			glsr_log()->error( 'Inline stylesheet is missing: '.$inlineStylesheetPath );
92
			return;
93
		}
94
		$inlineStylesheetValues = glsr()->config( 'inline-styles' );
95
		$stylesheet = str_replace(
96
			array_keys( $inlineStylesheetValues ),
97
			array_values( $inlineStylesheetValues ),
98
			file_get_contents( $inlineStylesheetPath )
99
		);
100
		wp_add_inline_style( Application::ID, $stylesheet );
101
	}
102
103
	/**
104
	 * @return void
105
	 */
106
	public function localizeAssets()
107
	{
108
		$variables = [
109
			'action' => Application::PREFIX.'action',
110
			'ajaxpagination' => $this->getFixedSelectorsForPagination(),
111
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
112
			'validationconfig' => $this->getValidationConfig(),
113
			'validationstrings' => $this->getValidationStrings(),
114
		];
115
		$variables = apply_filters( 'site-reviews/enqueue/public/localize', $variables );
116
		wp_localize_script( Application::ID, 'GLSR', $variables );
117
	}
118
119
	/**
120
	 * @return array
121
	 */
122
	protected function getFixedSelectorsForPagination()
123
	{
124
		$selectors = ['#wpadminbar','.site-navigation-fixed'];
125
		return apply_filters( 'site-reviews/localize/pagination/selectors', $selectors );
126
	}
127
128
	/**
129
	 * @return string
130
	 */
131
	protected function getStylesheet()
132
	{
133
		$currentStyle = glsr( Style::class )->style;
134
		return file_exists( glsr()->path( 'assets/styles/custom/'.$currentStyle.'.css' ))
135
			? glsr()->url( 'assets/styles/custom/'.$currentStyle.'.css' )
136
			: glsr()->url( 'assets/styles/'.Application::ID.'.css' );
137
	}
138
139
	/**
140
	 * @return array
141
	 */
142
	protected function getValidationConfig()
143
	{
144
		$config = [
145
			'errorClass' => 'glsr-invalid',
146
			'errorParentClass' => 'glsr-field',
147
			'errorTextClass' => 'glsr-field-error',
148
			'errorTextTag' => 'div',
149
			'fieldGroupClass' => 'glsr-field',
150
			'successClass' => 'glsr-valid',
151
		];
152
		return apply_filters( 'site-reviews/localize/validation/config', $config );
153
	}
154
155
	/**
156
	 * @return array
157
	 */
158
	protected function getValidationStrings()
159
	{
160
		$strings = [
161
			'email' => __( 'This field requires a valid e-mail address', 'site-reviews' ),
162
			'max' => __( 'Maximum value for this field is %s', 'site-reviews' ),
163
			'maxlength' => __( 'This field length must be < %s', 'site-reviews' ),
164
			'min' => __( 'Minimum value for this field is %s', 'site-reviews' ),
165
			'minlength' => __( 'This field length must be > %s', 'site-reviews' ),
166
			'number' => __( 'This field requires a number', 'site-reviews' ),
167
			'pattern' => __( 'Input must match the pattern %s', 'site-reviews' ),
168
			'required' => __( 'This field is required', 'site-reviews' ),
169
			'tel' => __( 'This field requires a valid telephone number', 'site-reviews' ),
170
			'url' => __( 'This field requires a valid website URL', 'site-reviews' ),
171
		];
172
		return apply_filters( 'site-reviews/localize/validation/strings', $strings );
173
	}
174
}
175