views
6 hours ago
Dashboard.php
4 months ago
EditProfile.php
5 months ago
Login.php
4 months ago
PublicProfile.php
5 months ago
Register.php
5 months ago
User.php
2 months ago
UserController.php
4 months ago
User.php
229 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace WPDM\User; |
| 5 | |
| 6 | |
| 7 | use PrivateMessage\__\__; |
| 8 | use WPDM\__\Template; |
| 9 | |
| 10 | class User |
| 11 | { |
| 12 | public $ID; |
| 13 | public $name; |
| 14 | public $profile; |
| 15 | public $avatar; |
| 16 | public $description; |
| 17 | public $signup_date; |
| 18 | public $title; |
| 19 | public $following = []; |
| 20 | public $contacts = []; |
| 21 | public $blocked = []; |
| 22 | public $me; |
| 23 | |
| 24 | private static $instance; |
| 25 | |
| 26 | public static function getInstance() |
| 27 | { |
| 28 | if (self::$instance === null) { |
| 29 | self::$instance = new self; |
| 30 | } |
| 31 | return self::$instance; |
| 32 | } |
| 33 | |
| 34 | function __construct($userID = null) |
| 35 | { |
| 36 | global $current_user; |
| 37 | |
| 38 | $this->ID = $userID ? $userID : get_current_user_id(); |
| 39 | |
| 40 | if($this->ID) { |
| 41 | $this->profile = get_userdata($this->ID); |
| 42 | $this->profile->avatar = get_avatar($this->ID, 512); |
| 43 | $this->description = get_user_meta($this->ID, 'description', true); |
| 44 | $this->signup_date = wp_date(get_option('date_format'), strtotime($this->profile->user_registered)); |
| 45 | $this->title = get_user_meta($this->ID, '__wpdm_title', true); |
| 46 | $this->name = $this->profile->display_name; |
| 47 | $this->avatar = get_avatar($this->ID, 512, '', $this->name, ['class' => 'profile-avatar']); |
| 48 | } |
| 49 | |
| 50 | $this->me = $current_user; |
| 51 | |
| 52 | add_shortcode("wpdm_user_favourites", [$this, 'favourites']); |
| 53 | add_shortcode('wpdm_members', [$this, 'members']); |
| 54 | add_shortcode('wpdm_authors', [$this, 'members']); |
| 55 | } |
| 56 | |
| 57 | function getFollowings($userID = null) |
| 58 | { |
| 59 | if(!is_user_logged_in()) return []; |
| 60 | $follower = $userID ? $userID : $this->ID; |
| 61 | $followings = get_user_meta($follower, '__wpdm_following', true); |
| 62 | $followings = maybe_unserialize($followings); |
| 63 | if(!is_array($followings)) $followings = []; |
| 64 | $this->following = $followings; |
| 65 | return $followings; |
| 66 | } |
| 67 | |
| 68 | function getContacts($userID = null) |
| 69 | { |
| 70 | if(!is_user_logged_in()) return []; |
| 71 | $user = $userID ? $userID : $this->ID; |
| 72 | $contacts = get_user_meta($user, '__wpdm_contacts', true); |
| 73 | $contacts = maybe_unserialize($contacts); |
| 74 | if(!is_array($contacts)) $contacts = []; |
| 75 | $this->contacts = $contacts; |
| 76 | return $contacts; |
| 77 | } |
| 78 | |
| 79 | function getBlockedContacts($userID = null) |
| 80 | { |
| 81 | if(!is_user_logged_in()) return []; |
| 82 | $user = $userID ? $userID : $this->ID; |
| 83 | $blockedContacts = get_user_meta($user, '__wpdm_blocked_contacts', true); |
| 84 | $blockedContacts = maybe_unserialize($blockedContacts); |
| 85 | if(!is_array($blockedContacts)) $blockedContacts = []; |
| 86 | $this->blocked = $blockedContacts; |
| 87 | return $blockedContacts; |
| 88 | } |
| 89 | |
| 90 | function follow($userID) |
| 91 | { |
| 92 | if(!is_user_logged_in()) return false; |
| 93 | $this->getFollowings($this->ID); |
| 94 | $this->following[$userID] = time(); |
| 95 | update_user_meta($this->ID, '__wpdm_following', $this->following); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | function isFollowing($userID) |
| 100 | { |
| 101 | if(!is_user_logged_in()) return false; |
| 102 | $this->getFollowings($this->ID); |
| 103 | return isset($this->following[$userID]) ? $this->following[$userID] : false; |
| 104 | } |
| 105 | |
| 106 | function unfollow($userID) |
| 107 | { |
| 108 | if(!is_user_logged_in()) return false; |
| 109 | $this->getFollowings($this->ID); |
| 110 | if(isset($this->following[$userID])) unset($this->following[$userID]); |
| 111 | update_user_meta($this->ID, '__wpdm_following', $this->following); |
| 112 | } |
| 113 | |
| 114 | function addToContact($userID) |
| 115 | { |
| 116 | if(!is_user_logged_in()) return false; |
| 117 | $this->getContacts($this->ID); |
| 118 | $this->contacts[$userID] = time(); |
| 119 | update_user_meta($this->ID, '__wpdm_contacts', $this->contacts); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | function inContactList($userID) |
| 124 | { |
| 125 | if(!is_user_logged_in()) return false; |
| 126 | $this->getContacts($this->ID); |
| 127 | return isset($this->contacts[$userID]) ? $this->contacts[$userID] : false; |
| 128 | } |
| 129 | |
| 130 | function removeContact($userID) |
| 131 | { |
| 132 | if(!is_user_logged_in()) return false; |
| 133 | $this->getContacts($this->ID); |
| 134 | if(isset($this->contacts[$userID])) unset($this->contacts[$userID]); |
| 135 | update_user_meta($this->ID, '__wpdm_contacts', $this->contacts); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | function block($userID) |
| 140 | { |
| 141 | if(!is_user_logged_in()) return false; |
| 142 | $this->getBlockedContacts($this->ID); |
| 143 | $this->blocked[$userID] = time(); |
| 144 | update_user_meta($this->ID, '__wpdm_blocked_contacts', $this->blocked); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | function isBlocked($userID) |
| 149 | { |
| 150 | if(!is_user_logged_in()) return false; |
| 151 | $this->getBlockedContacts($this->ID); |
| 152 | return isset($this->blocked[$userID]) ? $this->blocked[$userID] : false; |
| 153 | } |
| 154 | |
| 155 | function unblock($userID) |
| 156 | { |
| 157 | if(!is_user_logged_in()) return false; |
| 158 | $this->getBlockedContacts($this->ID); |
| 159 | if(isset($this->blocked[$userID])) unset($this->blocked[$userID]); |
| 160 | update_user_meta($this->ID, '__wpdm_blocked_contacts', $this->contacts); |
| 161 | } |
| 162 | |
| 163 | function favourites($params = array()) |
| 164 | { |
| 165 | global $wpdb, $current_user; |
| 166 | if (!isset($params['user']) && !is_user_logged_in()) return WPDM()->user->login->form(); |
| 167 | ob_start(); |
| 168 | include Template::locate('user-favourites.php', __DIR__.'/views'); |
| 169 | return ob_get_clean(); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | function members($params = array()) |
| 174 | { |
| 175 | $sid = isset($params['sid']) ? preg_replace('/[^a-zA-Z0-9_\-]/', '', $params['sid']) : ''; |
| 176 | $params['sid'] = $sid; |
| 177 | update_post_meta(get_the_ID(), '__wpdm_users_params' . $sid, $params); |
| 178 | ob_start(); |
| 179 | include Template::locate("members.php", __DIR__.'/views'); |
| 180 | return ob_get_clean(); |
| 181 | } |
| 182 | |
| 183 | function listAuthors($params = []) |
| 184 | |
| 185 | { |
| 186 | |
| 187 | if (!$params) $params = get_post_meta(wpdm_query_var('_pid', 'int'), '__wpdm_users_params' . preg_replace('/[^a-zA-Z0-9_\-]/', '', wpdm_query_var('_sid')), true); |
| 188 | $page = isset($_REQUEST['cp']) && $_REQUEST['cp'] > 0 ? (int)$_REQUEST['cp'] : 1; |
| 189 | $items_per_page = isset($params['items_per_page']) ? $params['items_per_page'] : 12; |
| 190 | //$offset = $page * $items_per_page; |
| 191 | $cols = isset($params['cols']) && in_array($params['cols'], [1, 2, 3, 4, 6]) ? $params['cols'] : 0; |
| 192 | if ($cols > 0) $cols_class = "col-md-" . (12 / $cols); |
| 193 | |
| 194 | $args = array( |
| 195 | 'role' => isset($params['role']) ? $params['role'] : '', |
| 196 | 'role__in' => isset($params['role__in']) ? explode(",", $params['role__in']) : array(), |
| 197 | 'role__not_in' => isset($params['role__not_in']) ? explode(",", $params['role__not_in']) : array(), |
| 198 | 'meta_key' => isset($params['meta_key']) ? $params['meta_key'] : '', |
| 199 | 'meta_value' => isset($params['meta_value']) ? $params['meta_value'] : '', |
| 200 | 'meta_compare' => isset($params['meta_compare']) ? $params['meta_compare'] : '', |
| 201 | //'meta_query' => array(), |
| 202 | //'date_query' => array(), |
| 203 | 'include' => isset($params['include']) ? explode(",", $params['include']) : array(), |
| 204 | 'exclude' => isset($params['exclude']) ? explode(",", $params['exclude']) : array(), |
| 205 | 'orderby' => isset($params['orderby']) ? $params['orderby'] : 'login', |
| 206 | 'order' => isset($params['order']) ? $params['order'] : 'DESC', |
| 207 | //'offset' => $offset, |
| 208 | 'search' => isset($params['search']) ? $params['search'] : '', |
| 209 | 'number' => $items_per_page, |
| 210 | 'paged' => $page, |
| 211 | 'count_total' => true, |
| 212 | ); |
| 213 | $users = new \WP_User_Query($args); |
| 214 | if ($cols > 0) echo "<div class='row'>"; |
| 215 | foreach ($users->get_results() as $user) { |
| 216 | if (isset($cols_class)) echo "<div class='$cols_class'>"; |
| 217 | include Template::locate("profile-cards/default.php", __DIR__.'/views'); |
| 218 | if (isset($cols_class)) echo "</div>"; |
| 219 | } |
| 220 | if ($cols > 0) echo "</div>"; |
| 221 | $total = $users->get_total(); |
| 222 | $contid = isset($params['sid']) ? "-{$params['sid']}" : ''; |
| 223 | if (isset($params['paging']) && (int)$params['paging'] == 1) |
| 224 | echo wpdm_paginate_links($total, $items_per_page, $page, 'cp', array('async' => 1, 'container' => "#wpdm-authors{$contid}")); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | } |
| 229 |