Passed
Push — master ( 6b8ca8...3384db )
by Paul
04:57
created

EnqueueAssets::enqueueAssets()   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\Commands\EnqueueAssets as Command;
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Commands\EnqueueAssets was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
9
class EnqueueAssets
10
{
11
	/**
12
	 * @var array
13
	 */
14
	protected $dependencies;
15
16
	/**
17
	 * @return void
18
	 */
19
	public function handle( Command $command )
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed. ( Ignorable by Annotation )

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

19
	public function handle( /** @scrutinizer ignore-unused */ Command $command )

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
	{
21
		$this->dependencies = glsr( 'Html' )->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->enqueueAssets();
30
31
		wp_localize_script( Application::ID, 'site_reviews', apply_filters( 'site-reviews/enqueue/localize', $variables ));
32
	}
33
34
	/**
35
	 * @return void
36
	 */
37
	public function enqueueAssets()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
38
	{
39
		$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

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