-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathRequestReviewStatuses.php
171 lines (142 loc) · 5.94 KB
/
RequestReviewStatuses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Automattic\WooCommerce\GoogleListingsAndAds\Google;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
/**
* Helper class for Account request Review feature
*/
class RequestReviewStatuses implements Service {
public const ENABLED = 'ENABLED';
public const DISAPPROVED = 'DISAPPROVED';
public const WARNING = 'WARNING';
public const UNDER_REVIEW = 'UNDER_REVIEW';
public const PENDING_REVIEW = 'PENDING_REVIEW';
public const ONBOARDING = 'ONBOARDING';
public const APPROVED = 'APPROVED';
public const NO_OFFERS = 'NO_OFFERS_UPLOADED';
public const ELIGIBLE = 'ELIGIBLE';
public const MC_ACCOUNT_REVIEW_LIFETIME = MINUTE_IN_SECONDS * 20; // 20 minutes
/**
* Merges the different program statuses, issues and cooldown period date.
*
* @param array $response Associative array containing the response data from Google API
* @return array The computed status, with the issues and cooldown period.
*/
public function get_statuses_from_response( array $response ) {
$issues = [];
$cooldown = 0;
$status = null;
$valid_program_states = [ self::ENABLED, self::NO_OFFERS ];
$review_eligible_regions = [];
foreach ( $response as $program_type_name => $program_type ) {
// In case any Program is with no offers we consider it Onboarding
if ( $program_type['globalState'] === self::NO_OFFERS ) {
$status = self::ONBOARDING;
break;
}
// In case any Program is not enabled or there are no regionStatuses we return null status
if ( ! isset( $program_type['regionStatuses'] ) || ! in_array( $program_type['globalState'], $valid_program_states, true ) ) {
continue;
}
// Otherwise, we compute the new status, issues and cooldown period
foreach ( $program_type['regionStatuses'] as $region_status ) {
$issues = array_merge( $issues, $region_status['reviewIssues'] ?? [] );
$cooldown = $this->maybe_update_cooldown_period( $region_status, $cooldown );
$status = $this->maybe_update_status( $region_status['eligibilityStatus'], $status );
$review_eligible_regions = $this->maybe_load_eligible_region( $region_status, $review_eligible_regions, $program_type_name );
}
}
return [
'issues' => array_map( 'strtolower', array_values( array_unique( $issues ) ) ),
'cooldown' => $this->get_cooldown( $cooldown ), // add lifetime cache to cooldown time
'status' => $status,
'reviewEligibleRegions' => array_unique( $review_eligible_regions ),
];
}
/**
* Updates the cooldown period in case the new cooldown period date is available and later than the current cooldown period.
*
* @param array $region_status Associative array containing (maybe) a cooldown date property.
* @param int $cooldown Referenced current cooldown to compare with
*
* @return int The cooldown
*/
private function maybe_update_cooldown_period( $region_status, $cooldown ) {
if (
isset( $region_status['reviewIneligibilityReasonDetails'] ) &&
isset( $region_status['reviewIneligibilityReasonDetails']['cooldownTime'] )
) {
$region_cooldown = intval( strtotime( $region_status['reviewIneligibilityReasonDetails']['cooldownTime'] ) );
if ( ! $cooldown || $region_cooldown > $cooldown ) {
$cooldown = $region_cooldown;
}
}
return $cooldown;
}
/**
* Updates the status reference in case the new status has more priority.
*
* @param String $new_status New status to check has more priority than the current one
* @param String $status Referenced current status
*
* @return String The status
*/
private function maybe_update_status( $new_status, $status ) {
$status_priority_list = [
self::ONBOARDING, // highest priority
self::DISAPPROVED,
self::WARNING,
self::UNDER_REVIEW,
self::PENDING_REVIEW,
self::APPROVED,
];
$current_status_priority = array_search( $status, $status_priority_list, true );
$new_status_priority = array_search( $new_status, $status_priority_list, true );
if ( $new_status_priority !== false && ( is_null( $status ) || $current_status_priority > $new_status_priority ) ) {
return $new_status;
}
return $status;
}
/**
* Updates the regions where a request review is allowed.
*
* @param array $region_status Associative array containing the region eligibility.
* @param array $review_eligible_regions Indexed array with the current eligible regions.
* @param "freeListingsProgram"|"shoppingAdsProgram" $type The program type.
*
* @return array The (maybe) modified $review_eligible_regions array
*/
private function maybe_load_eligible_region( $region_status, $review_eligible_regions, $type = 'freeListingsProgram' ) {
if (
! empty( $region_status['regionCodes'] ) &&
isset( $region_status['reviewEligibilityStatus'] ) &&
$region_status['reviewEligibilityStatus'] === self::ELIGIBLE
) {
sort( $region_status['regionCodes'] ); // sometimes the regions come unsorted between the different programs
$region_id = $region_status['regionCodes'][0];
if ( ! isset( $review_eligible_regions[ $region_id ] ) ) {
$review_eligible_regions[ $region_id ] = [];
}
$review_eligible_regions[ $region_id ][] = strtolower( $type ); // lowercase as is how we expect it in WCS
}
return $review_eligible_regions;
}
/**
* Allows a hook to modify the lifetime of the Account review data.
*
* @return int
*/
public function get_account_review_lifetime(): int {
return apply_filters( 'woocommerce_gla_mc_account_review_lifetime', self::MC_ACCOUNT_REVIEW_LIFETIME );
}
/**
* @param int $cooldown The cooldown in PHP format (seconds)
*
* @return int The cooldown in milliseconds and adding the lifetime cache
*/
private function get_cooldown( int $cooldown ) {
if ( $cooldown ) {
$cooldown = ( $cooldown + $this->get_account_review_lifetime() ) * 1000;
}
return $cooldown;
}
}