|
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
|
|
|
'hidden_keys' => [ |
|
62
|
|
|
'site_reviews' => SiteReviewsShortcode::HIDDEN_KEYS, |
|
63
|
|
|
'site_reviews_form' => SiteReviewsFormShortcode::HIDDEN_KEYS, |
|
64
|
|
|
'site_reviews_summary' => SiteReviewsSummaryShortcode::HIDDEN_KEYS, |
|
65
|
|
|
], |
|
66
|
|
|
'mce_nonce' => wp_create_nonce( 'mce-shortcode' ), |
|
67
|
|
|
'pinned_nonce' => wp_create_nonce( 'toggle-pinned' ), |
|
68
|
|
|
'pointers' => $this->pointers, |
|
69
|
|
|
'shortcodes' => [], |
|
70
|
|
|
'status_nonce' => wp_create_nonce( 'change-review-status' ), |
|
71
|
|
|
]; |
|
72
|
|
|
if( user_can_richedit() ) { |
|
73
|
|
|
$variables['shortcodes'] = $this->localizeShortcodes(); |
|
74
|
|
|
} |
|
75
|
|
|
$variables = apply_filters( 'site-reviews/enqueue/admin/localize', $variables ); |
|
76
|
|
|
wp_localize_script( Application::ID, 'GLSR', $variables ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getDependencies() |
|
83
|
|
|
{ |
|
84
|
|
|
$dependencies = apply_filters( 'site-reviews/enqueue/admin/dependencies', [] ); |
|
85
|
|
|
$dependencies = array_merge( $dependencies, [ |
|
86
|
|
|
'jquery', 'jquery-ui-sortable', 'underscore', 'wp-util', |
|
87
|
|
|
]); |
|
88
|
|
|
return $dependencies; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function generatePointer( array $pointer ) |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
'id' => $pointer['id'], |
|
98
|
|
|
'options' => [ |
|
99
|
|
|
'content' => '<h3>'.$pointer['title'].'</h3><p>'.$pointer['content'].'</p>', |
|
100
|
|
|
'position' => $pointer['position'], |
|
101
|
|
|
], |
|
102
|
|
|
'screen' => $pointer['screen'], |
|
103
|
|
|
'target' => $pointer['target'], |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function generatePointers( array $pointers ) |
|
111
|
|
|
{ |
|
112
|
|
|
$dismissedPointers = get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ); |
|
113
|
|
|
$dismissedPointers = explode( ',', (string)$dismissedPointers ); |
|
114
|
|
|
$generatedPointers = []; |
|
115
|
|
|
foreach( $pointers as $pointer ) { |
|
116
|
|
|
if( $pointer['screen'] != glsr_current_screen()->id )continue; |
|
117
|
|
|
if( in_array( $pointer['id'], $dismissedPointers ))continue; |
|
118
|
|
|
$generatedPointers[] = $this->generatePointer( $pointer ); |
|
119
|
|
|
} |
|
120
|
|
|
$this->pointers = $generatedPointers; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function isCurrentScreen() |
|
127
|
|
|
{ |
|
128
|
|
|
$screen = glsr_current_screen(); |
|
129
|
|
|
return $screen && ( $screen->post_type == Application::POST_TYPE |
|
130
|
|
|
|| $screen->base == 'post' |
|
131
|
|
|
|| $screen->id == 'dashboard' |
|
132
|
|
|
|| $screen->id == 'widgets' |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function localizeShortcodes() |
|
140
|
|
|
{ |
|
141
|
|
|
$variables = []; |
|
142
|
|
|
foreach( glsr()->mceShortcodes as $tag => $args ) { |
|
143
|
|
|
if( empty( $args['required'] ))continue; |
|
144
|
|
|
$variables[$tag] = $args['required']; |
|
145
|
|
|
} |
|
146
|
|
|
return $variables; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|