Overview

Packages

  • Mauticommerce
  • None

Classes

  • Mauticommerce
  • Mauticommerce_Admin
  • Mauticommerce_Err
  • Mauticommerce_Order

Functions

  • __
  • _x
  • get_mautic_country_list
  • get_mautic_region
  • get_woocommerce_country_list
  • mautic_is_activate_woocommerce
  • Overview
  • Package
  • Class
  1: <?php
  2: class Mauticommerce_Order extends Mauticommerce {
  3:     /**
  4:      * Instance Class
  5:      * @access private
  6:      */
  7:     private static $instance;
  8: 
  9:     /**
 10:      * text domain
 11:      * @access private
 12:      */
 13:     private static $text_domain;
 14: 
 15:     /**
 16:      * Constructer
 17:      * Set text domain on class
 18:      *
 19:      * @since 0.0.1
 20:      */
 21:     private function __construct() {
 22:         self::$text_domain = Mauticommerce::text_domain();
 23:     }
 24: 
 25:     /**
 26:      * Get Instance Class
 27:      *
 28:      * @return Mauticommerce_Admin
 29:      * @since 0.0.1
 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:      * Subscribe order information to Mautic
 41:      *
 42:      * @param string $order_id Order
 43:      * @param string $status post status (default:'new')
 44:      * @param string $new_status new post status (default:'pending')
 45:      * @access public
 46:      * @since 0.0.1
 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:      * create query to send Mautic
 56:      *
 57:      * @param WC_Order $order WooCommerce order object
 58:      * @since 0.0.1
 59:      * @access private
 60:      * @return array $query posted mautic query
 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:          * Filter the query that customize Mautic query
 80:          *
 81:          * @since 0.0.1
 82:          * @param $query default mautic query
 83:          * @param WC_Order $order WooCommerce order object
 84:          **/
 85:         return apply_filters( 'mauticommerce_query_mapping', $query, $order );
 86:     }
 87: 
 88:     /**
 89:      * Get country name
 90:      *
 91:      * @since 0.0.1
 92:      * @access private
 93:      * @param string $country_code
 94:      * @return string $country_name | Exception
 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:      * Get state name
119:      *
120:      * @since 0.0.1
121:      * @access private
122:      * @param string $country_code country code
123:      * @param string $state_code state code
124:      * @return string | Exception
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:      * Subscribe to Mautic
148:      *
149:      * @param array $query Posted mautic query
150:      * @access private
151:      * @since 0.0.1
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:      * get ip
185:      *
186:      * @access private
187:      * @return string
188:      * @since 0.0.1
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: 
API documentation generated by ApiGen