class-ld-settings-page-setup.php
12.1 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
<?php
/**
* LearnDash Settings Page Overview.
*
* @since 4.4.0
* @package LearnDash\Settings\Pages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use LearnDash_Settings_Page_Help as Help_Page;
use LearnDash_Settings_Section_Stripe_Connect as Stripe_Connect;
if ( class_exists( 'LearnDash_Settings_Page' ) && ! class_exists( 'LearnDash_Settings_Page_Setup' ) ) {
/**
* Class LearnDash Settings Page Overview.
*
* @since 4.4.0
*/
class LearnDash_Settings_Page_Setup extends LearnDash_Settings_Page {
const SETUP_SLUG_CLOUD = 'learndash-cloud-setup';
const SETUP_SLUG = 'learndash-setup';
const OVERVIEW_PAGE_SLUG = 'learndash_lms_overview';
const ACTIVATION_URL_SLUG = 'learndash-activate-stripe-connect';
/**
* Public constructor for class
*
* @since 4.4.0
*/
public function __construct() {
$this->parent_menu_page_url = 'admin.php?page=learndash-setup';
$this->menu_page_capability = LEARNDASH_ADMIN_CAPABILITY_CHECK;
$this->settings_page_id = 'learndash-setup';
$this->settings_page_title = esc_html__( 'LearnDash Setup', 'learndash' );
$this->settings_tab_title = esc_html__( 'Setup', 'learndash' );
$this->settings_tab_priority = 0;
if ( ! learndash_cloud_is_enabled() ) {
add_filter( 'learndash_submenu', array( $this, 'submenu_item' ), 200 );
add_filter( 'learndash_admin_tab_sets', array( $this, 'learndash_admin_tab_sets' ), 10, 3 );
add_filter( 'learndash_header_data', array( $this, 'admin_header' ), 40, 3 );
add_action( 'admin_head', array( $this, 'output_admin_inline_scripts' ) );
parent::__construct();
}
add_action( 'wp_loaded', array( $this, 'redirect_overview_to_setup' ), 1 );
add_action( 'wp_loaded', array( $this, 'activate_connect_stripe' ) );
}
/**
* Control visibility of submenu items based on license status
*
* @since 4.4.0
*
* @param array $submenu Submenu item to check.
*
* @return array
*/
public function submenu_item( array $submenu ) : array {
if ( ! isset( $submenu[ $this->settings_page_id ] ) ) {
$submenu = array_merge(
array(
$this->settings_page_id => array(
'name' => $this->settings_tab_title,
'cap' => $this->menu_page_capability,
'link' => $this->parent_menu_page_url,
'class' => 'submenu-ldlms-setup',
),
),
$submenu
);
}
return $submenu;
}
/**
* Filter the admin header data. We don't want to show the header panel on the Overview page.
*
* @since 4.4.0
*
* @param array $header_data Array of header data used by the Header Panel React app.
* @param string $menu_key The menu key being displayed.
* @param array $menu_items Array of menu/tab items.
*
* @return array
*/
public function admin_header( array $header_data = array(), string $menu_key = '', array $menu_items = array() ) : array {
// Clear out $header_data if we are showing our page.
return $menu_key === $this->parent_menu_page_url ? array() : $header_data;
}
/**
* Output inline scripts or styles in HTML head tag.
*
* @since 4.4.0
*
* @return void
*/
public function output_admin_inline_scripts() : void {
// Setup page.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['page'] ) && 'learndash-setup' === $_GET['page'] ) {
?>
<style>
body .notice {
display: none;
}
</style>
<?php
}
}
/**
* Redirect the old LearnDash overview page to setup page.
*
* @since 4.4.0
*
* @return void
*/
public function redirect_overview_to_setup() : void {
global $pagenow;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $pagenow === 'admin.php' && isset( $_GET['page'] ) && $_GET['page'] === self::OVERVIEW_PAGE_SLUG ) {
$setup_slug = learndash_cloud_is_enabled() ? self::SETUP_SLUG_CLOUD : self::SETUP_SLUG;
wp_safe_redirect( admin_url( 'admin.php?page=' . $setup_slug ) );
exit();
}
}
/**
* Activate Stripe connect.
*
* @since 4.4.0.1
*
* @return void
*/
public function activate_connect_stripe(): void {
global $pagenow;
if ( $pagenow !== 'admin.php'
|| ! isset( $_GET['page'] ) || $_GET['page'] !== self::SETUP_SLUG
|| ! isset( $_GET['action'] ) || $_GET['action'] !== self::ACTIVATION_URL_SLUG
|| ! isset( $_GET['nonce'] )
) {
return;
}
if ( ! current_user_can( LEARNDASH_ADMIN_CAPABILITY_CHECK ) ) {
return;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! wp_verify_nonce( $_GET['nonce'], self::ACTIVATION_URL_SLUG ) ) {
return;
}
LearnDash_Settings_Section::set_section_setting( 'LearnDash_Settings_Section_Stripe_Connect', 'enabled', 'yes' );
$redirect_url = admin_url( 'admin.php?page=' . self::SETUP_SLUG );
learndash_safe_redirect( $redirect_url );
}
/**
* Filter for page title wrapper.
*
* @since 4.4.0
*
* @return string
*/
public function get_admin_page_title() : string {
/** This filter is documented in includes/settings/class-ld-settings-pages.php */
return apply_filters( 'learndash_admin_page_title', '<h1>' . $this->settings_page_title . '</h1>' );
}
/**
* Action function called when Add-ons page is loaded.
*
* @since 4.4.0
*
* @return void
*/
public function load_settings_page() : void {
global $learndash_assets_loaded;
$object = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'plugin_url' => LEARNDASH_LMS_PLUGIN_URL,
'admin_dashboard_url' => admin_url( '/' ),
'learndash_cloud_setup_url' => add_query_arg(
array( 'page' => 'learndash-setup' ),
admin_url( 'admin.php' )
),
'learndash_cloud_setup_wizard_url' => add_query_arg(
array( 'page' => 'learndash-setup-wizard' ),
admin_url( 'admin.php' )
),
'learndash_setup_wizard_url' => add_query_arg(
array( 'page' => 'learndash-setup-wizard' ),
admin_url( 'admin.php' )
),
);
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['page'] ) && 'learndash-setup' === $_GET['page'] ) {
Help_Page::enqueue_support_assets();
wp_enqueue_style(
'learndash-setup',
LEARNDASH_LMS_PLUGIN_URL . '/assets/css/setup.css',
array(),
LEARNDASH_VERSION,
'all'
);
$learndash_assets_loaded['styles']['learndash-admin-setup-page-style'] = __FUNCTION__;
wp_enqueue_script(
'learndash-setup',
LEARNDASH_LMS_PLUGIN_URL . '/assets/js/setup.js',
array( 'jquery', 'wp-element' ),
LEARNDASH_VERSION,
true
);
$learndash_assets_loaded['scripts']['learndash-admin-setup-page-script'] = __FUNCTION__;
wp_localize_script(
'learndash-setup',
'LearnDashSetup',
$object
);
}
}
/**
* Hide the tab menu items if on add-on page.
*
* @since 4.4.0
*
* @param array $tab_set Tab Set.
* @param string $tab_key Tab Key.
* @param string $current_page_id ID of shown page.
*
* @return array
*/
public function learndash_admin_tab_sets( array $tab_set = array(), string $tab_key = '', string $current_page_id = '' ) : array {
if ( ( ! empty( $tab_set ) ) && ( ! empty( $tab_key ) ) && ( ! empty( $current_page_id ) ) ) {
if ( 'admin_page_learndash-setup' === $current_page_id ) {
?>
<style> h1.nav-tab-wrapper { display: none; }</style>
<?php
}
}
return $tab_set;
}
/**
* Output the page HTML.
*
* @since 4.4.0
*
* @return void
*/
public function show_settings_page() : void {
$stripe_connect_connected = Stripe_Connect::is_stripe_connected();
$stripe_connect_activated = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Stripe_Connect', 'enabled' ) === 'yes';
$stripe_connect_needs_activation = $stripe_connect_connected && ! $stripe_connect_activated;
$stripe_connect_completed = $stripe_connect_connected && $stripe_connect_activated;
$stripe_activation_url = add_query_arg(
array(
'page' => self::SETUP_SLUG,
'action' => self::ACTIVATION_URL_SLUG,
'nonce' => wp_create_nonce( self::ACTIVATION_URL_SLUG ),
),
admin_url( 'admin.php' )
);
/**
* Filters steps shown on setup page.
*
* @since 4.4.0
*
* @param array $steps List of steps with arguments.
*/
$steps = apply_filters(
'learndash_setup_steps',
array(
'site_setup' => array(
'class' => 'setup',
'completed' => 'completed' === get_option( 'learndash_setup_wizard_status' ),
'time_in_minutes' => 5,
'url' => admin_url( 'admin.php?page=learndash-setup-wizard' ),
'title' => __( 'Set up your site', 'learndash' ),
'description' => __( 'This is where the fun begins.', 'learndash' ),
'action_label' => __( 'Site & Course Details', 'learndash' ),
'action_description' => __( 'Tell us a little bit about your site.', 'learndash' ),
'icon_url' => LEARNDASH_LMS_PLUGIN_URL . '/assets/images/setup.png',
'button_type' => 'arrow',
'button_class' => '',
'button_text' => '',
),
'design_setup' => array(
'class' => 'design',
'completed' => 'completed' === get_option( 'learndash_design_wizard_status' ),
'time_in_minutes' => 5,
'url' => admin_url( 'admin.php?page=learndash-design-wizard' ),
'title' => __( 'Design your site', 'learndash' ),
'description' => __( 'It\'s all about appearances.', 'learndash' ),
'action_label' => __( 'Select A Starter Template', 'learndash' ),
'action_description' => __( 'Choose a design to start with and customize. This will overwrite your current theme, may add additional content and change settings on your site.', 'learndash' ),
'icon_url' => LEARNDASH_LMS_PLUGIN_URL . '/assets/images/design.png',
'button_type' => 'arrow',
'button_class' => '',
'button_text' => '',
),
'payment_setup' => array(
'class' => 'payment',
'completed' => $stripe_connect_completed,
'time_in_minutes' => $stripe_connect_needs_activation ? null : 5,
'url' => $stripe_connect_needs_activation ? $stripe_activation_url : Stripe_Connect::generate_connect_url(),
'title' => __( 'Configure payment', 'learndash' ),
'description' => __( 'Don\'t leave money on the table.', 'learndash' ),
'action_label' => __( 'Set Up Stripe', 'learndash' ),
'action_description' => __( 'Charge credit cards and pay low merchant fees.', 'learndash' ),
'icon_url' => LEARNDASH_LMS_PLUGIN_URL . '/assets/images/payment.png',
'button_type' => 'button',
'button_class' => 'button-stripe',
'button_text' => $stripe_connect_needs_activation ? __( 'Activate', 'learndash' ) : __( 'Connect Stripe', 'learndash' ),
),
'documentation' => array(
'class' => 'courses',
'completed' => null,
'time_in_minutes' => null,
'url' => null,
'title' => __( 'Manage your courses', 'learndash' ),
'description' => __( 'Get your coursework set up for success.', 'learndash' ),
'action_label' => null,
'action_description' => null,
'icon_url' => null,
'button_type' => null,
'button_class' => null,
'button_text' => null,
'content_path' => 'setup/components/content-documentation',
),
)
);
SFWD_LMS::get_view(
'setup/setup',
array(
'steps' => $steps,
'setup_wizard' => $this,
'overview_video' => Help_Page::get_articles( 'overview_video' )[0],
'overview_article' => Help_Page::get_articles( 'overview_article' )[0],
),
true
);
}
}
}
add_action(
'learndash_settings_pages_init',
function() {
LearnDash_Settings_Page_Setup::add_page_instance();
}
);