| 1 |
<?php |
| 2 |
/** |
| 3 |
* Numbers Range by Meta Value Generator with schema support. |
| 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 |
* Num_Range class. |
| 17 |
* |
| 18 |
* Generates a list of numbers from 1 to the value stored in a post meta field. |
| 19 |
*/ |
| 20 |
class Num_Range extends Base_V2 { |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns generator ID. |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function get_id() { |
| 28 |
return 'num_range'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns generator name. |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function get_name() { |
| 37 |
return __( 'Numbers range by meta value', 'jet-form-builder' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns structured settings schema. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function get_settings_schema(): array { |
| 46 |
return array( |
| 47 |
'meta_key' => array( |
| 48 |
'type' => 'string', |
| 49 |
'default' => '', |
| 50 |
'label' => __( 'Meta Key', 'jet-form-builder' ), |
| 51 |
'control' => 'text', |
| 52 |
'placeholder' => '_max_quantity', |
| 53 |
'help' => __( 'Enter the meta key containing the maximum number for the range.', 'jet-form-builder' ), |
| 54 |
), |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns generated options list. |
| 60 |
* |
| 61 |
* @param array|string $args Settings array or legacy string. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function generate( $args ) { |
| 66 |
$result = array(); |
| 67 |
|
| 68 |
// Handle both new array format and legacy string/array format |
| 69 |
if ( is_string( $args ) ) { |
| 70 |
$meta_key = $args; |
| 71 |
} elseif ( is_array( $args ) ) { |
| 72 |
// New schema format |
| 73 |
$meta_key = $args['meta_key'] ?? ''; |
| 74 |
|
| 75 |
// Legacy format support |
| 76 |
if ( empty( $meta_key ) && ! empty( $args['generator_field'] ) ) { |
| 77 |
$meta_key = $args['generator_field']; |
| 78 |
} |
| 79 |
} else { |
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
if ( empty( $meta_key ) ) { |
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
$meta_value = get_post_meta( get_the_ID(), $meta_key, true ); |
| 88 |
$meta_value = absint( $meta_value ); |
| 89 |
|
| 90 |
if ( ! $meta_value ) { |
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
$start = apply_filters( |
| 95 |
'jet-form-builder/forms/generators/num-range/start-from', |
| 96 |
1, |
| 97 |
$args, |
| 98 |
$meta_value |
| 99 |
); |
| 100 |
|
| 101 |
for ( $i = $start; $i <= $meta_value; $i++ ) { |
| 102 |
$result[] = array( |
| 103 |
'value' => $i, |
| 104 |
'label' => $i, |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
return $result; |
| 109 |
} |
| 110 |
} |
| 111 |
|