__( 'Mautic', 'advanced-form-integration' ),
'tasks' => array(
'add_contact' => __( 'Add or Update Contact', 'advanced-form-integration' )
)
);
return $actions;
}
/**
* Get Mautic credentials by ID
*
* @param string $cred_id Credential ID (optional, can be from $_POST)
* @return array Array with 'url', 'username', and 'password' keys, or empty strings if not found
*/
function adfoin_mautic_get_credentials( $cred_id = '' ) {
// If no cred_id provided, try to get from POST
if ( empty( $cred_id ) && isset( $_POST['credId'] ) ) {
$cred_id = sanitize_text_field( wp_unslash( $_POST['credId'] ) );
}
$url = '';
$username = '';
$password = '';
if ( $cred_id ) {
$credentials = adfoin_read_credentials( 'mautic' );
foreach( $credentials as $single ) {
if( $single['id'] == $cred_id ) {
$url = $single['url'];
$username = $single['username'];
$password = $single['password'];
break;
}
}
} else {
// Fallback to old options if no cred_id provided
$old_credentials = maybe_unserialize( get_option( 'adfoin_mautic_api_key' ) );
if ( is_array( $old_credentials ) ) {
$url = isset( $old_credentials['url'] ) ? $old_credentials['url'] : '';
$username = isset( $old_credentials['username'] ) ? $old_credentials['username'] : '';
$password = isset( $old_credentials['password'] ) ? $old_credentials['password'] : '';
}
}
return array(
'url' => $url,
'username' => $username,
'password' => $password
);
}
add_filter( 'adfoin_settings_tabs', 'adfoin_mautic_settings_tab', 10, 1 );
function adfoin_mautic_settings_tab( $providers ) {
$providers['mautic'] = __( 'Mautic', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_mautic_settings_view', 10, 1 );
function adfoin_mautic_settings_view( $current_tab ) {
if( $current_tab != 'mautic' ) {
return;
}
// Load Account Manager
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
// Migrate old settings if they exist and no new credentials exist
$old_credentials = maybe_unserialize( get_option( 'adfoin_mautic_api_key' ) );
$existing_creds = adfoin_read_credentials( 'mautic' );
if ( is_array( $old_credentials ) && !empty( $old_credentials ) && empty( $existing_creds ) ) {
$new_cred = array(
'id' => wp_generate_uuid4(),
'title' => 'Default Account (Legacy)',
'url' => isset( $old_credentials['url'] ) ? $old_credentials['url'] : '',
'username' => isset( $old_credentials['username'] ) ? $old_credentials['username'] : '',
'password' => isset( $old_credentials['password'] ) ? $old_credentials['password'] : ''
);
adfoin_save_credentials( 'mautic', array( $new_cred ) );
}
$fields = array(
array(
'name' => 'url',
'label' => __( 'Mautic Account URL', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Enter account URL', 'advanced-form-integration' ),
'mask' => false,
'show_in_table' => true,
),
array(
'name' => 'username',
'label' => __( 'Username', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Username', 'advanced-form-integration' ),
'mask' => false,
'show_in_table' => true,
),
array(
'name' => 'password',
'label' => __( 'Password', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Password', 'advanced-form-integration' ),
'mask' => true,
'show_in_table' => false,
),
);
$instructions = '
- ' . __( 'Go to your Mautic installation settings.', 'advanced-form-integration' ) . '
- ' . __( 'Navigate to Configuration > API Settings and enable both API and basic auth.', 'advanced-form-integration' ) . '
- ' . __( 'Hit Save button.', 'advanced-form-integration' ) . '
- ' . __( 'Enter full Mautic account URL (e.g. http://email.example.com), Username, and Password in the fields above.', 'advanced-form-integration' ) . '
- ' . __( 'Click "Add Account" and save your credentials.', 'advanced-form-integration' ) . '
';
ADFOIN_Account_Manager::render_settings_view( 'mautic', 'Mautic', $fields, $instructions );
}
// AJAX Hooks for Account Manager
add_action( 'wp_ajax_adfoin_get_mautic_credentials', 'adfoin_get_mautic_credentials' );
function adfoin_get_mautic_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_get_credentials( 'mautic' );
}
add_action( 'wp_ajax_adfoin_save_mautic_credentials', 'adfoin_save_mautic_credentials' );
function adfoin_save_mautic_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_save_credentials( 'mautic', array( 'url', 'username', 'password' ) );
}
add_action( 'wp_ajax_adfoin_get_mautic_credentials_list', 'adfoin_mautic_get_credentials_list_ajax' );
function adfoin_mautic_get_credentials_list_ajax() {
adfoin_verify_nonce();
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
$fields = array(
array( 'name' => 'url', 'mask' => false ),
array( 'name' => 'username', 'mask' => false ),
array( 'name' => 'password', 'mask' => true ),
);
ADFOIN_Account_Manager::ajax_get_credentials_list( 'mautic', $fields );
}
add_action( 'admin_post_adfoin_save_mautic_api_key', 'adfoin_save_mautic_api_key', 10, 0 );
function adfoin_save_mautic_api_key() {
// Security Check
// Authorization check
adfoin_require_manage_options();
if (! wp_verify_nonce( $_POST['_nonce'], 'adfoin_mautic_settings' ) ) {
die( __( 'Security check Failed', 'advanced-form-integration' ) );
}
$url = isset( $_POST['adfoin_mautic_url'] ) ? sanitize_text_field( wp_unslash( $_POST['adfoin_mautic_url'] ) ) : '';
$username = isset( $_POST['adfoin_mautic_username'] ) ? sanitize_text_field( wp_unslash( $_POST['adfoin_mautic_username'] ) ) : '';
$password = isset( $_POST['adfoin_mautic_password'] ) ? sanitize_text_field( wp_unslash( $_POST['adfoin_mautic_password'] ) ) : '';
$credentials = array(
'url' => $url,
'username' => $username,
'password' => $password
);
// Save tokens
update_option( "adfoin_mautic_api_key", maybe_serialize( $credentials ) );
advanced_form_integration_redirect( "admin.php?page=advanced-form-integration-settings&tab=mautic" );
}
add_action( 'adfoin_add_js_fields', 'adfoin_mautic_js_fields', 10, 1 );
function adfoin_mautic_js_fields( $field_data ) {}
add_action( 'adfoin_action_fields', 'adfoin_mautic_action_fields' );
function adfoin_mautic_action_fields() {
?>
trim( $email ),
'firstname' => $firstname,
'lastname' => $lastname,
'title' => $title,
'mobile' => $mobile,
'phone' => $phone,
'fax' => $fax,
'company' => $company,
'position' => $position,
'address1' => $address1,
'address2' => $address2,
'city' => $city,
'state' => $state,
'zipcode' => $zipcode,
'country' => $country,
'website' => $website,
'facebook' => $facebook,
'instagram' => $instagram,
'linkedin' => $linkedin,
'twitter' => $twitter
);
$data = array_filter( $data );
$return = adfoin_mautic_request( '/api/contacts/new', 'POST', $data, $record, $cred_id );
}
return;
}
function adfoin_mautic_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) {
$credentials = adfoin_mautic_get_credentials( $cred_id );
$base_url = $credentials['url'];
$username = $credentials['username'];
$password = $credentials['password'];
if ( ! $base_url || ! $username || ! $password ) {
return new WP_Error( 'missing_credentials', __( 'Mautic API credentials not found', 'advanced-form-integration' ) );
}
$url = rtrim( $base_url, '/' ) . $endpoint;
$args = array(
'timeout' => 30,
'method' => $method,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password )
)
);
if( 'POST' == $method || 'PUT' == $method || 'PATCH' == $method ) {
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}