base-source.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
73 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | use Jet_Engine\Modules\Profile_Builder\Module; |
| 7 | use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 8 | |
| 9 | class Preset_Source_User extends Base_Source { |
| 10 | |
| 11 | public function get_id() { |
| 12 | return 'user'; |
| 13 | } |
| 14 | |
| 15 | public function on_sanitize(): bool { |
| 16 | if ( ! is_user_logged_in() ) { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | if ( get_current_user_id() !== $this->src()->ID && ! current_user_can( 'edit_users' ) ) { |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | public function query_source() { |
| 28 | $user_from = ! empty( $this->preset_data['user_from'] ) ? $this->preset_data['user_from'] : 'current_user'; |
| 29 | |
| 30 | if ( 'current_user' === $user_from && is_user_logged_in() ) { |
| 31 | return wp_get_current_user(); |
| 32 | } |
| 33 | |
| 34 | if ( 'queried_user' === $user_from ) { |
| 35 | return $this->get_queried_user(); |
| 36 | } |
| 37 | |
| 38 | $var = ! empty( $this->preset_data['query_var'] ) ? $this->preset_data['query_var'] : 'user_id'; |
| 39 | $user_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 40 | |
| 41 | return get_user_by( 'ID', $user_id ); |
| 42 | } |
| 43 | |
| 44 | protected function get_queried_user() { |
| 45 | $user = get_queried_object(); |
| 46 | |
| 47 | return is_a( $user, \WP_User::class ) ? $user : false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return bool |
| 52 | * @throws Preset_Exception |
| 53 | */ |
| 54 | protected function can_get_preset() { |
| 55 | return ( parent::can_get_preset() |
| 56 | && is_user_logged_in() |
| 57 | && ( get_current_user_id() === $this->src()->ID || current_user_can( 'edit_users' ) ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function source__user_meta() { |
| 62 | if ( empty( $this->field_data['key'] ) ) { |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | return get_user_meta( |
| 67 | $this->src()->ID, |
| 68 | $this->field_data['key'], |
| 69 | true |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 |