'Dynamics 365 Marketing', 'tasks' => array( 'create_marketing_contact' => __( 'Create / Update Marketing Contact (by email)', 'advanced-form-integration' ), ), ); return $actions; } public function register_settings_tab( $providers ) { $providers['dynamics365marketing'] = 'Dynamics 365 Marketing'; return $providers; } /** * The settings tab is informational only — no credentials are stored * for this platform. Users connect once under "Dynamics 365 CRM" and * both platforms share the resulting token. */ public function settings_view( $current_tab ) { if ( 'dynamics365marketing' !== $current_tab ) { return; } $crm_settings_url = admin_url( 'admin.php?page=advanced-form-integration-settings&tab=dynamics365' ); ?>

Dynamics 365 CRM tab. There is nothing to configure here.', 'advanced-form-integration' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $crm_settings_url ) ); ?>


Pro connector adds extended field mapping plus automatic membership in static Marketing Lists (Outbound Marketing).', 'advanced-form-integration' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array() ) ) ), esc_url( admin_url( 'admin.php?page=advanced-form-integration-settings-pricing' ) ) ); ?>

__( 'Security check failed', 'advanced-form-integration' ) ) ); } $cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : ''; if ( ! $cred_id ) { wp_send_json_error( array( 'message' => __( 'Missing credential id', 'advanced-form-integration' ) ) ); } if ( ! class_exists( 'ADFOIN_Dynamics365' ) ) { wp_send_json_error( array( 'message' => __( 'Dynamics 365 connection module is not loaded.', 'advanced-form-integration' ) ) ); } $d = ADFOIN_Dynamics365::get_instance(); $d->set_credentials( $cred_id ); $endpoint = 'lists?' . http_build_query( array( '$select' => 'listid,listname', '$filter' => 'createdfromcode eq 2 and type eq false', '$orderby' => 'listname asc', '$top' => 200, ) ); $response = $d->dynamics365_request( $endpoint, 'GET' ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message() ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); $msg = is_array( $body ) && isset( $body['error']['message'] ) ? $body['error']['message'] : sprintf( 'HTTP %d', $code ); wp_send_json_error( array( 'message' => $msg ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $lists = array(); if ( ! empty( $body['value'] ) && is_array( $body['value'] ) ) { foreach ( $body['value'] as $list ) { if ( ! empty( $list['listid'] ) && ! empty( $list['listname'] ) ) { $lists[ $list['listid'] ] = $list['listname']; } } } wp_send_json_success( $lists ); } /** * POST to /lists()/Members/$ref with `@odata.id` pointing at * the contact. Returns 204 No Content on success. */ public function add_contact_to_list( $d, $list_id, $contact_id, $record ) { $instance_url = trailingslashit( $d->instance_url ); $odata_id = $instance_url . 'api/data/v9.2/contacts(' . $contact_id . ')'; return $d->dynamics365_request( sprintf( 'lists(%s)/Members/$ref', $list_id ), 'POST', array( '@odata.id' => $odata_id ), $record ); } /** * Field list returned over AJAX so the JS component can refresh when * the user changes the task selection. Kept as a method (not hardcoded * in the JS) so Pro can extend it and so we can localize labels. */ public function ajax_get_fields() { adfoin_require_manage_options(); if ( ! wp_verify_nonce( $_POST['_nonce'] ?? '', 'advanced-form-integration' ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed', 'advanced-form-integration' ) ) ); } $task = isset( $_POST['task'] ) ? sanitize_text_field( wp_unslash( $_POST['task'] ) ) : 'create_marketing_contact'; wp_send_json_success( $this->get_fields_for_task( $task ) ); } protected function get_fields_for_task( $task ) { // Marketing-relevant subset of the Contact entity. Free tier // deliberately omits extended demographic/firmographic fields so // the Pro upgrade has obvious value. Consent flags ARE here in // Free because GDPR-aware sites should be able to honour them // without having to upgrade. return array( array( 'key' => 'firstname', 'value' => __( 'First Name', 'advanced-form-integration' ) ), array( 'key' => 'lastname', 'value' => __( 'Last Name', 'advanced-form-integration' ) ), array( 'key' => 'emailaddress1', 'value' => __( 'Email (required — used to dedupe)', 'advanced-form-integration' ), 'required' => true ), array( 'key' => 'telephone1', 'value' => __( 'Business Phone', 'advanced-form-integration' ) ), array( 'key' => 'mobilephone', 'value' => __( 'Mobile Phone', 'advanced-form-integration' ) ), array( 'key' => 'companyname', 'value' => __( 'Company Name', 'advanced-form-integration' ) ), array( 'key' => 'jobtitle', 'value' => __( 'Job Title', 'advanced-form-integration' ) ), array( 'key' => 'address1_city', 'value' => __( 'City', 'advanced-form-integration' ) ), array( 'key' => 'address1_country', 'value' => __( 'Country', 'advanced-form-integration' ) ), // Marketing-specific array( 'key' => 'leadsourcecode', 'value' => __( 'Lead Source (option set value)', 'advanced-form-integration' ), 'description' => __( 'Numeric option set value, e.g. 1 = Advertisement.', 'advanced-form-integration' ) ), array( 'key' => 'donotemail', 'value' => __( 'Do Not Email (true/false)', 'advanced-form-integration' ) ), array( 'key' => 'donotbulkemail', 'value' => __( 'Do Not Bulk Email (true/false)', 'advanced-form-integration' ) ), array( 'key' => 'donotphone', 'value' => __( 'Do Not Phone (true/false)', 'advanced-form-integration' ) ), ); } } ADFOIN_Dynamics365Marketing::get_instance(); /* ---------- Job queue + dispatch ---------- */ function adfoin_dynamics365marketing_job_queue( $data ) { if ( ! isset( $data['record'], $data['posted_data'] ) ) { return; } adfoin_dynamics365marketing_send_data( $data['record'], $data['posted_data'] ); } add_action( 'adfoin_dynamics365marketing_job_queue', 'adfoin_dynamics365marketing_job_queue', 10, 1 ); add_action( 'adfoin_job_queue', 'adfoin_dynamics365marketing_job_queue', 10, 1 ); function adfoin_dynamics365marketing_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( isset( $record_data['action_data']['cl'] ) && function_exists( 'adfoin_check_conditional_logic' ) && adfoin_check_conditional_logic( $record_data['action_data']['cl'], $posted_data ) ) { return; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : $record_data; $cred_id = $field_data['credId'] ?? ( $record['cred_id'] ?? '' ); $list_id = $field_data['listId'] ?? ''; $task = $record['task'] ?? ''; if ( ! $cred_id || ! $task ) { return; } if ( ! class_exists( 'ADFOIN_Dynamics365' ) ) { // Defensive — should never happen since the CRM platform is // required for the credential dropdown, but guard against a // misconfigured install that disabled CRM but kept Marketing. return; } $d = ADFOIN_Dynamics365::get_instance(); $d->set_credentials( $cred_id ); if ( ! $d->ensure_token() ) { return; // mark_connection_failed already fired inside the base class } // Resolve tag templates ({{billing_email}} etc.). Skip control keys so // they never end up in the Dynamics payload. $values = array(); $skip = array( 'credId', 'listId', 'lists', 'cl' ); foreach ( $field_data as $key => $value ) { if ( in_array( $key, $skip, true ) ) { continue; } $parsed = adfoin_get_parsed_values( $value, $posted_data ); if ( $parsed !== '' && $parsed !== null ) { $values[ $key ] = $parsed; } } if ( 'create_marketing_contact' === $task ) { adfoin_dynamics365marketing_handle_contact( $d, $values, $list_id, $record ); } } /** * Upsert a Marketing Contact by email, optionally adding the resulting * contact to a static Marketing List. Same dedupe semantics as the CRM * contact handler — the form-submitter doesn't have to choose between * "create a contact" and "add to a list" as separate steps. * * @param ADFOIN_Dynamics365 $d Hydrated CRM instance (auth ready). * @param array $values Resolved field map (email required). * @param string $list_id Optional Marketing List GUID. * @param array $record Integration record (for logging). */ function adfoin_dynamics365marketing_handle_contact( $d, $values, $list_id, $record ) { $email = $values['emailaddress1'] ?? ''; if ( ! $email ) { return; // email is the dedupe key } $payload = $d->finalize_payload( 'contact', $values ); $existing = $d->find_record_by_email( 'contacts', $email, $record ); $contact_id = $existing['contactid'] ?? ''; if ( $contact_id ) { // Update existing contact only if we have fields beyond email — // an empty PATCH is harmless but pollutes the audit log. if ( count( $payload ) > 1 || ! isset( $payload['emailaddress1'] ) ) { $d->dynamics365_request( "contacts({$contact_id})", 'PATCH', $payload, $record ); } } else { $response = $d->dynamics365_request( 'contacts', 'POST', $payload, $record ); if ( ! is_wp_error( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! empty( $body['contactid'] ) ) { $contact_id = $body['contactid']; } else { $hdr = wp_remote_retrieve_header( $response, 'OData-EntityId' ); if ( $hdr && preg_match( '/contacts\(([0-9a-fA-F-]+)\)/', $hdr, $m ) ) { $contact_id = $m[1]; } } } } if ( $list_id && $contact_id ) { ADFOIN_Dynamics365Marketing::get_instance()->add_contact_to_list( $d, $list_id, $contact_id, $record ); } }