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