Skip to content

Commit 87c33ea

Browse files
authored
Merge pull request #12414 from google/bug/12385-monthly-frequency
Bug/12385 monthly frequency
2 parents f9e93ae + dfd4276 commit 87c33ea

File tree

22 files changed

+149
-44
lines changed

22 files changed

+149
-44
lines changed

assets/js/components/email-reporting/FrequencySelector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ export default function FrequencySelector( { isUserSubscribed, isLoading } ) {
9999
},
100100
monthly: {
101101
label: __( 'Monthly', 'google-site-kit' ),
102-
period: __( 'Last 28 days', 'google-site-kit' ),
102+
period: __( 'Last month', 'google-site-kit' ),
103103
description: __(
104104
'Sent on the 1st of each month',
105105
'google-site-kit'
106106
),
107107
},
108108
quarterly: {
109109
label: __( 'Quarterly', 'google-site-kit' ),
110-
period: __( 'Last 90 days', 'google-site-kit' ),
110+
period: __( 'Last quarter', 'google-site-kit' ),
111111
description: __(
112112
'Sent on the 1st of each quarter',
113113
'google-site-kit'

assets/js/components/email-reporting/UserSettingsSelectionPanel/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ import {
4141
import UserSettingsSelectionPanel from '@/js/components/email-reporting/UserSettingsSelectionPanel';
4242
import SelectionPanelFooter from '@/js/components/email-reporting/UserSettingsSelectionPanel/SelectionPanelFooter';
4343

44+
// This suite tests panel behavior; mock the invite list to avoid async datastore
45+
// updates from child-level fetching that are covered in InviteOthersToSubscribe tests.
46+
jest.mock(
47+
'@/js/components/email-reporting/InviteOthersToSubscribe',
48+
() => () => null
49+
);
50+
4451
describe( 'UserSettingsSelectionPanel', () => {
4552
const features = [ 'proactiveUserEngagement' ];
4653
const emailReportingSettingsEndpoint = new RegExp(

assets/js/components/email-reporting/__snapshots__/FrequencySelector.test.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ exports[`FrequencySelector Story states (visual + DOM) Monthly selected renders
7575
<span
7676
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
7777
>
78-
Last 28 days
78+
Last month
7979
</span>
8080
</div>
8181
<div
@@ -117,7 +117,7 @@ exports[`FrequencySelector Story states (visual + DOM) Monthly selected renders
117117
<span
118118
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
119119
>
120-
Last 90 days
120+
Last quarter
121121
</span>
122122
</div>
123123
<div
@@ -210,7 +210,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
210210
<span
211211
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
212212
>
213-
Last 28 days
213+
Last month
214214
</span>
215215
</div>
216216
<div
@@ -252,7 +252,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
252252
<span
253253
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
254254
>
255-
Last 90 days
255+
Last quarter
256256
</span>
257257
</div>
258258
<div
@@ -345,7 +345,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
345345
<span
346346
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
347347
>
348-
Last 28 days
348+
Last month
349349
</span>
350350
</div>
351351
<div
@@ -387,7 +387,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
387387
<span
388388
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
389389
>
390-
Last 90 days
390+
Last quarter
391391
</span>
392392
</div>
393393
<div
@@ -480,7 +480,7 @@ exports[`FrequencySelector Story states (visual + DOM) Quarterly selected render
480480
<span
481481
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
482482
>
483-
Last 28 days
483+
Last month
484484
</span>
485485
</div>
486486
<div
@@ -522,7 +522,7 @@ exports[`FrequencySelector Story states (visual + DOM) Quarterly selected render
522522
<span
523523
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
524524
>
525-
Last 90 days
525+
Last quarter
526526
</span>
527527
</div>
528528
<div
@@ -615,7 +615,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected (default
615615
<span
616616
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
617617
>
618-
Last 28 days
618+
Last month
619619
</span>
620620
</div>
621621
<div
@@ -657,7 +657,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected (default
657657
<span
658658
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
659659
>
660-
Last 90 days
660+
Last quarter
661661
</span>
662662
</div>
663663
<div
@@ -750,7 +750,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected with Sund
750750
<span
751751
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
752752
>
753-
Last 28 days
753+
Last month
754754
</span>
755755
</div>
756756
<div
@@ -792,7 +792,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected with Sund
792792
<span
793793
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
794794
>
795-
Last 90 days
795+
Last quarter
796796
</span>
797797
</div>
798798
<div

includes/Core/Email_Reporting/Email_Log.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static function get_date_range_from_log( $email_log ) {
131131
$normalized = array();
132132
$keys = array(
133133
'startDate' => 'startDate',
134-
'sendDate' => 'endDate',
134+
'endDate' => 'endDate',
135135
'compareStartDate' => 'compareStartDate',
136136
'compareEndDate' => 'compareEndDate',
137137
);
@@ -573,7 +573,7 @@ public static function sanitize_admin_notified( $value ) {
573573
* @return array Normalized timestamps keyed by meta field.
574574
*/
575575
protected static function normalize_reference_dates( array $raw_dates ) {
576-
$keys = array( 'startDate', 'sendDate', 'compareStartDate', 'compareEndDate' );
576+
$keys = array( 'startDate', 'endDate', 'compareStartDate', 'compareEndDate' );
577577
$normalized = array();
578578

579579
foreach ( $keys as $key ) {

includes/Core/Email_Reporting/Initiator_Task.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,49 @@ public static function build_reference_dates( $frequency, $timestamp ) {
113113
->setTime( 0, 0, 0 );
114114
// Initiators are scheduled at period-boundary midnight in site timezone.
115115
// The reporting window should end on "yesterday" (inclusive end date), so subtract one day.
116-
$send_date = $scheduled_date->sub( new DateInterval( 'P1D' ) );
116+
$end_date = $scheduled_date->sub( new DateInterval( 'P1D' ) );
117117

118-
$period_lengths = array(
119-
Email_Reporting_Settings::FREQUENCY_WEEKLY => 7,
120-
Email_Reporting_Settings::FREQUENCY_MONTHLY => 30,
121-
Email_Reporting_Settings::FREQUENCY_QUARTERLY => 90,
122-
);
118+
$period_days = 7;
123119

124-
$period_days = isset( $period_lengths[ $frequency ] ) ? $period_lengths[ $frequency ] : $period_lengths[ Email_Reporting_Settings::FREQUENCY_WEEKLY ];
120+
if ( Email_Reporting_Settings::FREQUENCY_MONTHLY === $frequency ) {
121+
$period_days = (int) $end_date->format( 't' );
122+
} elseif ( Email_Reporting_Settings::FREQUENCY_QUARTERLY === $frequency ) {
123+
$period_days = self::get_days_in_quarter( $end_date );
124+
}
125125

126126
// endDate is inclusive, so startDate must be endDate - (period_days - 1) for an exact period-length window.
127-
$start_date = $send_date->sub( new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) ) );
127+
$start_date = $end_date->sub( new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) ) );
128128
$compare_end_date = $start_date->sub( new DateInterval( 'P1D' ) );
129129
$compare_start_date = $compare_end_date->sub(
130130
new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) )
131131
);
132132

133133
return array(
134134
'startDate' => $start_date->format( 'Y-m-d' ),
135-
'sendDate' => $send_date->format( 'Y-m-d' ),
135+
'endDate' => $end_date->format( 'Y-m-d' ),
136136
'compareStartDate' => $compare_start_date->format( 'Y-m-d' ),
137137
'compareEndDate' => $compare_end_date->format( 'Y-m-d' ),
138138
);
139139
}
140+
141+
/**
142+
* Gets the number of days in the quarter for a date.
143+
*
144+
* @since n.e.x.t
145+
*
146+
* @param DateTimeImmutable $date Date in the target quarter.
147+
* @return int Days in the quarter.
148+
*/
149+
private static function get_days_in_quarter( DateTimeImmutable $date ) {
150+
$year = (int) $date->format( 'Y' );
151+
$month = (int) $date->format( 'n' );
152+
$quarter_start_month = floor( ( $month - 1 ) / 3 ) * 3 + 1;
153+
154+
$quarter_start = $date->setDate( $year, $quarter_start_month, 1 );
155+
$quarter_end = $quarter_start
156+
->add( new DateInterval( 'P3M' ) )
157+
->sub( new DateInterval( 'P1D' ) );
158+
159+
return (int) $quarter_start->diff( $quarter_end )->days + 1;
160+
}
140161
}
-947 Bytes
Loading
-453 Bytes
Loading
-449 Bytes
Loading
Loading
Loading

0 commit comments

Comments
 (0)