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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / inc / class-wcml-wc-shipping.php
woocommerce-multilingual / inc Last commit date
admin-menus 4 weeks ago currencies 4 weeks ago template-classes 4 weeks ago translation-editor 4 weeks ago class-wcml-ajax-setup.php 4 weeks ago class-wcml-attributes.php 4 weeks ago class-wcml-capabilities.php 4 weeks ago class-wcml-cart-removed-items-widget.php 1 year ago class-wcml-cart-switch-lang-functions.php 4 weeks ago class-wcml-cart-sync-warnings.php 4 weeks ago class-wcml-cart.php 4 weeks ago class-wcml-comments.php 4 weeks ago class-wcml-compatibility.php 4 weeks ago class-wcml-coupons.php 4 weeks ago class-wcml-dependencies.php 4 weeks ago class-wcml-emails.php 4 weeks ago class-wcml-endpoints.php 4 weeks ago class-wcml-install.php 4 weeks ago class-wcml-languages-upgrader.php 4 weeks ago class-wcml-locale.php 4 weeks ago class-wcml-orders.php 4 weeks ago class-wcml-products-screen-options.php 4 weeks ago class-wcml-products.php 4 weeks ago class-wcml-reports.php 4 weeks ago class-wcml-requests.php 4 weeks ago class-wcml-resources.php 4 weeks ago class-wcml-store-pages.php 4 weeks ago class-wcml-terms.php 4 weeks ago class-wcml-tp-support.php 4 weeks ago class-wcml-troubleshooting.php 4 weeks ago class-wcml-upgrade.php 4 weeks ago class-wcml-url-translation.php 4 weeks ago class-wcml-wc-gateways.php 4 weeks ago class-wcml-wc-shipping.php 4 weeks ago class-wcml-wc-strings.php 4 weeks ago class-wcml-widgets.php 4 weeks ago constants.php 4 weeks ago functions-pure.php 4 weeks ago functions.php 4 weeks ago installer-loader.php 4 weeks ago missing-php-functions.php 4 weeks ago wcml-core-functions.php 4 weeks ago wcml-switch-lang-request.php 4 weeks ago
class-wcml-wc-shipping.php
260 lines
1 <?php
2
3 use WPML\FP\Fns;
4
5 class WCML_WC_Shipping {
6
7 const STRINGS_CONTEXT = 'admin_texts_woocommerce_shipping';
8 const NAME_SUFFIX = '_shipping_method_title';
9
10 private $current_language;
11 private $sitepress;
12
13 public function __construct( \WPML\Core\ISitePress $sitepress ) {
14 $this->sitepress = $sitepress;
15
16 $this->current_language = $this->sitepress->get_current_language();
17 if ( $this->current_language == 'all' ) {
18 $this->current_language = $this->sitepress->get_default_language();
19 }
20
21 }
22
23 public function add_hooks() {
24 add_action( 'wp_ajax_woocommerce_shipping_zone_methods_save_settings', [ $this, 'save_shipping_zone_method_from_ajax' ], 9 );
25 add_action( 'icl_save_term_translation', [ $this, 'sync_class_costs_for_new_shipping_classes' ], 100, 2 );
26 add_action( 'wp_ajax_woocommerce_shipping_zone_methods_save_settings', [ $this, 'update_woocommerce_shipping_settings_for_class_costs_from_ajax' ], 9 );
27
28 add_filter( 'woocommerce_package_rates', [ $this, 'translate_shipping_methods_in_package' ] );
29 add_filter( 'pre_update_option_woocommerce_flat_rate_settings', [ $this, 'update_woocommerce_shipping_settings_for_class_costs' ] );
30 add_filter( 'pre_update_option_woocommerce_international_delivery_settings', [ $this, 'update_woocommerce_shipping_settings_for_class_costs' ] );
31 add_filter( 'woocommerce_shipping_flat_rate_instance_option', [ $this, 'get_original_shipping_class_rate' ], 10, 3 );
32
33 add_action( 'woocommerce_load_shipping_methods', Fns::withoutRecursion(
34 Fns::identity(),
35 [ $this, 'shipping_methods_filters' ]
36 ), PHP_INT_MAX );
37 }
38
39 public function shipping_methods_filters() {
40
41 $shipping_methods = WC()->shipping()->get_shipping_methods();
42
43 foreach ( $shipping_methods as $shipping_method ) {
44 if ( empty( $shipping_method->id ) ) {
45 continue;
46 }
47
48 $shipping_method_id = $shipping_method->id;
49
50 add_filter(
51 'woocommerce_shipping_' . $shipping_method_id . '_instance_settings_values',
52 [
53 $this,
54 'register_zone_shipping_strings',
55 ],
56 9,
57 2
58 );
59 add_filter(
60 'option_woocommerce_' . $shipping_method_id . '_settings',
61 [
62 $this,
63 'translate_shipping_strings',
64 ],
65 9,
66 2
67 );
68 }
69 }
70
71 public function save_shipping_zone_method_from_ajax() {
72 if ( ! isset( $_POST['wc_shipping_zones_nonce'] ) ) {
73 return;
74 }
75 if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) {
76 return;
77 }
78 if ( ! current_user_can( 'manage_woocommerce' ) ) {
79 return;
80 }
81
82 foreach ( $_POST['data'] as $key => $value ) {
83 if ( strstr( $key, '_title' ) ) {
84 $shipping_id = str_replace( 'woocommerce_', '', $key );
85 $shipping_id = str_replace( '_title', '', $shipping_id );
86 $this->register_shipping_title( $shipping_id . $_POST['instance_id'], $value );
87 break;
88 }
89 }
90 }
91
92 public function register_zone_shipping_strings( $instance_settings, $object ) {
93
94 if ( ! empty( $instance_settings['title'] ) ) {
95 $this->register_shipping_title( $object->id . $object->instance_id, $instance_settings['title'] );
96
97 $instance_settings = $this->sync_flat_rate_class_cost( $object->get_post_data(), $instance_settings );
98 }
99
100 return $instance_settings;
101 }
102
103 public function register_shipping_title( $shipping_method_id, $title ) {
104 do_action( 'wpml_register_single_string', self::STRINGS_CONTEXT, $shipping_method_id . self::NAME_SUFFIX, $title );
105 }
106
107 public function translate_shipping_strings( $value, $option = false ) {
108
109 if ( $option && isset( $value['enabled'] ) && $value['enabled'] == 'no' ) {
110 return $value;
111 }
112
113 $shipping_id = str_replace( 'woocommerce_', '', $option );
114 $shipping_id = str_replace( '_settings', '', $shipping_id );
115
116 if ( isset( $value['title'] ) ) {
117 $value['title'] = $this->translate_shipping_method_title( $value['title'], $shipping_id );
118 }
119
120 return $value;
121 }
122
123 public function translate_shipping_methods_in_package( $available_methods ) {
124
125 foreach ( $available_methods as $key => $method ) {
126 if ( apply_filters( 'wcml_translate_shipping_method_in_package', true, $key, $method ) ) {
127 $available_methods[ $key ]->label = $this->translate_shipping_method_title( $method->label, $key );
128 }
129 }
130
131 return apply_filters( 'wcml_translated_package_rates', $available_methods );
132 }
133
134 public function translate_shipping_method_title( $title, $shipping_id, $language = false ) {
135
136 $is_edit_order = did_action( 'admin_init' ) && did_action( 'current_screen' ) && ( \WCML\Orders\Helper::isOrderEditAdminScreen() || \WCML\Orders\Helper::isOrderCreateAdminScreen() );
137 $translate_title = apply_filters( 'wcml_should_translate_shipping_method_title', ! is_admin() || $is_edit_order, $title, $shipping_id, $language );
138
139 if ( $translate_title ) {
140
141 $shipping_id = str_replace( ':', '', $shipping_id );
142
143 $translated_title = apply_filters(
144 'wpml_translate_single_string',
145 $title,
146 self::STRINGS_CONTEXT,
147 $shipping_id . self::NAME_SUFFIX,
148 $language ?: $this->current_language
149 );
150
151 return $translated_title ?: $title;
152 }
153
154 return $title;
155 }
156
157 public function sync_class_costs_for_new_shipping_classes( $original_tax, $result ) {
158 if ( $original_tax->taxonomy == 'product_shipping_class' ) {
159
160 $settings = get_option( 'woocommerce_flat_rate_settings' );
161 if ( is_array( $settings ) ) {
162 update_option( 'woocommerce_flat_rate_settings', $this->update_woocommerce_shipping_settings_for_class_costs( $settings ) );
163 }
164
165 $settings = get_option( 'woocommerce_international_delivery_settings' );
166 if ( is_array( $settings ) ) {
167 update_option( 'woocommerce_international_delivery_settings', $this->update_woocommerce_shipping_settings_for_class_costs( $settings ) );
168 }
169 }
170 }
171
172 public function update_woocommerce_shipping_settings_for_class_costs( $settings ) {
173 remove_filter( 'get_term', [ $this->sitepress, 'get_term_adjust_id' ], 1 );
174 foreach ( $settings as $setting_key => $value ) {
175
176 if ( substr( $setting_key, 0, 11 ) == 'class_cost_' ) {
177
178 $shipp_class_key = substr( $setting_key, 11 );
179
180 if ( is_numeric( $shipp_class_key ) ) {
181 $shipp_class = get_term( $shipp_class_key, 'product_shipping_class' );
182 } else {
183 $shipp_class = get_term_by( 'slug', $shipp_class_key, 'product_shipping_class' );
184 }
185 if ( isset( $shipp_class->term_taxonomy_id ) ) {
186 $trid = $this->sitepress->get_element_trid( $shipp_class->term_taxonomy_id, 'tax_product_shipping_class' );
187
188 $translations = $this->sitepress->get_element_translations( $trid, 'tax_product_shipping_class' );
189
190 foreach ( $translations as $translation ) {
191
192 $tr_shipp_class = get_term_by( 'term_taxonomy_id', $translation->element_id, 'product_shipping_class' );
193
194 if ( is_numeric( $shipp_class_key ) ) {
195 $settings[ 'class_cost_' . $tr_shipp_class->term_id ] = $value;
196 } else {
197 $settings[ 'class_cost_' . $tr_shipp_class->slug ] = $value;
198 }
199 }
200 }
201 }
202 }
203 add_filter( 'get_term', [ $this->sitepress, 'get_term_adjust_id' ], 1 );
204
205 return $settings;
206 }
207
208 public function update_woocommerce_shipping_settings_for_class_costs_from_ajax() {
209 if ( ! isset( $_POST['wc_shipping_zones_nonce'] ) ) {
210 return;
211 }
212 if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) {
213 return;
214 }
215 if ( ! current_user_can( 'manage_woocommerce' ) ) {
216 return;
217 }
218
219 if ( isset( $_POST['data']['woocommerce_flat_rate_type'] ) && $_POST['data']['woocommerce_flat_rate_type'] == 'class' ) {
220
221 $flat_rate_setting_id = 'woocommerce_flat_rate_' . $_POST['data']['instance_id'] . '_settings';
222 $settings = get_option( $flat_rate_setting_id, true );
223
224 $settings = $this->sync_flat_rate_class_cost( $_POST['data'], $settings );
225
226 update_option( $flat_rate_setting_id, $settings );
227 }
228 }
229
230 public function sync_flat_rate_class_cost( $data, $inst_settings ) {
231
232 $settings = [];
233 foreach ( $data as $key => $value ) {
234 if ( 0 === strpos( $key, 'woocommerce_flat_rate_class_cost_' ) ) {
235 $limit = strlen( 'woocommerce_flat_rate_' );
236 $settings[ substr( $key, $limit ) ] = stripslashes( $value );
237 }
238 }
239
240 $updated_costs_settings = $this->update_woocommerce_shipping_settings_for_class_costs( $settings );
241
242 if ( is_array( $inst_settings ) ) {
243 return array_merge( $inst_settings, $updated_costs_settings );
244 }
245 return $updated_costs_settings;
246 }
247
248 public function get_original_shipping_class_rate( $rate, $class_name, $shipping_method ) {
249 if ( ! $rate && 'class_cost_' === substr( $class_name, 0, 11 ) ) {
250 $original_class_id = $this->sitepress->term_translations()->get_original_element( substr( $class_name, 11 ) );
251 if ( $original_class_id && isset( $shipping_method->instance_settings[ 'class_cost_' . $original_class_id ] ) ) {
252 return $shipping_method->instance_settings[ 'class_cost_' . $original_class_id ];
253 }
254 }
255
256 return $rate;
257 }
258
259 }
260