Passed
Push — master ( 8b12e9...b994b0 )
by Paul
03:50
created

EnqueuePublicAssets::enqueueAssets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 18
ccs 0
cts 18
cp 0
crap 12
rs 9.8333
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->enqueueRecaptchaScript();
19
		$this->localizeAssets();
20
	}
21
22
	/**
23
	 * @return void
24
	 */
25
	public function enqueueAssets()
26
	{
27
		if( apply_filters( 'site-reviews/assets/css', true )) {
28
			wp_enqueue_style(
29
				Application::ID,
30
				$this->getStylesheet(),
31
				[],
32
				glsr()->version
33
			);
34
		}
35
		if( apply_filters( 'site-reviews/assets/js', true )) {
36
			$dependencies = apply_filters( 'site-reviews/enqueue/public/dependencies', [] );
37
			wp_enqueue_script(
38
				Application::ID,
39
				glsr()->url( 'assets/scripts/'.Application::ID.'.js' ),
40
				$dependencies,
41
				glsr()->version,
42
				true
43
			);
44
		}
45
	}
46
47
	/**
48
	 * @return void
49
	 */
50
	public function enqueueRecaptchaScript()
51
	{
52
		if( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' ) != 'custom' )return;
53
		$language = apply_filters( 'site-reviews/recaptcha/language', get_locale() );
54
		wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg([
55
			'hl' => $language,
56
			'onload' => 'glsr_render_recaptcha',
57
			'render' => 'explicit',
58
		], 'https://www.google.com/recaptcha/api.js' ));
59
		$inlineScript = file_get_contents( glsr()->path( 'assets/scripts/recaptcha.js' ));
60
		wp_add_inline_script( Application::ID.'/google-recaptcha', $inlineScript, 'before' );
61
	}
62
63
	/**
64
	 * @return void
65
	 */
66
	public function localizeAssets()
67
	{
68
		$variables = [
69
			'action' => Application::PREFIX.'action',
70
			'ajaxpagination' => $this->getFixedSelectorsForPagination(),
71
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
72
			'validationconfig' => $this->getValidationConfig(),
73
			'validationstrings' => $this->getValidationStrings(),
74
		];
75
		$variables = apply_filters( 'site-reviews/enqueue/public/localize', $variables );
76
		wp_localize_script( Application::ID, 'GLSR', $variables );
77
	}
78
79
	/**
80
	 * @return array
81
	 */
82
	protected function getFixedSelectorsForPagination()
83
	{
84
		$selectors = ['#wpadminbar','.site-navigation-fixed'];
85
		return apply_filters( 'site-reviews/localize/pagination/selectors', $selectors );
86
87
	}
88
89
	/**
90
	 * @return string
91
	 */
92
	protected function getStylesheet()
93
	{
94
		$currentStyle = glsr( Style::class )->style;
95
		return file_exists( glsr()->path( 'assets/styles/custom/'.$currentStyle.'.css' ))
96
			? glsr()->url( 'assets/styles/custom/'.$currentStyle.'.css' )
97
			: glsr()->url( 'assets/styles/'.Application::ID.'.css' );
98
	}
99
100
	/**
101
	 * @return array
102
	 */
103
	protected function getValidationConfig()
104
	{
105
		$config = [
106
			'errorClass' => 'has-danger',
107
			'errorParentClass' => 'form-group',
108
			'errorTextClass' => 'text-help',
109
			'errorTextTag' => 'div',
110
			'fieldGroupClass' => 'form-group',
111
			'successClass' => 'has-success',
112
		];
113
		return apply_filters( 'site-reviews/localize/validation/config', $config );
114
	}
115
116
	/**
117
	 * @return array
118
	 */
119
	protected function getValidationStrings()
120
	{
121
		$strings = [
122
			'email' => __( 'This field requires a valid e-mail address', 'site-reviews' ),
123
			'max' => __( 'Maximum value for this field is %s', 'site-reviews' ),
124
			'maxlength' => __( 'This fields length must be < %s', 'site-reviews' ),
125
			'min' => __( 'Minimum value for this field is %s', 'site-reviews' ),
126
			'minlength' => __( 'This fields length must be > %s', 'site-reviews' ),
127
			'number' => __( 'This field requires a number', 'site-reviews' ),
128
			'pattern' => __( 'Input must match the pattern %s', 'site-reviews' ),
129
			'required' => __( 'This field is required', 'site-reviews' ),
130
			'tel' => __( 'This field requires a valid telephone number', 'site-reviews' ),
131
			'url' => __( 'This field requires a valid website URL', 'site-reviews' ),
132
		];
133
		return apply_filters( 'site-reviews/localize/validation/strings', $strings );
134
	}
135
}
136