['label','value','type'] * @type string $email * @type int $files_count * } * @return array|null The stored receipt row, or null when receipts are off for this form. */ public static function create( array $submission ): ?array { $form_key = $submission['source'] . ':' . $submission['form_id']; $cfg = Settings::form( $form_key ); /** Allow code to force receipts on or off for a form. */ if ( ! apply_filters( 'actr_form_enabled', ! empty( $cfg['enabled'] ), $form_key, $submission ) ) { return null; } $snapshot = SnapshotService::build( (array) $submission['fields'], $cfg['fields'] ); $canonical = SnapshotService::canonical( $snapshot ); $created_at = current_time( 'mysql', true ); // UTC. $settings = Settings::get(); $retention = (int) $settings['retention_days']; $id = 0; $reference = ''; for ( $attempt = 0; $attempt < 4 && ! $id; $attempt++ ) { $reference = ReferenceGenerator::generate( $cfg, $attempt >= 3 ); // Suffix only as a last resort. $id = ReceiptRepository::insert( array( 'reference' => $reference, 'token_salt' => TokenService::new_salt(), 'source' => (string) $submission['source'], 'form_id' => (string) $submission['form_id'], 'form_title' => (string) ( $submission['form_title'] ?? '' ), 'status_key' => StatusPipeline::DEFAULT_KEY, 'email' => (string) ( $submission['email'] ?? '' ), 'user_id' => get_current_user_id(), 'snapshot' => $canonical, 'snapshot_hash' => SnapshotService::hash( $reference, $created_at, $canonical ), 'files_count' => (int) ( $submission['files_count'] ?? 0 ), 'created_at' => $created_at, 'purge_after' => $retention > 0 ? gmdate( 'Y-m-d H:i:s', time() + $retention * DAY_IN_SECONDS ) : null, ) ); } if ( ! $id ) { return null; } EventRepository::add( $id, EventRepository::CREATED, array( 'to_status' => StatusPipeline::DEFAULT_KEY ) ); $receipt = ReceiptRepository::find( $id ); /** * Fires after a receipt is stored. * * @param array $receipt * @param array $submission */ do_action( 'actr_receipt_created', $receipt, $submission ); return $receipt; } /** * @return bool True if the status changed. */ public static function change_status( array $receipt, string $new_status, string $public_note = '', string $private_note = '', bool $notify = true ): bool { if ( ! StatusPipeline::exists( $new_status ) ) { return false; } $old_status = (string) $receipt['status_key']; $id = (int) $receipt['id']; if ( $old_status !== $new_status ) { ReceiptRepository::update_status( $id, $new_status ); EventRepository::add( $id, EventRepository::STATUS_CHANGED, array( 'from_status' => $old_status, 'to_status' => $new_status, 'public_note' => $public_note, 'private_note' => $private_note, 'actor_id' => get_current_user_id(), ) ); } elseif ( '' !== $public_note || '' !== $private_note ) { self::add_note( $receipt, $public_note, $private_note ); } else { return false; } $receipt = ReceiptRepository::find( $id ); if ( $notify && $receipt ) { NotificationService::send_update( $receipt, $public_note ); } do_action( 'actr_status_changed', $receipt, $old_status, $new_status ); return true; } public static function add_note( array $receipt, string $public_note, string $private_note ): void { EventRepository::add( (int) $receipt['id'], EventRepository::NOTE, array( 'public_note' => $public_note, 'private_note' => $private_note, 'actor_id' => get_current_user_id(), ) ); ReceiptRepository::touch( (int) $receipt['id'] ); } /** * Private link to the visitor's status page. Empty when no status page is set. */ public static function status_url( array $receipt, array $extra_args = array() ): string { $settings = Settings::get(); $page_id = (int) $settings['status_page_id']; $base = $page_id ? get_permalink( $page_id ) : ''; if ( ! $base ) { return ''; } return add_query_arg( array_merge( array( 'ref' => rawurlencode( $receipt['reference'] ), 'k' => TokenService::token( $receipt['reference'], $receipt['token_salt'] ), ), $extra_args ), $base ); } }