| @@ -1,56 +1,235 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Jet_Form_Builder\Generators; | |
| 4 | - | |
| 5 | -class Get_From_DB extends Base { | |
| 6 | - | |
| 7 | - /** | |
| 8 | - * Returns generator ID | |
| 9 | - * | |
| 10 | - * @return string | |
| 11 | - */ | |
| 12 | - public function get_id() { | |
| 13 | - return 'get_from_db'; | |
| 14 | - } | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * Returns generator name | |
| 18 | - * | |
| 19 | - * @return string | |
| 20 | - */ | |
| 21 | - public function get_name() { | |
| 22 | - return __( 'Get values list from database', 'jet-engine' ); | |
| 23 | - } | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Returns generated options list | |
| 27 | - * | |
| 28 | - * @return array | |
| 29 | - */ | |
| 30 | - public function generate( $field ) { | |
| 31 | - | |
| 32 | - global $wpdb; | |
| 33 | - | |
| 34 | - $result = array(); | |
| 35 | - $table = $wpdb->postmeta; | |
| 36 | - $rows = $wpdb->get_results( | |
| 37 | - $wpdb->prepare( "SELECT `meta_value` FROM `$table` WHERE `meta_key` = '%s'", $field ), | |
| 38 | - ARRAY_A | |
| 39 | - ); | |
| 40 | - | |
| 41 | - if ( empty( $rows ) ) { | |
| 42 | - return $result; | |
| 43 | - } | |
| 44 | - | |
| 45 | - foreach ( $rows as $row ) { | |
| 46 | - $result[] = array( | |
| 47 | - 'value' => $row['meta_value'], | |
| 48 | - 'label' => $row['meta_value'], | |
| 49 | - ); | |
| 50 | - } | |
| 51 | - | |
| 52 | - return $result; | |
| 53 | - | |
| 54 | - } | |
| 55 | - | |
| 56 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Database Meta Values 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 | + * Get_From_DB class. | |
| 17 | + * | |
| 18 | + * Gets unique meta values from the database for a given meta key. | |
| 19 | + */ | |
| 20 | +class Get_From_DB extends Base_V2 { | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Returns generator ID. | |
| 24 | + * | |
| 25 | + * @return string | |
| 26 | + */ | |
| 27 | + public function get_id() { | |
| 28 | + return 'get_from_db'; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Returns generator name. | |
| 33 | + * | |
| 34 | + * @return string | |
| 35 | + */ | |
| 36 | + public function get_name() { | |
| 37 | + return __( 'Get values list from database', '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 | + 'required' => true, | |
| 53 | + 'placeholder' => '_custom_meta_key', | |
| 54 | + 'help' => __( 'Enter the meta key to get values from wp_postmeta table.', 'jet-form-builder' ), | |
| 55 | + ), | |
| 56 | + ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Whether this generator supports auto-update. | |
| 61 | + * | |
| 62 | + * @return bool | |
| 63 | + */ | |
| 64 | + public function supports_auto_update(): bool { | |
| 65 | + return true; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Returns context field descriptions for auto-update. | |
| 70 | + * | |
| 71 | + * @return array | |
| 72 | + */ | |
| 73 | + public function get_auto_update_context_fields(): array { | |
| 74 | + return array( | |
| 75 | + array( | |
| 76 | + 'single' => true, | |
| 77 | + 'description' => __( 'The Trigger Field value can switch the Meta Key when it matches a saved Trigger Field choice or an allowed server-side key. Otherwise the static Meta Key is used.', 'jet-form-builder' ), | |
| 78 | + 'example' => __( 'Choose the field whose saved option values should be allowed as dynamic Meta Keys.', 'jet-form-builder' ), | |
| 79 | + ), | |
| 80 | + ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Returns the auto-update value type supported by this generator. | |
| 85 | + * | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | + public function get_auto_update_value_type(): string { | |
| 89 | + return 'scalar'; | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Empty trigger should fall back to the static Meta Key setting. | |
| 94 | + * | |
| 95 | + * @return string | |
| 96 | + */ | |
| 97 | + public function get_auto_update_empty_context_policy(): string { | |
| 98 | + return 'fallback_to_static'; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Generate options with context from dependent fields. | |
| 103 | + * Context may override the meta key only when the candidate value is | |
| 104 | + * explicitly allowed by saved field choices or server-side filters. | |
| 105 | + * | |
| 106 | + * @param array $settings Parsed settings. | |
| 107 | + * @param array $context ['field_name' => 'value'] from listened fields. | |
| 108 | + * | |
| 109 | + * @return array | |
| 110 | + */ | |
| 111 | + public function generate_with_context( array $settings, array $context = array() ): array { | |
| 112 | + if ( ! empty( $context ) ) { | |
| 113 | + $context_value = reset( $context ); | |
| 114 | + $meta_key = is_scalar( $context_value ) ? trim( sanitize_text_field( (string) $context_value ) ) : ''; | |
| 115 | + | |
| 116 | + if ( '' !== $meta_key ) { | |
| 117 | + $runtime = $settings['_jfb_runtime'] ?? array(); | |
| 118 | + $block_attrs = $settings['_jfb_block_attrs'] ?? array(); | |
| 119 | + $validate_dynamic = apply_filters( | |
| 120 | + 'jet-form-builder/generators/get-from-db/validate_dynamic_meta_key', | |
| 121 | + true, | |
| 122 | + $meta_key, | |
| 123 | + $settings, | |
| 124 | + $context, | |
| 125 | + $runtime, | |
| 126 | + $block_attrs | |
| 127 | + ); | |
| 128 | + | |
| 129 | + if ( ! $validate_dynamic ) { | |
| 130 | + $settings['meta_key'] = $meta_key; | |
| 131 | + | |
| 132 | + return $this->generate( $settings ); | |
| 133 | + } | |
| 134 | + | |
| 135 | + $allowed_meta_keys = $runtime['allowed_meta_keys'] ?? array(); | |
| 136 | + $allowed_meta_keys = apply_filters( | |
| 137 | + 'jet-form-builder/generators/get-from-db/allowed-meta-keys', | |
| 138 | + $allowed_meta_keys, | |
| 139 | + $meta_key, | |
| 140 | + $settings, | |
| 141 | + $context, | |
| 142 | + $runtime, | |
| 143 | + $block_attrs | |
| 144 | + ); | |
| 145 | + $allowed_meta_keys = $this->sanitize_meta_keys_allowlist( $allowed_meta_keys ); | |
| 146 | + | |
| 147 | + if ( in_array( $meta_key, $allowed_meta_keys, true ) ) { | |
| 148 | + $settings['meta_key'] = $meta_key; | |
| 149 | + } | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $this->generate( $settings ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Sanitize an allowlist of meta keys received from runtime or filters. | |
| 158 | + * | |
| 159 | + * @param mixed $allowed_meta_keys Meta keys allowlist. | |
| 160 | + * | |
| 161 | + * @return array | |
| 162 | + */ | |
| 163 | + private function sanitize_meta_keys_allowlist( $allowed_meta_keys ): array { | |
| 164 | + if ( ! is_array( $allowed_meta_keys ) ) { | |
| 165 | + return array(); | |
| 166 | + } | |
| 167 | + | |
| 168 | + $allowed_meta_keys = array_filter( | |
| 169 | + array_map( | |
| 170 | + static function ( $meta_key ) { | |
| 171 | + return is_scalar( $meta_key ) | |
| 172 | + ? trim( sanitize_text_field( (string) $meta_key ) ) | |
| 173 | + : ''; | |
| 174 | + }, | |
| 175 | + $allowed_meta_keys | |
| 176 | + ) | |
| 177 | + ); | |
| 178 | + | |
| 179 | + return array_values( array_unique( $allowed_meta_keys ) ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Returns generated options list. | |
| 184 | + * | |
| 185 | + * @param array|string $args Settings array or legacy string. | |
| 186 | + * | |
| 187 | + * @return array | |
| 188 | + */ | |
| 189 | + public function generate( $args ) { | |
| 190 | + global $wpdb; | |
| 191 | + | |
| 192 | + $result = array(); | |
| 193 | + | |
| 194 | + // Handle both new array format and legacy string/array format | |
| 195 | + if ( is_string( $args ) ) { | |
| 196 | + $meta_key = $args; | |
| 197 | + } elseif ( is_array( $args ) ) { | |
| 198 | + // New schema format | |
| 199 | + $meta_key = $args['meta_key'] ?? ''; | |
| 200 | + | |
| 201 | + // Legacy format support | |
| 202 | + if ( empty( $meta_key ) && ! empty( $args['generator_field'] ) ) { | |
| 203 | + $meta_key = $args['generator_field']; | |
| 204 | + } | |
| 205 | + } else { | |
| 206 | + return $result; | |
| 207 | + } | |
| 208 | + | |
| 209 | + if ( empty( $meta_key ) ) { | |
| 210 | + return $result; | |
| 211 | + } | |
| 212 | + | |
| 213 | + $table = $wpdb->postmeta; | |
| 214 | + | |
| 215 | + // phpcs:disable WordPress.DB | |
| 216 | + $rows = $wpdb->get_results( | |
| 217 | + $wpdb->prepare( "SELECT DISTINCT `meta_value` FROM `$table` WHERE `meta_key` = %s AND `meta_value` != ''", $meta_key ), | |
| 218 | + ARRAY_A | |
| 219 | + ); | |
| 220 | + // phpcs:enable WordPress.DB | |
| 221 | + | |
| 222 | + if ( empty( $rows ) ) { | |
| 223 | + return $result; | |
| 224 | + } | |
| 225 | + | |
| 226 | + foreach ( $rows as $row ) { | |
| 227 | + $result[] = array( | |
| 228 | + 'value' => $row['meta_value'], | |
| 229 | + 'label' => $row['meta_value'], | |
| 230 | + ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + return $result; | |
| 234 | + } | |
| 235 | +} | |