Skip to content

Commit 80c42c4

Browse files
committed
convert "set valuable post types" to interactive
1 parent 423a1fc commit 80c42c4

4 files changed

Lines changed: 157 additions & 8 deletions

File tree

assets/css/page-widgets/suggested-tasks.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,18 @@
596596
}
597597
}
598598
}
599+
600+
/*------------------------------------*\
601+
Post types selection.
602+
\*------------------------------------*/
603+
.prpl-post-types-selection {
604+
605+
label {
606+
display: block;
607+
margin-top: 0.75rem;
608+
609+
&:first-child {
610+
margin-top: 0;
611+
}
612+
}
613+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* global prplInteractiveTaskFormListener, progressPlanner */
2+
3+
/*
4+
* Set valuable post types.
5+
*
6+
* Dependencies: progress-planner/recommendations/interactive-task
7+
*/
8+
9+
prplInteractiveTaskFormListener.customSubmit( {
10+
taskId: 'set-valuable-post-types',
11+
popoverId: 'prpl-popover-set-valuable-post-types',
12+
callback: () => {
13+
return new Promise( ( resolve, reject ) => {
14+
const postTypes = document.querySelectorAll(
15+
'#prpl-popover-set-valuable-post-types input[name="prpl-post-types-include[]"]:checked'
16+
);
17+
18+
if ( ! postTypes.length ) {
19+
reject( {
20+
success: false,
21+
error: new Error( 'No post types selected' ),
22+
} );
23+
return;
24+
}
25+
26+
const postTypesValues = Array.from( postTypes ).map(
27+
( type ) => type.value
28+
);
29+
30+
fetch( progressPlanner.ajaxUrl, {
31+
method: 'POST',
32+
headers: {
33+
'Content-Type': 'application/x-www-form-urlencoded',
34+
},
35+
body: new URLSearchParams( {
36+
action: 'prpl_interactive_task_submit_set-valuable-post-types',
37+
nonce: progressPlanner.nonce,
38+
'prpl-post-types-include': postTypesValues,
39+
} ),
40+
} )
41+
.then( ( response ) => {
42+
resolve( { response, success: true } );
43+
} )
44+
.catch( ( error ) => {
45+
reject( { success: false, error } );
46+
} );
47+
} );
48+
},
49+
} );

classes/admin/class-page-settings.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,14 @@ public function store_settings_form_options() {
146146
$this->set_page_values( $pages );
147147
}
148148

149+
// It's possible that none of the post types are selected, so we need to handle that case.
150+
$post_types = isset( $_POST['prpl-post-types-include'] )
151+
? \array_map( 'sanitize_text_field', \wp_unslash( $_POST['prpl-post-types-include'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
152+
: [];
153+
154+
$this->save_post_types( $post_types );
155+
149156
$this->save_settings();
150-
$this->save_post_types();
151157

152158
\do_action( 'progress_planner_settings_form_options_stored' );
153159

@@ -221,13 +227,13 @@ public function save_settings() {
221227
/**
222228
* Save the post types.
223229
*
230+
* @param array $post_types The post types.
231+
*
224232
* @return void
225233
*/
226-
public function save_post_types() {
227-
// Nonce is already checked in store_settings_form_options() which calls this method.
228-
$include_post_types = isset( $_POST['prpl-post-types-include'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
229-
? \array_map( 'sanitize_text_field', \wp_unslash( $_POST['prpl-post-types-include'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
230-
// If no post types are selected, use the default post types (post and page can be deregistered).
234+
public function save_post_types( $post_types = [] ) {
235+
$include_post_types = ! empty( $post_types )
236+
? $post_types
231237
: \array_intersect( [ 'post', 'page' ], \progress_planner()->get_settings()->get_public_post_types() );
232238

233239
\progress_planner()->get_settings()->set( 'include_post_types', $include_post_types );

classes/suggested-tasks/providers/class-set-valuable-post-types.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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

Comments
 (0)