Files
2026-07-02 15:54:39 -06:00

75 lines
3.1 KiB
PHP

<?php
/**
* Entry_Integration - records the INSTANDA result against the native Gravity Forms
* entry: registered entry meta (sortable, exportable Entries columns) and the
* {instanda_quote_ref} / {instanda_quote_url} merge tags. The meta values are
* written by the add-on's process_feed(); this class registers and reads them.
*
* @package ESP\Instanda
*/
declare(strict_types=1);
namespace ESP\Instanda;
final class Entry_Integration {
public const META_REF = 'instanda_quote_ref';
public const META_URL = 'instanda_quote_url';
public const META_STATUS = 'instanda_status';
public const META_ENV = 'instanda_environment';
/** gform_entry_meta - registers our meta as Entries-list columns. */
public function register_entry_meta(array $entry_meta, int $form_id): array {
$entry_meta[self::META_REF] = [
'label' => 'INSTANDA quoteRef',
'is_numeric' => false,
'is_default_column' => true,
];
$entry_meta[self::META_STATUS] = [
'label' => 'INSTANDA status',
'is_numeric' => false,
'is_default_column' => true,
];
$entry_meta[self::META_URL] = [
'label' => 'INSTANDA quote URL',
'is_numeric' => false,
'is_default_column' => false,
];
$entry_meta[self::META_ENV] = [
'label' => 'INSTANDA environment',
'is_numeric' => false,
'is_default_column' => false,
];
return $entry_meta;
}
/** gform_custom_merge_tags - advertise our merge tags in the editor. */
public function register_merge_tags(array $merge_tags, int $form_id, array $fields, int $element_id): array {
$merge_tags[] = ['label' => 'INSTANDA quote ref', 'tag' => '{instanda_quote_ref}'];
$merge_tags[] = ['label' => 'INSTANDA quote URL', 'tag' => '{instanda_quote_url}'];
return $merge_tags;
}
/** gform_replace_merge_tags - resolve our merge tags from entry meta. */
public function replace_merge_tags(string $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format): string {
if (!is_array($entry) || empty($entry['id'])) {
return $text;
}
$ref = (string) gform_get_meta((int) $entry['id'], self::META_REF);
$url = (string) gform_get_meta((int) $entry['id'], self::META_URL);
$text = str_replace('{instanda_quote_ref}', esc_html($ref), $text);
$text = str_replace('{instanda_quote_url}', esc_url($url), $text);
return $text;
}
/** Write the result to the entry. Called from the add-on's process_feed(). */
public static function record(int $entry_id, string $quote_ref, string $quote_url, string $status, string $environment): void {
gform_update_meta($entry_id, self::META_REF, $quote_ref);
gform_update_meta($entry_id, self::META_URL, $quote_url);
gform_update_meta($entry_id, self::META_STATUS, $status);
gform_update_meta($entry_id, self::META_ENV, $environment);
}
}