base-source.php
3 years ago
preset-source-options-page.php
3 years ago
preset-source-post.php
3 years ago
preset-source-query-var.php
3 years ago
preset-source-user.php
3 years ago
preset-source-user.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | class Preset_Source_User extends Base_Source { |
| 7 | |
| 8 | public function get_id() { |
| 9 | return 'user'; |
| 10 | } |
| 11 | |
| 12 | public function on_sanitize(): bool { |
| 13 | if ( ! is_user_logged_in() ) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | if ( get_current_user_id() !== $this->src()->ID && ! current_user_can( 'edit_users' ) ) { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | public function query_source() { |
| 25 | $user_from = ! empty( $this->preset_data['user_from'] ) ? $this->preset_data['user_from'] : 'current_user'; |
| 26 | |
| 27 | if ( 'current_user' === $user_from && is_user_logged_in() ) { |
| 28 | return wp_get_current_user(); |
| 29 | } |
| 30 | |
| 31 | $var = ! empty( $this->preset_data['query_var'] ) ? $this->preset_data['query_var'] : 'user_id'; |
| 32 | $user_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 33 | |
| 34 | return get_user_by( 'ID', $user_id ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return mixed |
| 39 | */ |
| 40 | protected function can_get_preset() { |
| 41 | return ( parent::can_get_preset() |
| 42 | && is_user_logged_in() |
| 43 | && ( get_current_user_id() === $this->src()->ID || current_user_can( 'edit_users' ) ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function source__user_meta() { |
| 48 | if ( empty( $this->field_data['key'] ) ) { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | return get_user_meta( |
| 53 | $this->src()->ID, |
| 54 | $this->field_data['key'], |
| 55 | true |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 |