| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Placeholder; |
| 9 |
|
| 10 |
/** |
| 11 |
* Resolves {{wp.*}} placeholders in public form field strings at serve time. |
| 12 |
*/ |
| 13 |
class PlaceholderLocalizationHelper |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @return array<string, string|int> |
| 17 |
*/ |
| 18 |
public static function getGeneralData(): array |
| 19 |
{ |
| 20 |
return PlaceholderService::buildGeneralData(0, [ |
| 21 |
'postId' => self::resolvePostId(), |
| 22 |
'referer' => wp_get_referer() ?: '', |
| 23 |
]); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param array<string, string|int> $generalData |
| 28 |
*/ |
| 29 |
public static function resolveFieldString(string $value, array $generalData): string |
| 30 |
{ |
| 31 |
if (strpos($value, '{{') === false) { |
| 32 |
return $value; |
| 33 |
} |
| 34 |
|
| 35 |
return PlaceholderService::replacePlaceholders($value, [], $generalData); |
| 36 |
} |
| 37 |
|
| 38 |
private static function resolvePostId(): int |
| 39 |
{ |
| 40 |
$postId = get_queried_object_id(); |
| 41 |
if ($postId) { |
| 42 |
return (int) $postId; |
| 43 |
} |
| 44 |
|
| 45 |
$theId = get_the_ID(); |
| 46 |
if ($theId) { |
| 47 |
return (int) $theId; |
| 48 |
} |
| 49 |
|
| 50 |
if (is_admin()) { |
| 51 |
$adminPostId = filter_input(INPUT_GET, 'post', FILTER_VALIDATE_INT); |
| 52 |
if (is_int($adminPostId) && $adminPostId > 0) { |
| 53 |
return $adminPostId; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return 0; |
| 58 |
} |
| 59 |
} |
| 60 |
|