Skip to content

Commit 7a90fc6

Browse files
committed
handle custom ajax callback
1 parent 3c705c0 commit 7a90fc6

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

assets/js/recommendations/interactive-task.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const prplInteractiveTaskFormListener = {
100100
settingPath = false,
101101
popoverId,
102102
settingCallbackValue = ( settingValue ) => settingValue,
103+
action = 'prpl_interactive_task_submit',
103104
} = {} ) => {
104105
const formElement = document.querySelector( `#${ popoverId } form` );
105106

@@ -119,7 +120,7 @@ const prplInteractiveTaskFormListener = {
119120
progressPlannerAjaxRequest( {
120121
url: progressPlanner.ajaxUrl,
121122
data: {
122-
action: 'prpl_interactive_task_submit',
123+
action,
123124
_ajax_nonce: progressPlanner.nonce,
124125
post_id: taskId,
125126
setting,

assets/js/recommendations/select-timezone.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
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',
1415
} );
1516

1617
prplDocumentReady( () => {

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

Lines changed: 78 additions & 1 deletion
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
*
@@ -125,7 +134,7 @@ public function print_popover_form_contents() {
125134
}
126135
?>
127136
<label>
128-
<select id="gmt_offset" name="gmt_offset" data-timezone-saved="<?php echo \esc_attr( $was_tzstring_saved ); ?>">
137+
<select id="timezone" name="timezone" data-timezone-saved="<?php echo \esc_attr( $was_tzstring_saved ); ?>">
129138
<?php echo \wp_timezone_choice( $tzstring, \get_user_locale() ); ?>
130139
</select>
131140
</label>
@@ -134,4 +143,72 @@ public function print_popover_form_contents() {
134143
</button>
135144
<?php
136145
}
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+
}
137214
}

0 commit comments

Comments
 (0)