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