File: /home/globfdxw/www/wp-content/plugins/wpforms-google-calendar/src/Provider/Process.php
<?php
namespace WPFormsGoogleCalendar\Provider;
use Exception;
use WPForms\Tasks\Meta;
use WPFormsGoogleCalendar\Api\Client;
use WPFormsGoogleCalendar\Plugin;
use WPForms\Providers\Provider\Process as ProcessBase;
/**
* Google Calendar processing.
*
* @since 1.0.0
*/
class Process extends ProcessBase {
/**
* Async task action.
*
* @since 1.0.0
*/
private const ACTION = 'wpforms_google_calendar_process';
/**
* Connection data.
*
* @since 1.0.0
*
* @var array
*/
private $connection;
/**
* Add hooks.
*
* @since 1.0.0
*/
public function hooks(): void {
add_filter( 'wpforms_process_after_filter', [ $this, 'validate' ], 10, 3 );
add_action( self::ACTION, [ $this, 'process_action' ], 10, 4 );
}
/**
* Validate submission.
*
* @since 1.0.0
*
* @param array $fields Array of form fields.
* @param array $entry Submitted form content.
* @param array $form_data Form data and settings.
*
* @return array
*
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
*/
public function validate( $fields, $entry, $form_data ): array {
if ( empty( $form_data['providers'][ Plugin::SLUG ] ) ) {
return $fields;
}
$this->fields = $fields;
$this->form_data = $form_data;
foreach ( $form_data['providers'][ Plugin::SLUG ] as $key => $connection_data ) {
if ( $key === '__lock__' ) {
continue;
}
$this->connection = $connection_data;
$this->validate_each_connection();
}
return $fields;
}
/**
* Validate a specific connection.
*
* @since 1.0.0
*
* @return void
* @noinspection NullPointerExceptionInspection
*/
private function validate_each_connection(): void {
if ( ! $this->process_conditionals( $this->fields, $this->form_data, $this->connection ) ) {
return;
}
$client = $this->get_client();
if ( $client === null ) {
return;
}
$mapper = $this->get_mapper( $client );
$start_field_id = $mapper->get_option( 'start_datetime' );
$duration = $mapper->get_option( 'duration' );
$end_field_id = $mapper->get_option( 'end_datetime' );
if ( $start_field_id === null || $end_field_id === null || $duration !== 'user_defined' ) {
return;
}
if ( empty( $this->fields[ $start_field_id ]['unix'] ) || empty( $this->fields[ $end_field_id ]['unix'] ) ) {
return;
}
if ( $this->fields[ $start_field_id ]['unix'] >= $this->fields[ $end_field_id ]['unix'] ) {
wpforms()->obj( 'process' )->errors[ $this->form_data['id'] ]['header'] = wpforms_google_calendar()->get( 'validation' )->get_validation_message( 'end-before-start' );
}
}
/**
* Receive all wpforms_process_complete params and do the actual processing.
*
* @since 1.0.0
*
* @param array $fields Array of form fields.
* @param array $entry Submitted form content.
* @param array $form_data Form data and settings.
* @param int $entry_id ID of a saved entry.
*/
public function process( $fields, $entry, $form_data, $entry_id ): void {
if ( empty( $form_data['providers'][ Plugin::SLUG ] ) ) {
return;
}
$this->fields = $fields;
$this->form_data = $form_data;
$this->entry_id = $entry_id;
foreach ( $form_data['providers'][ Plugin::SLUG ] as $key => $connection_data ) {
if ( $key === '__lock__' ) {
continue;
}
$this->connection = $connection_data;
$this->process_each_connection();
}
}
/**
* Iteration loop for connections - add action for each connection.
*
* @since 1.0.0
*/
protected function process_each_connection(): void {
$tasks_obj = wpforms()->obj( 'tasks' );
if ( ! $tasks_obj ) {
return;
}
if ( ! $this->process_conditionals( $this->fields, $this->form_data, $this->connection ) ) {
wpforms_log(
sprintf( 'The Google Calendar connection %s was not processed due to conditional logic.', $this->connection['name'] ?? '' ),
$this->fields,
[
'type' => [ 'provider', 'conditional_logic' ],
'parent' => $this->entry_id,
'form_id' => $this->form_data['id'],
]
);
return;
}
$tasks_obj
->create( self::ACTION )
->async()
->params( $this->connection, $this->fields, $this->form_data, $this->entry_id )
->register();
}
/**
* Process the addon async tasks.
*
* @since 1.0.0
*
* @param int|mixed $meta_id Task meta ID.
*/
public function process_action( $meta_id ): void {
$task_meta = new Meta();
$meta = $task_meta->get( $meta_id );
// We should actually receive something.
if ( empty( $meta ) || empty( $meta->data ) ) {
return;
}
if ( count( $meta->data ) !== 4 ) {
return;
}
// We expect a certain metadata structure for this task.
[ $this->connection, $this->fields, $this->form_data, $this->entry_id ] = $meta->data;
$this->create_event();
}
/**
* Create a Google Calendar event.
*
* @since 1.0.0
*/
private function create_event(): void {
$client = $this->get_client();
if ( $client === null ) {
return;
}
$mapper = $this->get_mapper( $client );
try {
$client->create_event( $mapper->prepare_event() );
} catch ( Exception $e ) {
wpforms_log(
sprintf( 'Google Calendar event creation failed: %s', $e->getMessage() ),
[
'connection' => $this->connection,
'fields' => $this->fields,
],
[
'type' => [ 'provider', 'error' ],
'parent' => $this->entry_id,
'form_id' => $this->form_data['id'],
]
);
}
}
/**
* Get a client by account ID.
*
* @since 1.0.0
*
* @return Client|null
*/
private function get_client(): ?Client {
if ( empty( $this->connection['account_id'] ) ) {
return null;
}
$account = wpforms_google_calendar()->get( 'account' );
if ( ! $account ) {
return null;
}
return $account->get_client_by_id( $this->connection['account_id'] );
}
/**
* Get a field mapper.
*
* @since 1.0.0
*
* @param Client $client Client instance.
*
* @return FieldMapper
*/
private function get_mapper( Client $client ): FieldMapper {
return new FieldMapper( $this->connection, $this->form_data, $this->fields, $this->entry_id, $client->get_calendar_list() );
}
}