HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/kevin-demo/wp-content/plugins/wpforms-sendinblue/src/Tasks/ProcessActionTask.php
<?php

namespace WPFormsSendinblue\Tasks;

// phpcs:disable WPForms.PHP.UseStatement.UnusedUseStatement
use WP_Error;
use WPFormsSendinblue\Api\Connection;
// phpcs:enable WPForms.PHP.UseStatement.UnusedUseStatement
use Exception;
use WPForms\Tasks\Task;
use WPForms\Tasks\Meta;
use WPFormsSendinblue\Provider\Account;
use WPFormsSendinblue\Provider\CustomFields\FieldMapper;

/**
 * Class ProcessActionTask.
 *
 * @since 1.0.0
 */
class ProcessActionTask extends Task {

	/**
	 * Async task action.
	 *
	 * @since 1.0.0
	 */
	const ACTION = 'wpforms_sendinblue_process_action';

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		parent::__construct( self::ACTION );
	}

	/**
	 * Hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() {

		add_action( self::ACTION, [ $this, 'process' ] );
	}

	/**
	 * Process the addon async tasks.
	 *
	 * @since 1.0.0
	 *
	 * @param int $meta_id Task meta ID.
	 */
	public function process( $meta_id ) {

		$task_meta = new Meta();
		$meta      = $task_meta->get( (int) $meta_id );

		// We should actually receive something.
		if ( empty( $meta ) || empty( $meta->data ) || ! is_array( $meta->data ) || count( $meta->data ) !== 4 ) {
			return;
		}

		// We expect a certain meta data structure for this task.
		list( $connection_data, $fields, $form_data, $entry_id ) = $meta->data;

		$this->run_action( $connection_data, $fields, $form_data, $entry_id );
	}

	/**
	 * Get the connection.
	 *
	 * @since 1.3.0
	 *
	 * @param string  $action     Connection data action.
	 * @param Account $account    Account model instance.
	 * @param string  $account_id Account ID.
	 *
	 * @return Connection|null|WP_Error
	 */
	private function get_connection( $action, $account, $account_id ) {

		if ( $action !== 'track_event' ) {
			return $account->get_connection( $account_id );
		}

		$connection = $account->get_tracker_connection( $account_id );

		if ( $connection !== null ) {
			return $connection;
		}

		/*
		 * It's possible that connection returns `null` because MA key does not exist.
		 * In this case, we'll attempt to fetch the MA key.
		 */
		$get_ma_key = $account->get_and_save_ma_key( $account_id );

		if ( ! is_wp_error( $get_ma_key ) ) {
			// Try to initialize tracker connection again.
			return $account->get_tracker_connection( $account_id );
		}

		return $get_ma_key;
	}

	/**
	 * Process the addon run action.
	 *
	 * @since 1.0.0
	 *
	 * @param array $connection_data Connection data.
	 * @param array $fields          Array of form fields.
	 * @param array $form_data       Form data and settings.
	 * @param int   $entry_id        ID of a saved entry.
	 *
	 * @noinspection NullPointerExceptionInspection
	 */
	private function run_action( $connection_data, $fields, $form_data, $entry_id ) {

		$account    = wpforms_sendinblue()->get( 'account' );
		$connection = $this->get_connection( $connection_data['action'], $account, $connection_data['account_id'] );

		if ( is_wp_error( $connection ) ) {
			$this->log_errors(
				$connection->get_error_message(),
				$connection_data,
				$entry_id,
				$form_data['id']
			);

			return;
		}

		if ( $connection === null ) {
			$this->log_errors(
				sprintf(
					'Invalid connection %s',
					esc_html( $connection_data['name'] )
				),
				$connection_data,
				$entry_id,
				$form_data['id']
			);

			return;
		}

		$class = '\WPFormsSendinblue\Provider\Actions\\' . str_replace( ' ', '', ucwords( str_replace( '_', ' ', $connection_data['action'] ) ) ) . 'Action';

		if ( ! class_exists( $class ) ) {
			$this->log_errors(
				sprintf(
					'Can\'t find the %s action class',
					esc_html( $connection_data['action'] )
				),
				$connection_data,
				$entry_id,
				$form_data['id']
			);

			return;
		}

		try {
			$action = new $class(
				$connection,
				new FieldMapper( $fields, $form_data, $connection_data, $entry_id ),
				$connection_data
			);

			$action->run();
		} catch ( Exception $e ) {
			$this->log_errors(
				$e->getMessage(),
				$connection_data,
				$entry_id,
				$form_data['id']
			);
		}
	}

	/**
	 * Log an API-related error with all the data.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message         Message.
	 * @param array  $connection_data Specific connection data that errored.
	 * @param int    $entry_id        Entry ID.
	 * @param int    $form_id         Form ID.
	 */
	protected function log_errors( $message, $connection_data, $entry_id, $form_id ) {

		wpforms_log(
			'Submission to Brevo failed' . "(#{$entry_id}).",
			[
				'message'    => $message,
				'connection' => $connection_data,
			],
			[
				'type'    => [ 'provider', 'error' ],
				'parent'  => $entry_id,
				'form_id' => $form_id,
			]
		);
	}
}