| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
|
| 4 |
/** |
| 5 |
* People Class |
| 6 |
*/ |
| 7 |
class People extends Item { |
| 8 |
|
| 9 |
/** |
| 10 |
* Generate people data |
| 11 |
* |
| 12 |
* @param object the item wpdb object |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
protected function populate( $item ) { |
| 17 |
if ( $item && ! is_wp_error( $item ) ) { |
| 18 |
$this->id = (int) $item->id; |
| 19 |
$this->data = $item; |
| 20 |
|
| 21 |
} else { |
| 22 |
$this->id = 0; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Fetch a single people |
| 28 |
* |
| 29 |
* @param int $people_id |
| 30 |
* |
| 31 |
* @return object |
| 32 |
*/ |
| 33 |
protected function get_by_id( $people_id ) { |
| 34 |
return erp_get_people( $people_id ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Check if this people is a WP_User type |
| 39 |
* |
| 40 |
* @return boolean |
| 41 |
*/ |
| 42 |
function is_wp_user() { |
| 43 |
return intval( $this->user_id ) !== 0; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get full name |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @since 1.2.0 Return trimmed string |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
function get_full_name() { |
| 55 |
$full_name = ''; |
| 56 |
|
| 57 |
if ( ! empty( $this->types ) && in_array( 'company', $this->types ) ) { |
| 58 |
$full_name = $this->company; |
| 59 |
|
| 60 |
} elseif ( $this->is_wp_user() ) { |
| 61 |
$user = \get_user_by( 'id', $this->user_id ); |
| 62 |
|
| 63 |
if ( ! empty( $user->first_name ) ) { |
| 64 |
$full_name = $user->first_name . ' ' . $user->last_name; |
| 65 |
} |
| 66 |
|
| 67 |
$full_name = $user->display_name; |
| 68 |
} else { |
| 69 |
$full_name = $this->first_name . ' ' . $this->last_name; |
| 70 |
} |
| 71 |
|
| 72 |
return trim( $full_name ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get email address of a user |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
function get_email() { |
| 81 |
if ( $this->is_wp_user() ) { |
| 82 |
return \get_user_by( 'id', $this->user_id )->user_email; |
| 83 |
} else { |
| 84 |
return $this->email; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get website address of a user |
| 90 |
* |
| 91 |
* @since 1.0 |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function get_website() { |
| 96 |
if ( $this->is_wp_user() ) { |
| 97 |
$user = \get_user_by( 'id', $this->user_id ); |
| 98 |
return ( $user->user_url ) ? erp_get_clickable( 'url', $user->user_url ) : '—'; |
| 99 |
} else { |
| 100 |
return ( $this->website ) ? erp_get_clickable( 'url', $this->website ) : '—'; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get meta data of a user |
| 106 |
* |
| 107 |
* @param string $meta_key |
| 108 |
* @param string $meta_value |
| 109 |
*/ |
| 110 |
function get_meta( $meta_key, $single = true ) { |
| 111 |
if ( $this->is_wp_user() ) { |
| 112 |
return \get_user_meta( $this->user_id, $meta_key, $single ); |
| 113 |
} else { |
| 114 |
return \erp_people_get_meta( $this->id, $meta_key, $single ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add meta data to a user |
| 120 |
* |
| 121 |
* @param string $meta_key |
| 122 |
* @param string $meta_value |
| 123 |
*/ |
| 124 |
function add_meta( $meta_key, $meta_value ) { |
| 125 |
if ( $this->is_wp_user() ) { |
| 126 |
\add_user_meta( $this->user_id, $meta_key, $meta_value ); |
| 127 |
} else { |
| 128 |
\erp_people_add_meta( $this->id, $meta_key, $meta_value ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Update meta data to a user |
| 134 |
* |
| 135 |
* @param string $meta_key |
| 136 |
* @param string $meta_value |
| 137 |
*/ |
| 138 |
function update_meta( $meta_key, $meta_value ) { |
| 139 |
if ( $this->is_wp_user() ) { |
| 140 |
\update_user_meta( $this->user_id, $meta_key, $meta_value ); |
| 141 |
} else { |
| 142 |
\erp_people_update_meta( $this->id, $meta_key, $meta_value ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Delete meta data to a user |
| 148 |
* |
| 149 |
* @param string $meta_key |
| 150 |
* @param string $meta_value |
| 151 |
*/ |
| 152 |
function delete_meta( $meta_key ) { |
| 153 |
if ( $this->is_wp_user() ) { |
| 154 |
\delete_user_meta( $this->user_id, $meta_key ); |
| 155 |
} else { |
| 156 |
\erp_people_delete_meta( $this->id, $meta_key ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Update any property of people |
| 162 |
* |
| 163 |
* @since 1.2.6 |
| 164 |
* |
| 165 |
* @param $property |
| 166 |
* @param $value |
| 167 |
* |
| 168 |
* @return \WP_Error |
| 169 |
*/ |
| 170 |
public function update_property( $property, $value ) { |
| 171 |
$data_array = json_decode( json_encode( $this->data ), true ); |
| 172 |
|
| 173 |
if ( ! array_key_exists( $property, $data_array ) ) { |
| 174 |
return new \WP_Error( 'unauthorized-erp-people-property', __( 'Unauthorized people property', 'erp' ) ); |
| 175 |
} |
| 176 |
|
| 177 |
$people = \WeDevs\ERP\Framework\Models\People::find( $this->id ); |
| 178 |
$wor = $people->update([$property => $value]); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
} |
| 183 |
|