Passed
Push — master ( 06bde6...6d2f62 )
by Paul
04:59
created

EnqueueAdminAssets::localizeAssets()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 0
dl 0
loc 24
ccs 0
cts 24
cp 0
crap 6
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\EnqueueAdminAssets as Command;
7
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode;
8
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsFormShortcode;
9
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsSummaryShortcode;
10
11
class EnqueueAdminAssets
12
{
13
	/**
14
	 * @var array
15
	 */
16
	protected $pointers;
17
18
	/**
19
	 * @return void
20
	 */
21
	public function handle( Command $command )
22
	{
23
		$this->generatePointers( $command->pointers );
24
		$this->enqueueAssets();
25
		$this->localizeAssets();
26
	}
27
28
	/**
29
	 * @return void
30
	 */
31
	public function enqueueAssets()
32
	{
33
		wp_enqueue_style(
34
			Application::ID,
35
			glsr()->url( 'assets/styles/'.Application::ID.'-admin.css' ),
36
			[],
37
			glsr()->version
38
		);
39
		if( !$this->isCurrentScreen() )return;
40
		wp_enqueue_script(
41
			Application::ID,
42
			glsr()->url( 'assets/scripts/'.Application::ID.'-admin.js' ),
43
			$this->getDependencies(),
44
			glsr()->version,
45
			true
46
		);
47
		if( !empty( $this->pointers )) {
48
			wp_enqueue_style( 'wp-pointer' );
49
			wp_enqueue_script( 'wp-pointer' );
50
		}
51
	}
52
53
	/**
54
	 * @return void
55
	 */
56
	public function localizeAssets()
57
	{
58
		$variables = [
59
			'action' => Application::PREFIX.'action',
60
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
61
			'hiddenkeys' => [
62
				'site_reviews' => SiteReviewsShortcode::HIDDEN_KEYS,
63
				'site_reviews_form' => SiteReviewsFormShortcode::HIDDEN_KEYS,
64
				'site_reviews_summary' => SiteReviewsSummaryShortcode::HIDDEN_KEYS,
65
			],
66
			'nonce' => [
67
				'change-review-status' => wp_create_nonce( 'change-review-status' ),
68
				'clear-log' => wp_create_nonce( 'clear-log' ),
69
				'mce-shortcode' => wp_create_nonce( 'mce-shortcode' ),
70
				'toggle-pinned' => wp_create_nonce( 'toggle-pinned' ),
71
			],
72
			'pointers' => $this->pointers,
73
			'shortcodes' => [],
74
		];
75
		if( user_can_richedit() ) {
76
			$variables['shortcodes'] = $this->localizeShortcodes();
77
		}
78
		$variables = apply_filters( 'site-reviews/enqueue/admin/localize', $variables );
79
		wp_localize_script( Application::ID, 'GLSR', $variables );
80
	}
81
82
	/**
83
	 * @return array
84
	 */
85
	protected function getDependencies()
86
	{
87
		$dependencies = apply_filters( 'site-reviews/enqueue/admin/dependencies', [] );
88
		$dependencies = array_merge( $dependencies, [
89
			'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util',
90
		]);
91
		return $dependencies;
92
	}
93
94
	/**
95
	 * @return array
96
	 */
97
	protected function generatePointer( array $pointer )
98
	{
99
		return [
100
			'id' => $pointer['id'],
101
			'options' => [
102
				'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>',
103
				'position' => $pointer['position'],
104
			],
105
			'screen' => $pointer['screen'],
106
			'target' => $pointer['target'],
107
		];
108
	}
109
110
	/**
111
	 * @return void
112
	 */
113
	protected function generatePointers( array $pointers )
114
	{
115
		$dismissedPointers = get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true );
116
		$dismissedPointers = explode( ',', (string)$dismissedPointers );
117
		$generatedPointers = [];
118
		foreach( $pointers as $pointer ) {
119
			if( $pointer['screen'] != glsr_current_screen()->id )continue;
120
			if( in_array( $pointer['id'], $dismissedPointers ))continue;
121
			$generatedPointers[] = $this->generatePointer( $pointer );
122
		}
123
		$this->pointers = $generatedPointers;
124
	}
125
126
	/**
127
	 * @return bool
128
	 */
129
	protected function isCurrentScreen()
130
	{
131
		$screen = glsr_current_screen();
132
		return $screen && ( $screen->post_type == Application::POST_TYPE
133
			|| $screen->base == 'post'
134
			|| $screen->id == 'dashboard'
135
			|| $screen->id == 'widgets'
136
		);
137
	}
138
139
	/**
140
	 * @return array
141
	 */
142
	protected function localizeShortcodes()
143
	{
144
		$variables = [];
145
		foreach( glsr()->mceShortcodes as $tag => $args ) {
146
			if( empty( $args['required'] ))continue;
147
			$variables[$tag] = $args['required'];
148
		}
149
		return $variables;
150
	}
151
}
152