-
Notifications
You must be signed in to change notification settings - Fork 49
/
class-wizards.php
174 lines (156 loc) · 4.67 KB
/
class-wizards.php
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
<?php
/**
* Newspack Wizards manager.
*
* @package Newspack
*/
namespace Newspack;
use Newspack\Wizards\Newspack\Newspack_Settings;
defined( 'ABSPATH' ) || exit;
/**
* Manages the wizards.
*/
class Wizards {
/**
* Information about all of the wizards.
* See `init` for structure of the data.
*
* @var array
*/
protected static $wizards = [];
/**
* Initialize and register all of the wizards.
*/
public static function init() {
self::$wizards = [
'setup' => new Setup_Wizard(),
'site-design' => new Site_Design_Wizard(),
'reader-revenue' => new Reader_Revenue_Wizard(),
'advertising' => new Advertising_Wizard(),
'syndication' => new Syndication_Wizard(),
'analytics' => new Analytics_Wizard(),
'components-demo' => new Components_Demo(),
'seo' => new SEO_Wizard(),
'health-check' => new Health_Check_Wizard(),
'engagement' => new Engagement_Wizard(),
'popups' => new Popups_Wizard(),
'connections' => new Connections_Wizard(),
'settings' => new Settings(),
// v2 Information Architecture.
'newspack-dashboard' => new Newspack_Dashboard(),
'newspack-settings' => new Newspack_Settings(
[
'sections' => [
'custom-events' => 'Newspack\Wizards\Newspack\Custom_Events_Section',
'social-pixels' => 'Newspack\Wizards\Newspack\Pixels_Section',
'recirculation' => 'Newspack\Wizards\Newspack\Recirculation_Section',
],
]
),
'advertising-display-ads' => new Advertising_Display_Ads(),
'advertising-sponsors' => new Advertising_Sponsors(),
'network' => new Network_Wizard(),
'newsletters' => new Newsletters_Wizard(),
];
// Allow custom menu order.
add_filter( 'custom_menu_order', '__return_true' );
// Fix menu order for wizards with parent menu items.
add_filter( 'menu_order', [ __CLASS__, 'menu_order' ], 11 );
}
/**
* Get a wizard's object.
*
* @param string $wizard_slug The wizard to get. Use slug from self::$wizards.
* @return Wizard | bool The wizard on success, false on failure.
*/
public static function get_wizard( $wizard_slug ) {
if ( isset( self::$wizards[ $wizard_slug ] ) ) {
return self::$wizards[ $wizard_slug ];
}
return false;
}
/**
* Get a wizard's URL.
*
* @param string $wizard_slug The wizard to get URL for. Use slug from self::$wizards.
* @return string | bool The URL on success, false on failure.
*/
public static function get_url( $wizard_slug ) {
$wizard = self::get_wizard( $wizard_slug );
if ( $wizard ) {
return $wizard->get_url();
}
return false;
}
/**
* Get all the URLs for all the wizards.
*
* @return array of slug => URL pairs.
*/
public static function get_urls() {
$urls = [];
foreach ( self::$wizards as $slug => $wizard ) {
$urls[ $slug ] = $wizard->get_url();
}
return $urls;
}
/**
* Get a wizard's name.
*
* @param string $wizard_slug The wizard to get name for. Use slug from self::$wizards.
* @return string | bool The name on success, false on failure.
*/
public static function get_name( $wizard_slug ) {
$wizard = self::get_wizard( $wizard_slug );
if ( $wizard ) {
return $wizard->get_name();
}
return false;
}
/**
* Get whether a wizard is completed.
*
* @param string $wizard_slug The wizard to get completion for. Use slug from self::$wizards.
* @return bool True if completed. False otherwise.
*/
public static function is_completed( $wizard_slug ) {
$wizard = self::get_wizard( $wizard_slug );
if ( $wizard ) {
return $wizard->is_completed();
}
return false;
}
/**
* Update menu order for wizards with parent menu items.
*
* @param array $menu_order The current menu order.
*
* @return array The updated menu order.
*/
public static function menu_order( $menu_order ) {
$index = array_search( 'newspack-dashboard', $menu_order, true );
if ( false === $index ) {
return $menu_order;
}
$ordered_wizards = [];
foreach ( self::$wizards as $slug => $wizard ) {
if ( ! empty( $wizard->parent_menu ) && ! empty( $wizard->menu_order ) ) {
$ordered_wizards[ $wizard->menu_order ] = $wizard->parent_menu;
}
}
if ( empty( $ordered_wizards ) ) {
return $menu_order;
}
ksort( $ordered_wizards );
foreach ( array_reverse( $ordered_wizards ) as $menu_item ) {
$key = array_search( $menu_item, $menu_order, true );
if ( false === $key ) {
continue;
}
array_splice( $menu_order, $key, 1 );
array_splice( $menu_order, $index + 1, 0, $menu_item );
}
return $menu_order;
}
}
Wizards::init();