__( 'Gist', 'advanced-form-integration' ), 'tasks' => array( 'create_contact' => __( 'Create or Update Contact', 'advanced-form-integration' ), ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_gist_settings_tab', 10, 1 ); function adfoin_gist_settings_tab( $providers ) { $providers['gist'] = __( 'Gist', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_gist_settings_view', 10, 1 ); function adfoin_gist_settings_view( $current_tab ) { if( $current_tab != 'gist' ) { return; } $title = __( 'Gist', 'advanced-form-integration' ); $key = 'gist'; $arguments = wp_json_encode([ 'platform' => $key, 'fields' => [ [ 'key' => 'api_key', 'label' => __( 'API Key', 'advanced-form-integration' ), 'hidden' => true ] ] ]); $instructions = sprintf( __( '

  1. Log in to your Gist account.
  2. Navigate to Settings > API & Installation Code > Secret API Key.
  3. Copy the API key and paste it into the field below.

', 'advanced-form-integration' ) ); echo adfoin_platform_settings_template( $title, $key, $arguments, $instructions ); } add_action( 'wp_ajax_adfoin_get_gist_credentials', 'adfoin_get_gist_credentials', 10, 0 ); function adfoin_get_gist_credentials() { adfoin_verify_nonce(); $all_credentials = adfoin_read_credentials( 'gist' ); wp_send_json_success( $all_credentials ); } add_action( 'wp_ajax_adfoin_save_gist_credentials', 'adfoin_save_gist_credentials', 10, 0 ); function adfoin_save_gist_credentials() { adfoin_verify_nonce(); $platform = sanitize_text_field( wp_unslash( $_POST['platform'] ) ); if( 'gist' == $platform ) { $data = adfoin_array_map_recursive( 'sanitize_text_field', $_POST['data'] ); adfoin_save_credentials( $platform, $data ); } wp_send_json_success(); } function adfoin_gist_credentials_list() { $html = ''; $credentials = adfoin_read_credentials( 'gist' ); foreach( $credentials as $option ) { $html .= ''; } echo $html; } add_action( 'adfoin_action_fields', 'adfoin_gist_action_fields' ); function adfoin_gist_action_fields() { ?> 30, 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ), ); if ( in_array( $method, array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) && ! empty( $data ) ) { $args['body'] = wp_json_encode( $data ); } $response = wp_remote_request( $url, $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } add_action( 'adfoin_gist_job_queue', 'adfoin_gist_job_queue', 10, 1 ); function adfoin_gist_job_queue( $data ) { adfoin_gist_send_data( $data['record'], $data['posted_data'] ); } function adfoin_gist_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? [], $posted_data ) ) { return; } $data = $record_data['field_data']; $cred_id = isset( $data['credId'] ) ? $data['credId'] : ''; $task = $record['task']; if ( 'create_contact' !== $task ) { return; } // Whitelist of Gist API fields supported in the free edition. Per // POST /contacts docs, email or user_id is required — the Gist // backend identifies/updates existing contacts by id > user_id > // email, in that order. $allowed_fields = array( 'email', 'name', 'phone', 'user_id' ); $contact = array(); foreach ( $allowed_fields as $key ) { if ( ! isset( $data[ $key ] ) ) { continue; } $value = adfoin_get_parsed_values( $data[ $key ], $posted_data ); if ( '' === $value || null === $value ) { continue; } $contact[ $key ] = $value; } // Require at least one identifier — sending neither email nor // user_id returns 400 from Gist. if ( empty( $contact['email'] ) && empty( $contact['user_id'] ) ) { return; } adfoin_gist_request( 'contacts', 'POST', $contact, $record, $cred_id ); }