@@ -59,39 +59,69 @@ public function init() {
59
59
*
60
60
* @access public
61
61
* @since 1.1.0
62
+ *
63
+ * @param array $args
62
64
* @return void
63
65
*/
64
- public function maybe_send_data () {
66
+ public function maybe_send_data ( $ args = array () ) {
65
67
// Check if the user has consented to the data sending.
66
68
if ( ! get_option ( 'repack_telemetry_optin ' ) ) {
67
69
return ;
68
70
}
69
71
70
- // Send data & update sent value
71
- if ( ! is_wp_error ( $ this ->send_data () ) ) {
72
- update_option ( 'repack_telemetry_sent ' , time () );
72
+ // Send data
73
+ $ request = $ this ->send_data ( $ args );
74
+
75
+ // Get Response
76
+ $ api_response = json_decode ( wp_remote_retrieve_body ( $ request ), true );
77
+ $ api_response_code = wp_remote_retrieve_response_code ( $ request );
78
+
79
+ // Blocking request (e.g. Sync) retrieve a proper debuggable response
80
+ if ( isset ( $ args ['blocking ' ] ) && $ args ['blocking ' ] ) {
81
+ foreach ( $ api_response as $ message ) {
82
+ if ( $ message ) {
83
+ $ classes = strval ( $ api_response_code ) === '200 ' ? 'notice notice-success ' : 'notice notice-error ' ;
84
+ add_action (
85
+ 'admin_notices ' ,
86
+ function () use ( $ message , $ classes ) {
87
+ echo '<div class=" ' . esc_html ( $ classes ) . '"><p><b> ' . esc_html__ ( 'WeRePack.org API Response: ' , 'repack-for-woocommerce ' ) . '</b> ' . esc_html ( $ message ) . '</p></div> ' ;
88
+ }
89
+ );
90
+ }
91
+ }
92
+
93
+ if ( strval ( $ api_response_code ) !== '200 ' ) {
94
+ return ;
95
+ }
73
96
}
97
+
98
+ update_option ( 'repack_telemetry_sent ' , time () );
74
99
}
75
100
76
101
/**
77
102
* Sends data.
78
103
*
79
104
* @access private
80
105
* @since 1.1.0
106
+ *
107
+ * @param array $args
81
108
* @return array|WP_Error
82
109
*/
83
- private function send_data () {
110
+ private function send_data ( $ args = array () ) {
84
111
// Ping remote server.
85
112
return wp_remote_post (
86
113
'https://werepack.org/api/community/v1/sites ' ,
87
- array (
88
- 'method ' => 'POST ' ,
89
- 'blocking ' => false ,
90
- 'body ' => $ this ->get_data (
91
- array (
92
- 'repack_last_sent ' => time (),
93
- )
94
- ),
114
+ wp_parse_args (
115
+ $ args ,
116
+ array (
117
+ 'method ' => 'POST ' ,
118
+ 'blocking ' => false ,
119
+ 'body ' => $ this ->get_data (
120
+ array (
121
+ 'repack_last_sent ' => time (),
122
+ )
123
+ ),
124
+ )
95
125
)
96
126
);
97
127
}
@@ -146,9 +176,10 @@ public function admin_notice() {
146
176
* Builds and returns the data or uses cached if data already exists.
147
177
*
148
178
* @access private
179
+ * @since 1.1.0
180
+ *
149
181
* @param array $data
150
182
* @return array
151
- * @since 1.1.0
152
183
*/
153
184
public function get_data ( $ data = array () ) {
154
185
// Build data and return the array.
@@ -250,7 +281,7 @@ private function run_action() {
250
281
if ( 'sync ' === sanitize_text_field ( wp_unslash ( $ _GET ['repack-action ' ] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
251
282
// Check the wp-nonce.
252
283
if ( wp_verify_nonce ( sanitize_text_field ( wp_unslash ( $ _GET ['_wpnonce ' ] ) ) ) ) {
253
- $ this ->maybe_send_data ();
284
+ $ this ->maybe_send_data ( array ( ' blocking ' => true ) );
254
285
}
255
286
}
256
287
}
0 commit comments