-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-page.php
More file actions
394 lines (348 loc) · 11.7 KB
/
class-page.php
File metadata and controls
394 lines (348 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Create the admin page.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Admin;
/**
* Admin page class.
*/
class Page {
/**
* Whether the branding inline styles have been added.
*
* @var boolean
*/
protected static $branding_inline_styles_added = false;
/**
* Constructor.
*/
public function __construct() {
$this->register_hooks();
}
/**
* Register the hooks.
*
* @return void
*/
private function register_hooks() {
\add_action( 'admin_menu', [ $this, 'add_page' ] );
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
\add_action( 'in_admin_header', [ $this, 'remove_admin_notices' ], PHP_INT_MAX );
// Clear the cache for the activity scores widget.
\add_action( 'progress_planner_activity_saved', [ $this, 'clear_activity_scores_cache' ] );
\add_action( 'progress_planner_activity_deleted', [ $this, 'clear_activity_scores_cache' ] );
// Add a custom admin footer.
\add_action( 'admin_footer', [ $this, 'admin_footer' ] );
}
/**
* Get the widgets objects
*
* @return array<\Progress_Planner\Admin\Widgets\Widget>
*/
public function get_widgets() {
$widgets = [
\progress_planner()->get_admin__widgets__suggested_tasks(),
\progress_planner()->get_admin__widgets__todo(),
\progress_planner()->get_admin__widgets__monthly_badges(),
\progress_planner()->get_admin__widgets__badge_streak_content(),
\progress_planner()->get_admin__widgets__badge_streak_maintenance(),
\progress_planner()->get_admin__widgets__challenge(),
\progress_planner()->get_admin__widgets__activity_scores(),
\progress_planner()->get_admin__widgets__content_activity(),
\progress_planner()->get_admin__widgets__whats_new(),
];
/**
* Filter the widgets.
*
* @param array<\Progress_Planner\Admin\Widgets\Widget> $widgets The widgets.
*
* @return array<\Progress_Planner\Admin\Widgets\Widget>
*/
return \apply_filters( 'progress_planner_admin_widgets', $widgets ); // @phpstan-ignore-line parameter.phpDocType
}
/**
* Get a widget object.
*
* @param string $id The widget ID.
*
* @return \Progress_Planner\Admin\Widgets\Widget|void
*/
public function get_widget( $id ) {
$widgets = $this->get_widgets();
foreach ( $widgets as $widget ) {
if ( $widget->get_id() === $id ) {
return $widget;
}
}
}
/**
* Add the admin page.
*
* @return void
*/
public function add_page() {
global $admin_page_hooks;
$page_identifier = 'progress-planner';
\add_menu_page(
\progress_planner()->get_ui__branding()->get_admin_submenu_name(),
\progress_planner()->get_ui__branding()->get_admin_menu_name() . $this->get_notification_counter(),
'manage_options',
$page_identifier,
'__return_empty_string',
\progress_planner()->get_ui__branding()->get_admin_menu_icon()
);
\add_submenu_page(
$page_identifier,
\progress_planner()->get_ui__branding()->get_admin_submenu_name(),
\progress_planner()->get_ui__branding()->get_admin_submenu_name() . $this->get_notification_counter(),
'manage_options',
$page_identifier,
[ $this, 'render_page' ],
);
// Wipe notification bits from hooks.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride -- This is a deliberate action.
$admin_page_hooks[ $page_identifier ] = $page_identifier;
}
/**
* Returns the notification count in HTML format.
*
* @return string The notification count in HTML format.
*/
protected function get_notification_counter() {
$notification_count = \wp_count_posts( 'prpl_recommendations' )->pending;
if ( 0 === $notification_count ) {
return '';
}
/* translators: Hidden accessibility text; %s: number of notifications. */
$notifications = \sprintf( \_n( '%s pending celebration', '%s pending celebrations', $notification_count, 'progress-planner' ), \number_format_i18n( $notification_count ) );
return \sprintf( '<span class="update-plugins count-%1$d" style="background-color:var(--prpl-background-banner);color:#38296d;"><span class="plugin-count" aria-hidden="true">%1$d</span><span class="screen-reader-text">%2$s</span></span>', $notification_count, $notifications );
}
/**
* Render the admin page.
*
* @return void
*/
public function render_page() {
\progress_planner()->the_view( 'admin-page.php' );
}
/**
* Enqueue scripts and styles.
*
* @param string $hook The current admin page.
*
* @return void
*/
public function enqueue_assets( $hook ) {
$this->maybe_enqueue_focus_el_script( $hook );
if ( 'toplevel_page_progress-planner' !== $hook && 'progress-planner_page_progress-planner-settings' !== $hook ) {
return;
}
$this->enqueue_scripts();
$this->enqueue_styles();
}
/**
* Enqueue scripts.
*
* @return void
*/
public function enqueue_scripts() {
$current_screen = \get_current_screen();
if ( ! $current_screen ) {
return;
}
if ( 'toplevel_page_progress-planner' === $current_screen->id ) {
$default_localization_data = [
'name' => 'progressPlanner',
'data' => [
'onboardNonceURL' => \progress_planner()->get_utils__onboard()->get_remote_nonce_url(),
'onboardAPIUrl' => \progress_planner()->get_utils__onboard()->get_remote_url(),
'ajaxUrl' => \admin_url( 'admin-ajax.php' ),
'nonce' => \wp_create_nonce( 'progress_planner' ),
],
];
if ( true === \progress_planner()->is_privacy_policy_accepted() ) {
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-gauge' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-badge-progress-bar' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-chart-bar' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-chart-line' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-big-counter' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-tooltip' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'web-components/prpl-popover-post-content' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'header-filters', $default_localization_data );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'settings', $default_localization_data );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'grid-masonry' );
\progress_planner()->get_admin__enqueue()->enqueue_script( 'upgrade-tasks' );
} else {
\progress_planner()->get_admin__enqueue()->enqueue_script( 'onboard', $default_localization_data );
}
\progress_planner()->get_admin__enqueue()->enqueue_script( 'external-link-accessibility-helper' );
}
if ( 'progress-planner_page_progress-planner-settings' === $current_screen->id ) {
\progress_planner()->get_admin__enqueue()->enqueue_script(
'settings-page',
[
'name' => 'progressPlannerSettingsPage',
'data' => [
'siteUrl' => \get_site_url(),
],
]
);
\progress_planner()->get_admin__enqueue()->enqueue_script( 'external-link-accessibility-helper' );
}
}
/**
* Enqueue the focus element script.
*
* @param string $hook The current admin page.
*
* @return void
*/
public function maybe_enqueue_focus_el_script( $hook ) {
$tasks_providers = \progress_planner()->get_suggested_tasks()->get_tasks_manager()->get_task_providers();
$tasks_details = [];
$total_points = 0;
$completed_points = 0;
foreach ( $tasks_providers as $provider ) {
if ( 'configuration' !== $provider->get_provider_category() ) {
continue;
}
$link_setting = $provider->get_link_setting();
if ( ! isset( $link_setting['hook'] ) ||
$hook !== $link_setting['hook']
) {
continue;
}
$details = [
'link_setting' => $link_setting,
'task_id' => $provider->get_task_id(),
'points' => $provider->get_points(),
'is_complete' => $provider->is_task_completed(),
];
$tasks_details[] = $details;
$total_points += $details['points'];
if ( $details['is_complete'] ) {
$completed_points += $details['points'];
}
}
if ( empty( $tasks_details ) ) {
return;
}
// Register the scripts.
\progress_planner()->get_admin__enqueue()->enqueue_script(
'focus-element',
[
'name' => 'progressPlannerFocusElement',
'data' => [
'tasks' => $tasks_details,
'totalPoints' => $total_points,
'completedPoints' => $completed_points,
'base_url' => \constant( 'PROGRESS_PLANNER_URL' ),
'l10n' => [
/* translators: %d: The number of points. */
'fixThisIssue' => \esc_html__( 'Fix this issue to get %d point(s) in Progress Planner', 'progress-planner' ),
],
],
]
);
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/focus-element' );
}
/**
* Enqueue styles.
*
* @return void
*/
public function enqueue_styles() {
$current_screen = \get_current_screen();
if ( ! $current_screen ) {
return;
}
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/variables-color' );
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/admin' );
if ( ! static::$branding_inline_styles_added ) {
\wp_add_inline_style( 'progress-planner/admin', \progress_planner()->get_ui__branding()->get_custom_css() );
static::$branding_inline_styles_added = true;
}
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-tooltip' );
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-install-plugin' );
if ( 'progress-planner_page_progress-planner-settings' === $current_screen->id ) {
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/settings-page' );
}
if ( 'toplevel_page_progress-planner' === $current_screen->id ) {
// Enqueue ugprading (onboarding) tasks styles, these are needed both when privacy policy is accepted and when it is not.
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/upgrade-tasks' );
}
$prpl_privacy_policy_accepted = \progress_planner()->is_privacy_policy_accepted();
if ( ! $prpl_privacy_policy_accepted ) {
// Enqueue welcome styles.
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/welcome' );
// Enqueue onboarding styles.
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/onboard' );
}
}
/**
* Remove all admin notices when the user is on the Progress Planner page.
*
* @return void
*/
public function remove_admin_notices() {
$current_screen = \get_current_screen();
if ( ! $current_screen ) {
return;
}
if ( ! \in_array(
$current_screen->id,
[
'toplevel_page_progress-planner',
'progress-planner_page_progress-planner-settings',
],
true
) ) {
return;
}
\remove_all_actions( 'admin_notices' );
\remove_all_actions( 'all_admin_notices' );
}
/**
* Clear the cache.
*
* @param \Progress_Planner\Activities\Activity $activity The activity.
*
* @return void
*/
public function clear_activity_scores_cache( $activity ) {
if ( 'content' !== $activity->category ) {
return;
}
// Clear the cache for the activity scores widget.
\progress_planner()->get_settings()->set( \progress_planner()->get_admin__widgets__activity_scores()->get_cache_key(), [] );
}
/**
* Add a custom admin footer.
*
* @return void
*/
public function admin_footer() {
?>
<style>
#toplevel_page_progress-planner {
position: relative;
.update-plugins {
position: absolute;
left: 18px;
bottom: 0px;
min-width: 15px;
height: 15px;
line-height: 1.5;
}
.wp-submenu {
.update-plugins {
display: none;
}
}
}
</style>
<?php
}
}