Test Failed
Push — master ( 98f0ba...364259 )
by Paul
03:58
created

EnqueuePublicAssets::enqueueRecaptchaScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 10
rs 9.4285
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
9
class EnqueuePublicAssets
10
{
11
	/**
12
	 * @return void
13
	 */
14
	public function handle()
15
	{
16
		$this->enqueueAssets();
17
		$this->enqueueRecaptchaScript();
18
		$this->localizeAssets();
19
	}
20
21
	/**
22
	 * @return void
23
	 */
24
	public function enqueueAssets()
25
	{
26
		if( apply_filters( 'site-reviews/assets/css', true )) {
27
			wp_enqueue_style(
28
				Application::ID,
29
				$this->getStylesheet(),
30
				[],
31
				glsr()->version
32
			);
33
		}
34
		if( apply_filters( 'site-reviews/assets/js', true )) {
35
			wp_enqueue_script(
36
				Application::ID,
37
				glsr()->url( 'assets/scripts/'.Application::ID.'.js' ),
38
				[],//glsr( Html::class )->getDependencies(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
				glsr()->version,
40
				true
41
			);
42
		}
43
	}
44
45
	/**
46
	 * @return void
47
	 */
48
	public function enqueueRecaptchaScript()
49
	{
50
		if( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' ) != 'custom' )return;
51
		wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg([
52
			'hl' => apply_filters( 'site-reviews/recaptcha/language', get_locale() ),
53
			'onload' => 'glsr_render_recaptcha',
54
			'render' => 'explicit',
55
		], 'https://www.google.com/recaptcha/api.js' ));
56
		$inlineScript = file_get_contents( glsr()->path( 'assets/scripts/recaptcha.js' ));
57
		wp_add_inline_script( Application::ID.'/google-recaptcha', $inlineScript, 'before' );
58
	}
59
60
	/**
61
	 * @return void
62
	 */
63
	public function localizeAssets()
64
	{
65
		$variables = [
66
			'action' => Application::PREFIX.'action',
67
			'ajaxnonce' => wp_create_nonce( Application::ID.'-ajax-nonce' ),
68
			'ajaxpagination' => ['#wpadminbar','.site-navigation-fixed'],
69
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
70
		];
71
		$variables = apply_filters( 'site-reviews/enqueue/localize', $variables );
72
		wp_localize_script( Application::ID, 'site_reviews', $variables );
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	protected function getStylesheet()
79
	{
80
		$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

80
		$currentTheme = sanitize_title( /** @scrutinizer ignore-type */ wp_get_theme()->get( 'Name' ));
Loading history...
81
		return file_exists( glsr()->path.'assets/styles/'.$currentTheme.'.css' )
82
			? glsr()->url( 'assets/styles/'.$currentTheme.'.css' )
83
			: glsr()->url( 'assets/styles/'.Application::ID.'.css' );
84
	}
85
}
86