Skip to content

Commit 1cdef93

Browse files
committed
v1.4.1: Better Error Handling and Notifications
1 parent 9d38268 commit 1cdef93

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Otherwise just start to translate on [translate.wordpress.org/projects/wp-plugin
114114

115115
== Changelog ==
116116

117+
= 1.4.1 =
118+
* Enh: Dashboard Settings: Improved Notifications
119+
117120
= 1.4.0 =
118121
* Enh: Tested with WooCommerce 6.3.1
119122
* Fix: WeRePack Community Updates by...

admin/partials/class-repack-telemetry.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,69 @@ public function init() {
5959
*
6060
* @access public
6161
* @since 1.1.0
62+
*
63+
* @param array $args
6264
* @return void
6365
*/
64-
public function maybe_send_data() {
66+
public function maybe_send_data( $args = array() ) {
6567
// Check if the user has consented to the data sending.
6668
if ( ! get_option( 'repack_telemetry_optin' ) ) {
6769
return;
6870
}
6971

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+
}
7396
}
97+
98+
update_option( 'repack_telemetry_sent', time() );
7499
}
75100

76101
/**
77102
* Sends data.
78103
*
79104
* @access private
80105
* @since 1.1.0
106+
*
107+
* @param array $args
81108
* @return array|WP_Error
82109
*/
83-
private function send_data() {
110+
private function send_data( $args = array() ) {
84111
// Ping remote server.
85112
return wp_remote_post(
86113
'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+
)
95125
)
96126
);
97127
}
@@ -146,9 +176,10 @@ public function admin_notice() {
146176
* Builds and returns the data or uses cached if data already exists.
147177
*
148178
* @access private
179+
* @since 1.1.0
180+
*
149181
* @param array $data
150182
* @return array
151-
* @since 1.1.0
152183
*/
153184
public function get_data( $data = array() ) {
154185
// Build data and return the array.
@@ -250,7 +281,7 @@ private function run_action() {
250281
if ( 'sync' === sanitize_text_field( wp_unslash( $_GET['repack-action'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
251282
// Check the wp-nonce.
252283
if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
253-
$this->maybe_send_data();
284+
$this->maybe_send_data( array( 'blocking' => true ) );
254285
}
255286
}
256287
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
]
3030
},
3131
"require": {
32+
"ext-json": "*"
3233
},
3334
"config": {
3435
"allow-plugins": {

repack.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Plugin Name: WeRePack - Reuse Packaging for WooCommerce
1212
* Plugin URI: https://WeRePack.org/
1313
* Description: Get permission from your customers to reuse already used shipping packaging. As a shop owner it is an easy way to save resources, money and above all to protect the environment.
14-
* Version: 1.4.0
14+
* Version: 1.4.1
1515
* Author: WeRePack.org
1616
* Author URI: https://WeRePack.org
1717
* Text Domain: repack-for-woocommerce
@@ -32,7 +32,7 @@
3232
/**
3333
* Currently plugin version.
3434
*/
35-
define( 'REPACK_VERSION', '1.4.0' );
35+
define( 'REPACK_VERSION', '1.4.1' );
3636
define( 'REPACK_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
3737
define( 'REPACK_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
3838

0 commit comments

Comments
 (0)