forked from MaxFurnitureMagento/MagentoSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
151 lines (133 loc) · 3.28 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @category Fishpig
* @package Fishpig_Wordpress
* @license http://fishpig.co.uk/license.txt
* @author Ben Tideswell <help@fishpig.co.uk>
*/
class Fishpig_Wordpress_Model_User extends Mage_Core_Model_Abstract
{
public function _construct()
{
$this->_init('wordpress/user');
}
/**
* Store meta keys for retrieves meta values
*
* @var array
*/
protected $_metaKeys = array('first_name', 'last_name', 'wp_capabilities', 'wp_user_level', 'nickname');
public function loadByEmail($email)
{
return $this->load($email, 'user_email');
}
/**
* Get the URL for this user
*
* @return string
*/
public function getUrl()
{
$slug = preg_replace("/[^a-zA-Z0-9-]/", "-", strtolower($this->getUserLogin()));
$slug = str_replace('--', '-', $slug);
return Mage::helper('wordpress')->getUrl('author/' . $slug) . '/';
}
/**
* @deprecated after 2.0.4
* @see self::getCustomField()
*/
public function getMeta($key)
{
return $this->getCustomField($key);
}
/**
* Load the WordPress user model associated with the current logged in customer
*
* @return Fishpig_Wordpress_Model_User
*/
public function loadCurrentLoggedInUser()
{
return $this->getResource()->loadCurrentLoggedInUser($this);
}
/**
* Retrieve the meta_keys array
*
* @return array
*/
public function getLoadedMetaKeys()
{
return $this->_metaKeys;
}
/**
* Retrieve a custom field value from the database
*
* @param string $key
* @return string
*/
public function getCustomField($key)
{
if (!$this->hasData($key)) {
$this->setData($key, $this->getResource()->getMetaValue($this, $key));
}
return $this->getData($key);
}
/**
* Load the user\s role
*
* @return $this
*/
protected function _afterLoad()
{
if ($roles = $this->getCustomField('wp_capabilities')) {
$roles = unserialize($roles);
foreach($roles as $role => $enabled) {
$this->setRole($role);
break;
}
}
return parent::_afterLoad();
}
/**
* Retrieve the URL for Gravatar
*
* @return string
*/
public function getGravatarUrl($size = 50)
{
return "http://www.gravatar.com/avatar/" . md5(strtolower(trim($this->getUserEmail()))) . "?d=" . urlencode( $this->_getDefaultGravatarImage() ) . "&s=" . $size;
}
/**
* Retrieve the URL to the default gravatar image
*
* @return string
*/
protected function _getDefaultGravatarImage()
{
return '';
}
/**
* Retrieve the user's photo
* The UserPhoto plugin must be installed in WordPress
*
* @param bool $thumb
* @return null|string
*/
public function getUserPhoto($thumb = false)
{
$dataKey = $thumb ? 'userphoto_thumb_file' : 'userphoto_image_file';
if (!$this->hasData($dataKey)) {
if ($photo = $this->getCustomField($dataKey)) {
$this->setData($dataKey, Mage::helper('wordpress')->getFileUploadUrl() . 'userphoto/' . $photo);
}
else if (Mage::helper('wordpress')->getWpOption('userphoto_use_avatar_fallback')) {
if ($thumb) {
$this->setData($dataKey, $this->getGravatarUrl(Mage::helper('wordpress')->getWpOption('userphoto_thumb_dimension')));
}
else {
$this->setData($dataKey, $this->getGravatarUrl(Mage::helper('wordpress')->getWpOption('userphoto_maximum_dimension')));
}
}
}
return $this->_getData($dataKey);
}
}