| 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' ) |
| 78 |
|| ! current_user_can( 'install_plugins' ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
learn_press_admin_view( 'setup/header' ); |
| 83 |
learn_press_admin_view( 'setup/content', array( 'steps' => $this->get_steps() ) ); |
| 84 |
learn_press_admin_view( 'setup/footer' ); |
| 85 |
|
| 86 |
die(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Return array of all steps are available when running setup wizard. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
public function get_steps() { |
| 95 |
static $steps; |
| 96 |
if ( is_null( $steps ) ) { |
| 97 |
$steps = apply_filters( |
| 98 |
'learn-press/setup-wizard/steps', |
| 99 |
array( |
| 100 |
'welcome' => array( |
| 101 |
'title' => __( 'Welcome', 'learnpress' ), |
| 102 |
'callback' => array( $this, 'step_welcome' ), |
| 103 |
'next_button' => __( 'Get Started', 'learnpress' ), |
| 104 |
), |
| 105 |
'learning-experience' => array( |
| 106 |
'title' => __( 'Learning Experience', 'learnpress' ), |
| 107 |
'callback' => array( $this, 'step_learning_experience' ), |
| 108 |
), |
| 109 |
'pages' => array( |
| 110 |
'title' => __( 'Pages', 'learnpress' ), |
| 111 |
'callback' => array( $this, 'step_pages' ), |
| 112 |
), |
| 113 |
'payment' => array( |
| 114 |
'title' => __( 'Payment & Currency', 'learnpress' ), |
| 115 |
'callback' => array( $this, 'step_payment' ), |
| 116 |
), |
| 117 |
'emails' => array( |
| 118 |
'title' => __( 'Emails', 'learnpress' ), |
| 119 |
'callback' => array( $this, 'step_emails' ), |
| 120 |
), |
| 121 |
'finish' => array( |
| 122 |
'title' => __( 'Finish', 'learnpress' ), |
| 123 |
'callback' => array( $this, 'step_finish' ), |
| 124 |
), |
| 125 |
) |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
return $steps; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get all keys of available steps. |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function get_step_keys() { |
| 138 |
return array_keys( $this->get_steps() ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get key of current step. |
| 143 |
* |
| 144 |
* @param bool $key |
| 145 |
* |
| 146 |
* @return mixed|string |
| 147 |
*/ |
| 148 |
public function get_current_step( $key = true ) { |
| 149 |
$current = LP_Request::get_param( 'step' ); |
| 150 |
$steps = $this->get_steps(); |
| 151 |
|
| 152 |
if ( empty( $steps[ $current ] ) ) { |
| 153 |
$key_steps = array_keys( $steps ); |
| 154 |
$current = reset( $key_steps ); |
| 155 |
} |
| 156 |
|
| 157 |
$step = $steps[ $current ]; |
| 158 |
if ( empty( $step['slug'] ) ) { |
| 159 |
$step['slug'] = $current; |
| 160 |
} |
| 161 |
|
| 162 |
return $key ? $current : $step; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Return true if the first step is viewing. |
| 167 |
* |
| 168 |
* @return bool |
| 169 |
*/ |
| 170 |
public function is_first_step() { |
| 171 |
$steps = $this->get_step_keys(); |
| 172 |
|
| 173 |
return array_search( $this->get_current_step(), $steps ) === 0; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Return true if the last steo is viewing. |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public function is_last_step() { |
| 182 |
$steps = $this->get_step_keys(); |
| 183 |
|
| 184 |
return array_search( $this->get_current_step(), $steps ) === sizeof( $steps ) - 1; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get url of next step. |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function get_next_url() { |
| 193 |
$current = $this->get_current_step(); |
| 194 |
$steps = $this->get_step_keys(); |
| 195 |
$at = array_search( $current, $steps ); |
| 196 |
if ( $at < sizeof( $steps ) - 1 ) { |
| 197 |
++$at; |
| 198 |
} |
| 199 |
|
| 200 |
return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Get url of prev step. |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
public function get_prev_url() { |
| 209 |
$current = $this->get_current_step(); |
| 210 |
$steps = $this->get_step_keys(); |
| 211 |
$at = array_search( $current, $steps ); |
| 212 |
if ( $at > 0 ) { |
| 213 |
--$at; |
| 214 |
} |
| 215 |
|
| 216 |
return esc_url_raw( add_query_arg( 'step', $steps[ $at ], admin_url( $this->_base_url ) ) ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get position number of a step. |
| 221 |
* |
| 222 |
* @param string $step - Optional. |
| 223 |
* |
| 224 |
* @return mixed |
| 225 |
*/ |
| 226 |
public function get_step_position( $step = '' ) { |
| 227 |
if ( ! $step ) { |
| 228 |
$step = $this->get_current_step(); |
| 229 |
} |
| 230 |
|
| 231 |
$steps = $this->get_step_keys(); |
| 232 |
|
| 233 |
return array_search( $step, $steps ); |
| 234 |
} |
| 235 |
|
| 236 |
public function get_payments() { |
| 237 |
return array( |
| 238 |
'paypal' => array( |
| 239 |
'name' => __( 'PayPal', 'learnpress' ), |
| 240 |
'icon' => LearnPress::instance()->plugin_url( 'assets/images/paypal-logo-preview.png' ), |
| 241 |
'callback' => array( $this, 'setup_paypal' ), |
| 242 |
), |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
public function setup_paypal() { |
| 247 |
learn_press_admin_view( 'setup/setup-paypal' ); |
| 248 |
} |
| 249 |
|
| 250 |
public function setup_stripe() { |
| 251 |
learn_press_admin_view( 'setup/setup-stripe' ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Welcome step content. |
| 256 |
*/ |
| 257 |
public function step_welcome() { |
| 258 |
learn_press_admin_view( 'setup/steps/welcome' ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Learning Experience step content. |
| 263 |
*/ |
| 264 |
public function step_learning_experience() { |
| 265 |
learn_press_admin_view( 'setup/steps/learning-experience' ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Currency step content. |
| 270 |
*/ |
| 271 |
public function step_currency() { |
| 272 |
learn_press_admin_view( 'setup/steps/currency' ); |
| 273 |
} |
| 274 |
|
| 275 |
public function step_pages() { |
| 276 |
learn_press_admin_view( 'setup/steps/pages' ); |
| 277 |
} |
| 278 |
|
| 279 |
public function step_payment() { |
| 280 |
learn_press_admin_view( 'setup/steps/payment' ); |
| 281 |
} |
| 282 |
|
| 283 |
public function step_emails() { |
| 284 |
learn_press_admin_view( 'setup/steps/emails' ); |
| 285 |
} |
| 286 |
|
| 287 |
public function step_finish() { |
| 288 |
learn_press_admin_view( 'setup/steps/finish' ); |
| 289 |
update_option( 'learn_press_setup_wizard_completed', 'yes' ); |
| 290 |
} |
| 291 |
|
| 292 |
public function scripts() { |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Get singleton instance |
| 297 |
* |
| 298 |
* @return bool|LP_Setup_Wizard |
| 299 |
*/ |
| 300 |
public static function instance() { |
| 301 |
static $instance; |
| 302 |
if ( is_null( $instance ) ) { |
| 303 |
$instance = new self(); |
| 304 |
} |
| 305 |
|
| 306 |
return $instance; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
return LP_Setup_Wizard::instance(); |
| 311 |
|