woocommerce-multilingual
/
compatibility
/
WcCheckoutFieldEditor
/
class-wcml-checkout-field-editor.php
class-wcml-checkout-field-editor.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Checkout_Field_Editor implements \IWPML_Action { |
| 4 | |
| 5 | protected $package, $billing, $shipping, $additional; |
| 6 | |
| 7 | public function __construct( $package = false ) { |
| 8 | $this->package = $package ? $package : (object) [ |
| 9 | 'kind' => 'WooCommerce Add-On', |
| 10 | 'kind_slug' => 'woocommerce-add-on', |
| 11 | 'name' => 'checkout-field-editor', |
| 12 | 'title' => 'WooCommerce Checkout Field Editor', |
| 13 | ]; |
| 14 | } |
| 15 | |
| 16 | public function add_hooks() { |
| 17 | global $supress_field_modification; |
| 18 | if ( ! is_admin() && ! $supress_field_modification ) { |
| 19 | add_filter( 'pre_option_wc_fields_billing', [ $this, 'get_billing' ] ); |
| 20 | add_filter( 'pre_option_wc_fields_shipping', [ $this, 'get_shipping' ] ); |
| 21 | add_filter( 'pre_option_wc_fields_additional', [ $this, 'get_additional' ] ); |
| 22 | } |
| 23 | add_filter( 'pre_update_option_wc_fields_billing', [ $this, 'register_fields' ] ); |
| 24 | add_filter( 'pre_update_option_wc_fields_shipping', [ $this, 'register_fields' ] ); |
| 25 | add_filter( 'pre_update_option_wc_fields_additional', [ $this, 'register_fields' ] ); |
| 26 | } |
| 27 | |
| 28 | private function get_exclude_fields() { |
| 29 | |
| 30 | $exclude_fields = [ |
| 31 | 'shipping_address_1', |
| 32 | 'shipping_city', |
| 33 | 'shipping_state', |
| 34 | 'shipping_postcode', |
| 35 | 'billing_address_1', |
| 36 | 'billing_city', |
| 37 | 'billing_state', |
| 38 | 'billing_postcode', |
| 39 | ]; |
| 40 | |
| 41 | return apply_filters( 'wcml_cfe_exclude_fields_to_register', $exclude_fields ); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | public function register_fields( $fields ) { |
| 46 | foreach ( $fields as $string_name => $field ) { |
| 47 | |
| 48 | if ( in_array( $string_name, $this->get_exclude_fields() ) ) { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | if ( ! empty( $field['label'] ) ) { |
| 53 | do_action( |
| 54 | 'wpml_register_string', |
| 55 | $field['label'], |
| 56 | "{$string_name}_label", |
| 57 | $this->package, |
| 58 | "{$string_name} Label", |
| 59 | $this->package->kind |
| 60 | ); |
| 61 | } |
| 62 | if ( ! empty( $field['placeholder'] ) ) { |
| 63 | do_action( |
| 64 | 'wpml_register_string', |
| 65 | $field['placeholder'], |
| 66 | "{$string_name}_placeholder", |
| 67 | $this->package, |
| 68 | "{$string_name} Placeholder", |
| 69 | $this->package->kind |
| 70 | ); |
| 71 | } |
| 72 | if ( ! empty( $field['options'] ) ) { |
| 73 | $i = 1; |
| 74 | foreach ( $field['options'] as $option ) { |
| 75 | do_action( |
| 76 | 'wpml_register_string', |
| 77 | $option, |
| 78 | "{$string_name}_option_{$i}", |
| 79 | $this->package, |
| 80 | "{$string_name} Option {$i}", |
| 81 | $this->package->kind |
| 82 | ); |
| 83 | $i++; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return $fields; |
| 88 | } |
| 89 | |
| 90 | public function translate_fields( $fields ) { |
| 91 | foreach ( $fields as $string_name => &$field ) { |
| 92 | |
| 93 | if ( in_array( $string_name, $this->get_exclude_fields() ) ) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | if ( ! empty( $field['label'] ) ) { |
| 98 | $field['label'] = apply_filters( |
| 99 | 'wpml_translate_string', |
| 100 | $field['label'], |
| 101 | "{$string_name}_label", |
| 102 | $this->package |
| 103 | ); |
| 104 | } |
| 105 | if ( ! empty( $field['placeholder'] ) ) { |
| 106 | $field['placeholder'] = apply_filters( |
| 107 | 'wpml_translate_string', |
| 108 | $field['label'], |
| 109 | "{$string_name}_placeholder", |
| 110 | $this->package |
| 111 | ); |
| 112 | } |
| 113 | if ( ! empty( $field['options'] ) ) { |
| 114 | $i = 1; |
| 115 | foreach ( $field['options'] as $k => $option ) { |
| 116 | $field['options'][ $k ] = apply_filters( |
| 117 | 'wpml_translate_string', |
| 118 | $option, |
| 119 | "{$string_name}_option_{$i}", |
| 120 | $this->package |
| 121 | ); |
| 122 | $i++; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return $fields; |
| 127 | } |
| 128 | |
| 129 | public function get_billing() { |
| 130 | if ( is_null( $this->billing ) ) { |
| 131 | remove_filter( 'pre_option_wc_fields_billing', [ $this, 'get_billing' ] ); |
| 132 | $this->billing = $this->translate_fields( get_option( 'wc_fields_billing', [] ) ); |
| 133 | add_filter( 'pre_option_wc_fields_billing', [ $this, 'get_billing' ] ); |
| 134 | } |
| 135 | return $this->billing; |
| 136 | } |
| 137 | |
| 138 | public function get_shipping() { |
| 139 | if ( is_null( $this->shipping ) ) { |
| 140 | remove_filter( 'pre_option_wc_fields_shipping', [ $this, 'get_shipping' ] ); |
| 141 | $this->shipping = $this->translate_fields( get_option( 'wc_fields_shipping', [] ) ); |
| 142 | add_filter( 'pre_option_wc_fields_shipping', [ $this, 'get_shipping' ] ); |
| 143 | } |
| 144 | return $this->shipping; |
| 145 | } |
| 146 | |
| 147 | public function get_additional() { |
| 148 | if ( is_null( $this->additional ) ) { |
| 149 | remove_filter( 'pre_option_wc_fields_additional', [ $this, 'get_additional' ] ); |
| 150 | $this->additional = $this->translate_fields( get_option( 'wc_fields_additional', [] ) ); |
| 151 | add_filter( 'pre_option_wc_fields_additional', [ $this, 'get_additional' ] ); |
| 152 | } |
| 153 | return $this->additional; |
| 154 | } |
| 155 | } |
| 156 |