| 1 |
<?php |
| 2 |
/** |
| 3 |
* Legacy Parser - handles backward compatibility for old generator configurations. |
| 4 |
* |
| 5 |
* @package Jet_Form_Builder\Generators |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Jet_Form_Builder\Generators; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Legacy_Parser class. |
| 17 |
* |
| 18 |
* Parses legacy macro-style configurations (e.g., "26|ID|post_title|calc_field") |
| 19 |
* and converts them to structured settings for the new generator system. |
| 20 |
*/ |
| 21 |
class Legacy_Parser { |
| 22 |
|
| 23 |
/** |
| 24 |
* Known generator IDs that use pipe-delimited macro format. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private static $pipe_delimited_generators = array( |
| 29 |
'get_from_query', |
| 30 |
); |
| 31 |
|
| 32 |
/** |
| 33 |
* Parses legacy field value into structured settings. |
| 34 |
* |
| 35 |
* @param string $generator_id Generator ID. |
| 36 |
* @param string $field_value Legacy field value (may contain macros). |
| 37 |
* |
| 38 |
* @return array Parsed settings with proper keys. |
| 39 |
*/ |
| 40 |
public static function parse_legacy_field( string $generator_id, string $field_value ): array { |
| 41 |
if ( empty( $field_value ) ) { |
| 42 |
return array(); |
| 43 |
} |
| 44 |
|
| 45 |
// Check if this generator uses pipe-delimited format |
| 46 |
if ( in_array( $generator_id, self::$pipe_delimited_generators, true ) ) { |
| 47 |
return self::parse_pipe_delimited( $generator_id, $field_value ); |
| 48 |
} |
| 49 |
|
| 50 |
// For all other generators, return as-is |
| 51 |
return array( |
| 52 |
'generator_field' => $field_value, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Parses pipe-delimited macro format. |
| 58 |
* |
| 59 |
* @param string $generator_id Generator ID. |
| 60 |
* @param string $value Pipe-delimited value. |
| 61 |
* |
| 62 |
* @return array Parsed settings. |
| 63 |
*/ |
| 64 |
private static function parse_pipe_delimited( string $generator_id, string $value ): array { |
| 65 |
switch ( $generator_id ) { |
| 66 |
case 'get_from_query': |
| 67 |
return self::parse_je_query_macro( $value ); |
| 68 |
|
| 69 |
default: |
| 70 |
return array( 'generator_field' => $value ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Parses JetEngine Query macro format. |
| 76 |
* |
| 77 |
* Format: "query_id|value_field|label_field|calc_field" |
| 78 |
* Examples: |
| 79 |
* - "26|ID|post_title" (3 parts) |
| 80 |
* - "26|ID|post_title|price" (4 parts) |
| 81 |
* |
| 82 |
* @param string $value Pipe-delimited macro string. |
| 83 |
* |
| 84 |
* @return array Parsed settings. |
| 85 |
*/ |
| 86 |
private static function parse_je_query_macro( string $value ): array { |
| 87 |
$parts = explode( '|', $value ); |
| 88 |
|
| 89 |
return array( |
| 90 |
'query_id' => $parts[0] ?? '', |
| 91 |
'value_field' => $parts[1] ?? 'ID', |
| 92 |
'label_field' => $parts[2] ?? 'post_title', |
| 93 |
'calc_field' => $parts[3] ?? '', |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Checks if block attributes contain legacy data that needs migration. |
| 99 |
* |
| 100 |
* @param array $attrs Block attributes. |
| 101 |
* |
| 102 |
* @return bool True if migration is needed. |
| 103 |
*/ |
| 104 |
public static function needs_migration( array $attrs ): bool { |
| 105 |
// Must have a generator function selected |
| 106 |
if ( empty( $attrs['generator_function'] ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
$generator_id = $attrs['generator_function']; |
| 111 |
|
| 112 |
// Check for pipe-delimited generators |
| 113 |
if ( in_array( $generator_id, self::$pipe_delimited_generators, true ) ) { |
| 114 |
// Has legacy field value with pipe delimiter |
| 115 |
if ( ! empty( $attrs['generator_field'] ) && strpos( $attrs['generator_field'], '|' ) !== false ) { |
| 116 |
// Check if new attributes don't exist yet |
| 117 |
$new_attr_prefix = 'gen_' . $generator_id . '_'; |
| 118 |
foreach ( $attrs as $key => $val ) { |
| 119 |
if ( strpos( $key, $new_attr_prefix ) === 0 && ! empty( $val ) ) { |
| 120 |
return false; // Already migrated |
| 121 |
} |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Migrates legacy attributes to new format. |
| 132 |
* |
| 133 |
* @param array $attrs Original block attributes. |
| 134 |
* |
| 135 |
* @return array Migrated attributes. |
| 136 |
*/ |
| 137 |
public static function migrate( array $attrs ): array { |
| 138 |
if ( ! self::needs_migration( $attrs ) ) { |
| 139 |
return $attrs; |
| 140 |
} |
| 141 |
|
| 142 |
$generator_id = $attrs['generator_function']; |
| 143 |
$legacy_value = $attrs['generator_field'] ?? ''; |
| 144 |
|
| 145 |
// Parse legacy format |
| 146 |
$parsed = self::parse_legacy_field( $generator_id, $legacy_value ); |
| 147 |
|
| 148 |
if ( empty( $parsed ) ) { |
| 149 |
return $attrs; |
| 150 |
} |
| 151 |
|
| 152 |
// Convert to new attribute format |
| 153 |
$new_attrs = $attrs; |
| 154 |
|
| 155 |
foreach ( $parsed as $key => $value ) { |
| 156 |
// Skip if it's the original generator_field |
| 157 |
if ( 'generator_field' === $key ) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
$attr_name = 'gen_' . $generator_id . '_' . $key; |
| 162 |
$new_attrs[ $attr_name ] = $value; |
| 163 |
} |
| 164 |
|
| 165 |
// Clear legacy field to prevent re-migration |
| 166 |
// Note: We keep the original value for backward compatibility |
| 167 |
// but new code should use the new attributes |
| 168 |
$new_attrs['_legacy_generator_field'] = $legacy_value; |
| 169 |
|
| 170 |
return $new_attrs; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Converts new structured settings back to legacy format. |
| 175 |
* Useful for backward compatibility with third-party code. |
| 176 |
* |
| 177 |
* @param string $generator_id Generator ID. |
| 178 |
* @param array $settings Structured settings. |
| 179 |
* |
| 180 |
* @return string Legacy format string. |
| 181 |
*/ |
| 182 |
public static function to_legacy_format( string $generator_id, array $settings ): string { |
| 183 |
switch ( $generator_id ) { |
| 184 |
case 'get_from_query': |
| 185 |
$parts = array( |
| 186 |
$settings['query_id'] ?? '', |
| 187 |
$settings['value_field'] ?? 'ID', |
| 188 |
$settings['label_field'] ?? 'post_title', |
| 189 |
); |
| 190 |
|
| 191 |
// Add calc_field only if it exists |
| 192 |
if ( ! empty( $settings['calc_field'] ) ) { |
| 193 |
$parts[] = $settings['calc_field']; |
| 194 |
} |
| 195 |
|
| 196 |
return implode( '|', $parts ); |
| 197 |
|
| 198 |
default: |
| 199 |
return $settings['generator_field'] ?? ''; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Extracts settings from block attributes, handling both legacy and new formats. |
| 205 |
* |
| 206 |
* @param string $generator_id Generator ID. |
| 207 |
* @param array $attrs Block attributes. |
| 208 |
* |
| 209 |
* @return array Settings in structured format. |
| 210 |
*/ |
| 211 |
public static function extract_settings( string $generator_id, array $attrs ): array { |
| 212 |
// First, try to get from new format |
| 213 |
$new_attr_prefix = 'gen_' . $generator_id . '_'; |
| 214 |
$settings = array(); |
| 215 |
$has_new_format = false; |
| 216 |
|
| 217 |
foreach ( $attrs as $key => $value ) { |
| 218 |
if ( strpos( $key, $new_attr_prefix ) === 0 ) { |
| 219 |
$setting_key = substr( $key, strlen( $new_attr_prefix ) ); |
| 220 |
$settings[ $setting_key ] = $value; |
| 221 |
$has_new_format = true; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if ( $has_new_format ) { |
| 226 |
return $settings; |
| 227 |
} |
| 228 |
|
| 229 |
// Fall back to legacy format |
| 230 |
if ( ! empty( $attrs['generator_field'] ) ) { |
| 231 |
return self::parse_legacy_field( $generator_id, $attrs['generator_field'] ); |
| 232 |
} |
| 233 |
|
| 234 |
// Check for direct attributes (like num_range_manual) |
| 235 |
$legacy_attrs = array( |
| 236 |
'generator_numbers_min', |
| 237 |
'generator_numbers_max', |
| 238 |
'generator_numbers_step', |
| 239 |
'generator_field', |
| 240 |
); |
| 241 |
|
| 242 |
foreach ( $legacy_attrs as $attr ) { |
| 243 |
if ( isset( $attrs[ $attr ] ) ) { |
| 244 |
$settings[ $attr ] = $attrs[ $attr ]; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
return $settings; |
| 249 |
} |
| 250 |
} |
| 251 |
|