add_filter( 'voxel/dynamic-tags/modifiers', function( $modifiers ) {
class Get_Address_Parts extends \Voxel\Dynamic_Tags\Base_Modifier {
public function get_label(): string {
return 'Get address parts';
}
public function get_key(): string {
return 'get_address_parts';
}
public function get_arguments(): array {
return [
'parts' => [
'type' => \Voxel\Form_Models\Text_Model::class,
'label' => _x( 'Enter parts, e.g. 1,3', 'modifiers', 'voxel-backend' ),
'classes' => 'x-col-12',
],
];
}
public function apply( $value, $args, $group ) {
$parts = explode( ', ', $value );
$requested = array_filter( array_map( 'intval', explode( ',', $args[0] ?? '' ) ) );
$last_item = trim(end($parts));
if ($last_item === 'United States' || $last_item === 'Canada') {
array_pop($parts);
}
if (preg_match('/[A-Z]\d[A-Z]\s\d[A-Z]\d/', $value)) {
$parts = array_map(function($part) {
return preg_replace('/[A-Z]\d[A-Z]\s\d[A-Z]\d/', '', $part);
}, $parts);
} else {
$parts = array_map(function($part) {
return preg_replace('/\d+$/', '', $part);
}, $parts);
}
return join( ', ', array_filter( array_map( function( $i ) use ( $parts ) {
return $i < 0 ? ( $parts[ count( $parts ) + $i ] ?? null ) : ( $parts[ $i - 1 ] ?? null );
}, $requested ) ) );
}
}
$modifiers['get_address_parts'] = \Get_Address_Parts::class;
return $modifiers;
});