add_filter( 'voxel/dynamic-data/modifiers', function( $modifiers ) {
class Randomize_Output_Modifier extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
public function get_label(): string {
return 'Randomize Output';
}
public function get_key(): string {
return 'randomize_output';
}
protected function define_args(): void {
// Define number of outputs
$this->define_arg([
'type' => 'number',
'label' => _x( 'Number of Outputs', 'modifiers', 'voxel-backend' ),
'default' => 1,
]);
// Define up to 6 additional inputs
for ( $i = 1; $i define_arg([
'type' => 'text',
'label' => _x( "Additional Option $i", 'modifiers', 'voxel-backend' ),
'default' => null,
]);
}
}
public function apply( string $value ) {
// Sanitize the input value and split it into an array
$sanitized_value = sanitize_text_field( $value );
$options = array_filter( array_map( 'trim', explode( ',', $sanitized_value ) ) );
// Add additional inputs to the list if provided
for ( $i = 1; $i get_arg( $i ) );
if ( ! empty( $input ) ) {
$options[] = $input;
}
}
// Get the user-defined number of outputs
$num_outputs = (int) $this->get_arg(0);
$num_outputs = max(1, min(count($options), $num_outputs)); // Ensure valid range
// If there are valid options, randomize and return the selected ones
if ( ! empty( $options ) ) {
shuffle( $options );
$selected_options = array_slice( $options, 0, $num_outputs );
return esc_html( implode(', ', $selected_options) );
}
// If no valid options are provided, return the original value
return esc_html( $value );
}
}
$modifiers['randomize_output'] = \Randomize_Output_Modifier::class;
return $modifiers;
} );
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
There are no results matching your search
There are no results matching your search