PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / RemoteInboxNotifications / RemoteInboxNotificationsDataSourcePoller.php
RemoteInboxNotificationsDataSourcePoller.php
240 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles polling and storage of specs
4 */
5
6 namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller;
11 use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\GetRuleProcessor;
12 use WC_Helper;
13
14 /**
15 * Specs data source poller class.
16 * This handles polling specs from JSON endpoints, and
17 * stores the specs in to the database as an option.
18 */
19 class RemoteInboxNotificationsDataSourcePoller extends DataSourcePoller {
20 const ID = 'remote_inbox_notifications';
21
22 /**
23 * Default data sources array.
24 *
25 * @deprecated since 9.5.0. Use get_data_sources() instead.
26 */
27 const DATA_SOURCES = array();
28
29 /**
30 * Class instance.
31 *
32 * @var RemoteInboxNotificationsDataSourcePoller instance
33 */
34 protected static $instance = null;
35
36 /**
37 * Get class instance.
38 */
39 public static function get_instance() {
40 if ( ! self::$instance ) {
41 self::$instance = new self(
42 self::ID,
43 self::get_data_sources(),
44 array(
45 'spec_key' => 'slug',
46 'transient_expiry' => DAY_IN_SECONDS,
47 )
48 );
49 }
50 return self::$instance;
51 }
52
53 /**
54 * Validate the spec.
55 *
56 * @param object $spec The spec to validate.
57 * @param string $url The url of the feed that provided the spec.
58 *
59 * @return bool The result of the validation.
60 */
61 protected function validate_spec( $spec, $url ) {
62 $logger = self::get_logger();
63 $logger_context = array( 'source' => $url );
64
65 if ( ! isset( $spec->slug ) ) {
66 $logger->error(
67 'Spec is invalid because the slug is missing in feed',
68 $logger_context
69 );
70 // phpcs:ignore
71 $logger->error( print_r( $spec, true ), $logger_context );
72
73 return false;
74 }
75
76 if ( ! isset( $spec->status ) ) {
77 $logger->error(
78 'Spec is invalid because the status is missing in feed',
79 $logger_context
80 );
81 // phpcs:ignore
82 $logger->error( print_r( $spec, true ), $logger_context );
83
84 return false;
85 }
86
87 if ( ! isset( $spec->locales ) || ! is_array( $spec->locales ) ) {
88 $logger->error(
89 'Spec is invalid because the status is missing or empty in feed',
90 $logger_context
91 );
92 // phpcs:ignore
93 $logger->error( print_r( $spec, true ), $logger_context );
94
95 return false;
96 }
97
98 if ( null === SpecRunner::get_locale( $spec->locales ) ) {
99 $logger->error(
100 'Spec is invalid because the locale could not be retrieved in feed',
101 $logger_context
102 );
103 // phpcs:ignore
104 $logger->error( print_r( $spec, true ), $logger_context );
105
106 return false;
107 }
108
109 if ( ! isset( $spec->type ) ) {
110 $logger->error(
111 'Spec is invalid because the type is missing in feed',
112 $logger_context
113 );
114 // phpcs:ignore
115 $logger->error( print_r( $spec, true ), $logger_context );
116
117 return false;
118 }
119
120 if ( isset( $spec->actions ) && is_array( $spec->actions ) ) {
121 foreach ( $spec->actions as $action ) {
122 if ( ! $this->validate_action( $action, $url ) ) {
123 $logger->error(
124 'Spec is invalid because an action is invalid in feed',
125 $logger_context
126 );
127 // phpcs:ignore
128 $logger->error( print_r( $spec, true ), $logger_context );
129
130 return false;
131 }
132 }
133 }
134
135 if ( isset( $spec->rules ) && is_array( $spec->rules ) ) {
136 foreach ( $spec->rules as $rule ) {
137 if ( ! isset( $rule->type ) ) {
138 $logger->error(
139 'Spec is invalid because a rule type is empty in feed',
140 $logger_context
141 );
142 // phpcs:ignore
143 $logger->error( print_r( $rule, true ), $logger_context );
144 // phpcs:ignore
145 $logger->error( print_r( $spec, true ), $logger_context );
146
147 return false;
148 }
149
150 $processor = GetRuleProcessor::get_processor( $rule->type );
151
152 if ( ! $processor->validate( $rule ) ) {
153 $logger->error(
154 'Spec is invalid because a rule is invalid in feed',
155 $logger_context
156 );
157 // phpcs:ignore
158 $logger->error( print_r( $rule, true ), $logger_context );
159 // phpcs:ignore
160 $logger->error( print_r( $spec, true ), $logger_context );
161
162 return false;
163 }
164 }
165 }
166
167 return true;
168 }
169
170 /**
171 * Validate the action.
172 *
173 * @param object $action The action to validate.
174 * @param string $url The url of the feed containing the action (for error reporting).
175 *
176 * @return bool The result of the validation.
177 */
178 private function validate_action( $action, $url ) {
179 $logger = self::get_logger();
180 $logger_context = array( 'source' => $url );
181
182 if ( ! isset( $action->locales ) || ! is_array( $action->locales ) ) {
183 $logger->error(
184 'Action is invalid because it has empty or missing locales in feed',
185 $logger_context
186 );
187 // phpcs:ignore
188 $logger->error( print_r( $action, true ), $logger_context );
189
190 return false;
191 }
192
193 if ( null === SpecRunner::get_action_locale( $action->locales ) ) {
194 $logger->error(
195 'Action is invalid because the locale could not be retrieved in feed',
196 $logger_context
197 );
198 // phpcs:ignore
199 $logger->error( print_r( $action, true ), $logger_context );
200
201 return false;
202 }
203
204 if ( ! isset( $action->name ) ) {
205 $logger->error(
206 'Action is invalid because the name is missing in feed',
207 $logger_context
208 );
209 // phpcs:ignore
210 $logger->error( print_r( $action, true ), $logger_context );
211
212 return false;
213 }
214
215 if ( ! isset( $action->status ) ) {
216 $logger->error(
217 'Action is invalid because the status is missing in feed',
218 $logger_context
219 );
220 // phpcs:ignore
221 $logger->error( print_r( $action, true ), $logger_context );
222
223 return false;
224 }
225
226 return true;
227 }
228
229 /**
230 * Get data sources.
231 *
232 * @return array
233 */
234 public static function get_data_sources() {
235 return array(
236 WC_Helper::get_woocommerce_com_base_url() . 'wp-json/wccom/inbox-notifications/2.0/notifications.json',
237 );
238 }
239 }
240