PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Helpers / ClassificationLandingPageMetadata.php

ClassificationLandingPageMetadata.php in Yatra – Travel Booking & Tour Operator Software trunk, at app/Helpers/ClassificationLandingPageMetadata.php

125 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Helpers;
6
7 /**
8 * Decode and merge {@see classifications.metadata} payloads (serialized PHP arrays, occasional JSON array strings).
9 *
10 * Used so {@see landing_page_id} and SEO keys stay in one record without overwriting each other.
11 */
12 final class ClassificationLandingPageMetadata
13 {
14 /**
15 * Decode a classification `metadata` column value into an associative array.
16 *
17 * @param mixed $stored Serialized string from DB, JSON string, empty, or already an array (tests).
18 *
19 * @return array<string, mixed>
20 */
21 public static function decodeStoredMetadata($stored): array
22 {
23 if ($stored === null || $stored === '') {
24 return [];
25 }
26
27 if (is_array($stored)) {
28 return $stored;
29 }
30
31 if (!is_string($stored)) {
32 return [];
33 }
34
35 $trimmed = trim($stored);
36 if ($trimmed === '') {
37 return [];
38 }
39
40 $fromPhp = maybe_unserialize($trimmed);
41 if (is_array($fromPhp)) {
42 return $fromPhp;
43 }
44
45 $slashes = stripslashes($trimmed);
46 if ($slashes !== $trimmed) {
47 $fromPhp2 = maybe_unserialize($slashes);
48 if (is_array($fromPhp2)) {
49 return $fromPhp2;
50 }
51 }
52
53 $fromJson = json_decode($trimmed, true);
54
55 return is_array($fromJson) ? $fromJson : [];
56 }
57
58 /**
59 * Existing row metadata merged with inbound `metadata` from the REST payload (decoded).
60 * Request keys overwrite stored keys for same name.
61 *
62 * @param array<string, mixed> $payload Current request slice (typically full body); reads `metadata` only.
63 *
64 * @return array<string, mixed>
65 */
66 public static function baseMetadataMergedWithRequest(
67 ?int $classificationId,
68 object $repository,
69 array $payload
70 ): array {
71 $fromDb = [];
72 if ($classificationId !== null && $classificationId > 0 && method_exists($repository, 'find')) {
73 $row = $repository->find($classificationId);
74 if ($row && isset($row->metadata)) {
75 $fromDb = self::decodeStoredMetadata($row->metadata);
76 }
77 }
78
79 $fromRequest = [];
80 if (isset($payload['metadata']) && is_array($payload['metadata'])) {
81 $fromRequest = $payload['metadata'];
82 } elseif (isset($payload['metadata'])) {
83 $fromRequest = self::decodeStoredMetadata($payload['metadata']);
84 }
85
86 return array_merge($fromDb, $fromRequest);
87 }
88
89 /**
90 * Merges {@see landing_page_id} from REST/admin payloads into classification `metadata`
91 * (always grounded on stored row metadata + inbound metadata).
92 *
93 * @param array<string, mixed> $data Incoming create/update payload (modified in place).
94 */
95 public static function mergeLandingPageIntoData(array &$data, ?int $classificationId, object $repository): void
96 {
97 if (!array_key_exists('landing_page_id', $data)) {
98 return;
99 }
100
101 $raw = $data['landing_page_id'];
102 unset($data['landing_page_id']);
103
104 $merged = self::baseMetadataMergedWithRequest($classificationId, $repository, $data);
105
106 $pageId = 0;
107 if ($raw !== null && $raw !== '') {
108 $pageId = absint($raw);
109 }
110
111 if ($pageId <= 0) {
112 unset($merged['landing_page_id']);
113 } else {
114 $post = get_post($pageId);
115 if ($post && $post->post_type === 'page') {
116 $merged['landing_page_id'] = $pageId;
117 } else {
118 unset($merged['landing_page_id']);
119 }
120 }
121
122 $data['metadata'] = $merged;
123 }
124 }
125