__( 'Flamingo', 'advanced-form-integration' ), 'tasks' => array( 'add_contact' => __( 'Add Contact', 'advanced-form-integration' ), 'log_message' => __( 'Log Message', 'advanced-form-integration' ), ), ); return $actions; } add_action( 'adfoin_action_fields', 'adfoin_flamingo_action_fields' ); function adfoin_flamingo_action_fields() { ?> 'subject', 'value' => __( 'Subject', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'from_name', 'value' => __( 'From Name', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'from_email', 'value' => __( 'From Email', 'advanced-form-integration' ), 'description' => '' ), array( 'key' => 'message', 'value' => __( 'Message', 'advanced-form-integration' ), 'description' => '' ), ); } else { $fields = array( array( 'key' => 'email', 'value' => __( 'Email', 'advanced-form-integration' ), 'description' => __( 'Required.', 'advanced-form-integration' ) ), array( 'key' => 'name', 'value' => __( 'Name', 'advanced-form-integration' ), 'description' => '' ), ); } wp_send_json_success( $fields ); } add_action( 'adfoin_flamingo_job_queue', 'adfoin_flamingo_job_queue', 10, 1 ); function adfoin_flamingo_job_queue( $data ) { adfoin_flamingo_send_data( $data['record'], $data['posted_data'] ); } /* * Handles creating a Flamingo contact or inbound message */ function adfoin_flamingo_send_data( $record, $posted_data ) { if ( ! class_exists( 'Flamingo_Contact' ) && ! class_exists( 'Flamingo_Inbound_Message' ) ) { return; } $record_data = json_decode( $record['data'], true ); if ( isset( $record_data['action_data']['cl'] ) && adfoin_check_conditional_logic( $record_data['action_data']['cl'], $posted_data ) ) { return; } $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $task = isset( $record['task'] ) ? $record['task'] : ''; $prepared_data = array(); foreach ( $field_data as $key => $value ) { if ( '' === $key ) { continue; } $parsed_value = adfoin_get_parsed_values( $value, $posted_data ); if ( '' === $parsed_value || null === $parsed_value ) { continue; } $prepared_data[ $key ] = $parsed_value; } $request_payload = $prepared_data; $response_body = array( 'success' => false ); $status_code = 400; if ( 'add_contact' === $task ) { if ( empty( $prepared_data['email'] ) || ! is_email( $prepared_data['email'] ) ) { $response_body['message'] = __( 'A valid email address is required.', 'advanced-form-integration' ); } elseif ( ! class_exists( 'Flamingo_Contact' ) ) { $response_body['message'] = __( 'Flamingo is not active.', 'advanced-form-integration' ); } else { $contact = Flamingo_Contact::add( array( 'email' => $prepared_data['email'], 'name' => isset( $prepared_data['name'] ) ? $prepared_data['name'] : '', ) ); if ( $contact ) { $status_code = 200; $response_body['success'] = true; $response_body['message'] = __( 'Contact added successfully.', 'advanced-form-integration' ); $response_body['id'] = $contact->id; } else { $response_body['message'] = __( 'Failed to add the contact. Please verify the supplied email.', 'advanced-form-integration' ); } } } elseif ( 'log_message' === $task ) { if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) { $response_body['message'] = __( 'Flamingo is not active.', 'advanced-form-integration' ); } else { $message = Flamingo_Inbound_Message::add( array( 'channel' => __( 'Advanced Form Integration', 'advanced-form-integration' ), 'subject' => isset( $prepared_data['subject'] ) ? $prepared_data['subject'] : '', 'from_name' => isset( $prepared_data['from_name'] ) ? $prepared_data['from_name'] : '', 'from_email' => isset( $prepared_data['from_email'] ) ? $prepared_data['from_email'] : '', 'fields' => $posted_data, ) ); if ( $message ) { $status_code = 200; $response_body['success'] = true; $response_body['message'] = __( 'Message logged successfully.', 'advanced-form-integration' ); $response_body['id'] = $message->id; } else { $response_body['message'] = __( 'Failed to log the message.', 'advanced-form-integration' ); } } } else { return; } $log_args = array( 'method' => 'LOCAL', 'body' => $request_payload, ); $log_response = array( 'response' => array( 'code' => $status_code, 'message' => $response_body['message'], ), 'body' => $response_body, ); adfoin_add_to_log( $log_response, 'flamingo', $log_args, $record ); }