1: <?php
2: class Mauticommerce_Order extends Mauticommerce {
3: 4: 5: 6:
7: private static $instance;
8:
9: 10: 11: 12:
13: private static $text_domain;
14:
15: 16: 17: 18: 19: 20:
21: private function __construct() {
22: self::$text_domain = Mauticommerce::text_domain();
23: }
24:
25: 26: 27: 28: 29: 30:
31: public static function get_instance() {
32: if ( ! isset( self::$instance ) ) {
33: $c = __CLASS__;
34: self::$instance = new $c();
35: }
36: return self::$instance;
37: }
38:
39: 40: 41: 42: 43: 44: 45: 46: 47:
48: public function subscribe_to_mautic( $order_id, $status = 'new', $new_status = 'pending' ) {
49: $order = wc_get_order( $order_id );
50: $query = $this->_create_query( $order );
51: $this->_subscribe( $query );
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61:
62: private function _create_query( WC_Order $order ) {
63: $query = array(
64: 'address1' => $order->billing_address_1,
65: 'address2' => $order->billing_address_2,
66: 'city' => $order->billing_city,
67: 'company' => $order->billing_company,
68: 'country' => $this->_get_country_name( $order->billing_country ),
69: 'email' => $order->billing_email,
70: 'firstname' => $order->billing_first_name,
71: 'lastname' => $order->billing_last_name,
72: 'phone' => $order->billing_phone,
73: 'zipcode' => $order->billing_postcode,
74: 'state' => $this->_get_states_name( $order->billing_country, $order->billing_state ),
75: 'order_id' => $order->id,
76: );
77:
78: 79: 80: 81: 82: 83: 84:
85: return apply_filters( 'mauticommerce_query_mapping', $query, $order );
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96:
97: private function _get_country_name( $country_code ) {
98: $countries = wp_remote_get( path_join( MAUTICOMMERCE_URL, 'inc/assets/json/country.json' ) );
99: try {
100: if ( is_wp_error( $countries ) ) {
101: throw new Exception( 'invalid json data.' );
102: }
103: $json = json_decode( $countries['body'], true );
104: if ( ! isset( $json[ $country_code ] ) ) {
105: throw new Exception( "Country[{$country_code}] is not found." );
106: }
107: $country_name = $json[ $country_code ];
108: return $country_name;
109: } catch ( Exception $e ) {
110: $msg = 'Mauticommerce Error:' . $e->getMessage();
111: error_log( $msg );
112: $wc_country = new WC_Countries();
113: return $wc_country->get_countries()[ $country_code ];
114: }
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125:
126: private function _get_states_name( $country_code, $state_code ) {
127: $states = wp_remote_get( path_join( MAUTICOMMERCE_URL, 'inc/assets/json/states.json' ) );
128: try {
129: if ( is_wp_error( $states ) ) {
130: throw new Exception( 'invalid json data.' );
131: }
132: $json = json_decode( $states['body'], true );
133: if ( ! isset( $json[ $country_code ][ $state_code ] ) ) {
134: throw new Exception( "Country[{$country_code}] is not found." );
135: }
136: $state_name = $json[ $country_code ][ $state_code ];
137: return $state_name;
138: } catch ( Exception $e ) {
139: $msg = 'Mauticommerce Error:' . $e->getMessage();
140: error_log( $msg );
141: $wc_country = new WC_Countries();
142: return $wc_country->get_states( $country_code )[ $state_code ];
143: }
144: }
145:
146: 147: 148: 149: 150: 151: 152:
153: private function _subscribe( $query ) {
154: $ip = $this->_get_ip();
155: $settings = get_option( 'mauticommece_settings' );
156: if ( ! isset( $query['return'] ) ) {
157: $query['return'] = get_home_url();
158: }
159: $query['formId'] = $settings['form_id'];
160: $data = array(
161: 'mauticform' => $query,
162: );
163: $url = path_join( $settings['url'], "form/submit?formId={$settings['form_id']}" );
164:
165: $response = wp_remote_post(
166: $url,
167: array(
168: 'method' => 'POST',
169: 'timeout' => 45,
170: 'headers' => array(
171: 'X-Forwarded-For' => $ip,
172: ),
173: 'body' => $data,
174: 'cookies' => array(),
175: )
176: );
177: if ( is_wp_error( $response ) ) {
178: $error_message = $response->get_error_message();
179: error_log( "MautiCommerce Error: $error_message" );
180: }
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: private function _get_ip() {
191: $ip_list = [
192: 'REMOTE_ADDR',
193: 'HTTP_CLIENT_IP',
194: 'HTTP_X_FORWARDED_FOR',
195: 'HTTP_X_FORWARDED',
196: 'HTTP_X_CLUSTER_CLIENT_IP',
197: 'HTTP_FORWARDED_FOR',
198: 'HTTP_FORWARDED',
199: ];
200: foreach ( $ip_list as $key ) {
201: if ( ! isset( $_SERVER[ $key ] ) ) {
202: continue;
203: }
204: $ip = esc_attr( $_SERVER[ $key ] );
205: if ( ! strpos( $ip, ',' ) ) {
206: $ips = explode( ',', $ip );
207: foreach ( $ips as &$val ) {
208: $val = trim( $val );
209: }
210: $ip = end( $ips );
211: }
212: $ip = trim( $ip );
213: break;
214: }
215: return $ip;
216: }
217: }
218: