ccount'], $subscriber ); } /** * Create subscriber array from submitted data and form settings * returns a subscriber array or false on error * * @param Form_Record $record * * @return array|bool */ private function create_subscriber_object( Form_Record $record ) { $form_settings = $record->get( 'form_settings' ); $email = $this->map_email_field( $record ); if ( ! $email ) { return false; } $subscriber = [ 'ip_address' => Utils::get_client_ip(), 'email' => $email, ]; if ( isset( $form_settings['tags'] ) && ! empty( $form_settings['tags'] ) ) { $tags = $record->replace_setting_shortcodes( $form_settings['tags'] ); $subscriber['tags'] = explode( ',', $tags ); } $custom_fields = []; if ( isset( $form_settings['drip_custom_fields'] ) && 'yes' === $form_settings['drip_custom_fields'] ) { $custom_fields = $this->get_drip_custom_fields( $record ); } $subscriber['custom_fields'] = $custom_fields; return $subscriber; } /** * @param Form_Record $record * * @return array */ private function get_drip_custom_fields( Form_Record $record ) { $local_email_id = ''; foreach ( $record->get_form_settings( 'drip_fields_map' ) as $map_item ) { if ( 'email' === $map_item['remote_id'] ) { $local_email_id = $map_item['local_id']; } } $custom_fields = []; foreach ( $record->get( 'fields' ) as $id => $field ) { if ( $local_email_id === $id ) { continue; } $custom_fields[ $id ] = $field['value']; } return $custom_fields; } /** * extracts Email field from form based on mapping * returns email address or false if missing * * @param Form_Record $record * * @return bool */ private function map_email_field( Form_Record $record ) { $fields = $record->get( 'fields' ); foreach ( $record->get_form_settings( 'drip_fields_map' ) as $map_item ) { if ( empty( $fields[ $map_item['local_id'] ]['value'] ) ) { continue; } $value = $fields[ $map_item['local_id'] ]['value']; if ( 'email' === $map_item['remote_id'] ) { return $value; } } return false; } /** * @param array $data * * @return array * @throws \Exception */ public function handle_panel_request( array $data ) { if ( ! empty( $data['api_token'] ) && 'default' === $data['api_token'] ) { $api_key = $this->get_global_api_key(); } elseif ( ! empty( $data['custom_api_token'] ) ) { $api_key = $data['custom_api_token']; } if ( empty( $api_key ) ) { throw new \Exception( '`api_token` is required', 400 ); } $handler = new Drip_Handler( $api_key ); return $handler->get_accounts(); } public function register_admin_fields( Settings $settings ) { $settings->add_section( Settings::TAB_INTEGRATIONS, 'drip', [ 'callback' => function() { echo '