base.php
2 years ago
get-from-db.php
2 years ago
num-range-manual.php
2 years ago
num-range.php
2 years ago
get-from-db.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Generators; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | if ( ! defined( 'WPINC' ) ) { |
| 7 | die; |
| 8 | } |
| 9 | |
| 10 | class Get_From_DB extends Base { |
| 11 | |
| 12 | /** |
| 13 | * Returns generator ID |
| 14 | * |
| 15 | * @return string |
| 16 | */ |
| 17 | public function get_id() { |
| 18 | return 'get_from_db'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Returns generator name |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | public function get_name() { |
| 27 | return __( 'Get values list from database', 'jet-form-builder' ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Returns generated options list |
| 32 | * |
| 33 | * @param $args |
| 34 | * |
| 35 | * @return array |
| 36 | */ |
| 37 | public function generate( $args ) { |
| 38 | |
| 39 | global $wpdb; |
| 40 | |
| 41 | $result = array(); |
| 42 | $field = $args['generator_field']; |
| 43 | $table = $wpdb->postmeta; |
| 44 | |
| 45 | // phpcs:disable WordPress.DB |
| 46 | $rows = $wpdb->get_results( |
| 47 | $wpdb->prepare( "SELECT `meta_value` FROM `$table` WHERE `meta_key` = '%s'", $field ), |
| 48 | ARRAY_A |
| 49 | ); |
| 50 | // phpcs:enable WordPress.DB |
| 51 | |
| 52 | if ( empty( $rows ) ) { |
| 53 | return $result; |
| 54 | } |
| 55 | |
| 56 | foreach ( $rows as $row ) { |
| 57 | $result[] = array( |
| 58 | 'value' => $row['meta_value'], |
| 59 | 'label' => $row['meta_value'], |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | } |
| 67 |