PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / Ajax / SetupWizardAjax.php

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

251 lines 7.6 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;
21 use LP_Settings_Cache;
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 $page_keys = array(
157 'courses_page_id',
158 'instructors_page_id',
159 'single_instructor_page_id',
160 'profile_page_id',
161 'checkout_page_id',
162 'become_a_teacher_page_id',
163 'term_conditions_page_id',
164 );
165
166 foreach ( $page_keys as $page_key ) {
167 $option_key = 'learn_press_' . $page_key;
168 $page_id = (int) get_option( $option_key );
169 if ( ! $page_id || 'publish' !== get_post_status( $page_id ) ) {
170 $page_id = (int) \LP_Setup_Wizard::instance()->create_page( $page_key );
171 if ( $page_id ) {
172 update_option( $option_key, $page_id );
173 }
174 }
175 }
176 }
177
178 /**
179 * Save currency and payment gateway settings.
180 *
181 * @param array $settings Submitted settings.
182 *
183 * @return void
184 */
185 protected function save_payment( array $settings ) {
186 $currency = is_array( $settings['currency'] ?? null ) ? $settings['currency'] : array();
187 $code = strtoupper( sanitize_text_field( $currency['currency'] ?? '' ) );
188 if ( array_key_exists( $code, learn_press_currencies() ) ) {
189 update_option( 'learn_press_currency', $code );
190 }
191
192 $position = sanitize_key( $currency['currency_pos'] ?? '' );
193 if ( array_key_exists( $position, learn_press_currency_positions() ) ) {
194 update_option( 'learn_press_currency_pos', $position );
195 }
196
197 update_option( 'learn_press_thousands_separator', sanitize_text_field( $currency['thousands_separator'] ?? ',' ) );
198 update_option( 'learn_press_decimals_separator', sanitize_text_field( $currency['decimals_separator'] ?? '.' ) );
199 update_option( 'learn_press_number_of_decimals', min( 8, max( 0, (int) ( $currency['number_of_decimals'] ?? 2 ) ) ) );
200
201 foreach ( array( 'offline-payment', 'paypal' ) as $gateway ) {
202 $gateway_settings = get_option( 'learn_press_' . $gateway, array() );
203 $gateway_settings = is_array( $gateway_settings ) ? $gateway_settings : array();
204 $gateway_settings['enable'] = $this->toggle_value( $settings[ $gateway ]['enable'] ?? 0 );
205 update_option( 'learn_press_' . $gateway, $gateway_settings );
206 }
207 }
208
209 /**
210 * Save the supported email notification groups.
211 *
212 * @param array $settings Submitted settings.
213 *
214 * @return void
215 */
216 protected function save_emails( array $settings ) {
217 $groups = array(
218 'new_order' => array( 'new-order-admin', 'new-order-instructor' ),
219 'order_completed' => array( 'completed-order-user' ),
220 'course_enrolled' => array( 'enrolled-course-user' ),
221 'course_completed' => array( 'finished-course-user' ),
222 'become_instructor_request' => array( 'become-an-instructor' ),
223 'become_instructor_accepted' => array( 'instructor-accepted' ),
224 );
225 $notifications = is_array( $settings['emails']['notifications'] ?? null )
226 ? $settings['emails']['notifications']
227 : array();
228
229 foreach ( $groups as $key => $email_ids ) {
230 $enabled = 'yes' === $this->toggle_value( $notifications[ $key ] ?? 0 );
231 foreach ( $email_ids as $email_id ) {
232 $email = LP_Emails::get_email( $email_id );
233 if ( $email ) {
234 $email->enable( $enabled );
235 }
236 }
237 }
238 }
239
240 /**
241 * Convert an AdminTemplate toggle value to a LearnPress option value.
242 *
243 * @param mixed $value Submitted toggle value.
244 *
245 * @return string
246 */
247 protected function toggle_value( $value ): string {
248 return in_array( (string) $value, array( '1', 'yes', 'true' ), true ) ? 'yes' : 'no';
249 }
250 }
251