Passed
Push — master ( 78f140...0de278 )
by Paul
04:49
created

EnqueueAssets::enqueue()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 25
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 16
nop 0
crap 30
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
9
class EnqueueAssets
10
{
11
	/**
12
	 * @var array
13
	 */
14
	protected $dependencies;
15
16
	/**
17
	 * @return void
18
	 */
19
	public function handle()
20
	{
21
		$this->dependencies = glsr( Html::class )->getDependencies();
22
		$ajaxNonce = wp_create_nonce( Application::ID.'-ajax-nonce' );
23
		$variables = [
24
			'action'  => glsr()->prefix . '_action',
25
			'ajaxurl' => add_query_arg( '_nonce', $ajaxNonce, admin_url( 'admin-ajax.php' )),
26
			'ajaxnonce' => $ajaxNonce,
27
			'ajaxpagination' => ['#wpadminbar','.site-navigation-fixed'],
28
		];
29
		$this->enqueue();
30
		wp_localize_script( Application::ID, 'site_reviews', apply_filters( 'site-reviews/enqueue/localize', $variables ));
31
	}
32
33
	/**
34
	 * @return void
35
	 */
36
	public function enqueue()
37
	{
38
		$currentTheme = sanitize_title( wp_get_theme()->get( 'Name' ));
0 ignored issues
show
Bug introduced by
It seems like wp_get_theme()->get('Name') can also be of type false; however, parameter $title of sanitize_title() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
		$currentTheme = sanitize_title( /** @scrutinizer ignore-type */ wp_get_theme()->get( 'Name' ));
Loading history...
39
		$stylesheet = file_exists( glsr()->path.'assets/css/'.$currentTheme.'.css' )
40
			? glsr()->url.'assets/css/'.$currentTheme.'.css'
41
			: glsr()->url.'assets/css/'.Application::ID.'.css';
42
		if( apply_filters( 'site-reviews/assets/css', true )) {
43
			wp_enqueue_style(
44
				Application::ID,
45
				$stylesheet,
46
				[],
47
				glsr()->version
48
			);
49
		}
50
		if( apply_filters( 'site-reviews/assets/js', true )) {
51
			wp_enqueue_script(
52
				Application::ID,
53
				glsr()->url.'assets/js/'.Application::ID.'.js',
54
				['jquery'],
55
				glsr()->version,
56
				true
57
			);
58
		}
59
		if( glsr( OptionManager::class )->get( 'settings.reviews-form.recaptcha.integration' ) == 'custom' ) {
60
			$this->enqueueRecaptchaScript();
61
		}
62
	}
63
64
	/**
65
	 * @return void
66
	 */
67
	public function enqueueRecaptchaScript()
68
	{
69
		wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg([
70
			'hl' => apply_filters( 'site-reviews/recaptcha/language', get_locale() ),
71
			'onload' => 'glsr_render_recaptcha',
72
			'render' => 'explicit',
73
		], 'https://www.google.com/recaptcha/api.js' ));
74
		$inlineScript = file_get_contents( glsr()->path.'js/recaptcha.js' );
75
		wp_add_inline_script( Application::ID.'/google-recaptcha', $inlineScript, 'before' );
76
	}
77
}
78