1010/**
1111 * Add tasks for settings saved.
1212 */
13- class Set_Valuable_Post_Types extends Tasks {
13+ class Set_Valuable_Post_Types extends Tasks_Interactive {
1414
1515 /**
1616 * The provider ID.
@@ -19,6 +19,13 @@ class Set_Valuable_Post_Types extends Tasks {
1919 */
2020 protected const PROVIDER_ID = 'set-valuable-post-types ' ;
2121
22+ /**
23+ * The provider ID.
24+ *
25+ * @var string
26+ */
27+ public const POPOVER_ID = 'set-valuable-post-types ' ;
28+
2229 /**
2330 * Whether the task is an onboarding task.
2431 *
@@ -56,6 +63,7 @@ protected function get_url() {
5663 */
5764 public function init () {
5865 \add_action ( 'progress_planner_settings_form_options_stored ' , [ $ this , 'remove_upgrade_option ' ] );
66+ \add_action ( 'wp_ajax_prpl_interactive_task_submit_set-valuable-post-types ' , [ $ this , 'handle_interactive_task_specific_submit ' ] );
5967 }
6068
6169 /**
@@ -119,6 +127,77 @@ public function is_task_completed( $task_id = '' ) {
119127 return false === \get_option ( 'progress_planner_set_valuable_post_types ' , false );
120128 }
121129
130+ /**
131+ * Handle the interactive task submit.
132+ *
133+ * @return void
134+ */
135+ public function handle_interactive_task_specific_submit () {
136+ // Check if the user has the necessary capabilities.
137+ if ( ! \current_user_can ( 'manage_options ' ) ) {
138+ \wp_send_json_error ( [ 'message ' => \esc_html__ ( 'You do not have permission to update settings. ' , 'progress-planner ' ) ] );
139+ }
140+
141+ // Check the nonce.
142+ if ( ! \check_ajax_referer ( 'progress_planner ' , 'nonce ' , false ) ) {
143+ \wp_send_json_error ( [ 'message ' => \esc_html__ ( 'Invalid nonce. ' , 'progress-planner ' ) ] );
144+ }
145+
146+ if ( ! isset ( $ _POST ['prpl-post-types-include ' ] ) ) {
147+ \wp_send_json_error ( [ 'message ' => \esc_html__ ( 'Missing post types. ' , 'progress-planner ' ) ] );
148+ }
149+
150+ $ post_types = \wp_unslash ( $ _POST ['prpl-post-types-include ' ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- array elements are sanitized below.
151+ $ post_types = explode ( ', ' , $ post_types );
152+ $ post_types = array_map ( 'sanitize_text_field ' , $ post_types );
153+
154+ \progress_planner ()->get_admin__page_settings ()->save_post_types ( $ post_types );
155+
156+ \wp_send_json_success ( [ 'message ' => \esc_html__ ( 'Setting updated. ' , 'progress-planner ' ) ] );
157+ }
158+
159+ /**
160+ * Print the popover instructions.
161+ *
162+ * @return void
163+ */
164+ public function print_popover_instructions () {
165+ echo '<p> ' ;
166+ \esc_html_e ( 'You \'re in control of what counts as valuable content. We \'ll track and reward activity only for the post types you select here. ' , 'progress-planner ' );
167+ echo '</p> ' ;
168+ }
169+
170+ /**
171+ * Print the popover form contents.
172+ *
173+ * @return void
174+ */
175+ public function print_popover_form_contents () {
176+ $ prpl_saved_settings = \progress_planner ()->get_settings ()->get_post_types_names ();
177+ $ prpl_post_types = \progress_planner ()->get_settings ()->get_public_post_types ();
178+
179+ // Early exit if there are no public post types.
180+ if ( empty ( $ prpl_post_types ) ) {
181+ return ;
182+ }
183+ ?>
184+ <div class="prpl-post-types-selection">
185+ <?php foreach ( $ prpl_post_types as $ prpl_post_type ) : ?>
186+ <label>
187+ <input
188+ type="checkbox"
189+ name="prpl-post-types-include[]"
190+ value="<?php echo \esc_attr ( $ prpl_post_type ); ?> "
191+ <?php \checked ( \in_array ( $ prpl_post_type , $ prpl_saved_settings , true ) ); ?>
192+ />
193+ <?php echo \esc_html ( \get_post_type_object ( $ prpl_post_type )->labels ->name ); // @phpstan-ignore-line property.nonObject ?>
194+ </label>
195+ <?php endforeach ; ?>
196+ </div>
197+ <?php
198+ $ this ->print_submit_button ( \__ ( 'Set ' , 'progress-planner ' ) );
199+ }
200+
122201 /**
123202 * Add task actions specific to this task.
124203 *
@@ -130,7 +209,7 @@ public function is_task_completed( $task_id = '' ) {
130209 public function add_task_actions ( $ data = [], $ actions = [] ) {
131210 $ actions [] = [
132211 'priority ' => 10 ,
133- 'html ' => '<a class="prpl-tooltip-action-text" href=" ' . \admin_url ( ' admin.php?page=progress-planner-settings ' ) . '" target="_self" > ' . \esc_html__ ( 'Go to the settings page ' , 'progress-planner ' ) . '</a> ' ,
212+ 'html ' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById( \' prpl-popover- ' . \esc_attr ( static :: POPOVER_ID ) . '\' )?.showPopover()" > ' . \esc_html__ ( 'Set ' , 'progress-planner ' ) . '</a> ' ,
134213 ];
135214
136215 return $ actions ;
0 commit comments