OrderItems
2 weeks ago
Factory.php
2 weeks ago
MulticurrencyHooks.php
2 weeks ago
class-wcml-table-rate-shipping.php
2 weeks ago
class-wcml-table-rate-shipping.php
252 lines
| 1 | <?php |
| 2 | |
| 3 | use WCML\Utilities\WcAdminPages; |
| 4 | use WPML\FP\Fns; |
| 5 | use WPML\FP\Logic; |
| 6 | use WPML\FP\Obj; |
| 7 | |
| 8 | class WCML_Table_Rate_Shipping implements \IWPML_Action { |
| 9 | |
| 10 | public $sitepress; |
| 11 | |
| 12 | public $woocommerce_wpml; |
| 13 | |
| 14 | private $wpdb; |
| 15 | |
| 16 | const PRIORITY_BEFORE_DELETE = 5; |
| 17 | |
| 18 | const PRIORITY_REGISTER_RATE_LABELS = 11; |
| 19 | |
| 20 | const RATE_SHIPPING_METHOD_ID = 'table_rate'; |
| 21 | const RATE_LABEL_NAME_FORMAT = 'table_rate%1$s%2$s_shipping_method_title'; |
| 22 | const RATE_ABORT_REASON_NAME_FORMAT = 'table_rate_shipping_abort_reason_%s'; |
| 23 | |
| 24 | public function __construct( SitePress $sitepress, woocommerce_wpml $woocommerce_wpml, wpdb $wpdb ) { |
| 25 | $this->sitepress = $sitepress; |
| 26 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 27 | $this->wpdb = $wpdb; |
| 28 | } |
| 29 | |
| 30 | public function add_hooks() { |
| 31 | |
| 32 | if ( ! is_admin() ) { |
| 33 | add_filter( 'get_the_terms', [ $this, 'shipping_class_id_in_default_language' ], 10, 3 ); |
| 34 | add_filter( 'woocommerce_shipping_table_rate_is_available', [ $this, 'shipping_table_rate_is_available' ], 10, 3 ); |
| 35 | } |
| 36 | |
| 37 | if ( is_admin() ) { |
| 38 | add_action( 'woocommerce_settings_shipping', [ $this, 'registerShippingRatesStrings' ], self::PRIORITY_REGISTER_RATE_LABELS ); |
| 39 | add_action( 'wp_ajax_woocommerce_table_rate_delete', [ $this, 'unregister_abort_messages_ajax' ], self::PRIORITY_BEFORE_DELETE ); |
| 40 | add_action( 'delete_product_shipping_class', [ $this, 'unregister_abort_messages_shipping_class' ], self::PRIORITY_BEFORE_DELETE ); |
| 41 | } |
| 42 | add_filter( 'woocommerce_table_rate_query_rates', [ $this, 'translate_abort_messages' ] ); |
| 43 | |
| 44 | add_filter( 'wcml_order_item_shipping_method_translators', [ $this, 'registerOrderShippingMethodTranslator' ] ); |
| 45 | } |
| 46 | |
| 47 | public function registerShippingRatesStrings() { |
| 48 | $isEditingShippingInstance = WcAdminPages::isShippingSettings() && isset( $_GET['instance_id'] ); |
| 49 | $isSavingTRSInstanceWithTableRates = isset( $_POST['rate_id'] ); |
| 50 | $canGetStoredTableRates = class_exists( '\WooCommerce\Shipping\Table_Rate\Helpers' ); |
| 51 | |
| 52 | if ( ! ( |
| 53 | $isEditingShippingInstance |
| 54 | && $isSavingTRSInstanceWithTableRates |
| 55 | && $canGetStoredTableRates |
| 56 | ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | $instanceId = (int) $_GET['instance_id']; |
| 61 | $rates = \WooCommerce\Shipping\Table_Rate\Helpers::get_shipping_rates( $instanceId, ARRAY_A ); |
| 62 | |
| 63 | if ( ! is_array( $rates ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $registerLabel = function( $rate ) use ( $instanceId ) { |
| 68 | do_action( |
| 69 | 'wpml_register_single_string', |
| 70 | WCML_WC_Shipping::STRINGS_CONTEXT, |
| 71 | sprintf( self::RATE_LABEL_NAME_FORMAT, $instanceId, $rate['rate_id'] ), |
| 72 | $rate['rate_label'] |
| 73 | ); |
| 74 | }; |
| 75 | |
| 76 | wpml_collect( $rates ) |
| 77 | ->filter( Obj::prop( 'rate_label' ) ) |
| 78 | ->each( $registerLabel ); |
| 79 | |
| 80 | $registerAbortReason = function( $rate ) { |
| 81 | do_action( |
| 82 | 'wpml_register_single_string', |
| 83 | WCML_WC_Shipping::STRINGS_CONTEXT, |
| 84 | sprintf( self::RATE_ABORT_REASON_NAME_FORMAT, $rate['rate_id'] ), |
| 85 | $rate['rate_abort_reason'] |
| 86 | ); |
| 87 | }; |
| 88 | |
| 89 | wpml_collect( $rates ) |
| 90 | ->filter( Obj::prop( 'rate_abort_reason' ) ) |
| 91 | ->each( $registerAbortReason ); |
| 92 | } |
| 93 | |
| 94 | public function shipping_class_id_in_default_language( $terms, $post_id, $taxonomy ) { |
| 95 | global $icl_adjust_id_url_filter_off; |
| 96 | |
| 97 | $is_product_object = 'product' === get_post_type( $post_id ) || 'product_variation' === get_post_type( $post_id ); |
| 98 | if ( $terms && $is_product_object && 'product_shipping_class' === $taxonomy ) { |
| 99 | |
| 100 | if ( is_admin() ) { |
| 101 | $shipp_class_language = $this->woocommerce_wpml->products->get_original_product_language( $post_id ); |
| 102 | } else { |
| 103 | $shipp_class_language = $this->sitepress->get_default_language(); |
| 104 | } |
| 105 | |
| 106 | $cache_key = md5( wp_json_encode( $terms ) ); |
| 107 | $cache_key .= ':' . $post_id . $shipp_class_language; |
| 108 | |
| 109 | $cache_group = 'trnsl_shipping_class'; |
| 110 | $cache_terms = wp_cache_get( $cache_key, $cache_group ); |
| 111 | |
| 112 | if ( $cache_terms ) { |
| 113 | return $cache_terms; |
| 114 | } |
| 115 | |
| 116 | foreach ( $terms as $k => $term ) { |
| 117 | |
| 118 | $shipping_class_id = apply_filters( 'wpml_object_id', $term->term_id, 'product_shipping_class', false, $shipp_class_language ); |
| 119 | |
| 120 | $icl_adjust_id_url_filter = $icl_adjust_id_url_filter_off; |
| 121 | $icl_adjust_id_url_filter_off = true; |
| 122 | |
| 123 | $terms[ $k ] = get_term( $shipping_class_id, 'product_shipping_class' ); |
| 124 | |
| 125 | $icl_adjust_id_url_filter_off = $icl_adjust_id_url_filter; |
| 126 | } |
| 127 | |
| 128 | wp_cache_set( $cache_key, $terms, $cache_group ); |
| 129 | } |
| 130 | |
| 131 | return $terms; |
| 132 | } |
| 133 | |
| 134 | public function show_pointer_info() { |
| 135 | $pointerFactory = new WCML\PointerUi\Factory(); |
| 136 | $dashboardUrl = \WCML\Utilities\AdminUrl::getWPMLTMDashboardStringDomain( \WCML_WC_Shipping::STRINGS_CONTEXT ); |
| 137 | |
| 138 | $pointerFactory |
| 139 | ->create( [ |
| 140 | 'content' => sprintf( |
| 141 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 142 | esc_html__( 'To translate the Method Title, go to the %1$sTranslation Dashboard%2$s.', 'woocommerce-multilingual' ), |
| 143 | '<a href="' . esc_url( $dashboardUrl ) . '">', |
| 144 | '</a>' |
| 145 | ), |
| 146 | 'selectorId' => 'woocommerce_table_rate_title', |
| 147 | 'docLink' => WCML_Tracking_Link::getWcmlTableRateShippingDoc(), |
| 148 | ] ) |
| 149 | ->show(); |
| 150 | |
| 151 | $pointerFactory |
| 152 | ->create( [ |
| 153 | 'content' => sprintf( |
| 154 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 155 | esc_html__( 'To translate Labels, go to the %1$sTranslation Dashboard%2$s.', 'woocommerce-multilingual' ), |
| 156 | '<a href="' . esc_url( $dashboardUrl ) . '">', |
| 157 | '</a>' |
| 158 | ), |
| 159 | 'selectorId' => 'shipping_rates', |
| 160 | 'docLink' => WCML_Tracking_Link::getWcmlTableRateShippingDoc(), |
| 161 | ] ) |
| 162 | ->show(); |
| 163 | } |
| 164 | |
| 165 | public function unregister_abort_messages_ajax() { |
| 166 | check_ajax_referer( 'delete-rate', 'security' ); |
| 167 | |
| 168 | wpml_collect( (array) Obj::prop( 'rate_id', $_POST ) ) |
| 169 | ->map( Fns::unary( 'intval' ) ) |
| 170 | ->map( [ $this, 'unregister_abort_messages' ] ); |
| 171 | } |
| 172 | |
| 173 | public function unregister_abort_messages_shipping_class( $term_id ) { |
| 174 | $table = $this->wpdb->prefix . 'woocommerce_shipping_table_rates'; |
| 175 | $query = $this->wpdb->prepare( |
| 176 | "SELECT rate_id FROM $table WHERE rate_class=%d", |
| 177 | [ $term_id ] |
| 178 | ); |
| 179 | wpml_collect( $this->wpdb->get_col( $query ) ) |
| 180 | ->map( [ $this, 'unregister_abort_messages' ] ); |
| 181 | } |
| 182 | |
| 183 | public function translate_abort_messages( $rates ) { |
| 184 | $translateAbortReason = function( $rate ) { |
| 185 | return Obj::assoc( |
| 186 | 'rate_abort_reason', |
| 187 | apply_filters( |
| 188 | 'wpml_translate_single_string', |
| 189 | $rate->rate_abort_reason, |
| 190 | WCML_WC_Shipping::STRINGS_CONTEXT, |
| 191 | sprintf( self::RATE_ABORT_REASON_NAME_FORMAT, $rate->rate_id ) |
| 192 | ), |
| 193 | $rate |
| 194 | ); |
| 195 | }; |
| 196 | |
| 197 | return wpml_collect( $rates ) |
| 198 | ->map( Logic::ifElse( Obj::prop( 'rate_abort_reason' ), $translateAbortReason, Fns::identity() ) ) |
| 199 | ->toArray(); |
| 200 | } |
| 201 | |
| 202 | public function unregister_abort_messages( $rate_id ) { |
| 203 | icl_unregister_string( |
| 204 | WCML_WC_Shipping::STRINGS_CONTEXT, |
| 205 | sprintf( self::RATE_ABORT_REASON_NAME_FORMAT, $rate_id ) |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | public function shipping_table_rate_is_available( $available, $package, $object ) { |
| 210 | |
| 211 | add_filter( |
| 212 | 'option_woocommerce_table_rate_priorities_' . $object->instance_id, |
| 213 | [ $this, 'filter_table_rate_priorities' ] |
| 214 | ); |
| 215 | remove_filter( |
| 216 | 'woocommerce_shipping_table_rate_is_available', |
| 217 | [ $this, 'shipping_table_rate_is_available' ], |
| 218 | 10 |
| 219 | ); |
| 220 | |
| 221 | $available = $object->is_available( $package ); |
| 222 | |
| 223 | add_filter( |
| 224 | 'woocommerce_shipping_table_rate_is_available', |
| 225 | [ $this, 'shipping_table_rate_is_available' ], |
| 226 | 10, |
| 227 | 3 |
| 228 | ); |
| 229 | |
| 230 | return $available; |
| 231 | } |
| 232 | |
| 233 | public function filter_table_rate_priorities( $priorities ) { |
| 234 | |
| 235 | foreach ( $priorities as $slug => $priority ) { |
| 236 | |
| 237 | $shipping_class_term = get_term_by( 'slug', $slug, 'product_shipping_class' ); |
| 238 | if ( $shipping_class_term->slug !== $slug ) { |
| 239 | unset( $priorities[ $slug ] ); |
| 240 | $priorities[ $shipping_class_term->slug ] = $priority; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return $priorities; |
| 245 | } |
| 246 | |
| 247 | public function registerOrderShippingMethodTranslator( $translators ) { |
| 248 | $translators[ self::RATE_SHIPPING_METHOD_ID ] = \WCML\Compatibility\TableRateShipping\OrderItems\ShippingRate::class; |
| 249 | return $translators; |
| 250 | } |
| 251 | } |
| 252 |