PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 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, at inc/admin/class-lp-setup-wizard.php

420 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
116 // LP_Install::create_options();
117
118 update_option( 'learn_press_setup_wizard_completed', 'yes' );
119 }
120
121 // LP_Install::create_tables();
122
123 $this->save();
124
125 // Refresh new changes
126 // LP_Settings::instance()->refresh();
127
128 $assets = learn_press_admin_assets();
129
130 // tungnx: fix error with Woocommerce
131 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Loader', 'register_scripts' ) );
132 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Loader', 'load_scripts' ), 15 );
133 remove_action( 'admin_enqueue_scripts', array( 'Automattic\WooCommerce\Admin\Features\Features', 'load_scripts' ), 15 );
134 // End fix
135 // @do_action( 'admin_enqueue_scripts' );
136
137 wp_enqueue_style( 'buttons' );
138 wp_enqueue_style( 'common' );
139 wp_enqueue_style( 'forms' );
140 wp_enqueue_style( 'themes' );
141 wp_enqueue_style( 'dashboard' );
142 wp_enqueue_style( 'widgets' );
143 wp_enqueue_style( 'lp-admin', $assets->url( 'css/admin/admin.css' ) );
144 wp_enqueue_style( 'lp-setup', $assets->url( 'css/admin/setup.css' ) );
145 wp_enqueue_style( 'lp-select2', $assets->url( 'src/css/vendor/select2.min.css' ) );
146
147 wp_enqueue_script( 'lp-select2', $assets->url( 'src/js/vendor/select2.full.min.js' ) );
148 wp_enqueue_script( 'lp-utils', $assets->url( 'js/dist/utils.js' ) );
149 wp_enqueue_script( 'lp-admin', $assets->url( 'src/js/admin/admin.js' ), uniqid(), true );
150 wp_enqueue_script( 'drop-down-page', $assets->url( 'src/js/admin/share/dropdown-pages.js' ), uniqid(), true );
151 wp_register_script( 'lp-setup', $assets->url( 'js/dist/admin/pages/setup.js' ), array( 'jquery', 'lp-admin' ), uniqid(), true );
152 wp_localize_script( 'lp-setup', 'lpGlobalSettings', learn_press_global_script_params() );
153 wp_enqueue_script( 'lp-setup' );
154 learn_press_admin_view( 'setup/header' );
155 learn_press_admin_view( 'setup/content', array( 'steps' => $this->get_steps() ) );
156 learn_press_admin_view( 'setup/footer' );
157
158 die();
159 }
160
161 public function save() {
162 $step = LP_Request::get_string( 'lp-setup-step' );
163
164 if ( ! wp_verify_nonce( LP_Request::get_string( 'lp-setup-nonce' ), 'lp-setup-step-' . $step ) ) {
165 return;
166 }
167
168 $postdata = LP_Request::get_array( 'settings' );
169 $steps = array( 'payment', 'pages', 'currency', 'emails' );
170
171 if ( ( 'yes' !== LP_Request::get( 'skip' ) ) && in_array( $step, $steps ) ) {
172 if ( array_key_exists( 'paypal', $postdata ) ) {
173 update_option( 'learn_press_paypal', $postdata['paypal'] );
174 }
175
176 if ( array_key_exists( 'currency', $postdata ) ) {
177 foreach ( $postdata['currency'] as $k => $v ) {
178 update_option( 'learn_press_' . $k, $v );
179 }
180 }
181
182 if ( array_key_exists( 'pages', $postdata ) ) {
183 foreach ( $postdata['pages'] as $k => $v ) {
184 update_option( 'learn_press_' . $k, $v );
185 }
186 }
187
188 if ( array_key_exists( 'emails', $postdata ) ) {
189 if ( ! empty( $postdata['emails']['enable'] ) && ( $postdata['emails']['enable'] === 'yes' ) ) {
190 $emails = LP_Emails::instance()->emails;
191
192 if ( $emails ) {
193 foreach ( $emails as $email ) {
194 $response[ $email->id ] = $email->enable( true );
195 }
196 }
197 }
198 }
199 }
200
201 do_action( 'learn-press/setup-wizard/update-settings', $postdata, $step );
202 }
203
204 /**
205 * Return array of all steps are available when running setup wizard.
206 *
207 * @return array
208 */
209 public function get_steps() {
210 static $steps = false;
211 if ( ! $steps ) {
212 $steps = apply_filters(
213 'learn-press/setup-wizard/steps',
214 array(
215 'welcome' => array(
216 'title' => __( 'Welcome', 'learnpress' ),
217 'callback' => array( $this, 'step_welcome' ),
218 'next_button' => __( 'Run Setup Wizard', 'learnpress' ),
219 ),
220 'pages' => array(
221 'title' => __( 'Pages', 'learnpress' ),
222 'callback' => array( $this, 'step_pages' ),
223 ),
224 // 'currency' => array(
225 // 'title' => __( 'Currency', 'learnpress' ),
226 // 'callback' => array( $this, 'step_currency' ),
227 // 'back_button' => false,
228 // 'skip_prev_button' => false
229 // ),
230 'payment' => array(
231 'title' => __( 'Payment', 'learnpress' ),
232 'callback' => array( $this, 'step_payment' ),
233 ),
234 // 'emails' => array(
235 // 'title' => __( 'Emails', 'learnpress' ),
236 // 'callback' => array( $this, 'step_emails' )
237 // ),
238 'finish' => array(
239 'title' => __( 'Finish', 'learnpress' ),
240 'callback' => array( $this, 'step_finish' ),
241 ),
242 )
243 );
244 }
245
246 return $steps;
247 }
248
249 /**
250 * Get all keys of available steps.
251 *
252 * @return array
253 */
254 public function get_step_keys() {
255 return array_keys( $this->get_steps() );
256 }
257
258 /**
259 * Get key of current step.
260 *
261 * @param bool $key
262 *
263 * @return mixed|string
264 */
265 public function get_current_step( $key = true ) {
266 $current = LP_Request::get_string( 'step' );
267 $steps = $this->get_steps();
268
269 if ( empty( $steps[ $current ] ) ) {
270 $key_steps = array_keys( $steps );
271 $current = reset( $key_steps );
272 }
273
274 $step = $steps[ $current ];
275 if ( empty( $step['slug'] ) ) {
276 $step['slug'] = $current;
277 }
278
279 return $key ? $current : $step;
280 }
281
282 /**
283 * Return true if the first step is viewing.
284 *
285 * @return bool
286 */
287 public function is_first_step() {
288 $steps = $this->get_step_keys();
289
290 return array_search( $this->get_current_step(), $steps ) === 0;
291 }
292
293 /**
294 * Return true if the last steo is viewing.
295 *
296 * @return bool
297 */
298 public function is_last_step() {
299 $steps = $this->get_step_keys();
300
301 return array_search( $this->get_current_step(), $steps ) === sizeof( $steps ) - 1;
302 }
303
304 /**
305 * Get url of next step.
306 *
307 * @return string
308 */
309 public function get_next_url() {
310 $current = $this->get_current_step();
311 $steps = $this->get_step_keys();
312 $at = array_search( $current, $steps );
313 if ( $at < sizeof( $steps ) - 1 ) {
314 $at ++;
315 }
316
317 return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) );
318 }
319
320 /**
321 * Get url of prev step.
322 *
323 * @return string
324 */
325 public function get_prev_url() {
326 $current = $this->get_current_step();
327 $steps = $this->get_step_keys();
328 $at = array_search( $current, $steps );
329 if ( $at > 0 ) {
330 $at --;
331 }
332
333 return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) );
334 }
335
336 /**
337 * Get position number of a step.
338 *
339 * @param string $step - Optional.
340 *
341 * @return mixed
342 */
343 public function get_step_position( $step = '' ) {
344 if ( ! $step ) {
345 $step = $this->get_current_step();
346 }
347
348 $steps = $this->get_step_keys();
349
350 return array_search( $step, $steps );
351 }
352
353 public function get_payments() {
354 return array(
355 'paypal' => array(
356 'name' => __( 'PayPal', 'learnpress' ),
357 'icon' => LP()->plugin_url( '/assets/images/paypal-2.png' ),
358 'callback' => array( $this, 'setup_paypal' ),
359 ),
360 );
361 }
362
363 public function setup_paypal() {
364 learn_press_admin_view( 'setup/setup-paypal' );
365 }
366
367 public function setup_stripe() {
368 learn_press_admin_view( 'setup/setup-stripe' );
369 }
370
371 /**
372 * Welcome step content.
373 */
374 public function step_welcome() {
375 learn_press_admin_view( 'setup/steps/welcome' );
376 }
377
378 /**
379 * Currency step content.
380 */
381 public function step_currency() {
382 learn_press_admin_view( 'setup/steps/currency' );
383 }
384
385 public function step_pages() {
386 learn_press_admin_view( 'setup/steps/pages' );
387 }
388
389 public function step_payment() {
390 learn_press_admin_view( 'setup/steps/payment' );
391 }
392
393 public function step_emails() {
394 learn_press_admin_view( 'setup/steps/emails' );
395 }
396
397 public function step_finish() {
398 learn_press_admin_view( 'setup/steps/finish' );
399 }
400
401 public function scripts() {
402 }
403
404 /**
405 * Get singleton instance
406 *
407 * @return bool|LP_Setup_Wizard
408 */
409 public static function instance() {
410 static $instance = false;
411
412 if ( ! $instance ) {
413 $instance = new self();
414 }
415
416 return $instance;
417 }
418 }
419 return LP_Setup_Wizard::instance();
420