Skip to content

Commit 6d8d92a

Browse files
authored
Merge pull request #565 from ProgressPlanner/filip/update-set-timezone-task
Set the timezone task - Update
2 parents 8de5249 + 61addd9 commit 6d8d92a

3 files changed

Lines changed: 105 additions & 32 deletions

File tree

assets/js/recommendations/interactive-task.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,9 @@ const prplInteractiveTaskFormListener = {
5757
if ( ! postId ) {
5858
return;
5959
}
60+
61+
// This will trigger the celebration event (confetti) as well.
6062
prplSuggestedTask.maybeComplete( postId );
61-
taskEl.setAttribute( 'data-task-action', 'celebrate' );
62-
document.dispatchEvent(
63-
new CustomEvent( 'prpl/celebrateTasks', {
64-
detail: {
65-
element: taskEl,
66-
},
67-
} )
68-
);
6963
} );
7064
} );
7165
} );
@@ -94,15 +88,9 @@ const prplInteractiveTaskFormListener = {
9488
if ( ! postId ) {
9589
return;
9690
}
91+
92+
// This will trigger the celebration event (confetti) as well.
9793
prplSuggestedTask.maybeComplete( postId );
98-
taskEl.setAttribute( 'data-task-action', 'celebrate' );
99-
document.dispatchEvent(
100-
new CustomEvent( 'prpl/celebrateTasks', {
101-
detail: {
102-
element: taskEl,
103-
},
104-
} )
105-
);
10694
} );
10795
},
10896

@@ -112,6 +100,7 @@ const prplInteractiveTaskFormListener = {
112100
settingPath = false,
113101
popoverId,
114102
settingCallbackValue = ( settingValue ) => settingValue,
103+
action = 'prpl_interactive_task_submit',
115104
} = {} ) => {
116105
const formElement = document.querySelector( `#${ popoverId } form` );
117106

@@ -131,7 +120,7 @@ const prplInteractiveTaskFormListener = {
131120
progressPlannerAjaxRequest( {
132121
url: progressPlanner.ajaxUrl,
133122
data: {
134-
action: 'prpl_interactive_task_submit',
123+
action,
135124
_ajax_nonce: progressPlanner.nonce,
136125
post_id: taskId,
137126
setting,
@@ -153,15 +142,9 @@ const prplInteractiveTaskFormListener = {
153142
if ( ! postId ) {
154143
return;
155144
}
145+
146+
// This will trigger the celebration event (confetti) as well.
156147
prplSuggestedTask.maybeComplete( postId );
157-
taskEl.setAttribute( 'data-task-action', 'celebrate' );
158-
document.dispatchEvent(
159-
new CustomEvent( 'prpl/celebrateTasks', {
160-
detail: {
161-
element: taskEl,
162-
},
163-
} )
164-
);
165148
} );
166149
} );
167150
},
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
/* global prplInteractiveTaskFormListener */
1+
/* global prplInteractiveTaskFormListener, prplDocumentReady */
22

33
/*
44
* Set the site timezone.
55
*
66
* Dependencies: progress-planner/recommendations/interactive-task
77
*/
88

9-
prplInteractiveTaskFormListener.siteSettings( {
9+
prplInteractiveTaskFormListener.settings( {
1010
settingAPIKey: 'timezone',
1111
setting: 'timezone',
1212
taskId: 'select-timezone',
1313
popoverId: 'prpl-popover-select-timezone',
14+
action: 'prpl_interactive_task_submit_select-timezone',
15+
} );
16+
17+
prplDocumentReady( () => {
18+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
19+
const timezoneSelect = document.querySelector( 'select#timezone' );
20+
const timezoneSaved = timezoneSelect?.dataset?.timezoneSaved || 'false';
21+
22+
// Try to preselect the timezone.
23+
if ( timezone && timezoneSelect && 'false' === timezoneSaved ) {
24+
timezoneSelect.value = timezone;
25+
}
1426
} );

classes/suggested-tasks/providers/class-select-timezone.php

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class Select_Timezone extends Tasks_Interactive {
3333
*/
3434
protected $is_dismissable = true;
3535

36+
/**
37+
* Initialize the task.
38+
*
39+
* @return void
40+
*/
41+
public function init() {
42+
\add_action( 'wp_ajax_prpl_interactive_task_submit_select-timezone', [ $this, 'handle_interactive_task_specific_submit' ] );
43+
}
44+
3645
/**
3746
* Get the task URL.
3847
*
@@ -69,7 +78,7 @@ protected function get_title() {
6978
* @return string
7079
*/
7180
protected function get_description() {
72-
return \esc_html__( 'Set site timezone to ensure scheduled posts and pages are published at desired time.', 'progress-planner' );
81+
return \esc_html__( 'Setting the time zone correctly on your site is valuable. By setting the correct time zone, you ensure scheduled tasks happen exactly when you want them to happen. To correctly account for daylight savings\', we recommend you use the city-based time zone instead of the UTC offset (e.g. Amsterdam or London).', 'progress-planner' );
7382
}
7483

7584
/**
@@ -95,7 +104,7 @@ public function should_add_task() {
95104
*/
96105
public function print_popover_instructions() {
97106
echo '<p>';
98-
\esc_html_e( 'Set site timezone to ensure scheduled posts and pages are published at desired time.', 'progress-planner' );
107+
\esc_html_e( 'Setting the time zone correctly on your site is valuable. By setting the correct time zone, you ensure scheduled tasks happen exactly when you want them to happen. To correctly account for daylight savings\', we recommend you use the city-based time zone instead of the UTC offset (e.g. Amsterdam or London).', 'progress-planner' );
99108
echo '</p>';
100109
}
101110

@@ -105,8 +114,9 @@ public function print_popover_instructions() {
105114
* @return void
106115
*/
107116
public function print_popover_form_contents() {
108-
$current_offset = \get_option( 'gmt_offset' );
109-
$tzstring = \get_option( 'timezone_string' );
117+
$current_offset = \get_option( 'gmt_offset' );
118+
$tzstring = \get_option( 'timezone_string' );
119+
$was_tzstring_saved = '' !== $tzstring || '0' !== $current_offset ? 'true' : 'false';
110120

111121
// Remove old Etc mappings. Fallback to gmt_offset.
112122
if ( str_contains( $tzstring, 'Etc/GMT' ) ) {
@@ -124,7 +134,7 @@ public function print_popover_form_contents() {
124134
}
125135
?>
126136
<label>
127-
<select id="timezone" name="timezone">
137+
<select id="timezone" name="timezone" data-timezone-saved="<?php echo \esc_attr( $was_tzstring_saved ); ?>">
128138
<?php echo \wp_timezone_choice( $tzstring, \get_user_locale() ); ?>
129139
</select>
130140
</label>
@@ -133,4 +143,72 @@ public function print_popover_form_contents() {
133143
</button>
134144
<?php
135145
}
146+
147+
/**
148+
* Handle the interactive task submit.
149+
*
150+
* This is only for interactive tasks that change non-core settings.
151+
* The $_POST data is expected to be:
152+
* - setting: (string) The setting to update.
153+
* - value: (mixed) The value to update the setting to.
154+
* - setting_path: (array) The path to the setting to update.
155+
* Use an empty array if the setting is not nested.
156+
* If the value is nested, use an array of keys.
157+
* Example: [ 'a', 'b', 'c' ] will update the value of $option['a']['b']['c'].
158+
* - nonce: (string) The nonce.
159+
*
160+
* @return void
161+
*/
162+
public function handle_interactive_task_specific_submit() {
163+
// Check the nonce.
164+
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
165+
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
166+
}
167+
168+
if ( ! isset( $_POST['setting'] ) ) {
169+
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing setting.', 'progress-planner' ) ] );
170+
}
171+
172+
if ( ! isset( $_POST['value'] ) ) {
173+
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing value.', 'progress-planner' ) ] );
174+
}
175+
176+
if ( ! isset( $_POST['setting_path'] ) ) {
177+
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing setting path.', 'progress-planner' ) ] );
178+
}
179+
180+
$timezone_string = \sanitize_text_field( \wp_unslash( $_POST['value'] ) );
181+
182+
if ( empty( $timezone_string ) ) {
183+
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid timezone.', 'progress-planner' ) ] );
184+
}
185+
186+
$update_options = false;
187+
188+
// Map UTC+- timezones to gmt_offsets and set timezone_string to empty.
189+
if ( preg_match( '/^UTC[+-]/', $timezone_string ) ) {
190+
// Set the gmt_offset to the value of the timezone_string, strip the UTC prefix.
191+
$gmt_offset = preg_replace( '/UTC\+?/', '', $timezone_string );
192+
193+
// Reset the timezone_string to empty.
194+
$timezone_string = '';
195+
196+
$update_options = true;
197+
} elseif ( in_array( $timezone_string, \timezone_identifiers_list( \DateTimeZone::ALL_WITH_BC ), true ) ) {
198+
// $timezone_string is already set, reset the value for $gmt_offset.
199+
$gmt_offset = '';
200+
201+
$update_options = true;
202+
}
203+
204+
if ( $update_options ) {
205+
\update_option( 'timezone_string', $timezone_string );
206+
\update_option( 'gmt_offset', $gmt_offset );
207+
208+
// We're not checking for the return value of the update_option calls, because it will return false if the value is the same (for example if gmt_offset is already set to '').
209+
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
210+
}
211+
212+
\wp_send_json_error( [ 'message' => \esc_html__( 'Failed to update setting.', 'progress-planner' ) ] );
213+
}
136214
}

0 commit comments

Comments
 (0)