PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4.1
3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / generators / get-from-db.php
jetformbuilder / includes / generators Last commit date
base-v2.php 3 months ago base.php 2 years ago get-from-db.php 3 weeks ago get-from-rest-api.php 3 months ago get-from-users.php 3 weeks ago get-related-posts.php 3 months ago legacy-parser.php 3 months ago num-range-manual.php 3 months ago num-range.php 3 months ago registry.php 3 weeks ago
get-from-db.php
236 lines
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 }
236