provider = $provider; } public static function boot() { add_action( 'chargeforms_retry_marketing', [ __CLASS__, 'retry' ], 10, 5 ); } public function key(): string { return $this->provider; } public function label(): string { return 'brevo' === $this->provider ? 'Brevo' : 'ActiveCampaign'; } public function is_configured( array $settings ): bool { return ! empty( $settings['enabled'] ) && absint( $settings['list_id'] ?? 0 ) > 0 && '' !== Settings::get( $this->provider . '_api_key', '' ); } public function process( array $values, array $form, int $entry_id, array $settings ) { $email = Contact_Field_Mapper::find_email( $form, $values ); if ( '' === $email ) { return; } $this->send( $email, Contact_Field_Mapper::split_name( $form, $values ), $form, $entry_id, $settings, 1 ); } private function send( $email, array $name, array $form, $entry_id, array $settings, $attempt ) { // Re-check current account and form settings on retries. Disabling a // feed must stop delivery, even if a job was already queued. if ( ! $this->is_configured( $settings ) ) { return; } $result = Marketing_Client::upsert( $this->provider, Settings::get( $this->provider . '_api_key', '' ), Settings::get( $this->provider . '_api_url', '' ), $email, $name, $settings ); $data = is_wp_error( $result ) ? $result->get_error_data() : []; Api_Log::log( $this->provider, $form['id'], '', is_wp_error( $result ) ? ( $data['status'] ?? null ) : 200, is_wp_error( $result ) ? $result->get_error_message() : __( 'Contact synchronized.', 'chargeforms' ) ); if ( is_wp_error( $result ) && ! empty( $data['retryable'] ) && $attempt < 3 ) { // Only identity data is queued; credentials are read at execution. wp_schedule_single_event( time() + 300 * $attempt, 'chargeforms_retry_marketing', [ $this->provider, $email, $name, [ 'form_id' => (int) $form['id'], 'entry_id' => (int) $entry_id ], $attempt + 1 ] ); } } public static function retry( $provider, $email, $name, $context, $attempt ) { if ( ! in_array( $provider, [ 'brevo', 'activecampaign' ], true ) || $attempt < 2 || $attempt > 3 ) { return; } $form = \ChargeForms\Includes\Form::get( (int) $context['form_id'] ); if ( ! $form ) { return; } ( new self( $provider ) )->send( $email, $name, $form, (int) $context['entry_id'], $form['settings']['integrations'][ $provider ] ?? [], $attempt ); } }