Skip to content

Commit d5970f5

Browse files
author
Mike van den Hoek
committed
(feat): prefill all advanced date fields
1 parent a4b5387 commit d5970f5

File tree

16 files changed

+117
-58
lines changed

16 files changed

+117
-58
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
- Tested up to: WordPress 6.3.2
44

5+
## v1.1
6+
7+
### Feat
8+
9+
- Prefill all advanced date fields.
10+
- Small clean-up/refactoring & run composer format script.
11+
512
## v1.0.17
613

714
### Feat

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php // Silence is golden
1+
<?php // Silence is golden

prefill-gravity-forms.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: Yard | BRP Prefill GravityForms
55
* Plugin URI: https://www.openwebconcept.nl/
66
* Description: Prefill GravityForms fields, based on the dutch BSN number. Retrieve personal information and place these values in the corrensponding fields.
7-
* Version: 1.0.17
7+
* Version: 1.1
88
* Author: Yard | Digital Agency
99
* Author URI: https://www.yard.nl/
1010
* License: GPL-3.0
@@ -16,11 +16,11 @@
1616
/**
1717
* If this file is called directly, abort.
1818
*/
19-
if (!defined('WPINC')) {
19+
if (! defined('WPINC')) {
2020
die;
2121
}
2222

23-
define('PG_VERSION', '1.0.17');
23+
define('PG_VERSION', '1.1');
2424
define('PG_DIR', basename(__DIR__));
2525
define('PG_ROOT_PATH', __DIR__);
2626
define('PG_PLUGIN_SLUG', 'prefill-gravity-forms');

src/PrefillGravityForms/Controllers/BaseController.php

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
namespace OWC\PrefillGravityForms\Controllers;
44

5+
use DateTime;
6+
use GF_Field;
57
use Exception;
6-
use function OWC\PrefillGravityForms\Foundation\Helpers\decrypt;
7-
use function OWC\PrefillGravityForms\Foundation\Helpers\view;
8-
use function Yard\DigiD\Foundation\Helpers\resolve;
98
use OWC\PrefillGravityForms\Foundation\TeamsLogger;
109
use OWC\PrefillGravityForms\GravityForms\GravityFormsSettings;
1110

11+
use function Yard\DigiD\Foundation\Helpers\resolve;
12+
use function OWC\PrefillGravityForms\Foundation\Helpers\view;
13+
use function OWC\PrefillGravityForms\Foundation\Helpers\decrypt;
14+
1215
abstract class BaseController
1316
{
1417
protected GravityFormsSettings $settings;
1518
protected TeamsLogger $teams;
1619
protected string $supplier;
17-
20+
1821
public function __construct()
1922
{
2023
$this->settings = GravityFormsSettings::make();
@@ -25,11 +28,11 @@ public function resolveTeams(): TeamsLogger
2528
{
2629
try {
2730
if (! function_exists('Yard\DigiD\Foundation\Helpers\resolve')) {
28-
throw new \Exception;
31+
throw new Exception();
2932
}
3033

3134
return TeamsLogger::make(resolve('teams'));
32-
} catch (\Exception $e) {
35+
} catch (Exception $e) {
3336
return TeamsLogger::make(new \Psr\Log\NullLogger());
3437
}
3538
}
@@ -73,21 +76,23 @@ protected function preFillFields(array $form, array $response): array
7376
}
7477

7578
$foundValue = $this->findLinkedValue($linkedValue, $response);
76-
79+
7780
if (empty($foundValue)) {
78-
$field->cssClass = 'owc_prefilled'; // When field is mapped but there is no value found, set to read-only.
81+
$field->cssClass = 'owc_prefilled'; // When field has mapping but there is no value found, set to read-only.
82+
7983
continue;
8084
}
8185

8286
if ($field->type === 'text') {
83-
$field->defaultValue = ucfirst($foundValue);
84-
$field->cssClass = 'owc_prefilled';
87+
$this->handleFieldText($field, $foundValue);
88+
89+
continue;
8590
}
8691

8792
if ($field->type === 'date') {
88-
$field->defaultValue = (new \DateTime($foundValue))->format('d-m-Y');
89-
$field->displayOnly = true;
90-
$field->cssClass = 'owc_prefilled';
93+
$this->handleFieldDate($field, $foundValue);
94+
95+
continue;
9196
}
9297
}
9398

@@ -110,12 +115,13 @@ public function findLinkedValue(string $linkedValue = '', array $response = []):
110115
public function explodeDotNotationValue(string $dotNotationString, array $response): string
111116
{
112117
$exploded = explode('.', $dotNotationString);
113-
$holder = [];
118+
$holder = [];
114119

115120
foreach ($exploded as $key => $item) {
116121
if ($key === 0) {
117122
// Place the wanted part of the response in $holder.
118123
$holder = $response[$item] ?? '';
124+
119125
continue;
120126
}
121127

@@ -124,9 +130,9 @@ public function explodeDotNotationValue(string $dotNotationString, array $respon
124130
break;
125131
}
126132

127-
// If holder is a multidimensional array, unflatten.
128-
if (!empty($holder[0]) && is_array($holder[0])) {
129-
$holder = $this->unflattenHolderArray($holder);
133+
// If holder is a multidimensional array, flatten.
134+
if (! empty($holder[0]) && is_array($holder[0])) {
135+
$holder = $this->flattenMultidimensionalArray($holder);
130136
}
131137

132138
// Place the nested part of the response in $holder.
@@ -136,15 +142,57 @@ public function explodeDotNotationValue(string $dotNotationString, array $respon
136142
return is_string($holder) || is_numeric($holder) ? $holder : '';
137143
}
138144

139-
protected function unflattenHolderArray(array $holder): array
145+
protected function flattenMultidimensionalArray(array $array): array
140146
{
141-
$backupHolder = [];
147+
$holder = [];
142148

143-
foreach ($holder as $part) {
144-
$backupHolder = array_merge($backupHolder, $part);
149+
foreach ($array as $part) {
150+
$holder = array_merge($holder, $part);
151+
}
152+
153+
return $holder;
154+
}
155+
156+
protected function handleFieldText(GF_Field $field, string $foundValue): void
157+
{
158+
if ($this->isPossibleDate($foundValue)) {
159+
$field->defaultValue = (new \DateTime($foundValue))->format('d-m-Y');
160+
} else {
161+
$field->defaultValue = ucfirst($foundValue);
145162
}
146163

147-
return $backupHolder;
164+
$field->cssClass = 'owc_prefilled';
165+
}
166+
167+
public function isPossibleDate(string $value): bool
168+
{
169+
return (date('Y-m-d', strtotime($value)) == $value);
170+
}
171+
172+
protected function handleFieldDate(GF_Field $field, string $foundValue): void
173+
{
174+
try {
175+
$date = new DateTime($foundValue);
176+
} catch(Exception $e) {
177+
return;
178+
}
179+
180+
// Field consists of 1 part.
181+
if (empty($field->inputs) || $field->dateType === 'datepicker') {
182+
$field->defaultValue = $date->format('d-m-Y');
183+
$field->displayOnly = true;
184+
$field->cssClass = 'owc_prefilled';
185+
186+
return;
187+
}
188+
189+
// Field consists of 3 parts which are represented by the input attribute.
190+
if (! empty($field->inputs) && ($field->dateType === 'datefield' || $field->dateType === 'datedropdown')) {
191+
$field->inputs[0]['defaultValue'] = $date->format('m');
192+
$field->inputs[1]['defaultValue'] = $date->format('d');
193+
$field->inputs[2]['defaultValue'] = $date->format('Y');
194+
$field->cssClass = 'owc_prefilled';
195+
}
148196
}
149197

150198
public function getRequestURL(string $identifier = '', string $expand = ''): string
@@ -198,22 +246,22 @@ protected function handleCurl(array $args): array
198246
if (! empty($this->settings->getPassphrase())) {
199247
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $this->settings->getPassphrase());
200248
}
201-
249+
202250
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
203251
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
204-
252+
205253
$output = curl_exec($curl);
206-
254+
207255
if (curl_error($curl)) {
208256
throw new \Exception(curl_error($curl));
209257
}
210-
258+
211259
$decoded = json_decode($output, true);
212-
260+
213261
if (! $decoded || json_last_error() !== JSON_ERROR_NONE) {
214262
throw new \Exception('Something went wrong with decoding of the JSON output.');
215263
}
216-
264+
217265
return $decoded;
218266
} catch (\Exception $e) {
219267
return [

src/PrefillGravityForms/Controllers/EnableUController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function handle(array $form)
3636
'message' => 'Retrieving prefill data failed.',
3737
'status' => $response['status']
3838
]);
39+
3940
return $form;
4041
}
41-
42+
4243
echo $this->disableFormFields();
4344

4445
return $this->preFillFields($form, $response);

src/PrefillGravityForms/Foundation/Cryptor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(bool $method = false)
1717
$this->key = $key;
1818
}
1919
if ($method) {
20-
if (!in_array(strtolower($method), openssl_get_cipher_methods())) {
20+
if (! in_array(strtolower($method), openssl_get_cipher_methods())) {
2121
throw new \Exception(__METHOD__ . ": unrecognised cipher method: {$method}");
2222
}
2323
$this->method = $method;
@@ -32,6 +32,7 @@ protected function ivBytes()
3232
public function encrypt($data)
3333
{
3434
$iv = openssl_random_pseudo_bytes($this->ivBytes());
35+
3536
return bin2hex($iv) . openssl_encrypt($data, $this->method, $this->key, 0, $iv);
3637
}
3738

@@ -45,6 +46,7 @@ public function decrypt($data)
4546
return openssl_decrypt($crypted_string, $this->method, $this->key, 0, hex2bin($iv));
4647
}
4748
}
49+
4850
return false; // failed to decrypt
4951
}
5052
}

src/PrefillGravityForms/Foundation/Plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class Plugin
1414
*
1515
* @var string
1616
*/
17-
const NAME = 'prefill-gravity-forms';
17+
public const NAME = 'prefill-gravity-forms';
1818

1919
/**
2020
* Version of the plugin.
2121
* Used for setting versions of enqueue scripts and styles.
2222
*/
23-
const VERSION = \PG_VERSION;
23+
public const VERSION = \PG_VERSION;
2424

2525
/**
2626
* Path to the root of the plugin.

src/PrefillGravityForms/Foundation/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function render(string $templateFile = '', array $vars = []): string
3131
if (! is_file($this->templateDirectory . $templateFile)) {
3232
return '';
3333
}
34-
34+
3535
$this->bindAll($vars);
3636
ob_start();
3737
include($this->templateDirectory . $templateFile);

src/PrefillGravityForms/GravityForms/GravityFormsAddon.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace OWC\PrefillGravityForms\GravityForms;
44

5-
use function OWC\PrefillGravityForms\Foundation\Helpers\storage_path;
65
use GFAddOn;
76

7+
use function OWC\PrefillGravityForms\Foundation\Helpers\storage_path;
8+
89
class GravityFormsAddon extends GFAddOn
910
{
1011
/**

src/PrefillGravityForms/GravityForms/GravityFormsFieldSettings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace OWC\PrefillGravityForms\GravityForms;
44

5-
use function OWC\PrefillGravityForms\Foundation\Helpers\get_supplier;
65
use function OWC\PrefillGravityForms\Foundation\Helpers\view;
6+
use function OWC\PrefillGravityForms\Foundation\Helpers\get_supplier;
77

88
class GravityFormsFieldSettings
99
{

0 commit comments

Comments
 (0)