base-v2.php
2 months ago
base.php
2 years ago
get-from-db.php
2 months ago
get-from-rest-api.php
2 months ago
get-from-users.php
2 months ago
get-related-posts.php
2 months ago
legacy-parser.php
2 months ago
num-range-manual.php
2 months ago
num-range.php
2 months ago
registry.php
2 months ago
get-from-db.php
179 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 is used as the Meta Key and overrides the static setting above. If the Trigger Field is empty, the static Meta Key is used.', 'jet-form-builder' ), |
| 78 | 'example' => __( 'Choose the field whose value will be used as the dynamic Meta Key.', '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 | * Uses the watched field value as meta key. |
| 104 | * |
| 105 | * @param array $settings Parsed settings. |
| 106 | * @param array $context ['field_name' => 'value'] from listened fields. |
| 107 | * |
| 108 | * @return array |
| 109 | */ |
| 110 | public function generate_with_context( array $settings, array $context = array() ): array { |
| 111 | if ( ! empty( $context ) ) { |
| 112 | $context_value = reset( $context ); |
| 113 | $meta_key = is_scalar( $context_value ) ? trim( sanitize_text_field( (string) $context_value ) ) : ''; |
| 114 | |
| 115 | // Use watched field value as override only when it is non-empty. |
| 116 | // Otherwise keep static meta_key from generator settings. |
| 117 | if ( '' !== $meta_key ) { |
| 118 | $settings['meta_key'] = $meta_key; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $this->generate( $settings ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns generated options list. |
| 127 | * |
| 128 | * @param array|string $args Settings array or legacy string. |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | public function generate( $args ) { |
| 133 | global $wpdb; |
| 134 | |
| 135 | $result = array(); |
| 136 | |
| 137 | // Handle both new array format and legacy string/array format |
| 138 | if ( is_string( $args ) ) { |
| 139 | $meta_key = $args; |
| 140 | } elseif ( is_array( $args ) ) { |
| 141 | // New schema format |
| 142 | $meta_key = $args['meta_key'] ?? ''; |
| 143 | |
| 144 | // Legacy format support |
| 145 | if ( empty( $meta_key ) && ! empty( $args['generator_field'] ) ) { |
| 146 | $meta_key = $args['generator_field']; |
| 147 | } |
| 148 | } else { |
| 149 | return $result; |
| 150 | } |
| 151 | |
| 152 | if ( empty( $meta_key ) ) { |
| 153 | return $result; |
| 154 | } |
| 155 | |
| 156 | $table = $wpdb->postmeta; |
| 157 | |
| 158 | // phpcs:disable WordPress.DB |
| 159 | $rows = $wpdb->get_results( |
| 160 | $wpdb->prepare( "SELECT DISTINCT `meta_value` FROM `$table` WHERE `meta_key` = %s AND `meta_value` != ''", $meta_key ), |
| 161 | ARRAY_A |
| 162 | ); |
| 163 | // phpcs:enable WordPress.DB |
| 164 | |
| 165 | if ( empty( $rows ) ) { |
| 166 | return $result; |
| 167 | } |
| 168 | |
| 169 | foreach ( $rows as $row ) { |
| 170 | $result[] = array( |
| 171 | 'value' => $row['meta_value'], |
| 172 | 'label' => $row['meta_value'], |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | return $result; |
| 177 | } |
| 178 | } |
| 179 |