| @@ -1,57 +1,110 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Jet_Form_Builder\Generators; | |
| 4 | - | |
| 5 | -class Num_Range extends Base { | |
| 6 | - | |
| 7 | - /** | |
| 8 | - * Returns generator ID | |
| 9 | - * | |
| 10 | - * @return string | |
| 11 | - */ | |
| 12 | - public function get_id() { | |
| 13 | - return 'num_range'; | |
| 14 | - } | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * Returns generator name | |
| 18 | - * | |
| 19 | - * @return string | |
| 20 | - */ | |
| 21 | - public function get_name() { | |
| 22 | - return __( 'Numbers range', 'jet-engine' ); | |
| 23 | - } | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Returns generated options list | |
| 27 | - * | |
| 28 | - * @return array | |
| 29 | - */ | |
| 30 | - public function generate( $field ) { | |
| 31 | - | |
| 32 | - $result = array(); | |
| 33 | - | |
| 34 | - if ( ! $field ) { | |
| 35 | - return $result; | |
| 36 | - } | |
| 37 | - | |
| 38 | - $meta_value = get_post_meta( get_the_ID(), $field, true ); | |
| 39 | - $meta_value = absint( $meta_value ); | |
| 40 | - | |
| 41 | - if ( ! $meta_value ) { | |
| 42 | - return $result; | |
| 43 | - } | |
| 44 | - | |
| 45 | - $start = apply_filters( 'jet-form-builder/forms/generators/num-range/start-from', 1 ); | |
| 46 | - | |
| 47 | - for ( $i = $start; $i <= $meta_value; $i ++ ) { | |
| 48 | - $result[] = array( | |
| 49 | - 'value' => $i, | |
| 50 | - 'label' => $i, | |
| 51 | - ); | |
| 52 | - } | |
| 53 | - | |
| 54 | - return $result; | |
| 55 | - } | |
| 56 | - | |
| 57 | -} | |
| 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 | +} | |