DataSourcePoller.php
5.2 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* Handles polling and storage of specs
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*/
class DataSourcePoller extends \Automattic\WooCommerce\Admin\DataSourcePoller {
const ID = 'remote_inbox_notifications';
const DATA_SOURCES = array(
'https://woocommerce.com/wp-json/wccom/inbox-notifications/1.0/notifications.json',
);
/**
* Class instance.
*
* @var Analytics instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self(
self::ID,
self::DATA_SOURCES,
array(
'spec_key' => 'slug',
)
);
}
return self::$instance;
}
/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $spec->slug ) ) {
$logger->error(
'Spec is invalid because the slug is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->status ) ) {
$logger->error(
'Spec is invalid because the status is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->locales ) || ! is_array( $spec->locales ) ) {
$logger->error(
'Spec is invalid because the status is missing or empty in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( null === SpecRunner::get_locale( $spec->locales ) ) {
$logger->error(
'Spec is invalid because the locale could not be retrieved in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( ! isset( $spec->type ) ) {
$logger->error(
'Spec is invalid because the type is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
if ( isset( $spec->actions ) && is_array( $spec->actions ) ) {
foreach ( $spec->actions as $action ) {
if ( ! $this->validate_action( $action, $url ) ) {
$logger->error(
'Spec is invalid because an action is invalid in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
}
}
if ( isset( $spec->rules ) && is_array( $spec->rules ) ) {
foreach ( $spec->rules as $rule ) {
if ( ! isset( $rule->type ) ) {
$logger->error(
'Spec is invalid because a rule type is empty in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $rule, true ), $logger_context );
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
$processor = GetRuleProcessor::get_processor( $rule->type );
if ( ! $processor->validate( $rule ) ) {
$logger->error(
'Spec is invalid because a rule is invalid in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $rule, true ), $logger_context );
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );
return false;
}
}
}
return true;
}
/**
* Validate the action.
*
* @param object $action The action to validate.
* @param string $url The url of the feed containing the action (for error reporting).
*
* @return bool The result of the validation.
*/
private function validate_action( $action, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );
if ( ! isset( $action->locales ) || ! is_array( $action->locales ) ) {
$logger->error(
'Action is invalid because it has empty or missing locales in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( null === SpecRunner::get_action_locale( $action->locales ) ) {
$logger->error(
'Action is invalid because the locale could not be retrieved in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( ! isset( $action->name ) ) {
$logger->error(
'Action is invalid because the name is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
if ( ! isset( $action->status ) ) {
$logger->error(
'Action is invalid because the status is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $action, true ), $logger_context );
return false;
}
return true;
}
}