PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.9 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 All 142 releases
learnpress / inc / Ajax / SetupWizardAjax.php

SetupWizardAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.10, at inc/Ajax/SetupWizardAjax.php

254 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class SetupWizardAjax
4 *
5 * Handle Setup Wizard requests via the LearnPress AJAX dispatcher.
6 *
7 * @since 4.4.6
8 * @version 1.0.0
9 */
10
11 namespace LearnPress\Ajax;
12
13 use Exception;
14 use LearnPress\Helpers\Response;
15 use LearnPress\Models\UserModel;
16 use LearnPress\Services\SetupDemoCourseService;
17 use LP_Emails;
18 use LP_Helper;
19 use LP_Request;
20 use LP_Settings_Cache;
21 use LP_Setup_Wizard;
22 use Throwable;
23
24 defined( 'ABSPATH' ) || exit;
25
26 /**
27 * Class SetupWizardAjax
28 */
29 class SetupWizardAjax extends AbstractAjax {
30 /**
31 * Save the current wizard step.
32 *
33 * @return void
34 */
35 public function lp_setup_wizard_save_step() {
36 $response = new Response();
37
38 try {
39 if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) {
40 throw new Exception( esc_html__( 'You do not have permission to update setup settings.', 'learnpress' ) );
41 }
42
43 $form = $this->get_form_data();
44 $step = sanitize_key( $form['lp-setup-step'] ?? '' );
45
46 $allowed_steps = array( 'welcome', 'learning-experience', 'pages', 'payment', 'emails', 'finish' );
47 if ( ! in_array( $step, $allowed_steps, true ) ) {
48 throw new Exception( esc_html__( 'Invalid setup step.', 'learnpress' ) );
49 }
50
51 $settings = is_array( $form['settings'] ?? null ) ? $form['settings'] : array();
52 switch ( $step ) {
53 case 'learning-experience':
54 $this->save_learning_experience( $settings );
55 break;
56 case 'pages':
57 $this->save_pages();
58 break;
59 case 'payment':
60 $this->save_payment( $settings );
61 break;
62 case 'emails':
63 $this->save_emails( $settings );
64 break;
65 }
66
67 ( new LP_Settings_Cache( true ) )->clean_lp_settings();
68 do_action( 'learn-press/setup-wizard/update-settings', $settings, $step );
69
70 $response->status = Response::STATUS_SUCCESS;
71 $response->message = esc_html__( 'Setup settings saved.', 'learnpress' );
72 } catch ( Throwable $error ) {
73 $response->message = $error->getMessage();
74 }
75
76 wp_send_json( $response );
77 }
78
79 /**
80 * Import one demo course for the Finish step.
81 *
82 * @return void
83 */
84 public function lp_setup_import_demo_course() {
85 $response = new Response();
86
87 try {
88 if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) {
89 throw new Exception( esc_html__( 'You do not have permission to install demo courses.', 'learnpress' ) );
90 }
91
92 $params = LP_Helper::json_decode( LP_Request::get_param( 'data' ), true );
93 $index = is_array( $params ) ? ( $params['index'] ?? 0 ) : -1;
94 if ( ! is_numeric( $index ) || (int) $index < 0 ) {
95 throw new Exception( esc_html__( 'Invalid demo course index.', 'learnpress' ) );
96 }
97
98 $setupDemoCourseService = new SetupDemoCourseService();
99 $response->data = $setupDemoCourseService->import_course( (int) $index );
100 $response->status = Response::STATUS_SUCCESS;
101 $response->message = esc_html__( 'The demo course was processed successfully.', 'learnpress' );
102 } catch ( Throwable $error ) {
103 $response->message = $error->getMessage();
104 }
105
106 wp_send_json( $response );
107 }
108
109 /**
110 * Convert flat bracketed form keys returned by getDataOfForm into arrays.
111 *
112 * @return array
113 * @throws Exception
114 */
115 protected function get_form_data(): array {
116 $params = LP_Helper::json_decode( LP_Request::get_param( 'data' ), true );
117 if ( ! is_array( $params ) ) {
118 throw new Exception( esc_html__( 'Invalid setup data.', 'learnpress' ) );
119 }
120
121 parse_str( http_build_query( $params ), $form );
122
123 return is_array( $form ) ? $form : array();
124 }
125
126 /**
127 * Save Learning Experience settings.
128 *
129 * @param array $settings Submitted settings.
130 *
131 * @return void
132 */
133 protected function save_learning_experience( array $settings ) {
134 $course = is_array( $settings['course'] ?? null ) ? $settings['course'] : array();
135 if ( ! wp_is_block_theme() ) {
136 $layout = sanitize_key( $course['layout_single_course'] ?? '' );
137 if ( in_array( $layout, array( 'modern', 'classic' ), true ) ) {
138 update_option( 'learn_press_layout_single_course', $layout );
139 }
140
141 $listing = sanitize_key( $course['archive_courses_layout'] ?? '' );
142 if ( in_array( $listing, array( 'grid', 'list' ), true ) ) {
143 update_option( 'learn_press_archive_courses_layout', $listing );
144 }
145 }
146
147 update_option( 'learn_press_auto_enroll', $this->toggle_value( $course['auto_enroll'] ?? 0 ) );
148 }
149
150 /**
151 * Ensure all required LearnPress pages exist.
152 *
153 * @return void
154 */
155 protected function save_pages() {
156 // LP_Setup_Wizard is only loaded in admin context, so need include here to use.
157 include_once LP_PLUGIN_PATH . 'inc/admin/class-lp-setup-wizard.php';
158
159 $page_keys = array(
160 'courses_page_id',
161 'instructors_page_id',
162 'single_instructor_page_id',
163 'profile_page_id',
164 'checkout_page_id',
165 'become_a_teacher_page_id',
166 'term_conditions_page_id',
167 );
168
169 foreach ( $page_keys as $page_key ) {
170 $option_key = 'learn_press_' . $page_key;
171 $page_id = (int) get_option( $option_key );
172 if ( ! $page_id || 'publish' !== get_post_status( $page_id ) ) {
173 $page_id = (int) LP_Setup_Wizard::instance()->create_page( $page_key );
174 if ( $page_id ) {
175 update_option( $option_key, $page_id );
176 }
177 }
178 }
179 }
180
181 /**
182 * Save currency and payment gateway settings.
183 *
184 * @param array $settings Submitted settings.
185 *
186 * @return void
187 */
188 protected function save_payment( array $settings ) {
189 $currency = is_array( $settings['currency'] ?? null ) ? $settings['currency'] : array();
190 $code = strtoupper( sanitize_text_field( $currency['currency'] ?? '' ) );
191 if ( array_key_exists( $code, learn_press_currencies() ) ) {
192 update_option( 'learn_press_currency', $code );
193 }
194
195 $position = sanitize_key( $currency['currency_pos'] ?? '' );
196 if ( array_key_exists( $position, learn_press_currency_positions() ) ) {
197 update_option( 'learn_press_currency_pos', $position );
198 }
199
200 update_option( 'learn_press_thousands_separator', sanitize_text_field( $currency['thousands_separator'] ?? ',' ) );
201 update_option( 'learn_press_decimals_separator', sanitize_text_field( $currency['decimals_separator'] ?? '.' ) );
202 update_option( 'learn_press_number_of_decimals', min( 8, max( 0, (int) ( $currency['number_of_decimals'] ?? 2 ) ) ) );
203
204 foreach ( array( 'offline-payment', 'paypal' ) as $gateway ) {
205 $gateway_settings = get_option( 'learn_press_' . $gateway, array() );
206 $gateway_settings = is_array( $gateway_settings ) ? $gateway_settings : array();
207 $gateway_settings['enable'] = $this->toggle_value( $settings[ $gateway ]['enable'] ?? 0 );
208 update_option( 'learn_press_' . $gateway, $gateway_settings );
209 }
210 }
211
212 /**
213 * Save the supported email notification groups.
214 *
215 * @param array $settings Submitted settings.
216 *
217 * @return void
218 */
219 protected function save_emails( array $settings ) {
220 $groups = array(
221 'new_order' => array( 'new-order-admin', 'new-order-instructor' ),
222 'order_completed' => array( 'completed-order-user' ),
223 'course_enrolled' => array( 'enrolled-course-user' ),
224 'course_completed' => array( 'finished-course-user' ),
225 'become_instructor_request' => array( 'become-an-instructor' ),
226 'become_instructor_accepted' => array( 'instructor-accepted' ),
227 );
228 $notifications = is_array( $settings['emails']['notifications'] ?? null )
229 ? $settings['emails']['notifications']
230 : array();
231
232 foreach ( $groups as $key => $email_ids ) {
233 $enabled = 'yes' === $this->toggle_value( $notifications[ $key ] ?? 0 );
234 foreach ( $email_ids as $email_id ) {
235 $email = LP_Emails::get_email( $email_id );
236 if ( $email ) {
237 $email->enable( $enabled );
238 }
239 }
240 }
241 }
242
243 /**
244 * Convert an AdminTemplate toggle value to a LearnPress option value.
245 *
246 * @param mixed $value Submitted toggle value.
247 *
248 * @return string
249 */
250 protected function toggle_value( $value ): string {
251 return in_array( (string) $value, array( '1', 'yes', 'true' ), true ) ? 'yes' : 'no';
252 }
253 }
254