Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wc-multishipping/inc/admin/classes/abstract_classes/abstract_helper.php
+++ b/wc-multishipping/inc/admin/classes/abstract_classes/abstract_helper.php
@@ -192,35 +192,59 @@
public function save_admin_shipping_method_selection( $post_id ) {
- if ( 'shop_order' != get_post_type( $post_id ) && 'shop_order_placehold' != get_post_type( $post_id ) )
+ $nonce = $_POST['wms_admin_shipping_method_nonce'] ?? '';
+ if ( ! is_admin() || ! is_string( $nonce ) || '' === $nonce
+ || ! current_user_can( 'edit_shop_order', $post_id )
+ || ! wp_verify_nonce( wp_unslash( $nonce ), 'wms_admin_shipping_method_selection_' . $post_id ) ) {
return;
+ }
$order = wc_get_order( $post_id );
- $order_class = static::get_order_class();
+ if ( ! $order instanceof WC_Order || 'shop_order' !== $order->get_type() ) {
+ return;
+ }
- $new_method = wms_get_var( 'string', 'wms_shipping_method_to_select', '' );
- $wms_pickup_info = wms_get_var( 'string', 'wms_pickup_info', '' );
- $wms_pickup_point = wms_get_var( 'cmd', 'wms_pickup_point', '' );
- $wms_order_item_id = wms_get_var( 'int', 'wms_order_item_id', '' );
+ $method = $_POST['wms_shipping_method_to_select'] ?? '';
+ $item_id = $_POST['wms_order_item_id'] ?? '';
+ if ( ! is_string( $method ) || ! is_string( $item_id ) || ! preg_match( '/^[1-9][0-9]*$/D', $item_id ) ) {
+ return;
+ }
- if ( ! array_key_exists( $new_method, $order_class::AVAILABLE_SHIPPING_METHODS ) )
+ $order_class = static::get_order_class();
+ $new_method = wp_unslash( $method );
+ if ( ! array_key_exists( $new_method, $order_class::AVAILABLE_SHIPPING_METHODS ) ) {
return;
+ }
- if ( ! is_admin() || empty( $new_method ) ) {
+ $shipping_items = $order->get_items( 'shipping' );
+ $shipping_item = $shipping_items[ $item_id ] ?? null;
+ if ( ! $shipping_item instanceof WC_Order_Item_Shipping ) {
return;
}
if ( in_array( $new_method, $order_class::ID_SHIPPING_METHODS_RELAY, true ) ) {
- $relay_info_raw = json_decode( stripslashes( $wms_pickup_info ), true );
- $relay_info = null;
+ $pickup_point = $_POST['wms_pickup_point'] ?? '';
+ $pickup_info = $_POST['wms_pickup_info'] ?? '';
+ if ( ! is_string( $pickup_point ) || ! is_string( $pickup_info ) ) {
+ return;
+ }
+
+ $wms_pickup_point = wms_clean_var( wp_unslash( $pickup_point ), 'cmd', 0 );
+ $relay_info = [];
+ if ( '' !== $pickup_info ) {
+ $relay_info = json_decode( wp_unslash( $pickup_info ), true );
+ if ( ! is_array( $relay_info ) || ! $relay_info ) {
+ return;
+ }
- if ( is_array( $relay_info_raw ) ) {
- if ( array_keys( $relay_info_raw ) === range( 0, count( $relay_info_raw ) - 1 ) ) {
- $pickup_name = isset( $relay_info_raw[0] ) ? $relay_info_raw[0] : '';
- $pickup_address = isset( $relay_info_raw[1] ) ? $relay_info_raw[1] : '';
- $city_zip = isset( $relay_info_raw[2] ) ? $relay_info_raw[2] : '';
- $pickup_country = isset( $relay_info_raw[3] ) ? $relay_info_raw[3] : '';
+ foreach ( $relay_info as $value ) {
+ if ( null !== $value && ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
+ return;
+ }
+ }
+ if ( array_keys( $relay_info ) === range( 0, count( $relay_info ) - 1 ) ) {
+ $city_zip = (string) ( $relay_info[2] ?? '' );
$pickup_city = '';
$pickup_zipcode = '';
@@ -234,56 +258,52 @@
}
}
- $relay_info = (object) array(
- 'pickup_name' => $pickup_name,
- 'pickup_address' => $pickup_address,
+ $relay_info = [
+ 'pickup_name' => $relay_info[0] ?? '',
+ 'pickup_address' => $relay_info[1] ?? '',
'pickup_city' => $pickup_city,
'pickup_zipcode' => $pickup_zipcode,
- 'pickup_country' => $pickup_country,
- );
- } else {
- $relay_info = (object) $relay_info_raw;
+ 'pickup_country' => $relay_info[3] ?? '',
+ ];
}
- } elseif ( is_object( $relay_info_raw ) ) {
- $relay_info = $relay_info_raw;
+
+ $relay_info = array_map( 'sanitize_text_field', $relay_info );
+ }
+
+ if ( 'chronopost_relais' === $new_method
+ && 'yes' === get_option( 'wms_chronopost_relais_exclude_lockers', 'no' )
+ && ( empty( $wms_pickup_point ) || empty( $relay_info['pickup_name'] ) ) ) {
+ return;
}
- if ( $relay_info && is_object( $relay_info ) ) {
- $relay_info->pickup_id = $wms_pickup_point;
+ if ( $relay_info ) {
+ $relay_info['pickup_id'] = $wms_pickup_point;
$order->update_meta_data(
'_wms_' . static::SHIPPING_PROVIDER_ID . '_pickup_info',
- array_map( 'sanitize_text_field', (array) $relay_info )
+ $relay_info
);
-
- if ( ! empty( $relay_info->pickup_address ) ) {
- $order->set_shipping_address_1( $relay_info->pickup_address );
+ if ( ! empty( $relay_info['pickup_address'] ) ) {
+ $order->set_shipping_address_1( $relay_info['pickup_address'] );
}
- if ( ! empty( $relay_info->pickup_zipcode ) ) {
- $order->set_shipping_postcode( $relay_info->pickup_zipcode );
+ if ( ! empty( $relay_info['pickup_zipcode'] ) ) {
+ $order->set_shipping_postcode( $relay_info['pickup_zipcode'] );
}
- if ( ! empty( $relay_info->pickup_city ) ) {
- $order->set_shipping_city( $relay_info->pickup_city );
+ if ( ! empty( $relay_info['pickup_city'] ) ) {
+ $order->set_shipping_city( $relay_info['pickup_city'] );
}
- if ( ! empty( $relay_info->pickup_country ) ) {
- $order->set_shipping_country( $relay_info->pickup_country );
+ if ( ! empty( $relay_info['pickup_country'] ) ) {
+ $order->set_shipping_country( $relay_info['pickup_country'] );
}
- if ( ! empty( $relay_info->pickup_name ) ) {
- $order->set_shipping_company( $relay_info->pickup_name );
+ if ( ! empty( $relay_info['pickup_name'] ) ) {
+ $order->set_shipping_company( $relay_info['pickup_name'] );
}
$order->save();
}
}
- $shipping_item = $order->get_item( $wms_order_item_id );
-
- if ( ! $shipping_item || ! is_a( $shipping_item, 'WC_Order_Item_Shipping' ) ) {
- error_log( 'Erreur : Élément de livraison introuvable ou invalide.' );
- return;
- }
-
$shipping_item->set_props( [
'method_id' => $new_method,
'method_title' => $order_class::AVAILABLE_SHIPPING_METHODS[ $new_method ],
@@ -305,6 +325,12 @@
return;
$label_class = static::get_label_class();
+ if ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ foreach ( $tracking_number as $number ) {
+ $label_class::delete( $number, [ $id ] );
+ }
+ return;
+ }
$label_class::delete( $tracking_number );
}
@@ -379,40 +405,52 @@
public function generate_label_PDF() {
if ( 1 !== wp_verify_nonce( wms_get_var( 'cmd', 'wms_nonce', '' ), 'wms_download_label' ) )
wp_die( 'You can not do that' );
- if ( ! current_user_can( 'edit_posts' ) )
- header( 'HTTP/1.0 401 Unauthorized' );
-
$shipping_provider = wms_get_var( 'string', 'wms_shipping_provider', '' );
if ( static::SHIPPING_PROVIDER_ID != $shipping_provider )
return false;
$tracking_numbers = wms_get_var( 'string', 'wms_tracking_numbers', '' );
- if ( empty( $tracking_numbers ) )
+ if ( ! is_string( $tracking_numbers ) || '' === $tracking_numbers )
wp_die( __( 'Generate label error: A parameter is missing', 'wc-multishipping' ) );
$label_class = static::get_label_class();
- $label_class::download_label_PDF( $tracking_numbers );
+ if ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ $order_id = wms_get_var( 'string', 'wms_order_id', null );
+ $label_class::download_label_PDF( $tracking_numbers, $order_id );
+ } else {
+ $label_class::download_label_PDF( $tracking_numbers );
+ }
}
public function print_label_PDF() {
if ( 1 !== wp_verify_nonce( wms_get_var( 'cmd', 'wms_nonce', '' ), 'wms_print_labels' ) )
wp_die( 'You can not do that' );
- if ( ! current_user_can( 'edit_posts' ) ) {
- header( 'HTTP/1.0 401 Unauthorized' );
- }
-
$shipping_provider = wms_get_var( 'string', 'wms_shipping_provider', '' );
if ( static::SHIPPING_PROVIDER_ID != $shipping_provider )
return false;
$tracking_numbers = wms_get_var( 'string', 'wms_tracking_numbers', '' );
- if ( empty( $tracking_numbers ) )
+ if ( ! is_string( $tracking_numbers ) || '' === $tracking_numbers )
wp_die( __( 'Print label error: A parameter is missing', 'wc-multishipping' ) );
$label_class = static::get_label_class();
- $label_class::print_label_PDF( $tracking_numbers );
+ $label_class::print_label_PDF( $tracking_numbers, $this->get_mondial_relay_label_order_scope() );
+ }
+
+ private function get_mondial_relay_label_order_scope() {
+ if ( 'mondial_relay' !== static::SHIPPING_PROVIDER_ID ) {
+ return null;
+ }
+ $order_ids = wms_get_var( 'string', 'wms_order_ids', null );
+ if ( null === $order_ids ) {
+ return null;
+ }
+ if ( ! is_string( $order_ids ) || ! preg_match( '/^[1-9][0-9]*(?:,[1-9][0-9]*)*$/D', $order_ids ) ) {
+ wp_die( __( 'The label order list is invalid.', 'wc-multishipping' ), '', [ 'response' => 400 ] );
+ }
+ return explode( ',', $order_ids );
}
public function delete_label_PDF() {
@@ -438,7 +476,9 @@
if ( in_array( '', $tracking_numbers, true ) ) {
wp_die( __( 'Unable to delete label: the tracking number list is invalid.', 'wc-multishipping' ), '', [ 'response' => 400 ] );
}
- $tracking_numbers = array_values( array_unique( $tracking_numbers ) );
+ if ( 'mondial_relay' !== static::SHIPPING_PROVIDER_ID ) {
+ $tracking_numbers = array_values( array_unique( $tracking_numbers ) );
+ }
$order_ids = [];
if ( 'chronopost' === static::SHIPPING_PROVIDER_ID ) {
@@ -454,8 +494,11 @@
$order_class = static::get_order_class();
$tracking_order_ids = [];
+ $mondial_relay_label_orders = [];
if ( 'chronopost' === static::SHIPPING_PROVIDER_ID ) {
$metadata_deleted = $order_class::delete_label_meta_from_tracking_numbers( $tracking_numbers, $order_ids, $tracking_order_ids );
+ } elseif ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ $metadata_deleted = $order_class::delete_label_meta_from_tracking_numbers( $tracking_numbers, $this->get_mondial_relay_label_order_scope(), $mondial_relay_label_orders );
} else {
$metadata_deleted = $order_class::delete_label_meta_from_tracking_numbers( $tracking_numbers );
}
@@ -468,6 +511,14 @@
}
$label_class = static::get_label_class();
+ if ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ foreach ( $mondial_relay_label_orders as $label_order ) {
+ $label_class::delete_label_PDF_from_tracking_number( $label_order['tracking_number'], [ $label_order['order_id'] ] );
+ }
+ $this->redirect_after_label_delete();
+ return true;
+ }
+
foreach ( $tracking_numbers as $one_tracking_number ) {
if ( 'chronopost' === static::SHIPPING_PROVIDER_ID ) {
$order_ids_for_tracking_number = $tracking_order_ids[$one_tracking_number] ?? [];
@@ -501,18 +552,16 @@
public function download_labels_zip() {
if ( 1 !== wp_verify_nonce( wms_get_var( 'cmd', 'wms_nonce', '' ), 'wms_download_labels_zip' ) )
wp_die( 'You can not do that' );
- if ( ! current_user_can( 'edit_posts' ) )
- header( 'HTTP/1.0 401 Unauthorized' );
-
$shipping_provider = wms_get_var( 'string', 'wms_shipping_provider', '' );
if ( static::SHIPPING_PROVIDER_ID != $shipping_provider )
return false;
$tracking_numbers_param = wms_get_var( 'string', 'wms_tracking_numbers', '' );
- if ( empty( $tracking_numbers_param ) ) {
+ if ( ! is_string( $tracking_numbers_param ) || '' === $tracking_numbers_param ) {
wms_enqueue_message( __( 'Unable to download label => No tracking number found', 'wc-multishipping' ), 'error' );
wp_redirect( admin_url( 'admin.php?page=wc_wms_view' ) );
+ return false;
}
@@ -524,7 +573,7 @@
}
$label_class = static::get_label_class();
- $label_class::download_labels_zip( $tracking_numbers );
+ $label_class::download_labels_zip( $tracking_numbers, $this->get_mondial_relay_label_order_scope() );
}
public function register_wms_post_statuses() {
--- a/wc-multishipping/inc/admin/classes/abstract_classes/abstract_label.php
+++ b/wc-multishipping/inc/admin/classes/abstract_classes/abstract_label.php
@@ -36,53 +36,61 @@
$table_name = $wpdb->prefix . self::WMS_TABLE_NAME;
- $label_type = ( ! $is_return_label ? 'outward' : 'inward' );
- $request_action = ( ! $is_return_label ? 'INSERT INTO' : 'UPDATE' );
+ if ( $is_return_label ) {
+ if ( ! is_int( $order_id ) && ! is_string( $order_id ) ) {
+ return false;
+ }
+ $label_order_id = (int) $order_id;
+ if ( $label_order_id <= 0 || (string) $label_order_id !== (string) $order_id || ! is_scalar( $outward_tracking_number ) || '' === trim( (string) $outward_tracking_number ) ) {
+ return false;
+ }
+ return $wpdb->update( $table_name, [
+ 'inward_label' => $label,
+ 'inward_label_format' => $label_format,
+ 'inward_tracking_number' => $tracking_number,
+ 'inward_label_created_at' => current_time( 'mysql' ),
+ 'inward_reservation_number' => $reservation_number,
+ ], [
+ 'order_id' => $label_order_id,
+ 'shipping_provider' => static::SHIPPING_PROVIDER_ID,
+ 'outward_tracking_number' => $outward_tracking_number,
+ ] );
+ }
$sql = <<<END_SQL
- $request_action $table_name SET
+ INSERT INTO $table_name SET
order_id = %d,
shipping_provider = %s,
- {$label_type}_label= %s,
- {$label_type}_label_format= %s,
- {$label_type}_tracking_number= %s,
- {$label_type}_label_created_at= %d,
- {$label_type}_reservation_number= %d
+ outward_label= %s,
+ outward_label_format= %s,
+ outward_tracking_number= %s,
+ outward_label_created_at= %d,
+ outward_reservation_number= %d
END_SQL;
- $additional_where_conditions = [];
- if ( ! empty( $outward_tracking_number ) )
- $additional_where_conditions['outward_tracking_number'] = $outward_tracking_number;
-
- if ( ! empty( $additional_where_conditions ) ) {
- $sql_additional_where_condition = implode( '= %s, ', array_keys( $additional_where_conditions ) ) . '= %s';
- $sql .= <<<END_SQL
- WHERE $sql_additional_where_condition
-END_SQL;
- }
-
$sql = $wpdb->prepare(
$sql,
- array_merge(
- [
- $order_id,
- static::SHIPPING_PROVIDER_ID,
- $label,
- $label_format,
- $tracking_number,
- current_time( 'mysql' ),
- $reservation_number,
- ],
- $additional_where_conditions
- )
+ [
+ $order_id,
+ static::SHIPPING_PROVIDER_ID,
+ $label,
+ $label_format,
+ $tracking_number,
+ current_time( 'mysql' ),
+ $reservation_number,
+ ]
);
return $wpdb->query( $sql );
}
- public static function get_info_from_tracking_numbers( $tracking_numbers ) {
+ public static function get_info_from_order( $tracking_numbers, $order_id ) {
+ return static::get_info_from_tracking_numbers( $tracking_numbers, $order_id );
+ }
+
+ public static function get_info_from_tracking_numbers( $tracking_numbers, $order_id = null ) {
if ( empty( $tracking_numbers ) ) {
wms_enqueue_message( __( 'Unable to get info from tracking number => Tracking numbers are empty', 'wc-multishipping' ), 'error' );
@@ -110,28 +118,88 @@
FROM $table_name
WHERE (outward_tracking_number IN ($prepare_in_condition)
OR inward_tracking_number IN ($prepare_in_condition))
-AND shipping_provider = %s;
+AND shipping_provider = %s
END_SQL;
- $query = $wpdb->prepare(
- $query,
- array_merge(
- $tracking_numbers,
- $tracking_numbers,
- [
- static::SHIPPING_PROVIDER_ID,
- ]
- )
- );
-
+ $args = array_merge( $tracking_numbers, $tracking_numbers, [ static::SHIPPING_PROVIDER_ID ] );
+ if ( null !== $order_id ) {
+ $query .= ' AND order_id = %d';
+ $args[] = $order_id;
+ }
+ $query = $wpdb->prepare( $query, $args );
$pdf_labels = wms_load_object_list( $query, 'order_id' );
if ( empty( $pdf_labels ) )
return [];
+ if ( null === $order_id && 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ $owners = [];
+ foreach ( $pdf_labels as $row ) {
+ foreach ( $tracking_numbers as $number ) {
+ if ( (string) $number !== (string) ( $row->outward_tracking_number ?? '' ) && (string) $number !== (string) ( $row->inward_tracking_number ?? '' ) ) { continue; }
+ if ( isset( $owners[ (string) $number ] ) && $owners[ (string) $number ] !== (string) $row->order_id ) { return []; }
+ $owners[ (string) $number ] = (string) $row->order_id;
+ }
+ }
+ }
return $pdf_labels;
}
+ public static function get_authorized_label_orders( $tracking_numbers, $order_ids = null ) {
+ $tracking_numbers = array_values( (array) $tracking_numbers );
+ $scoped = 'mondial_relay' === static::SHIPPING_PROVIDER_ID && null !== $order_ids;
+ if ( $scoped && ( ! is_array( $order_ids ) || count( $order_ids ) !== count( $tracking_numbers ) ) ) {
+ wp_die( esc_html__( 'The label order list is invalid.', 'wc-multishipping' ), '', [ 'response' => 400 ] );
+ }
+ $order_ids = array_values( (array) $order_ids );
+ global $wpdb;
+ $table = $wpdb->prefix . self::WMS_TABLE_NAME;
+ $owners = [];
+ $seen = [];
+ foreach ( $tracking_numbers as $index => $tracking ) {
+ if ( ! is_scalar( $tracking ) || '' === (string) $tracking ) {
+ wp_die( esc_html__( 'The label tracking number is invalid.', 'wc-multishipping' ), '', [ 'response' => 400 ] );
+ }
+ $tracking = (string) $tracking;
+ $sql = "SELECT DISTINCT order_id FROM $table WHERE shipping_provider = %s AND (outward_tracking_number = %s OR inward_tracking_number = %s)";
+ $args = [ static::SHIPPING_PROVIDER_ID, $tracking, $tracking ];
+ $scope = 0;
+ if ( $scoped ) {
+ $order_id = $order_ids[ $index ];
+ if ( ! is_scalar( $order_id ) || ! ctype_digit( (string) $order_id ) || (int) $order_id <= 0 || (string) (int) $order_id !== (string) $order_id ) {
+ wp_die( esc_html__( 'The label order list is invalid.', 'wc-multishipping' ), '', [ 'response' => 400 ] );
+ }
+ $scope = (int) $order_id;
+ $sql .= ' AND order_id = %d';
+ $args[] = $scope;
+ }
+ if ( isset( $seen[ $scope ][ $tracking ] ) ) {
+ continue;
+ }
+ $matching_ids = $wpdb->get_col( $wpdb->prepare( $sql, ...$args ) );
+ if ( ! is_array( $matching_ids ) || count( $matching_ids ) !== 1 || (int) $matching_ids[0] <= 0 || ! current_user_can( 'edit_shop_order', (int) $matching_ids[0] ) ) {
+ wp_die( esc_html__( 'You cannot access this label.', 'wc-multishipping' ), '', [ 'response' => 403 ] );
+ }
+ $seen[ $scope ][ $tracking ] = true;
+ $owners[] = [ 'tracking_number' => $tracking, 'order_id' => (int) $matching_ids[0] ];
+ }
+ return $owners;
+ }
+
+ private static function authorized_labels( $tracking_numbers, $order_ids = null ) {
+ $labels = [];
+ foreach ( self::get_authorized_label_orders( $tracking_numbers, $order_ids ) as $owner ) {
+ $tracking_number = $owner['tracking_number'];
+ $rows = self::get_info_from_tracking_numbers( $tracking_number, $owner['order_id'] );
+ $row = $rows ? reset( $rows ) : null;
+ if ( $tracking_number !== (string) ( $row->outward_tracking_number ?? '' ) && $tracking_number !== (string) ( $row->inward_tracking_number ?? '' ) ) {
+ wp_die( esc_html__( 'Label unavailable.', 'wc-multishipping' ), '', [ 'response' => 404 ] );
+ }
+ $labels[] = [ 'tracking_number' => $tracking_number, 'row' => $row ];
+ }
+ return $labels;
+ }
+
public static function delete( $tracking_number, $order_ids = [] ) {
if ( empty( $tracking_number ) ) {
wms_enqueue_message( __( 'Can't delete label => No tracking number selected', 'wc-multishipping' ) );
@@ -142,7 +210,10 @@
$order_scope_sql = '';
$normalized_order_ids = [];
$has_explicit_order_ids = null !== $order_ids && [] !== $order_ids && '' !== $order_ids;
- if ( 'chronopost' === static::SHIPPING_PROVIDER_ID && $has_explicit_order_ids ) {
+ if ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID && ( ! $has_explicit_order_ids || count( (array) $order_ids ) !== 1 ) )
+ return false;
+
+ if ( in_array( static::SHIPPING_PROVIDER_ID, [ 'chronopost', 'mondial_relay' ], true ) && $has_explicit_order_ids ) {
if ( ! is_array( $order_ids ) )
$order_ids = [ $order_ids ];
@@ -232,7 +303,7 @@
return $label;
}
- public static function get_url_for_download_label( $tracking_numbers = [] ) {
+ public static function get_url_for_download_label( $tracking_numbers = [], $order_id = null ) {
if ( empty( $tracking_numbers ) )
return '';
@@ -250,48 +321,33 @@
$wms_nonce = wp_create_nonce( 'wms_download_label' );
- return admin_url( 'admin-post.php?action=wms_download_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . $tracking_numbers_params . '&wms_nonce=' . $wms_nonce );
+ $order_scope = 'mondial_relay' === static::SHIPPING_PROVIDER_ID && (int) $order_id > 0 ? '&wms_order_id=' . (int) $order_id : '';
+ return admin_url( 'admin-post.php?action=wms_download_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . $tracking_numbers_params . '&wms_nonce=' . $wms_nonce . $order_scope );
}
- public static function download_label_PDF( $tracking_number ) {
- if ( ! current_user_can( 'edit_posts' ) ) {
- header( 'HTTP/1.0 401 Unauthorized' );
+ public static function download_label_PDF( $tracking_number, $order_id = null ) {
+ $labels = self::authorized_labels( [ $tracking_number ], null === $order_id ? null : [ $order_id ] );
+ $row = $labels[0]['row'];
+ $content = (string) $tracking_number === (string) ( $row->outward_tracking_number ?? '' ) ? $row->outward_label : $row->inward_label;
+ $temp = wp_tempnam( 'wms-label.pdf' );
+ if ( ! $temp ) {
+ return false;
}
try {
- $label = self::get_info_from_tracking_numbers( $tracking_number );
- if ( empty( $label ) ) {
- return;
+ if ( false === file_put_contents( $temp, $content ) ) {
+ return false;
}
-
-
- if ( $tracking_number == reset( $label )->outward_tracking_number )
- $label = reset( $label )->outward_label;
- else
- $label = reset( $label )->inward_label;
-
- if ( ! class_exists( 'PDF_lib' ) )
- include( WMS_LIB . 'PDF_lib.class.php' );
-
- $file_to_download_name = get_temp_dir() . DS . static::DOWNLOAD_NAME . '.outward(' . $tracking_number . ').pdf';
- $label_file_name = 'outward_label.pdf';
- $files_to_merge = [];
- $label_content_file = fopen( sys_get_temp_dir() . DS . $label_file_name, 'w' );
- fwrite( $label_content_file, $label );
- fclose( $label_content_file );
-
- $files_to_merge[] = sys_get_temp_dir() . DS . $label_file_name;
-
- @PDF_lib::merge( $files_to_merge, PDF_lib::DESTINATION__DISK_DOWNLOAD, $file_to_download_name );
- foreach ( $files_to_merge as $one_file_to_merge ) {
- unlink( $one_file_to_merge );
+ if ( ! class_exists( 'PDF_lib' ) ) {
+ include WMS_LIB . 'PDF_lib.class.php';
}
- unlink( $file_to_download_name );
- } catch (Exception $e) {
- header( 'HTTP/1.0 404 Not Found' );
+ $name = sanitize_file_name( static::DOWNLOAD_NAME . '.outward(' . $tracking_number . ').pdf' );
+ PDF_lib::merge( [ $temp ], PDF_lib::DESTINATION__DOWNLOAD, $name );
+ } finally {
+ unlink( $temp );
}
}
- public static function get_url_for_download_labels_zip( $tracking_numbers = [] ) {
+ public static function get_url_for_download_labels_zip( $tracking_numbers = [], $tracking_order_ids = [] ) {
if ( empty( $tracking_numbers ) )
return '';
@@ -309,18 +365,16 @@
$wms_nonce = wp_create_nonce( 'wms_download_labels_zip' );
- return admin_url( 'admin-post.php?action=wms_download_labels_zip&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . $tracking_numbers_params . '&wms_nonce=' . $wms_nonce );
+ return admin_url( 'admin-post.php?action=wms_download_labels_zip&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . rawurlencode( $tracking_numbers_params ) . '&wms_nonce=' . $wms_nonce . self::mondial_relay_order_scope_query( $tracking_order_ids ) );
}
- public static function download_labels_zip( $tracking_numbers ) {
+ public static function download_labels_zip( $tracking_numbers, $tracking_order_ids = null ) {
- if ( ! current_user_can( 'edit_posts' ) ) {
- header( 'HTTP/1.0 401 Unauthorized' );
- }
if ( empty( $tracking_numbers ) || ! is_array( $tracking_numbers ) )
return false;
- $zip_file = static::generate_zip( $tracking_numbers );
+ $labels = self::authorized_labels( $tracking_numbers, $tracking_order_ids );
+ $zip_file = static::generate_zip( $labels );
if ( empty( $zip_file ) )
return false;
@@ -329,14 +383,17 @@
header( 'Content-Type: application/octet-stream' );
header( 'Content-Transfer-Encoding: Binary' );
header( "Content-disposition: attachment; filename="$filename"" );
- wp_die( $zip_file, '', 200 );
- } catch (Exception $e) {
+ readfile( $zip_file );
+ } catch (Throwable $e) {
header( 'HTTP/1.0 404 Not Found' );
+ } finally {
+ unlink( $zip_file );
}
+ exit;
}
- public static function get_url_for_print_labels( $tracking_numbers = [] ) {
+ public static function get_url_for_print_labels( $tracking_numbers = [], $tracking_order_ids = [] ) {
if ( empty( $tracking_numbers ) )
return '';
@@ -354,26 +411,28 @@
$wms_nonce = wp_create_nonce( 'wms_print_labels' );
- return admin_url( 'admin-post.php?action=wms_print_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . $tracking_numbers_params . '&wms_nonce=' . $wms_nonce );
+ return admin_url( 'admin-post.php?action=wms_print_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . rawurlencode( $tracking_numbers_params ) . '&wms_nonce=' . $wms_nonce . self::mondial_relay_order_scope_query( $tracking_order_ids ) );
+ }
+
+ private static function mondial_relay_order_scope_query( $tracking_order_ids ) {
+ if ( 'mondial_relay' !== static::SHIPPING_PROVIDER_ID || empty( $tracking_order_ids ) ) {
+ return '';
+ }
+ return '&wms_order_ids=' . rawurlencode( implode( ',', (array) $tracking_order_ids ) );
}
- public static function print_label_PDF( $tracking_numbers ) {
+ public static function print_label_PDF( $tracking_numbers, $tracking_order_ids = null ) {
if ( empty( $tracking_numbers ) ) {
return false;
}
$tracking_numbers = explode( ',', $tracking_numbers );
+ $labels = self::authorized_labels( $tracking_numbers, $tracking_order_ids );
$label_types = [ 'outward', 'inward' ];
$files_to_merge = [];
try {
- foreach ( $tracking_numbers as $tracking_number ) {
- $label_info = self::get_info_from_tracking_numbers( $tracking_number );
- if ( empty( $label_info ) ) {
- continue;
- }
- $label = reset( $label_info );
-
+ foreach ( $labels as [ 'tracking_number' => $tracking_number, 'row' => $label ] ) {
if ( empty( $label->outward_label ) && empty( $label->inward_label ) ) {
continue;
}
@@ -382,10 +441,15 @@
$label_property = "{$label_type}_label";
$tracking_property = "{$label_type}_tracking_number";
- if ( ! empty( $label->$label_property ) && $tracking_number == $label->$tracking_property ) {
- $label_file_name = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "label({$tracking_number}).pdf";
- file_put_contents( $label_file_name, $label->$label_property );
+ if ( ! empty( $label->$label_property ) && $tracking_number === (string) $label->$tracking_property ) {
+ $label_file_name = wp_tempnam( 'wms-print-label.pdf' );
+ if ( ! $label_file_name ) {
+ throw new RuntimeException( 'Cannot create temporary label file' );
+ }
$files_to_merge[] = $label_file_name;
+ if ( false === file_put_contents( $label_file_name, $label->$label_property ) ) {
+ throw new RuntimeException( 'Cannot write temporary label file' );
+ }
}
}
}
@@ -397,11 +461,12 @@
PDF_lib::merge( $files_to_merge, PDF_lib::DESTINATION__INLINE );
- array_map( 'unlink', $files_to_merge );
}
- } catch (Exception $e) {
+ } catch (Throwable $e) {
error_log( 'Error printing label PDF: ' . $e->getMessage() );
header( 'HTTP/1.0 404 Not Found' );
+ } finally {
+ array_map( 'unlink', $files_to_merge );
}
}
@@ -420,7 +485,7 @@
);
$string_tracking_numbers = implode( ',', $tracking_numbers );
- $order_ids_query = '';
+ $order_ids_query = self::mondial_relay_order_scope_query( $order_ids );
if ( 'chronopost' === static::SHIPPING_PROVIDER_ID ) {
if ( ! is_array( $order_ids ) )
$order_ids = [ $order_ids ];
@@ -447,7 +512,7 @@
$wms_nonce = wp_create_nonce( 'wms_delete_labels' );
- return admin_url( 'admin-post.php?action=wms_delete_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . $string_tracking_numbers . $order_ids_query . '&wms_nonce=' . $wms_nonce );
+ return admin_url( 'admin-post.php?action=wms_delete_label&wms_shipping_provider=' . static::SHIPPING_PROVIDER_ID . '&' . self::TRACKING_NUMBER_VAR_NAME . '=' . rawurlencode( $string_tracking_numbers ) . $order_ids_query . '&wms_nonce=' . $wms_nonce );
}
public static function delete_label_PDF_from_tracking_number( $tracking_number, $order_ids = [] ) {
@@ -461,74 +526,66 @@
}
}
- private static function generate_zip( $tracking_numbers = [] ) {
- if ( empty( $tracking_numbers ) )
- return;
- if ( ! is_array( $tracking_numbers ) )
- $tracking_numbers = [ $tracking_numbers ];
-
+ private static function generate_zip( $labels ) {
$zip = new ZipArchive();
$filename = tempnam( sys_get_temp_dir(), static::DOWNLOAD_NAME . '_' );
- $tmp_files = [];
- $tracking_info_already_generated = [];
+ if ( false === $filename ) {
+ return false;
+ }
+ $ready = false;
+ $entry_count = 0;
try {
- $zip->open( $filename, ZipArchive::CREATE );
- foreach ( $tracking_numbers as $one_tracking_number ) {
- if ( in_array( $one_tracking_number, $tracking_info_already_generated ) )
- continue;
-
- $info_from_tracking = static::get_info_from_tracking_numbers( $one_tracking_number );
- if ( empty( $info_from_tracking ) )
- continue;
-
- $order_labels = reset( $info_from_tracking );
-
+ if ( true !== $zip->open( $filename, ZipArchive::OVERWRITE ) ) {
+ return false;
+ }
+ foreach ( $labels as [ 'tracking_number' => $one_tracking_number, 'row' => $order_labels ] ) {
$zip_dir_name = 'order_' . $order_labels->order_id;
- $zip->addEmptyDir( $zip_dir_name );
$order_labels_format = ! empty( $order_labels->outward_format ) ? $order_labels->outward_format : 'PDF';
- if ( ! empty( $order_labels->outward_label ) && $order_labels->outward_tracking_number == $one_tracking_number ) {
- $zip->addFromString(
+ if ( ! empty( $order_labels->outward_label ) && (string) $order_labels->outward_tracking_number === $one_tracking_number ) {
+ if ( ! $zip->addFromString(
$zip_dir_name . '/outward_labels/outward_label(' . $one_tracking_number . ').' . strtolower( $order_labels_format ),
$order_labels->outward_label
- );
- $tracking_info_already_generated[] = $one_tracking_number;
+ ) ) {
+ throw new RuntimeException( 'Cannot add outward label to archive' );
+ }
+ ++$entry_count;
}
- if ( ! empty( $order_labels->inward_label ) && $order_labels->inward_tracking_number == $one_tracking_number ) {
- $zip->addFromString(
+ if ( ! empty( $order_labels->inward_label ) && (string) $order_labels->inward_tracking_number === $one_tracking_number ) {
+ if ( ! $zip->addFromString(
$zip_dir_name . '/inward_labels/inward_label(' . $one_tracking_number . ').' . strtolower( $order_labels_format ),
$order_labels->inward_label
- );
- $tracking_info_already_generated[] = $one_tracking_number;
+ ) ) {
+ throw new RuntimeException( 'Cannot add inward label to archive' );
+ }
+ ++$entry_count;
}
}
- if ( empty( $tracking_info_already_generated ) )
- return false;
-
+ $closed = $zip->close();
+ $ready = $entry_count > 0 && $closed;
+ return $ready ? $filename : false;
+ } catch ( Throwable $e ) {
$zip->close();
-
- $content = readfile( $filename );
-
- return $content;
+ return false;
} finally {
- array_map(
- function ($tmp_file) {
- unlink( $tmp_file );
- },
- $tmp_files
- );
- unlink( $filename );
+ if ( ! $ready && is_file( $filename ) ) {
+ unlink( $filename );
+ }
}
}
public static function save_label_PDF( $tracking_number ) {
+ return static::save_label_PDF_for_order( $tracking_number, null );
+ }
+
+ public static function save_label_PDF_for_order( $tracking_number, $order_id ) {
if ( ! current_user_can( 'edit_posts' ) )
return false;
try {
- $label = self::get_info_from_tracking_numbers( $tracking_number );
+ $label = null === $order_id ? static::get_info_from_tracking_numbers( $tracking_number ) : static::get_info_from_order( $tracking_number, $order_id );
if ( empty( $label ) ) {
return;
}
@@ -538,13 +595,23 @@
include( WMS_LIB . 'PDF_lib.class.php' );
$file_to_save_name = get_temp_dir() . static::DOWNLOAD_NAME . '.outward(' . $tracking_number . ').pdf';
- $label_file_name = 'outward_label.pdf';
+ if ( null !== $order_id && 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ $file_to_save_name = tempnam( sys_get_temp_dir(), 'wms_mr_attachment_' );
+ $label_file = tempnam( sys_get_temp_dir(), 'wms_mr_source_' );
+ if ( false === $file_to_save_name || false === $label_file ) {
+ if ( $file_to_save_name ) { unlink( $file_to_save_name ); }
+ if ( $label_file ) { unlink( $label_file ); }
+ return false;
+ }
+ } else {
+ $label_file = sys_get_temp_dir() . DS . 'outward_label.pdf';
+ }
$files_to_merge = [];
- $label_content_file = fopen( sys_get_temp_dir() . DS . $label_file_name, 'w' );
+ $label_content_file = fopen( $label_file, 'w' );
fwrite( $label_content_file, $label );
fclose( $label_content_file );
- $files_to_merge[] = sys_get_temp_dir() . DS . $label_file_name;
+ $files_to_merge[] = $label_file;
@PDF_lib::merge( $files_to_merge, PDF_lib::DESTINATION__DISK, $file_to_save_name );
foreach ( $files_to_merge as $one_file_to_merge ) {
--- a/wc-multishipping/inc/admin/classes/abstract_classes/abstract_meta_box.php
+++ b/wc-multishipping/inc/admin/classes/abstract_classes/abstract_meta_box.php
@@ -59,7 +59,11 @@
) . '</a>';
}
if ( ! empty( $tracking_numbers[ $one_label_type ] ) ) {
- $download_link[ $one_label_type ] = ( count( $tracking_numbers[ $one_label_type ] ) <= 1 ) ? $label_class::get_url_for_download_label( $tracking_numbers[ $one_label_type ][0] ) : $label_class::get_url_for_download_labels_zip( $tracking_numbers[ $one_label_type ] );
+ $order_id = 'mondial_relay' === static::SHIPPING_PROVIDER_ID ? $order->get_id() : null;
+ $numbers = $tracking_numbers[ $one_label_type ];
+ $download_link[ $one_label_type ] = count( $numbers ) === 1
+ ? $label_class::get_url_for_download_label( $numbers[0], $order_id )
+ : $label_class::get_url_for_download_labels_zip( $numbers, null === $order_id ? [] : array_fill( 0, count( $numbers ), $order_id ) );
}
}
@@ -144,7 +148,7 @@
if ( empty( $shipment_data['_wms_outward_parcels']['_wms_parcels'] ) )
return;
- $can_generate = $this->can_generate_label();
+ $can_generate = $this->can_generate_label( true );
?>
<tbody id="wms_meta_box_generate_inward_label_button">
<tr>
@@ -193,6 +197,7 @@
$upgrade_url = wms_telemetry::get_pro_upgrade_url( 'order_metabox', $feature, 1 );
$supports_free_labels = in_array( static::SHIPPING_PROVIDER_ID, [ 'mondial_relay', 'chronopost' ], true );
$remaining = $supports_free_labels ? ( new wms_free_label_proxy_client() )->get_remaining() : 0;
+ $has_pending = $supports_free_labels && wms_free_label_proxy_client::has_pending_operation( static::SHIPPING_PROVIDER_ID, $this->order->get_id(), 'inward_label' === $feature );
if ( $supports_free_labels ) {
?>
@@ -201,6 +206,8 @@
<strong><?php echo esc_html( sprintf( _n( '%d génération d’étiquette gratuite restante', '%d générations d’étiquettes gratuites restantes', $remaining, 'wc-multishipping' ), $remaining ) ); ?></strong>
<p><?php esc_html_e( 'Créez cette étiquette dans WooCommerce. Les données nécessaires sont transmises de façon sécurisée à WcMultiShipping puis au transporteur, sans être conservées.', 'wc-multishipping' ); ?></p>
<p><strong><?php esc_html_e( 'Besoin de plus ? La version PRO permet des générations illimitées.', 'wc-multishipping' ); ?></strong></p>
+ <?php elseif ( $has_pending ) : ?>
+ <p><?php esc_html_e( 'A previous generation is pending. Retry to recover its result without creating another shipment.', 'wc-multishipping' ); ?></p>
<?php else : ?>
<strong><?php esc_html_e( 'Vos 3 générations gratuites ont été utilisées.', 'wc-multishipping' ); ?></strong>
<p><?php esc_html_e( 'Passez à la version PRO pour continuer à créer toutes vos étiquettes directement dans WooCommerce, sans limite.', 'wc-multishipping' ); ?></p>
@@ -223,7 +230,7 @@
<?php
}
- private function can_generate_label() {
+ private function can_generate_label( $is_return_order = false ) {
if ( ! wms_table_exists() ) {
return false;
}
@@ -236,7 +243,8 @@
return false;
}
- return ( new wms_free_label_proxy_client() )->get_remaining() > 0;
+ return ( new wms_free_label_proxy_client() )->get_remaining() > 0
+ || wms_free_label_proxy_client::has_pending_operation( static::SHIPPING_PROVIDER_ID, $this->order->get_id(), $is_return_order );
}
protected function display_hidden_inputs() {
--- a/wc-multishipping/inc/admin/classes/abstract_classes/abstract_order.php
+++ b/wc-multishipping/inc/admin/classes/abstract_classes/abstract_order.php
@@ -3,6 +3,7 @@
namespace WCMultiShippingincadminclassesabstract_classes;
use AutomatticWooCommerceUtilitiesOrderUtil;
+use WCMultiShippingincadminclassesconfigconfig_class;
use WCMultiShippingincadminclassesfree_label_generationwms_free_label_proxy_client;
abstract class abstract_order {
@@ -490,6 +491,14 @@
if ( empty( $order ) || ! isset( $is_return_order ) )
return false;
+ if ( wms_is_pro_package() ) {
+ $license = config_class::is_license_valid();
+ if ( empty( $license->status ) ) {
+ wms_enqueue_message( __( 'Unable to generate label : ', 'wc-multishipping' ) . $license->message, 'error' );
+ return false;
+ }
+ }
+
$labels_information = static::get_labels_from_order( $order->get_id() );
if ( empty( $labels_information ) && $is_return_order ) {
wms_enqueue_message( sprintf( __( 'Inward label can not be generated for order n°%s as the order has not been sent yet', 'wc-multishipping' ), $order->get_id() ), 'error' );
@@ -506,6 +515,11 @@
'plan_tier' => wms_is_pro_package() ? 'pro' : 'free',
'direction' => $is_return_order ? 'return' : 'outward',
];
+ if ( ! wms_is_pro_package() && in_array( static::SHIPPING_PROVIDER_ID, [ 'mondial_relay', 'chronopost' ], true )
+ && ! wms_free_label_proxy_client::begin_operation( static::SHIPPING_PROVIDER_ID, $order->get_id(), $is_return_order ) ) {
+ wms_enqueue_message( __( 'Unable to prepare this label request. Please try again.', 'wc-multishipping' ), 'error' );
+ return false;
+ }
if ( ! $parcel_class::register_parcels_from_order( $order, $is_return_order ) ) {
$telemetry::track( 'shipping_label_generation_failed', array_merge( $properties, [ 'failure_stage' => 'carrier_registration' ] ) );
return false;
@@ -517,12 +531,23 @@
$telemetry::track( 'shipping_label_generation_completed', $properties );
if ( ! wms_is_pro_package() ) {
+ if ( in_array( static::SHIPPING_PROVIDER_ID, [ 'mondial_relay', 'chronopost' ], true ) ) {
+ wms_free_label_proxy_client::complete_operation();
+ }
( new wms_free_label_proxy_client() )->clear_label_pdfs();
}
wms_enqueue_message( __( 'Label successfully generated', 'wc-multishipping' ) );
+ if ( 'mondial_relay' === static::SHIPPING_PROVIDER_ID ) {
+ $shipment = $order->get_meta( '_wms_mondial_relay_shipment_data', true );
+ $outward = $shipment['_wms_outward_parcels'] ?? [];
+ if ( 'v2' === ( $outward['_wms_label_api_version'] ?? '' ) && 'sandbox' === ( $outward['_wms_label_environment'] ?? '' ) ) {
+ return true;
+ }
+ }
+
return true;
}
@@ -563,6 +588,10 @@
return $tracking_numbers;
}
+ protected static function get_label_order_scope( $order_id ) {
+ return 'mondial_relay' === static::SHIPPING_PROVIDER_ID ? (int) $order_id : null;
+ }
+
public static function get_labels_from_order( $order_id, $is_return_order = false ) {
$tracking_numbers = self::get_tracking_numbers_from_order( $order_id, $is_return_order );
@@ -572,12 +601,13 @@
$integration_helper = static::get_integration_helper();
$label_class = $integration_helper->get_label_class();
$labels = [];
+ $label_order_scope = static::get_label_order_scope( $order_id );
foreach ( $tracking_numbers as $one_tracking_number ) {
if ( empty( $one_tracking_number ) )
continue;
- $label_information = $label_class::get_info_from_tracking_numbers( $one_tracking_number );
+ $label_information = null === $label_order_scope ? $label_class::get_info_from_tracking_numbers( $one_tracking_number ) : $label_class::get_info_from_order( $one_tracking_number, $label_order_scope );
if ( empty( $label_information ) )
continue;
--- a/wc-multishipping/inc/admin/classes/abstract_classes/abstract_settings.php
+++ b/wc-multishipping/inc/admin/classes/abstract_classes/abstract_settings.php
@@ -11,6 +11,8 @@
const SHIPPING_METHOD_ID = '';
const SHIPPING_METHOD_DISPLAYED_NAME = '';
+ const SETTINGS_NONCE_ACTION = 'wms_settings_ajax';
+
abstract public static function get_settings();
@@ -39,9 +41,23 @@
woocommerce_update_options(static::get_settings());
}
+ protected function wms_verify_settings_nonce()
+ {
+ if (check_ajax_referer(self::SETTINGS_NONCE_ACTION, 'wms_nonce', false)) return;
+
+ wp_send_json(
+ [
+ 'error' => true,
+ 'message' => __('Your session has expired. Please reload the page and try again.', 'wc-multishipping'),
+ ]
+ );
+ }
+
public function wms_export_log()
{
if (!current_user_can('administrator')) exit;
+ $this->wms_verify_settings_nonce();
+
$file = wms_get_current_log();
if (!$file || !is_file($file)) {
--- a/wc-multishipping/inc/admin/classes/chronopost/chronopost_api_helper.php
+++ b/wc-multishipping/inc/admin/classes/chronopost/chronopost_api_helper.php
@@ -170,10 +170,13 @@
'chronopost_2shop_europe',
];
+ $exclude_lockers = 'chronopost_relais' === $shipping_method_id
+ && 'yes' === get_option( 'wms_chronopost_relais_exclude_lockers', 'no' );
+
return [
'soap_method' => in_array( $shipping_method_id, $inter_methods, true ) ? 'recherchePointChronopostInter' : 'recherchePointChronopost',
'add_address' => ! in_array( $shipping_method_id, $inter_methods, true ),
- 'dropoff_type' => in_array( $shipping_method_id, $two_shop_methods, true ) ? 'C' : 'P',
+ 'dropoff_type' => ( $exclude_lockers || in_array( $shipping_method_id, $two_shop_methods, true ) ) ? 'C' : 'P',
'product_code' => $this->get_shipping_method_product_code( $shipping_method_id ),
];
}
--- a/wc-multishipping/inc/admin/classes/chronopost/chronopost_settings.php
+++ b/wc-multishipping/inc/admin/classes/chronopost/chronopost_settings.php
@@ -38,6 +38,7 @@
wp_localize_script( 'wms_chronopost_settings', 'wmsSettings', [
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'pro_nonce' => wp_create_nonce( 'wms_chronopost_pro_nonce' ),
+ 'settings_nonce' => wp_create_nonce( self::SETTINGS_NONCE_ACTION ),
'i18n_generating' => __( 'Generating keys…', 'wc-multishipping' ),
'i18n_revoking' => __( 'Disconnecting…', 'wc-multishipping' ),
'i18n_connecting' => __( 'Opening Chronopost Pro…', 'wc-multishipping' ),
@@ -169,6 +170,13 @@
"type" => "title",
"title" => __( "Pickup Points Map", "wc-multishipping" ),
],
+ [
+ "id" => "wms_chronopost_relais_exclude_lockers",
+ "type" => "checkbox",
+ "title" => __( "Chrono Relais 13 : exclure les consignes", "wc-multishipping" ),
+ "desc" => __( "Proposer uniquement les relais commerçants Pickup, sans consignes automatiques (Lockers / Pickup Station). Ce réglage concerne uniquement Chrono Relais 13, pour toutes ses zones de livraison.", "wc-multishipping" ),
+ "default" => "no",
+ ],
[
"id" => "wms_chronopost_section_pickup_points_map_type",
"type" => "select",
@@ -820,7 +828,7 @@
public function wms_chronopost_test_credentials_ajax() {
if ( ! current_user_can( 'administrator' ) )
exit;
-
+ $this->wms_verify_settings_nonce();
$account_number = wms_get_var( 'string', 'account_number', '' );
$account_password = wms_get_var( 'string', 'account_password', '' );
--- a/wc-multishipping/inc/admin/classes/config/config_class.php
+++ b/wc-multishipping/inc/admin/classes/config/config_class.php
@@ -50,6 +50,8 @@
[
'ajaxurl' => admin_url('admin-ajax.php'),
'pro_nonce' => wp_create_nonce('wms_chronopost_pro_nonce'),
+ 'mr_credentials_nonce' => wp_create_nonce('wms_mondial_relay_credentials'),
+ 'settings_nonce' => wp_create_nonce(WCMultiShippingincadminclassesabstract_classesabstract_settings::SETTINGS_NONCE_ACTION),
'pro_portal_uri' => $connection['chronopost']['portal_uri'],
'pro_portal_payload' => $connection['chronopost']['portal_payload'],
'i18n_connecting' => __('Opening Chronopost Pro…', 'wc-multishipping'),
--- a/wc-multishipping/inc/admin/classes/config/wms_onboarding.php
+++ b/wc-multishipping/inc/admin/classes/config/wms_onboarding.php
@@ -593,7 +593,6 @@
],
'mondial_relay' => [
'customer_code' => get_option( 'wms_mondial_relay_customer_code', '' ),
- 'private_key' => get_option( 'wms_mondial_relay_private_key', '' ),
'has_saved_private_key' => '' !== trim( (string) get_option( 'wms_mondial_relay_private_key', '' ) ),
'brand_code' => get_option( 'wms_mondial_relay_brand_code', '' ),
],
@@ -829,6 +828,8 @@
self::redirect_to_wizard( self::STEP_ADDRESSES );
}
+
+
private static function save_address_step() {
$draft = self::get_draft();
$selected_carriers = self::get_selected_carriers( $draft );
--- a/wc-multishipping/inc/admin/classes/free_label_generation/wms_free_label_proxy_client.php
+++ b/wc-multishipping/inc/admin/classes/free_label_generation/wms_free_label_proxy_client.php
@@ -8,6 +8,42 @@
const OPTION_REMAINING = 'wms_free_label_generation_available';
const DEFAULT_REMAINING = 3;
private static $label_pdfs = [];
+ private static $operation = null;
+
+ public static function begin_operation( $carrier, $order_id, $is_return_order = false ) {
+ $option = self::operation_option( $carrier, $order_id, $is_return_order );
+ $id = str_replace( '-', '', wp_generate_uuid4() );
+ add_option( $option, $id, '', false );
+ wp_cache_delete( 'notoptions', 'options' );
+ wp_cache_delete( $option, 'options' );
+ $id = (string) get_option( $option, '' );
+ if ( ! preg_match( '/^[a-f0-9]{32}$/', $id ) ) {
+ self::$operation = null;
+ return false;
+ }
+ self::$operation = [ 'option' => $option, 'id' => $id, 'carrier' => $carrier ];
+ return true;
+ }
+
+ private static function operation_option( $carrier, $order_id, $is_return_order ) {
+ return 'wms_free_label_intent_' . hash( 'sha256', $carrier . ':' . (int) $order_id . ':' . ( $is_return_order ? 'return' : 'outward' ) );
+ }
+
+ public static function has_pending_operation( $carrier, $order_id, $is_return_order = false ) {
+ $id = (string) get_option( self::operation_option( $carrier, $order_id, $is_return_order ), '' );
+ return 1 === preg_match( '/^[a-f0-9]{32}$/', $id );
+ }
+
+ public static function complete_operation() {
+ if ( null === self::$operation ) { return; }
+ global $wpdb;
+ $operation = self::$operation;
+ if ( $wpdb->delete( $wpdb->options, [ 'option_name' => $operation['option'], 'option_value' => $operation['id'] ] ) ) {
+ wp_cache_delete( $operation['option'], 'options' );
+ wp_cache_delete( 'notoptions', 'options' );
+ }
+ self::$operation = null;
+ }
public function get_remaining() {
$remaining = (int) get_option( $this->remaining_option_name(), self::DEFAULT_REMAINING );
@@ -40,18 +76,20 @@
);
}
+ $request_body = [
+ 'email' => $email,
+ 'carrier' => sanitize_key( $carrier ),
+ 'operation' => 'create',
+ 'label_format' => sanitize_text_field( $label_format ),
+ 'payload' => $payload,
+ ];
+ if ( null !== self::$operation && $carrier === self::$operation['carrier'] ) {
+ $request_body['request_key'] = self::$operation['id'];
+ }
$response = wp_remote_post(
self::API_URL,
[
- 'body' => wp_json_encode(
- [
- 'email' => $email,
- 'carrier' => sanitize_key( $carrier ),
- 'operation' => 'create',
- 'label_format' => sanitize_text_field( $label_format ),
- 'payload' => $payload,
- ]
- ),
+ 'body' => wp_json_encode( $request_body ),
'headers' => [
'Content-Type' => 'application/json',
'X-WMS-Security-Token' => wms_telemetry::SECURITY_TOKEN,
@@ -70,6 +108,9 @@
$http_code = (int) wp_remote_retrieve_response_code( $response );
$decoded = json_decode( wp_remote_retrieve_body( $response ) );
+ if ( 402 === $http_code && is_object( $decoded ) && 'free_label_quota_exhausted' === ( $decoded->code ?? '' ) ) {
+ self::complete_operation();
+ }
if ( is_object( $decoded ) && isset( $decoded->label_generation_available ) ) {
$this->set_remaining( $decoded->label_generation_available, $email );
--- a/wc-multishipping/inc/admin/classes/mondial_relay/label_generation/mondial_relay_v2_adapter.php
+++ b/wc-multishipping/inc/admin/classes/mondial_relay/label_generation/mondial_relay_v2_adapter.php
@@ -0,0 +1,402 @@
+<?php
+
+namespace WCMultiShippingincadminclassesmondial_relaylabel_generation;
+
+use WCMultiShippingincadminclassesmondial_relaymondial_relay_api_helper;
+
+class mondial_relay_v2_adapter implements mondial_relay_label_generator_interface {
+ const PRODUCTION_URL = 'https://connect-api.mondialrelay.com/api/shipment';
+ const SANDBOX_URL = 'https://connect-api-sandbox.mondialrelay.com/api/shipment';
+
+ public function register_parcels( $params, $context = [] ) {
+ if ( ! empty( $context['is_return_order'] ) ) {
+ return $this->error( __( 'Mondial Relay API V2 returns are not supported yet. No request was sent.', 'wc-multishipping' ), 'not_dispatched' );
+ }
+ try {
+ $request = $this->build_request( $params );
+ } catch ( InvalidArgumentException $exception ) {
+ return $this->error( $exception->getMessage(), 'not_dispatched' );
+ }
+
+ $environment = 'sandbox' === get_option( 'wms_mondial_relay_v2_environment', 'production' ) ? 'sandbox' : 'production';
+ $url = 'sandbox' === $environment ? self::SANDBOX_URL : self::PRODUCTION_URL;
+ $response = $this->post( $url, $request );
+ if ( is_wp_error( $response ) ) {
+ return $this->uncertain_response();
+ }
+ $code = (int) wp_remote_retrieve_response_code( $response );
+ if ( 200 !== $code ) {
+ return $this->uncertain_response();
+ }
+ return $this->parse_response( wp_remote_retrieve_body( $response ), $environment );
+ }
+
+ public function verify_credentials( array $credentials, $environment = 'production' ) {
+ $login = is_string( $credentials['login'] ?? null ) ? trim( $credentials['login'] ) : '';
+ $password = is_string( $credentials['password'] ?? null ) ? $credentials['password'] : '';
+ $customer_id = is_string( $credentials['customer_id'] ?? null ) ? trim( $credentials['customer_id'] ) : '';
+ if ( '' === $login || '' === $password || ! preg_match( '/^[x{9}x{A}x{D}x{20}-x{D7FF}x{E000}-x{FFFD}x{10000}-x{10FFFF}]*$/uD', $login . $password ) || ! preg_match( '/^[0-9A-Z]{2}[0-9A-Z ]{0,6}$/D', $customer_id ) ) {
+ return $this->credential_result( 'incomplete' );
+ }
+ if ( ! function_exists( 'simplexml_load_string' ) ) {
+ return $this->credential_result( 'unavailable' );
+ }
+ $xml = new SimpleXMLElement( '<?xml version="1.0" encoding="utf-8"?><ShipmentCreationRequest xmlns="http://www.example.org/Request" />' );
+ $context = $xml->addChild( 'Context' );
+ foreach ( [ 'Login' => $login, 'Password' => $password, 'CustomerId' => $customer_id, 'Culture' => 'fr-FR', 'VersionAPI' => '1.0' ] as $name => $value ) {
+ $this->text( $context, $name, $value );
+ }
+ $output = $xml->addChild( 'OutputOptions' );
+ $this->text( $output, 'OutputFormat', '10x15' );
+ $this->text( $output, 'OutputType', 'PdfUrl' );
+ $xml->addChild( 'ShipmentsList' );
+ $request = $xml->asXML();
+ $expected_metadata = [
+ 'Context' => [ 'Login' => $login, 'Password' => $password, 'CustomerId' => $customer_id, 'Culture' => 'fr-FR', 'VersionAPI' => '1.0' ],
+ 'OutputOptions' => [ 'OutputFormat' => '10x15', 'OutputType' => 'PdfUrl' ],
+ ];
+ $is_sandbox = 'sandbox' === $environment;
+ $selected_url = $is_sandbox ? self::SANDBOX_URL : self::PRODUCTION_URL;
+ $selected = $this->credential_probe( $selected_url, $request, $expected_metadata );
+ if ( 'recognised' === $selected ) {
+ return $this->credential_result( $is_sandbox ? 'test' : 'production' );
+ }
+ if ( 'rejected' !== $selected ) {
+ return $this->credential_result( 'unavailable' );
+ }
+ $alternate = $this->credential_probe( $is_sandbox ? self::PRODUCTION_URL : self::SANDBOX_URL, $request, $expected_metadata );
+ return $this->credential_result( 'recognised' === $alternate ? ( $is_sandbox ? 'production' : 'test' ) : ( 'rejected' === $alternate ? 'invalid' : 'unavailable' ) );
+ }
+
+ private function credential_result( $state ) {
+ $messages = [
+ 'production' => __( 'Production access recognised. No shipment was created. Delivery services and label creation still depend on your Mondial Relay contract.', 'wc-multishipping' ),
+ 'test' => __( 'Sandbox access recognised. No shipment was created. Test labels can be created when Sandbox is selected.', 'wc-multishipping' ),
+ 'invalid' => __( 'Mondial Relay rejected these credentials. Check the customer ID, login and password. No shipment was created.', 'wc-multishipping' ),
+ 'incomplete' => __( 'Enter a valid Mondial Relay API V2 customer ID, login and password before checking access.', 'wc-multishipping' ),
+ 'unavailable' => __( 'Unable to verify Mondial Relay access right now. Try again later. No shipment was created.', 'wc-multishipping' ),
+ ];
+ return [ 'state' => $state, 'message' => $messages[ $state ] ];
+ }
+
+ private function credential_probe( $url, $request, array $expected_metadata ) {
+ $response = $this->post( $url, $request );
+ if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
+ return 'unavailable';
+ }
+ $xml = $this->response_xml( wp_remote_retrieve_body( $response ) );
+ if ( false === $xml || 1 !== count( $xml->xpath( '/*[namespace-uri()="" or namespace-uri()="http://www.example.org/Response"]' ) ) ||
+ $xml->xpath( '//*[namespace-uri()!=namespace-uri(/*)]' ) ) {
+ return 'unavailable';
+ }
+ if ( $xml->xpath( './@*' ) || $xml->xpath( './*[local-name()!="ShipmentsList" and local-name()!="StatusList" and local-name()!="Context" and local-name()!="OutputOptions"]' ) ||
+ $xml->xpath( './*[local-name()="ShipmentsList"]/* | ./*/@* | ./*[local-name()="ShipmentsList"]/text()[normalize-space(.)!=""]' ) ||
+ 1 !== count( $xml->xpath( './*[local-name()="StatusList"]' ) ) ||
+ count( $xml->xpath( './*[local-name()="ShipmentsList"]' ) ) > 1 ) {
+ return 'unavailable';
+ }
+ foreach ( $expected_metadata as $block_name => $fields ) {
+ $blocks = $xml->xpath( './*[local-name()="' . $block_name . '"]' );
+ if ( count( $blocks ) > 1 ) {
+ return 'unavailable';
+ }
+ if ( ! $blocks ) {
+ continue;
+ }
+ if ( count( $blocks[0]->xpath( './*' ) ) !== count( $fields ) ) {
+ return 'unavailable';
+ }
+ foreach ( $fields as $name => $expected ) {
+ $values = $blocks[0]->xpath( './*[local-name()="' . $name . '"]' );
+ if ( 1 !== count( $values ) || $values[0]->xpath( './* | ./@*' ) || $expected !== (string) $values[0] ) {
+ return 'unavailable';
+ }
+ }
+ }
+ $statuses = $xml->xpath( './*[local-name()="StatusList"]/*' );
+ $codes = [];
+ foreach ( $statuses as $status ) {
+ if ( ! $this->is_well_formed_status( $status ) ) {
+ return 'unavailable';
+ }
+ $code = $this->status_value( $status, 'Code' );
+ $level = strtolower( $this->status_value( $status, 'Level' ) );
+ if ( ! in_array( $code, [ '0', '10001', '10005', '10011' ], true ) ||
+ ( '0' === $code ? '' !== $level : ! in_array( $level, '10011' === $code ? [ '', 'error', 'critical error', 'warning' ] : [ '', 'error', 'critical error' ], true ) ) ||
+ ( self::PRODUCTION_URL === $url && preg_match( '/bsandboxb/i', $this->status_value( $status, 'Message' ) ) ) ) {
+ return 'unavailable';
+ }
+ $codes[] = $code;
+ }
+ $codes = array_unique( array_diff( $codes, [ '0' ] ) );
+ if ( [ '10011' ] === array_values( $codes ) ) {
+ return 'recognised';
+ }
+ return 1 === count( $codes ) && in_array( reset( $codes ), [ '10001', '10005' ], true ) ? 'rejected' : 'unavailable';
+ }
+
+ protected function post( $url, $body ) {
+ return wp_remote_post( $url, [
+ 'headers' => [ 'Accept' => 'application/xml', 'Content-Type' => 'text/xml; charset=utf-8' ],
+ 'body' => $body,
+ 'timeout' => 45,
+ 'redirection' => 0,
+ 'sslverify' => true,
+ ] );
+ }
+
+ public function build_request( array $params ) {
+ if ( ! function_exists( 'simplexml_load_string' ) ) {
+ throw new InvalidArgumentException( __( 'The PHP SimpleXML extension is required for Mondial Relay API V2.', 'wc-multishipping' ) );
+ }
+ $login = (string) get_option( 'wms_mondial_relay_v2_login', '' );
+ $password = (string) get_option( 'wms_mondial_relay_v2_password', '' );
+ $customer_id = (string) get_option( 'wms_mondial_relay_v2_customer_id', '' );
+ if ( '' === trim( $login ) || '' === $password || ! preg_match( '/^[0-9A-Z]{2}[0-9A-Z ]{0,6}$/D', $customer_id ) ) {
+ throw new InvalidArgumentException( __( 'Complete the Mondial Relay API V2 login, password and customer ID in the carrier settings.', 'wc-multishipping' ) );
+ }
+ if ( 1 !== (int) ( $params['NbColis'] ?? 0 ) || '1' !== (string) ( $params['NbColis'] ?? '' ) ) {
+ throw new InvalidArgumentException( __( 'Mondial Relay API V2 currently supports one parcel per shipment in WcMultiShipping.', 'wc-multishipping' ) );
+ }
+ $mode = $params['ModeLiv'] ?? '';
+ if ( ! in_array( $mode, [ '24R', 'HOM', 'HOC', 'LD1', 'LDS' ], true ) || 'CCC' !== ( $params['ModeCol'] ?? '' ) ) {
+ throw new InvalidArgumentException( __( 'This delivery or collection mode is not supported by the Mondial Relay API V2 integration.', 'wc-multishipping' ) );
+ }
+ $location = '';
+ if ( '24R' === $mode ) {
+ $co