Skip to content

Commit 0335dbb

Browse files
committed
Merge branch '4.0.0-dev' of https://github.com/themeum/tutor into learning-area-quiz
2 parents 092d2ac + f420bfd commit 0335dbb

4 files changed

Lines changed: 74 additions & 55 deletions

File tree

assets/src/js/frontend/dashboard/pages/settings.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ interface PreferencesFormProps {
6363
auto_play_next: boolean;
6464
theme: string;
6565
font_scale: number;
66+
formId?: string;
67+
}
68+
69+
interface UpdateNotificationProps {
70+
[key: string]: boolean | string;
6671
}
6772

6873
const settings = () => {
@@ -100,7 +105,8 @@ const settings = () => {
100105
this.handleResetPassword = this.handleResetPassword.bind(this);
101106

102107
this.handleUpdateNotification = this.query.useMutation(this.updateNotification, {
103-
onSuccess: (data: TutorMutationResponse<string>) => {
108+
onSuccess: (data: TutorMutationResponse<string>, payload: UpdateNotificationProps) => {
109+
this.form.reset(payload?.formId as string, payload as unknown as Record<string, unknown>);
104110
this.toast.success(data?.message ?? __('Notification settings updated', 'tutor'));
105111
},
106112
onError: (error: Error) => {
@@ -168,7 +174,8 @@ const settings = () => {
168174
});
169175

170176
this.savePreferencesMutation = this.query.useMutation(this.updatePreferences, {
171-
onSuccess: (data: TutorMutationResponse<PreferencesFormProps>) => {
177+
onSuccess: (data: TutorMutationResponse<PreferencesFormProps>, payload: PreferencesFormProps) => {
178+
this.form.reset(payload?.formId || '', payload as unknown as Record<string, unknown>);
172179
this.toast.success(data?.message ?? __('Preferences saved successfully', 'tutor'));
173180
},
174181
onError: (error: Error) => {
@@ -177,16 +184,22 @@ const settings = () => {
177184
});
178185
},
179186

180-
async updatePreferences(payload: Record<string, unknown>) {
187+
async updatePreferences(payload: PreferencesFormProps) {
181188
return wpAjaxInstance.post(endpoints.UPDATE_USER_PREFERENCES, payload) as unknown as Promise<
182189
TutorMutationResponse<PreferencesFormProps>
183190
>;
184191
},
185192

186-
async updateNotification(payload: Record<string, boolean>) {
193+
async updateNotification(payload: UpdateNotificationProps) {
187194
const transformedPayload = Object.keys(payload).reduce(
188195
(formattedPayload, key) => {
189196
const value = payload[key];
197+
198+
if (typeof value !== 'boolean') {
199+
formattedPayload[`${key}`] = value;
200+
return formattedPayload;
201+
}
202+
190203
const stringValue = typeof value === 'boolean' ? (value ? 'on' : 'off') : value;
191204

192205
if (!key.includes('__')) {

assets/src/scss/frontend/dashboard/settings/_profile.scss

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ body:has(#wpadminbar) {
107107
}
108108
}
109109

110-
.tutor-profile-notification-content {
111-
border-top: 1px solid $tutor-border-idle;
112-
}
113-
114110
.tutor-profile-notification-toggle {
115111
display: flex;
116112
align-items: center;
@@ -142,8 +138,4 @@ body:has(#wpadminbar) {
142138
.tutor-input-field {
143139
margin-bottom: 0px;
144140
}
145-
146-
.tutor-profile-notification-content {
147-
border-top: 1px solid $tutor-border-idle;
148-
}
149141
}

classes/UserPreference.php

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
defined( 'ABSPATH' ) || exit;
1717

18+
use Tutor\Cache\TutorCache;
1819
use Tutor\Helpers\HttpHelper;
1920
use Tutor\Traits\JsonResponse;
2021

@@ -158,26 +159,6 @@ public function add_theme_attribute( $classes ) {
158159
return $classes;
159160
}
160161

161-
/**
162-
* Get default preferences.
163-
*
164-
* Keep this method very small and declarative so it is easy to extend.
165-
*
166-
* @since 4.0.0
167-
*
168-
* @return array
169-
*/
170-
public static function get_default_preferences() {
171-
return apply_filters(
172-
'tutor_user_preference_defaults',
173-
array(
174-
'auto_play_next' => true,
175-
'theme' => self::DEFAULT_THEME,
176-
'font_scale' => self::DEFAULT_FONT_SCALE,
177-
)
178-
);
179-
}
180-
181162
/**
182163
* Get merged preferences for a user.
183164
*
@@ -189,17 +170,46 @@ public static function get_default_preferences() {
189170
*
190171
* @return array
191172
*/
192-
public function get_preferences( $user_id = 0 ) {
173+
public static function get_preferences( $user_id = 0 ) {
193174
$user_id = tutor_utils()->get_user_id( $user_id );
194175
if ( ! $user_id ) {
195176
return array();
196177
}
197178

179+
// Check cache first.
180+
$cache_key = 'get_preferences_' . $user_id;
181+
$cached = TutorCache::get( $cache_key );
182+
if ( false !== $cached ) {
183+
return $cached;
184+
}
185+
186+
// Get from database.
198187
$preferences = get_user_meta( $user_id, self::META_KEY, true );
199188
if ( ! is_array( $preferences ) ) {
200189
$preferences = array();
201190
}
202-
return array_merge( self::get_default_preferences(), $preferences );
191+
$result = array_merge( self::get_default_preferences(), $preferences );
192+
193+
// Store in cache.
194+
TutorCache::set( $cache_key, $result );
195+
196+
return $result;
197+
}
198+
199+
/**
200+
* Get a single preference value with default support.
201+
*
202+
* @since 4.0.0
203+
*
204+
* @param string $key Preference key.
205+
* @param mixed $fallback Default value if not set. If false, use internal defaults.
206+
* @param int|string $user_id Optional user id.
207+
*
208+
* @return mixed
209+
*/
210+
public static function get( $key, $fallback = false, $user_id = 0 ) {
211+
$prefs = self::get_preferences( $user_id );
212+
return isset( $prefs[ $key ] ) ? $prefs[ $key ] : $fallback;
203213
}
204214

205215
/**
@@ -275,6 +285,27 @@ public function ajax_save_user_preferences() {
275285
);
276286
}
277287

288+
/**
289+
* Get default preferences.
290+
*
291+
* @since 4.0.0
292+
*
293+
* @return array
294+
*/
295+
private static function get_default_preferences() {
296+
// Get defaults with filter.
297+
$defaults = apply_filters(
298+
'tutor_user_preference_defaults',
299+
array(
300+
'auto_play_next' => (bool) tutor_utils()->get_option( 'autoload_next_course_content' ),
301+
'theme' => self::DEFAULT_THEME,
302+
'font_scale' => self::DEFAULT_FONT_SCALE,
303+
)
304+
);
305+
306+
return $defaults;
307+
}
308+
278309
/**
279310
* Get theme options for UI selects.
280311
*

templates/dashboard/account/settings/preferences.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@
1616
use Tutor\Components\Constants\InputType;
1717
use Tutor\Components\Constants\Size;
1818

19-
// Theme options.
2019
$theme_options = UserPreference::get_theme_options();
2120

2221
$font_scale_options = UserPreference::get_font_scale_options();
2322

2423
// Load current user preferences to seed the form.
25-
$user_preferences = ( new UserPreference( false ) )->get_preferences();
26-
27-
$default_values = array(
28-
'auto_play_next' => isset( $user_preferences['auto_play_next'] ) ? (bool) $user_preferences['auto_play_next'] : false,
29-
'theme' => isset( $user_preferences['theme'] ) ? $user_preferences['theme'] : UserPreference::DEFAULT_THEME,
30-
'font_scale' => isset( $user_preferences['font_scale'] ) ? (int) $user_preferences['font_scale'] : UserPreference::DEFAULT_FONT_SCALE,
31-
);
24+
$user_preferences = UserPreference::get_preferences();
3225

3326
?>
3427

@@ -39,15 +32,14 @@
3932
id: "<?php echo esc_attr( $form_id ); ?>",
4033
mode: "onChange",
4134
shouldFocusError: true,
42-
defaultValues: <?php echo wp_json_encode( $default_values ); ?>
35+
defaultValues: <?php echo wp_json_encode( $user_preferences ); ?>
4336
})'
44-
4537
x-bind="getFormBindings()"
46-
@submit="handleSubmit((data) => { savePreferencesMutation?.mutate(data); })($event)"
38+
@submit="handleSubmit((data) => { savePreferencesMutation?.mutate({...data, formId: '<?php echo esc_attr( $form_id ); ?>'}); })($event)"
4739
>
4840
<!-- Course Content Section -->
4941
<h5 class="tutor-preferences-section-header tutor-h5">
50-
<?php esc_html_e( 'Course Content', 'tutor' ); ?>
42+
<?php esc_html_e( 'Preferences', 'tutor' ); ?>
5143
</h5>
5244
<div class="tutor-card tutor-card-rounded-2xl tutor-mb-7">
5345
<div class="tutor-preferences-setting-item">
@@ -68,11 +60,6 @@
6860
?>
6961
</div>
7062
</div>
71-
</div>
72-
73-
<!-- Appearance Section -->
74-
<h5 class="tutor-preferences-section-header tutor-color-black tutor-mb-7"><?php esc_html_e( 'Appearance', 'tutor' ); ?></h5>
75-
<div class="tutor-card tutor-card-rounded-2xl tutor-mb-6">
7663
<div class="tutor-preferences-setting-item">
7764
<div class="tutor-preferences-setting-content">
7865
<div class="tutor-preferences-setting-icon">
@@ -95,10 +82,6 @@
9582
?>
9683
</div>
9784
</div>
98-
</div>
99-
100-
<h5 class="tutor-preferences-section-header tutor-color-black tutor-mb-7"><?php esc_html_e( 'Accessibility', 'tutor' ); ?></h5>
101-
<div class="tutor-card tutor-card-rounded-2xl tutor-mb-6">
10285
<div class="tutor-preferences-setting-item">
10386
<div class="tutor-preferences-setting-content">
10487
<div class="tutor-preferences-setting-icon">

0 commit comments

Comments
 (0)