Changeset 2580672
- Timestamp:
- 08/10/2021 06:52:28 AM (5 years ago)
- Location:
- mplus-intercom-subscription/trunk
- Files:
-
- 7 edited
-
assets/js/mplus-intercom-subscription-public.js (modified) (1 diff)
-
classes/mplus-intercom-subscription-handler.php (modified) (6 diffs)
-
includes/class-mplus-intercom-subscription-core.php (modified) (2 diffs)
-
includes/class-mplus-intercom-subscription-public.php (modified) (3 diffs)
-
includes/helper-function.php (modified) (2 diffs)
-
mplus-intercom-subscription.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mplus-intercom-subscription/trunk/assets/js/mplus-intercom-subscription-public.js
r1917369 r2580672 15 15 if ( data.success == 1 ) { 16 16 $( ".mplus-intercom-subscription-form form.mplus_intercom_subscription" ).remove(); 17 $( '.mplus-intercom-subscription-form .message' ). show();17 $( '.mplus-intercom-subscription-form .message' ).empty().html( data.message ).show(); 18 18 } else { 19 19 $( '.mplus-intercom-subscription-form .message' ).empty().html( data.message ).show(); 20 20 $( '.mplus-intercom-subscription-form .message' ).fadeIn().delay( 10000 ).fadeOut(); 21 console.log(data); 21 22 } 22 23 }, -
mplus-intercom-subscription/trunk/classes/mplus-intercom-subscription-handler.php
r2039422 r2580672 4 4 * Manages Intercom functionality of this plugin. 5 5 * 6 * @package Mplus_Intercom_Subscription7 * @subpackage Mplus_Intercom_Subscription/classes8 6 * @author 79mplus 9 7 */ … … 16 14 if ( ! class_exists( 'Mplus_Intercom_Subscription_Handler' ) ) { 17 15 class Mplus_Intercom_Subscription_Handler { 18 19 16 /** 20 * @var \Intercom\IntercomClient $clientHolds the Intercom client instance.17 * @var \Intercom\IntercomClient Holds the Intercom client instance. 21 18 */ 22 19 public $client; … … 26 23 * 27 24 * @param string|null $access_token Access token for Intercom API. 25 * 28 26 * @return void 29 27 */ 30 public function __construct( $access_token = null ) { 31 28 public function __construct($access_token = null) { 32 29 // Initializes the api with the accesstoken. 33 30 if ( empty( $access_token ) ) { … … 35 32 } 36 33 $this->client = new Intercom\IntercomClient( $access_token, null ); 37 38 34 } 39 35 … … 41 37 * Creates user with the given info. 42 38 * 43 * @param array $submitted_fields Fields to submit. 44 * @param string $user_type (optional) Either user or lead. 39 * @param array $submitted_fields Fields to submit. 40 * @param string $user_type (optional) Either user or lead. 41 * 45 42 * @return array 46 43 */ 47 public function create_user( $submitted_fields, $user_type = 'user' ) { 44 public function create_user($submitted_fields, $user_type = 'user') { 45 $contact_id = null; 46 $response = [ 47 'success' => 0, 48 ]; 49 $client = $this->client; 50 $fields = self::get_fields( $submitted_fields ); 51 $fields = array_merge( ['role' => $user_type ], $fields ); 52 $email = isset( $fields['email'] ) ? $fields['email'] : ''; 48 53 49 $client = $this->client;54 do_action( 'mplus_intercom_subscription_user_created_before', $fields, $user_type, $this->client ); 50 55 51 $fields = self::get_fields( $submitted_fields ); 56 // Email validation. 57 if ( '' == $email && ! is_email( $email ) ) { 58 $response['message'] = __( 'Email required.', 'mplus-intercom-subscription' ); 52 59 53 do_action( 'mplus_intercom_subscription_user_created_before', $fields, $user_type ); 60 return $response; 61 } 54 62 55 $response = array(); 56 if ( $user_type == 'lead' ) { 57 try { 58 $new_user = $client->leads->create( $fields ); 59 if ( ! empty( $new_user->id ) ) : 60 $response['message'] = __( 'Added New User.', 'mplus-intercom-subscription' ); 61 $response['success'] = 1; 62 $response['user_info'] = $new_user; 63 else : 64 $response['message'] = __( 'Something Wrong.', 'mplus-intercom-subscription' ); 65 $response['success'] = 0; 66 endif; 67 } catch ( Exception $e ) { 68 $response['success'] = 0; 69 if ( $e->getCode() == 409 ) { 70 /* 71 There are multiple users with this email. 72 in this case creates the user using a custom user_id and saves that in wp. 73 But at first checks if it has already been done this for this email. 74 */ 75 $user_id = get_option( 'mplus_intercom_subscription' . $fields->email ); 76 $user_found = $user_id; 77 if ( ! $user_found ) { 78 $user_id = 'mplus-intercom-subscription-' . time(); 79 } 80 $fields['user_id'] = "$user_id"; 81 $new_user = $client->leads->create( $fields ); 82 if ( $new_user && ! $user_found ) { 83 update_option( 'mplus_intercom_subscription' . $fields->email, $user_id ); 84 } 63 /* Checking if contact exist or not exist. */ 64 $search_contacts = $client->contacts->search([ 65 'pagination' => ['per_page' => 10], 66 'query' => [ 'field' => 'email', 'operator' => '=', 'value' => $email ], 67 ]); 85 68 86 } else { 87 $response['message'] = __( 'An error occurred while registering the user.', 'mplus-intercom-subscription' ); 88 return $response; 89 } 90 } 91 } else { 92 try { 93 $new_user = $client->users->create( $fields ); 94 if ( ! empty( $new_user->id ) ) : 95 $response['message'] = __( 'Added New User.', 'mplus-intercom-subscription' ); 96 $response['success'] = 1; 97 $response['user_info'] = $new_user; 98 else : 99 $response['message'] = __( 'Something Wrong.', 'mplus-intercom-subscription' ); 100 $response['success'] = 0; 101 endif; 102 } catch ( Exception $e ) { 103 $response['success'] = 0; 104 if ( $e->getCode() == 409 ) { 105 /* 106 There are multiple users with this email. 107 In this case it creates the user using a custom user_id and save that in wp. 108 But at first checks if it has already been done this for this email. 109 */ 110 $user_id = get_option( 'mplus_intercom_subscription' . $fields->email ); 111 $user_found = $user_id; 112 if ( ! $user_found ) { 113 $user_id = 'mplus-intercom-subscription-' . time(); 114 } 115 $fields['user_id'] = "$user_id"; 116 $new_user = $client->users->create( $fields ); 117 if ( $new_user && ! $user_found ) { 118 update_option( 'mplus_intercom_subscription' . $fields->email, $user_id ); 119 } 69 $contacts = $search_contacts->data; 120 70 121 } else { 122 $response['message'] = __( 'An error occurred while registering the user.', 'mplus-intercom-subscription' ); 123 return $response; 71 // Validate contact role with user type. 72 if ( count( $contacts ) > 0 ) { 73 foreach ( $contacts as $contact ) { 74 if ( $contact->role == $user_type ) { 75 $contact_id = $contact->id; 124 76 } 125 77 } 126 78 } 127 79 128 do_action( 'mplus_intercom_subscription_user_created_after', $new_user, $submitted_fields ); 80 // If contact id null create a new contact. If not null update existing contact. 81 if ( is_null( $contact_id ) ) { 82 try { 83 $new_contact = $client->contacts->create( $fields ); 84 85 if ( isset( $new_contact->id ) ) { 86 $response['message'] = __( 'Contact has been successfully registered.', 'mplus-intercom-subscription' ); 87 $response['success'] = 1; 88 $response['type'] = $user_type; 89 $response['user_info'] = $new_contact; 90 } 91 92 return $response; 93 } catch ( Exception $e ) { 94 $response['message'] = __( 'An error occurred while registering the contact.', 'mplus-intercom-subscription' ); 95 $response['code'] = $e->getCode(); 96 97 if ( 409 == $e->getCode() && 'lead' == $user_type ) { 98 $response['message'] = __( 'This email already registered as an user contact. Please used another email.', 'mplus-intercom-subscription' ); 99 } 100 101 return $response; 102 } 103 } else { 104 /* Update existing contact */ 105 try { 106 $fields['role'] = $user_type; 107 $new_contact = $client->contacts->update( $contact_id, $fields ); 108 $response['message'] = __( 'Contact has been successfully updated.', 'mplus-intercom-subscription' ); 109 $response['success'] = 1; 110 $response['type'] = $user_type; 111 $response['user_info'] = $new_contact; 112 } catch ( Exception $e ) { 113 $response['message'] = __( 'An error occurred while updating the contact.', 'mplus-intercom-subscription' ); 114 115 return $response; 116 } 117 } 118 119 do_action( 'mplus_intercom_subscription_user_created_after', $new_contact, $submitted_fields, $this->client ); 129 120 130 121 return $response; 131 132 122 } 133 123 … … 136 126 * 137 127 * @param object $fields Fields to submit. 128 * 138 129 * @return array 139 130 */ 140 public function get_fields( $fields ) { 141 142 $basic = array(); 143 $custom = array(); 131 public static function get_fields($fields) { 132 $basic = []; 133 $custom = []; 144 134 /*default value for unsubscribed_from_emails*/ 145 135 $basic['unsubscribed_from_emails'] = true; 146 136 foreach ( $fields as $field ) { 147 if ( array_key_exists( 'intercom_attribute', $field ) ) {148 if ( $field['intercom_attribute'] == 'unsubscribed_from_emails') {137 if ( array_key_exists( 'intercom_attribute', $field ) ) { 138 if ( 'unsubscribed_from_emails' == $field['intercom_attribute'] ) { 149 139 $field['value'] = false; 150 140 } 151 if ( $field['attribute_type'] == 'basic' ) { 141 142 if ( 'basic' == $field['attribute_type'] ) { 152 143 $basic[ $field['intercom_attribute'] ] = $field['value']; 153 } elseif ( $field['attribute_type'] == 'custom') {144 } elseif ( 'custom' == $field['attribute_type'] ) { 154 145 $custom[ $field['intercom_attribute'] ] = $field['value']; 155 146 } else { 156 157 147 } 158 148 } 159 149 } 160 if( ! empty( $custom ) ) { 150 151 if ( ! empty( $custom ) ) { 161 152 $basic['custom_attributes'] = $custom; 162 153 } 154 163 155 return $basic; 164 165 156 } 166 157 } -
mplus-intercom-subscription/trunk/includes/class-mplus-intercom-subscription-core.php
r1908675 r2580672 224 224 $this->loader->add_action( 'wp_ajax_mplus_intercom_subscription_company_form_submit', $plugin_public, 'company_submit_handler' ); 225 225 $this->loader->add_action( 'wp_ajax_nopriv_mplus_intercom_subscription_company_form_submit', $plugin_public, 'company_submit_handler' ); 226 $this->loader->add_action( 'mplus_intercom_subscription_user_created_after', $plugin_public, 'user_assign_to_company_handler', 10, 2);226 $this->loader->add_action( 'mplus_intercom_subscription_user_created_after', $plugin_public, 'user_assign_to_company_handler', 10, 3 ); 227 227 228 228 } … … 308 308 309 309 if ( is_null( self::$_client ) && class_exists( 'Intercom\IntercomClient' ) ) { 310 // Access token 311 $access_token = get_option( 'mplusis_api_key' ); 312 self::$_client = new Intercom\IntercomClient( $access_token, null ); 310 try { 311 // Access token 312 $access_token = get_option( 'mplusis_api_key' ); 313 self::$_client = new Intercom\IntercomClient( $access_token, null ); 314 } catch (Exception $e) { 315 } 313 316 } 314 317 -
mplus-intercom-subscription/trunk/includes/class-mplus-intercom-subscription-public.php
r2357049 r2580672 87 87 */ 88 88 public function company_submit_handler() { 89 90 89 $submitted_fields = array(); 91 $response = array(); 92 $intercom = Mplus_Intercom_Subscription_Core::get_client(); 93 $honeypot = false; 94 95 $fields = $_POST['fields']; 96 97 foreach ( $fields as $field ) : 98 if( $field['name'] == 'honeypot' ) : 99 if( $field['value'] != '' ) : 90 $response = array(); 91 $intercom = Mplus_Intercom_Subscription_Core::get_client(); 92 $honeypot = false; 93 $fields = wp_unslash( $_POST['fields'] ); 94 95 foreach ( $fields as $field ) { 96 if( $field['name'] == 'honeypot' ) { 97 if( $field['value'] != '' ) { 100 98 $honeypot = true; 101 endif;99 } 102 100 continue; 103 endif;101 } 104 102 105 103 $submitted_fields[ $field['name'] ] = $field['value']; 106 endforeach;104 } 107 105 108 106 $spam_protection = get_option( 'mplusis_subscription_spam_protection' ); 109 107 110 if( $spam_protection == 1 && $honeypot ) :108 if( $spam_protection == 1 && $honeypot ) { 111 109 $response['success'] = 0; 112 110 $response['message'] = __( 'Something Wrong.', 'mplus-intercom-subscription' ); 113 else : 111 wp_send_json( $response ); 112 die(); 113 } 114 115 try { 116 // Check in conpany exists or not. If exists update company informations. 114 117 $company = $intercom->companies->getCompanies( [ 115 'name' => $submitted_fields['name']118 'name' => esc_attr( trim( $submitted_fields['name'] ) ), 116 119 ] ); 117 120 118 if( property_exists( $company, 'id' ) ): 119 $company_fields = [ 120 'id' => $company->id, 121 'plan' => esc_attr( $submitted_fields['plan'] ), 122 'created_at' => strtotime( $submitted_fields['created_at'] ), 123 'size' => esc_attr( $submitted_fields['size'] ), 124 'website' => esc_url( $submitted_fields['website'] ), 125 'industry' => esc_attr( $submitted_fields['industry'] ), 126 ]; 127 $company = $intercom->companies->create( $company_fields ); 128 $response['company_info'] = $company; 129 $response['message'] = __( 'Company already exists. Company Information updated.', 'mplus-intercom-subscription' ); 121 // Conpany fields new data. 122 $company_fields = [ 123 'id' => $company->id, 124 'company_id' => $company->company_id, 125 'plan' => esc_attr( $submitted_fields['plan'] ), 126 'created_at' => strtotime( $submitted_fields['created_at'] ), 127 'size' => esc_attr( $submitted_fields['size'] ), 128 'website' => esc_url( $submitted_fields['website'] ), 129 'industry' => esc_attr( $submitted_fields['industry'] ), 130 ]; 131 132 // Update Company information 133 $company = $intercom->companies->update( $company_fields ); 134 135 $response['company_info'] = $company; 136 $response['message'] = __( 'Company already exists. Company Information updated.', 'mplus-intercom-subscription' ); 137 $response['success'] = 0; 138 wp_send_json( $response ); 139 die(); 140 } catch (Exception $e) { 141 // Conpany fields data. 142 $company_fields = [ 143 'name' => esc_attr( trim( $submitted_fields['name'] ) ), 144 'company_id' => mt_rand( 10,999999 ), 145 'plan' => esc_attr( $submitted_fields['plan'] ), 146 'created_at' => strtotime( $submitted_fields['created_at'] ), 147 'size' => esc_attr( $submitted_fields['size'] ), 148 'website' => esc_url( $submitted_fields['website'] ), 149 'industry' => esc_attr( $submitted_fields['industry'] ), 150 ]; 151 152 try { 153 // Create A New company. 154 $company = $intercom->companies->create($company_fields); 155 156 try { 157 // Create a new user using email address. And assign as a company user. 158 $creator_user = $intercom->contacts->create([ 159 'name' => ucwords( trim( $submitted_fields['name'] ) ) . ' Creator', 160 'email' => $submitted_fields['email'], 161 'companies' => [ 162 //[ 'company_id' => $company->company_id ] 163 [ 'id' => $company->id ] 164 ], 165 'type' => 'user', 166 ]); 167 /** 168 * Add companies to a contact with IDs 169 */ 170 $intercom->companies->attachContact( $creator_user->id, $company->id ); 171 172 $response['company_info'] = $creator_user; 173 $response['success'] = 1; 174 $response['message'] = __( 'Company Registration Completed.', 'mplus-intercom-subscription' ); 175 wp_send_json( $response ); 176 die(); 177 } catch (Exception $e) { 178 //If use exists for submitted email address. Update user's company information. 179 try { 180 /** Search for contacts */ 181 $query = [ 'field' => 'email', 'operator' => '=', 'value' => $submitted_fields['email'] ]; 182 $query_users = $intercom->contacts->search([ 183 'pagination' => ['per_page' => 1], 184 'query' => $query, 185 ]); 186 187 $exists_users = $query_users->data; 188 $exists_user = $exists_users[0]; 189 190 // Update user company information. 191 $creator_user = $intercom->contacts->update( $exists_user->id, [ 192 'email' => $submitted_fields['email'], 193 'companies' => [ 194 [ 'id' => $company->id ] 195 ], 196 ]); 197 /** 198 * Add companies to a contact with IDs 199 */ 200 $intercom->companies->attachContact( $creator_user->id, $company->id ); 201 202 $response['company_info'] = $creator_user; 203 $response['success'] = 1; 204 $response['message'] = __( 'Company Registration Completed.', 'mplus-intercom-subscription' ); 205 wp_send_json( $response ); 206 die(); 207 } catch (Exception $e) { 208 $response['success'] = 0; 209 $response['message'] = __( $e->getMessage(), 'mplus-intercom-subscription' ); 210 wp_send_json( $response ); 211 die(); 212 } 213 } 214 } catch (Exception $e) { 215 // If company not created properly send error message. 130 216 $response['success'] = 0; 131 else: 132 $company_fields = [ 133 'name' => esc_attr( $submitted_fields['name'] ), 134 'company_id' => mt_rand( 10,999999 ), 135 'plan' => esc_attr( $submitted_fields['plan'] ), 136 'created_at' => strtotime( $submitted_fields['created_at'] ), 137 'size' => esc_attr( $submitted_fields['size'] ), 138 'website' => esc_url( $submitted_fields['website'] ), 139 'industry' => esc_attr( $submitted_fields['industry'] ), 140 ]; 141 142 // Assign company creator as a company user 143 $creator_user = $intercom->users->create( [ 144 'email' => $submitted_fields['email'], 145 'name' => ucwords( trim( $submitted_fields['name'] ) ) . ' Creator', 146 'companies' => [ $company_fields ] 147 ] ); 148 149 $response['company_info'] = $creator_user->companies; 150 $response['success'] = 1; 151 $response['message'] = __( 'Company Registration Completed.', 'mplus-intercom-subscription' ); 152 endif; 153 endif; 154 wp_send_json( $response ); 155 156 die(); 217 $response['message'] = __( $e->getMessage(), 'mplus-intercom-subscription' ); 218 wp_send_json( $response ); 219 die(); 220 } 221 } 157 222 } 158 223 … … 162 227 * @return void 163 228 */ 164 public function user_assign_to_company_handler( $new_user, $submitted_fields ) {229 public function user_assign_to_company_handler( $new_user, $submitted_fields, $client ) { 165 230 166 231 if ( is_object( $new_user ) && isset( $new_user->email ) ) { 167 168 $intercom = Mplus_Intercom_Subscription_Core::get_client();169 170 232 foreach ( $submitted_fields as $field ) { 171 233 if ( array_key_exists( 'name', $field ) && $field['name'] == 'company_id' ) : … … 174 236 endif; 175 237 } 238 176 239 if ( isset( $company_id ) ) : 177 $intercom->users->update( [ 178 'email' => $new_user->email, 179 'companies' => [ 180 [ 181 'company_id' => $company_id 182 ] 183 ] 184 ] ); 240 $client->companies->attachContact( $new_user->id, $company_id ); 185 241 endif; 186 242 -
mplus-intercom-subscription/trunk/includes/helper-function.php
r2357049 r2580672 87 87 */ 88 88 function get_all_company_list() { 89 $intercom = Mplus_Intercom_Subscription_Core::get_client(); 89 $intercom = Mplus_Intercom_Subscription_Core::get_client(); 90 $companies_data = []; 90 91 $companies_options = array(); 91 $company = $intercom->companies->getCompanies( [ 'per_page' => 50 ] );92 92 93 if( property_exists( $company, 'companies' ) ): 94 $companies = $company->companies; 95 $companies_options[] = 'Select Company'; 96 foreach ( $companies as $company ) : 97 if ( isset( $company->name ) ) : 98 $companies_options[ $company->company_id ] = $company->name; 99 endif; 100 endforeach; 101 else: 102 $companies_options[] = 'Select Company'; 103 endif; 93 $companies = $intercom->companies->getCompanies( [ 94 "per_page" => 50, 95 ] ); 96 97 if ( property_exists( $companies, 'data' ) ) { 98 $companies_data = array_merge( $companies_data, $companies->data ); 99 $pages = $companies->pages; 100 $total_pages = $pages->total_pages; 101 102 $page = 2; 103 do { 104 $companies = $intercom->companies->getCompanies( [ 105 "per_page" => 50, 106 "page" => $page 107 ] ); 108 if ( property_exists( $companies, 'data' ) ) { 109 $companies_data = array_merge( $companies_data, $companies->data ); 110 } 111 ++$page; 112 } while( $page <= $total_pages ); 113 114 } else { 115 return $companies_options[] = 'Select Company'; 116 } 117 118 $companies_options[] = 'Select Company'; 119 foreach ( $companies_data as $company ) { 120 if ( isset( $company->name ) ) { 121 $companies_options[ $company->id ] = $company->name; 122 } 123 } 104 124 105 125 return $companies_options; … … 109 129 * Gets company information. 110 130 * 111 * @param int $company_ id Company idto be used for function.131 * @param int $company_ID Company ID to be used for function. 112 132 * @return mixed 113 133 */ 114 function get_company_information( $company_ id) {134 function get_company_information( $company_ID ) { 115 135 116 136 $intercom = Mplus_Intercom_Subscription_Core::get_client(); 117 return $company = $intercom->companies->getCompanies( [ 'company_id' => $company_id ] ); 137 /** Get a company by ID */ 138 return $company = $intercom->companies->getCompany( $company_ID ); 118 139 } -
mplus-intercom-subscription/trunk/mplus-intercom-subscription.php
r2357049 r2580672 4 4 * Plugin URI: https://www.79mplus.com/intercom-subscription/ 5 5 * Description: The easiest and most extendable WordPress plugin for Intercom. This lets you offer a subscription form for Intercom and offers a wide range of extensions to grow your user base with the power of Intercom. 6 * Version: 1. 0.276 * Version: 1.1.0 7 7 * Author: 79mplus 8 8 * Author URI: https://www.79mplus.com/ … … 28 28 * Plugin version. 29 29 */ 30 define( 'MPLUSISVERSION', '1. 0.27' );30 define( 'MPLUSISVERSION', '1.1.0' ); 31 31 /** 32 32 * Plugin directory. -
mplus-intercom-subscription/trunk/readme.txt
r2357049 r2580672 3 3 Donate link: https://www.79mplus.com/donate 4 4 Tags: intercom, email, newsletter, marketing, user base, grow, communication 5 Requires at least: 4.66 Tested up to: 5. 45 Requires at least: 5.0 6 Tested up to: 5.8 7 7 Stable tag: trunk 8 8 Requires PHP: 7.2 … … 134 134 == Changelog == 135 135 136 = 1.1.0 = 137 * Compatible with 2.3 latest version intercom api. 138 * Updated intercom php SDK file. 139 * Update user, lead and company registration add and update porcess. 140 136 141 = 1.0.27 = 137 142 * Fix PHP error.
Note: See TracChangeset
for help on using the changeset viewer.