PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.1
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / admin / class-lp-setup-wizard.php

class-lp-setup-wizard.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.1, at inc/admin/class-lp-setup-wizard.php

418 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Setup_Wizard
5 *
6 * Class helper for displaying the Setup Wizard page.
7 *
8 * @since 3.0.0
9 * @author ThimPress
10 * @package LearnPress/Classes
11 */
12 class LP_Setup_Wizard {
13 /**
14 * @var string
15 */
16 protected $_base_url = 'index.php?page=lp-setup';
17
18 /**
19 * LP_Setup_Wizard constructor.
20 */
21 protected function __construct() {
22 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
23 add_action( 'admin_init', array( $this, 'setup_wizard' ) );
24 add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
25
26 $actions = array(
27 'setup-create-pages' => 'create_pages',
28 'get-price-format' => 'get_price_format',
29 );
30
31 foreach ( $actions as $action => $callback ) {
32 LP_Request::register_ajax( $action, array( $this, $callback ) );
33 }
34 }
35
36 /**
37 * Create static pages action
38 */
39 public function create_pages() {
40 if ( ! wp_verify_nonce( LP_Request::get_string( '_wpnonce', 'setup-create-pages' ) ) ) {
41 die();
42 }
43
44 $settings = LP_Request::get( 'settings' );
45 foreach ( $settings['pages'] as $page => $page_id ) {
46 if ( empty( $page_id ) ) {
47 $_REQUEST['settings']['pages'][ $page ] = $this->create_page( $page );
48 }
49 }
50
51 LP_Request::$ajax_shutdown = false;
52 }
53
54 /**
55 * Get sample format price
56 */
57 public static function get_price_format() {
58 self::instance()->save();
59 // LP_Settings::instance()->refresh();
60 echo learn_press_format_price( 1234.56, true );
61 die();
62 }
63
64 /**
65 * Create page by type.
66 *
67 * @param string $page
68 *
69 * @return int|WP_Error
70 */
71 public function create_page( $page ) {
72 $page_titles = array(
73 'courses_page_id' => _x( 'LP Courses', 'static-page', 'learnpress' ),
74 'profile_page_id' => _x( 'LP Profile', 'static-page', 'learnpress' ),
75 'checkout_page_id' => _x( 'LP Checkout', 'static-page', 'learnpress' ),
76 'become_a_teacher_page_id' => _x( 'LP Become Teacher', 'static-page', 'learnpress' ),
77 'term_conditions_page_id' => _x( 'LP Terms and Conditions', 'static-page', 'learnpress' ),
78 );
79
80 if ( $page === 'profile_page_id' ) {
81 $page_content = '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->';
82 } else {
83 $page_content = '';
84 }
85
86 return wp_insert_post(
87 array(
88 'post_title' => $page_titles[ $page ],
89 'post_status' => 'publish',
90 'post_type' => 'page',
91 'post_content' => isset( $page_content ) ? $page_content : '',
92 )
93 );
94 }
95
96 /**
97 * Add an empty menu item for validating page.
98 */
99 public function admin_menu() {
100 if ( 'lp-setup' !== LP_Request::get_string( 'page' ) || ! current_user_can( 'install_plugins' ) ) {
101 return;
102 }
103 add_dashboard_page( '', '', 'manage_options', 'lp-setup', '' );
104 }
105
106 /**
107 * Display setup page a ignore anything else in the rest
108 */
109 public function setup_wizard() {
110 if ( 'lp-setup' !== LP_Request::get_string( 'page' ) || ! current_user_can( 'install_plugins' ) ) {
111 return;
112 }
113
114 if ( 'finish' === LP_Request::get_string( 'step' ) ) {
115 update_option( 'learn_press_setup_wizard_completed', 'yes' );
116 }
117
118 $this->save();
119
120 // Refresh new changes
121 // LP_Settings::instance()->refresh();
122
123 $assets = learn_press_admin_assets();
124
125 // tungnx: fix error with Woocommerce
126 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Loader', 'register_scripts' ) );
127 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Loader', 'load_scripts' ), 15 );
128 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Features\Features', 'load_scripts' ), 15 );
129 // End fix
130 // @do_action( 'admin_enqueue_scripts' );
131
132 wp_enqueue_style( 'buttons' );
133 wp_enqueue_style( 'common' );
134 wp_enqueue_style( 'forms' );
135 wp_enqueue_style( 'themes' );
136 wp_enqueue_style( 'dashboard' );
137 wp_enqueue_style( 'widgets' );
138 wp_enqueue_style( 'lp-admin', $assets->url( 'css/admin/admin.css' ) );
139 wp_enqueue_style( 'lp-setup', $assets->url( 'css/admin/setup.css' ) );
140 wp_enqueue_style( 'lp-select2', $assets->url( 'src/css/vendor/select2.min.css' ) );
141
142 wp_enqueue_script( 'lp-select2', $assets->url( 'src/js/vendor/select2.full.min.js' ) );
143 wp_enqueue_script( 'lp-utils', $assets->url( 'js/dist/utils.js' ) );
144 wp_enqueue_script( 'lp-admin', $assets->url( 'src/js/admin/admin.js' ), uniqid(), true );
145 wp_enqueue_script( 'drop-down-page', $assets->url( 'src/js/admin/share/dropdown-pages.js' ), uniqid(), true );
146 wp_register_script( 'lp-setup', $assets->url( 'js/dist/admin/pages/setup.js' ), array( 'jquery', 'lp-admin' ), uniqid(), true );
147 wp_localize_script( 'lp-setup', 'lpGlobalSettings', learn_press_global_script_params() );
148 wp_enqueue_script( 'lp-setup' );
149 learn_press_admin_view( 'setup/header' );
150 learn_press_admin_view( 'setup/content', array( 'steps' => $this->get_steps() ) );
151 learn_press_admin_view( 'setup/footer' );
152
153 die();
154 }
155
156 /**
157 * @TODO tungnx - need review
158 */
159 public function save() {
160 $step = LP_Request::get_string( 'lp-setup-step' );
161
162 if ( ! wp_verify_nonce( LP_Request::get_string( 'lp-setup-nonce' ), 'lp-setup-step-' . $step ) ) {
163 return;
164 }
165
166 $postdata = LP_Request::get_array( 'settings' );
167 $steps = array( 'payment', 'pages', 'currency', 'emails' );
168
169 if ( ( 'yes' !== LP_Request::get( 'skip' ) ) && in_array( $step, $steps ) ) {
170 if ( array_key_exists( 'paypal', $postdata ) ) {
171 update_option( 'learn_press_paypal', $postdata['paypal'] );
172 }
173
174 if ( array_key_exists( 'currency', $postdata ) ) {
175 foreach ( $postdata['currency'] as $k => $v ) {
176 update_option( 'learn_press_' . $k, $v );
177 }
178 }
179
180 if ( array_key_exists( 'pages', $postdata ) ) {
181 foreach ( $postdata['pages'] as $k => $v ) {
182 update_option( 'learn_press_' . $k, $v );
183 }
184 }
185
186 if ( array_key_exists( 'emails', $postdata ) ) {
187 if ( ! empty( $postdata['emails']['enable'] ) && ( $postdata['emails']['enable'] === 'yes' ) ) {
188 $emails = LP_Emails::instance()->emails;
189
190 if ( $emails ) {
191 foreach ( $emails as $email ) {
192 $response[ $email->id ] = $email->enable( true );
193 }
194 }
195 }
196 }
197 }
198
199 do_action( 'learn-press/setup-wizard/update-settings', $postdata, $step );
200 }
201
202 /**
203 * Return array of all steps are available when running setup wizard.
204 *
205 * @return array
206 */
207 public function get_steps() {
208 static $steps = false;
209 if ( ! $steps ) {
210 $steps = apply_filters(
211 'learn-press/setup-wizard/steps',
212 array(
213 'welcome' => array(
214 'title' => __( 'Welcome', 'learnpress' ),
215 'callback' => array( $this, 'step_welcome' ),
216 'next_button' => __( 'Run Setup Wizard', 'learnpress' ),
217 ),
218 'pages' => array(
219 'title' => __( 'Pages', 'learnpress' ),
220 'callback' => array( $this, 'step_pages' ),
221 ),
222 // 'currency' => array(
223 // 'title' => __( 'Currency', 'learnpress' ),
224 // 'callback' => array( $this, 'step_currency' ),
225 // 'back_button' => false,
226 // 'skip_prev_button' => false
227 // ),
228 'payment' => array(
229 'title' => __( 'Payment', 'learnpress' ),
230 'callback' => array( $this, 'step_payment' ),
231 ),
232 // 'emails' => array(
233 // 'title' => __( 'Emails', 'learnpress' ),
234 // 'callback' => array( $this, 'step_emails' )
235 // ),
236 'finish' => array(
237 'title' => __( 'Finish', 'learnpress' ),
238 'callback' => array( $this, 'step_finish' ),
239 ),
240 )
241 );
242 }
243
244 return $steps;
245 }
246
247 /**
248 * Get all keys of available steps.
249 *
250 * @return array
251 */
252 public function get_step_keys() {
253 return array_keys( $this->get_steps() );
254 }
255
256 /**
257 * Get key of current step.
258 *
259 * @param bool $key
260 *
261 * @return mixed|string
262 */
263 public function get_current_step( $key = true ) {
264 $current = LP_Request::get_string( 'step' );
265 $steps = $this->get_steps();
266
267 if ( empty( $steps[ $current ] ) ) {
268 $key_steps = array_keys( $steps );
269 $current = reset( $key_steps );
270 }
271
272 $step = $steps[ $current ];
273 if ( empty( $step['slug'] ) ) {
274 $step['slug'] = $current;
275 }
276
277 return $key ? $current : $step;
278 }
279
280 /**
281 * Return true if the first step is viewing.
282 *
283 * @return bool
284 */
285 public function is_first_step() {
286 $steps = $this->get_step_keys();
287
288 return array_search( $this->get_current_step(), $steps ) === 0;
289 }
290
291 /**
292 * Return true if the last steo is viewing.
293 *
294 * @return bool
295 */
296 public function is_last_step() {
297 $steps = $this->get_step_keys();
298
299 return array_search( $this->get_current_step(), $steps ) === sizeof( $steps ) - 1;
300 }
301
302 /**
303 * Get url of next step.
304 *
305 * @return string
306 */
307 public function get_next_url() {
308 $current = $this->get_current_step();
309 $steps = $this->get_step_keys();
310 $at = array_search( $current, $steps );
311 if ( $at < sizeof( $steps ) - 1 ) {
312 $at ++;
313 }
314
315 return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) );
316 }
317
318 /**
319 * Get url of prev step.
320 *
321 * @return string
322 */
323 public function get_prev_url() {
324 $current = $this->get_current_step();
325 $steps = $this->get_step_keys();
326 $at = array_search( $current, $steps );
327 if ( $at > 0 ) {
328 $at --;
329 }
330
331 return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) );
332 }
333
334 /**
335 * Get position number of a step.
336 *
337 * @param string $step - Optional.
338 *
339 * @return mixed
340 */
341 public function get_step_position( $step = '' ) {
342 if ( ! $step ) {
343 $step = $this->get_current_step();
344 }
345
346 $steps = $this->get_step_keys();
347
348 return array_search( $step, $steps );
349 }
350
351 public function get_payments() {
352 return array(
353 'paypal' => array(
354 'name' => __( 'PayPal', 'learnpress' ),
355 'icon' => LP()->plugin_url( '/assets/images/paypal-2.png' ),
356 'callback' => array( $this, 'setup_paypal' ),
357 ),
358 );
359 }
360
361 public function setup_paypal() {
362 learn_press_admin_view( 'setup/setup-paypal' );
363 }
364
365 public function setup_stripe() {
366 learn_press_admin_view( 'setup/setup-stripe' );
367 }
368
369 /**
370 * Welcome step content.
371 */
372 public function step_welcome() {
373 learn_press_admin_view( 'setup/steps/welcome' );
374 }
375
376 /**
377 * Currency step content.
378 */
379 public function step_currency() {
380 learn_press_admin_view( 'setup/steps/currency' );
381 }
382
383 public function step_pages() {
384 learn_press_admin_view( 'setup/steps/pages' );
385 }
386
387 public function step_payment() {
388 learn_press_admin_view( 'setup/steps/payment' );
389 }
390
391 public function step_emails() {
392 learn_press_admin_view( 'setup/steps/emails' );
393 }
394
395 public function step_finish() {
396 learn_press_admin_view( 'setup/steps/finish' );
397 }
398
399 public function scripts() {
400 }
401
402 /**
403 * Get singleton instance
404 *
405 * @return bool|LP_Setup_Wizard
406 */
407 public static function instance() {
408 static $instance = false;
409
410 if ( ! $instance ) {
411 $instance = new self();
412 }
413
414 return $instance;
415 }
416 }
417 return LP_Setup_Wizard::instance();
418