functions.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | function flexible_shipping_get_all_shipping_methods() { |
| 4 | $all_shipping_methods = WC()->shipping()->load_shipping_methods(); |
| 5 | |
| 6 | return $all_shipping_methods; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Returns FS methods selected in cart. |
| 11 | * |
| 12 | * @param string $shipping_method_integration . |
| 13 | * |
| 14 | * @return bool |
| 15 | */ |
| 16 | function flexible_shipping_method_selected_in_cart( $shipping_method_integration ) { |
| 17 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
| 18 | if ( is_array( $chosen_shipping_methods ) ) { |
| 19 | $all_shipping_methods = flexible_shipping_get_all_shipping_methods(); |
| 20 | $flexible_shipping = $all_shipping_methods['flexible_shipping']; |
| 21 | $flexible_shipping_rates = $flexible_shipping->get_all_rates(); |
| 22 | foreach ( $chosen_shipping_methods as $id => $shipping ) { |
| 23 | if ( isset( $flexible_shipping_rates[ $shipping ] ) ) { |
| 24 | $shipping_method = $flexible_shipping_rates[ $shipping ]; |
| 25 | if ( isset( $shipping_method['method_integration'] ) && $shipping_method['method_integration'] === $shipping_method_integration ) { |
| 26 | return $shipping_method; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | function flexible_shipping_method_selected( $order, $shipping_method_integration ) { |
| 36 | if ( is_numeric( $order ) ) { |
| 37 | $order = wc_get_order( $order ); |
| 38 | } |
| 39 | $shippings = $order->get_shipping_methods(); |
| 40 | $all_shipping_methods = flexible_shipping_get_all_shipping_methods(); |
| 41 | if ( isset( $all_shipping_methods['flexible_shipping'] ) ) { |
| 42 | $flexible_shipping_rates = $all_shipping_methods['flexible_shipping']->get_all_rates(); |
| 43 | foreach ( $shippings as $id => $shipping ) { |
| 44 | if ( isset( $flexible_shipping_rates[ $shipping['method_id'] ] ) ) { |
| 45 | $shipping_method = $flexible_shipping_rates[ $shipping['method_id'] ]; |
| 46 | if ( $shipping_method['method_integration'] == $shipping_method_integration ) { |
| 47 | return $shipping_method; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | function flexible_shipping_get_integration_for_method( $method_id ) { |
| 57 | $all_shipping_methods = flexible_shipping_get_all_shipping_methods(); |
| 58 | if ( isset( $all_shipping_methods['flexible_shipping'] ) ) { |
| 59 | $flexible_shipping_rates = $all_shipping_methods['flexible_shipping']->get_all_rates(); |
| 60 | if ( isset( $flexible_shipping_rates[ $method_id ] ) ) { |
| 61 | return $flexible_shipping_rates[ $method_id ]['method_integration']; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if ( ! function_exists( 'wpdesk_redirect' ) ) { |
| 69 | function wpdesk_redirect( $redirect ) { |
| 70 | if ( headers_sent() ) { |
| 71 | ?> |
| 72 | <span> |
| 73 | <?php |
| 74 | echo wp_kses_post( |
| 75 | sprintf( |
| 76 | // Translators: redirect URL. |
| 77 | __( 'Redirecting. If page not redirects click %1$s here %2$s.', 'flexible-shipping' ), |
| 78 | '<a href="' . esc_url( $redirect ) . '" >', |
| 79 | '</a>' |
| 80 | ) |
| 81 | ); |
| 82 | ?> |
| 83 | </span> |
| 84 | <script> |
| 85 | parent.location.replace('<?php echo esc_url( $redirect ); ?>'); |
| 86 | </script> |
| 87 | <?php |
| 88 | } else { |
| 89 | wp_safe_redirect( $redirect ); |
| 90 | } |
| 91 | exit; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ( ! function_exists( 'wpdesk__' ) ) { |
| 96 | function wpdesk__( $text, $domain ) { |
| 97 | if ( function_exists( 'pll__' ) ) { |
| 98 | return pll__( $text ); |
| 99 | } |
| 100 | |
| 101 | return __( $text, $domain ); // phpcs:ignore. |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if ( ! function_exists( 'wpdesk__e' ) ) { |
| 106 | function wpdesk__e( $text, $domain ) { |
| 107 | echo wpdesk__( $text, $domain ); // phpcs:ignore. |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | if ( ! function_exists( 'wpdesk_is_plugin_active' ) ) { |
| 113 | function wpdesk_is_plugin_active( $plugin_file ) { |
| 114 | $active_plugins = (array) get_option( 'active_plugins', [] ); |
| 115 | |
| 116 | if ( is_multisite() ) { |
| 117 | $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', [] ) ); |
| 118 | } |
| 119 | |
| 120 | return in_array( $plugin_file, $active_plugins ) || array_key_exists( $plugin_file, $active_plugins ); |
| 121 | } |
| 122 | } |
| 123 |