Skip to content

Commit 2951fe2

Browse files
authored
Merge pull request #590 from ProgressPlanner/filip/v18/set-date-format
New task provider: Set the date format
2 parents bf801f8 + 8026e1a commit 2951fe2

6 files changed

Lines changed: 452 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
= 1.8.0 =
33

4+
Added these recommendations from Ravi:
5+
6+
* Set date format.
7+
48
Enhancements:
59

610
* Redesign of the admin pages.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
&:not(.prpl-column-content) {
193193
display: flex;
194194
flex-direction: column;
195+
padding-top: 3px; /* To prevent custom radio and checkbox from being cut off. */
195196
}
196197

197198
/* Inputs. */
@@ -427,4 +428,15 @@
427428

428429
}
429430
}
431+
432+
/* Set the date format. */
433+
&#prpl-popover-set-date-format {
434+
435+
.prpl-radio-wrapper {
436+
437+
.date-time-text {
438+
min-width: 10rem;
439+
}
440+
}
441+
}
430442
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* global prplInteractiveTaskFormListener, prplDocumentReady, progressPlanner */
2+
3+
/*
4+
* Set the site date format.
5+
*
6+
* Dependencies: progress-planner/recommendations/interactive-task
7+
*/
8+
9+
prplInteractiveTaskFormListener.customSubmit( {
10+
taskId: 'set-date-format',
11+
popoverId: 'prpl-popover-set-date-format',
12+
callback: () => {
13+
const format = document.querySelector(
14+
'#prpl-popover-set-date-format input[name="date_format"]:checked'
15+
);
16+
const customFormat = document.querySelector(
17+
'#prpl-popover-set-date-format input[name="date_format_custom"]'
18+
);
19+
20+
fetch( progressPlanner.ajaxUrl, {
21+
method: 'POST',
22+
headers: {
23+
'Content-Type': 'application/x-www-form-urlencoded',
24+
},
25+
body: new URLSearchParams( {
26+
action: 'prpl_interactive_task_submit_set-date-format',
27+
nonce: progressPlanner.nonce,
28+
date_format: format.value,
29+
date_format_custom: customFormat.value,
30+
} ),
31+
} );
32+
},
33+
} );
34+
35+
prplDocumentReady( () => {
36+
// Handle date format radio button clicks
37+
document
38+
.querySelectorAll(
39+
'#prpl-popover-set-date-format input[name="date_format"]'
40+
)
41+
.forEach( function ( input ) {
42+
input.addEventListener( 'click', function () {
43+
if ( 'date_format_custom_radio' !== this.id ) {
44+
const customInput = document.querySelector(
45+
'#prpl-popover-set-date-format input[name="date_format_custom"]'
46+
);
47+
const fieldset = customInput.closest( 'fieldset' );
48+
const exampleElement = fieldset.querySelector( '.example' );
49+
const formatText =
50+
this.parentElement.querySelector(
51+
'.format-i18n'
52+
).textContent;
53+
54+
customInput.value = this.value;
55+
exampleElement.textContent = formatText;
56+
}
57+
} );
58+
} );
59+
60+
// Handle custom date format input
61+
const customDateInput = document.querySelector(
62+
'input[name="date_format_custom"]'
63+
);
64+
65+
customDateInput.addEventListener( 'click', function () {
66+
document.getElementById( 'date_format_custom_radio' ).checked = true;
67+
} );
68+
69+
customDateInput.addEventListener( 'input', function () {
70+
document.getElementById( 'date_format_custom_radio' ).checked = true;
71+
72+
const format = this;
73+
const fieldset = format.closest( 'fieldset' );
74+
const example = fieldset.querySelector( '.example' );
75+
76+
// Debounce the event callback while users are typing.
77+
clearTimeout( format.dataset.timer );
78+
format.dataset.timer = setTimeout( function () {
79+
// If custom date is not empty.
80+
if ( format.value ) {
81+
// Find the spinner element within the fieldset
82+
const spinner = fieldset.querySelector( '.spinner' );
83+
if ( spinner ) {
84+
spinner.classList.add( 'is-active' );
85+
}
86+
87+
// Use fetch instead of $.post
88+
fetch( progressPlanner.ajaxUrl, {
89+
method: 'POST',
90+
headers: {
91+
'Content-Type': 'application/x-www-form-urlencoded',
92+
},
93+
body: new URLSearchParams( {
94+
action: 'date_format',
95+
date: format.value,
96+
} ),
97+
} )
98+
.then( function ( response ) {
99+
return response.text();
100+
} )
101+
.then( function ( data ) {
102+
example.textContent = data;
103+
} )
104+
.catch( function ( error ) {
105+
console.error( 'Error:', error );
106+
} )
107+
.finally( function () {
108+
if ( spinner ) {
109+
spinner.classList.remove( 'is-active' );
110+
}
111+
} );
112+
}
113+
}, 500 );
114+
} );
115+
} );

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Progress_Planner\Suggested_Tasks\Providers\Unpublished_Content;
3636
use Progress_Planner\Suggested_Tasks\Providers\Collaborator;
3737
use Progress_Planner\Suggested_Tasks\Providers\Select_Timezone;
38+
use Progress_Planner\Suggested_Tasks\Providers\Set_Date_Format;
3839

3940
/**
4041
* Tasks_Manager class.
@@ -80,6 +81,7 @@ public function __construct() {
8081
new Unpublished_Content(),
8182
new Collaborator(),
8283
new Select_Timezone(),
84+
new Set_Date_Format(),
8385
];
8486

8587
// Add the plugin integration.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Progress_Planner\Suggested_Tasks\Providers;
99

1010
/**
11-
* Add task to select the site locale.
11+
* Add task to select the site timezone.
1212
*/
1313
class Select_Timezone extends Tasks_Interactive {
1414

0 commit comments

Comments
 (0)