Fishpigs_WpCustomerSync - Version 0.9.0

Version Notes

Extension created and released
Added backwards compatibility for old WordPress installations that use md5 password hashing

Download this release

Release Info

Developer Magento Core Team
Extension Fishpigs_WpCustomerSync
Version 0.9.0
Comparing to
See all releases


Version 0.9.0

app/code/community/Fishpig/WpCustomerSynch/Helper/Data.php ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category Fishpig
4
+ * @package Fishpig_WpCustomerSynch
5
+ * @license http://fishpig.co.uk/license.txt
6
+ * @author Ben Tideswell <help@fishpig.co.uk>
7
+ */
8
+
9
+ class Fishpig_WpCustomerSynch_Helper_Data extends Mage_Core_Helper_Abstract
10
+ {
11
+
12
+
13
+ }
app/code/community/Fishpig/WpCustomerSynch/Model/Observer.php ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category Fishpig
4
+ * @package Fishpig_WpCustomerSynch
5
+ * @license http://fishpig.co.uk/license.txt
6
+ * @author Ben Tideswell <help@fishpig.co.uk>
7
+ */
8
+
9
+ class Fishpig_WpCustomerSynch_Model_Observer
10
+ {
11
+ /**
12
+ * Synchronise the customer trying to login
13
+ * If they exist in WP but not Mage, copy over to Mage
14
+ * If they exist in WP but not Mage, they are NOT copied to WP
15
+ *
16
+ */
17
+ public function customerLogin(Varien_Event_Observer $observer)
18
+ {
19
+ if (!$this->isCustomerSynchronisationEnabled()) {
20
+ return false;
21
+ }
22
+
23
+ try {
24
+ if ($login = Mage::app()->getRequest()->getPost('login')) {
25
+ $login = new Varien_Object($login);
26
+
27
+ $customerId = $this->getMagentoCustomerIdByEmail($login->getUsername());
28
+
29
+ if (is_null($customerId) || !$customerId) {
30
+ if ($user = $this->getWordPressUserModelByEmail($login->getUsername())) {
31
+ if ($this->isValidWordPressPassword($login->getPassword(), $user->getUserPass())) {
32
+ $this->_copyCustomerFromWordPress($user, $login->getPassword());
33
+ }
34
+ }
35
+ }
36
+ else {
37
+ $this->_copyCustomerToWordPress($customerId, $login);
38
+ }
39
+ }
40
+ }
41
+ catch (Exception $e) {
42
+ Mage::helper('wordpress/log')->log($e->getMessage());
43
+ Mage::logException($e);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Initialise values needed to synchronise Magento/WordPress user records
49
+ *
50
+ * @param Varien_Event_Observer $observer
51
+ */
52
+ public function initCustomerUpdate(Varien_Event_Observer $observer)
53
+ {
54
+ if (!$this->isCustomerSynchronisationEnabled()) {
55
+ return false;
56
+ }
57
+
58
+ $customer = $observer->getEvent()->getCustomer();
59
+
60
+ $customer->setOriginalFirstname($customer->getFirstname());
61
+ $customer->setOriginalEmail($customer->getEmail());
62
+ $customer->setOriginalPasswordHash($customer->getPasswordHash());
63
+ }
64
+
65
+ /**
66
+ * Synchronise customer data when updating
67
+ *
68
+ */
69
+ public function customerUpdate(Varien_Event_Observer $observer)
70
+ {
71
+ if (!$this->isCustomerSynchronisationEnabled()) {
72
+ return false;
73
+ }
74
+
75
+ $session = Mage::getSingleton('customer/session');
76
+
77
+ if ($session->isLoggedIn()) {
78
+ $customer = $session->getCustomer();
79
+ $user = Mage::getModel('wordpress/user');
80
+ $user->load($customer->getOriginalEmail(), 'user_email');
81
+
82
+ if (!$user->getId()) {
83
+ $this->_copyCustomerToWordPress($customer, new Varien_Object(array('password' => $customer->getPassword())));
84
+ }
85
+ else {
86
+ $user->setUserEmail($customer->getEmail());
87
+ $user->setFirstName($customer->getFirstname());
88
+ $user->setLastName($customer->getLastname());
89
+
90
+ if ($customer->getOriginalEmail() == $user->getUserLogin()) {
91
+ $user->setUserLogin($customer->getEmail());
92
+ }
93
+
94
+ $user->getMeta('nickname');
95
+
96
+ if (!$user->getNickname() || $customer->getOriginalFirstname() == $user->getNickname()) {
97
+ $user->setNickname($customer->getFirstname());
98
+ }
99
+
100
+ try {
101
+ if ($customer->hasPassword()) {
102
+ if ($customer->validatePassword($customer->getPassword())) {
103
+ $wpHash = $this->hashPasswordForWordPress($customer->getPassword());
104
+
105
+ if ($this->isValidWordPressPassword($customer->getPassword(), $wpHash)) {
106
+ $user->setUserPass($wpHash);
107
+ }
108
+ }
109
+ }
110
+ }
111
+ catch (Exception $e) {
112
+ Mage::helper('wordpress')->log($e->getMessage());
113
+ Mage::logException($e);
114
+ }
115
+
116
+ try {
117
+ $user->save();
118
+ }
119
+ catch (Exception $e) {
120
+ Mage::helper('wordpress')->log($e->getMessage());
121
+ Mage::logException($e);
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Retrieve a WordPress user model based on an email address
129
+ *
130
+ * @param string $email
131
+ * @return Fishpig_Wordpress_Model_User
132
+ */
133
+ public function getWordPressUserModelByEmail($email)
134
+ {
135
+ $user = Mage::getModel('wordpress/user')->load($email, 'user_email');
136
+
137
+ return $user->getId() ? $user : false;
138
+ }
139
+
140
+ /**
141
+ * Retrieve a Magento customer ID by email
142
+ *
143
+ * @param string $email
144
+ * @return int
145
+ */
146
+ public function getMagentoCustomerIdByEmail($email)
147
+ {
148
+ $select = $this->getMagentoReadAdapter()
149
+ ->select()
150
+ ->from($this->getMagentoTableName('customer_entity'), 'entity_id')
151
+ ->where('email=?', $email)
152
+ ->limit(1);
153
+
154
+ return $this->getMagentoReadAdapter()->fetchOne($select);
155
+ }
156
+
157
+ /**
158
+ * Copies the basic information from WordPress to Magento
159
+ *
160
+ * @param Fishpig_Wordpress_Model_User $user
161
+ * @param string $password
162
+ * @return false | Mage_Customer_Model_Customer
163
+ */
164
+ protected function _copyCustomerFromWordPress($user, $password)
165
+ {
166
+ $customer = Mage::getModel('customer/customer');
167
+
168
+ $customer->setEmail($user->getUserEmail())
169
+ ->setPassword($password)
170
+ ->setFirstname($user->getMeta('first_name'))
171
+ ->setLastname($user->getMeta('last_name'))
172
+ ->setStoreId(Mage::app()->getStore()->getId());
173
+
174
+ try {
175
+ $customer->save();
176
+
177
+ return $customer;
178
+ }
179
+ catch (Exception $e) {
180
+ return false;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Copy the customer to WordPress
186
+ *
187
+ * @param int $customerId
188
+ * @param Varien_Object $login
189
+ */
190
+ protected function _copyCustomerToWordPress($customer, Varien_Object $login)
191
+ {
192
+ if (!is_object($customer)) {
193
+ $customer = Mage::getModel('customer/customer')->load($customer);
194
+ }
195
+
196
+ if ($customer->getId()) {
197
+ $user = Mage::getModel('wordpress/user');
198
+
199
+ $user->load($customer->getEmail(), 'user_email');
200
+
201
+ if (!$user->getId()) {
202
+ $user->setUserLogin($customer->getEmail());
203
+ $user->setUserPass($this->hashPasswordForWordPress($login->getPassword()));
204
+ $user->setUserNicename($customer->getName());
205
+ $user->setUserEmail($customer->getEmail());
206
+ $user->setUserRegistered($customer->getCreatedAt());
207
+ $user->setUserStatus(0);
208
+ $user->setDisplayName($customer->getFirstname());
209
+ $user->setFirstName($customer->getFirstname());
210
+ $user->setLastName($customer->getLastname());
211
+ $user->setWpCapabilities(serialize(array('subscriber' => 1)));
212
+ $user->setWpUserLevel(0);
213
+ $user->setNickname($customer->getFirstname());
214
+
215
+ try {
216
+ $user->save();
217
+ }
218
+ catch (Exception $e) {
219
+ Mage::helper('wordpress')->log($e->getMessage());
220
+ Mage::logException($e);
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+
227
+ /**
228
+ * Retrieve the read adapter for the Magento database
229
+ */
230
+ public function getMagentoReadAdapter()
231
+ {
232
+ return Mage::getSingleton('core/resource')->getConnection('core_read');
233
+ }
234
+
235
+ /**
236
+ * Retrieve the read adapter for the WordPress database
237
+ */
238
+ public function getWordPressReadAdapter()
239
+ {
240
+ if (Mage::helper('wordpress')->isSameDatabase()) {
241
+ return $this->getMagentoReadAdapter();
242
+ }
243
+
244
+ return Mage::helper('wordpress/db')->getWordpressRead();
245
+ }
246
+
247
+ /**
248
+ * Retrieve the write adapter for the Magento database
249
+ */
250
+ public function getMagentoWriteAdapter()
251
+ {
252
+ return Mage::helper('wordpress/db')->getMagentoWrite();
253
+ }
254
+
255
+ /**
256
+ * Retrieve Magento table name
257
+ *
258
+ * @return string
259
+ */
260
+ public function getMagentoTableName($table)
261
+ {
262
+ return Mage::getSingleton('core/resource')->getTableName($table);
263
+ }
264
+
265
+ /**
266
+ * Retrieve a WordPress table name
267
+ *
268
+ * @return string
269
+ */
270
+ public function getWordPressTableName($table)
271
+ {
272
+ return Mage::helper('wordpress/db')->getTableName($table);
273
+ }
274
+
275
+ /**
276
+ * Force inclusion of WordPress Password class file
277
+ */
278
+ protected function _requirePhPassClass()
279
+ {
280
+ if (is_null(Mage::registry('_wordpress_require_phpass'))) {
281
+ $classFile = Mage::helper('wordpress')->getWordPressPath() . 'wp-includes/class-phpass.php';
282
+
283
+ if (file_exists($classFile)) {
284
+ require_once($classFile);
285
+ Mage::register('_wordpress_require_phpass', true, true);
286
+ }
287
+ else {
288
+ Mage::register('_wordpress_require_phpass', false, true);
289
+ Mage::helper('wordpress')->log(Mage::helper('wordpress')->__('Error including password file (%s)', $classFile));
290
+ }
291
+ }
292
+
293
+ return Mage::registry('_wordpress_require_phpass');
294
+ }
295
+
296
+ /**
297
+ * Returns true if the password can be hashed to equal $hash
298
+ *
299
+ * @oaram string $password
300
+ * @param string(hash) $hash
301
+ * @return bool
302
+ */
303
+ public function isValidWordPressPassword($password, $hash)
304
+ {
305
+ $this->_requirePhPassClass();
306
+
307
+ $wpHasher = new PasswordHash(8, TRUE);
308
+
309
+ return $wpHasher->CheckPassword($password, $hash) ? true : $hash == md5($password);
310
+ }
311
+
312
+ /**
313
+ * Convert a string to a valid WordPress password hash
314
+ *
315
+ * @param string $password
316
+ * @return string
317
+ */
318
+ public function hashPasswordForWordPress($password)
319
+ {
320
+ $this->_requirePhPassClass();
321
+
322
+ if (class_exists('PasswordHash')) {
323
+ $wpHasher = new PasswordHash(8, TRUE);
324
+
325
+ return $wpHasher->HashPassword($password);
326
+ }
327
+
328
+ throw new Exception('Cannot find class PasswordHash');
329
+ }
330
+
331
+ /**
332
+ * Determine whether synchronisation can be ran
333
+ *
334
+ * @return bool
335
+ */
336
+ public function isCustomerSynchronisationEnabled()
337
+ {
338
+ if (Mage::helper('wordpress')->isEnabled()) {
339
+ if (Mage::helper('wordpress')->isCustomerSynchronisationEnabled()) {
340
+ if ($this->_requirePhPassClass()) {
341
+ return true;
342
+ }
343
+ }
344
+ }
345
+
346
+ return false;
347
+ }
348
+ }
app/code/community/Fishpig/WpCustomerSynch/etc/config.xml ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Fishpig_WpCustomerSynch>
5
+ <version>0.9.0</version>
6
+ </Fishpig_WpCustomerSynch>
7
+ </modules>
8
+ <global>
9
+ <events>
10
+ <controller_action_predispatch_customer_account_loginPost>
11
+ <observers>
12
+ <wp_synch_customerLogin>
13
+ <type>singleton</type>
14
+ <class>wpCustomerSynch/observer</class>
15
+ <method>customerLogin</method>
16
+ </wp_synch_customerLogin>
17
+ </observers>
18
+ </controller_action_predispatch_customer_account_loginPost>
19
+ <customer_load_after>
20
+ <observers>
21
+ <wp_synch_customerUpdate_pre>
22
+ <type>singleton</type>
23
+ <class>wpCustomerSynch/observer</class>
24
+ <method>initCustomerUpdate</method>
25
+ </wp_synch_customerUpdate_pre>
26
+ </observers>
27
+ </customer_load_after>
28
+ <customer_save_after>
29
+ <observers>
30
+ <wp_synch_customerUpdate>
31
+ <type>singleton</type>
32
+ <class>wpCustomerSynch/observer</class>
33
+ <method>customerUpdate</method>
34
+ </wp_synch_customerUpdate>
35
+ </observers>
36
+ </customer_save_after>
37
+ </events>
38
+ <models>
39
+ <wpCustomerSynch>
40
+ <class>Fishpig_WpCustomerSynch_Model</class>
41
+ </wpCustomerSynch>
42
+ </models>
43
+ <helpers>
44
+ <wpCustomerSynch>
45
+ <class>Fishpig_WpCustomerSynch_Helper</class>
46
+ </wpCustomerSynch>
47
+ </helpers>
48
+ </global>
49
+ <default>
50
+ <wordpress>
51
+ <customer_synchronisation>
52
+ <enabled>1</enabled>
53
+ </customer_synchronisation>
54
+ </wordpress>
55
+ </default>
56
+ </config>
app/code/community/Fishpig/WpCustomerSynch/etc/system.xml ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <wordpress>
5
+ <groups>
6
+ <customer_synchronisation translate="label" module="wpCustomerSynch">
7
+ <label>Customer Synchronisation</label>
8
+ <sort_order>18</sort_order>
9
+ <show_in_default>1</show_in_default>
10
+ <show_in_store>1</show_in_store>
11
+ <show_in_website>1</show_in_website>
12
+ <fields>
13
+ <enabled>
14
+ <label>Enabled</label>
15
+ <sort_order>1</sort_order>
16
+ <frontend_type>select</frontend_type>
17
+ <source_model>adminhtml/system_config_source_yesno</source_model>
18
+ <show_in_default>1</show_in_default>
19
+ </enabled>
20
+ </fields>
21
+ </customer_synchronisation>
22
+ </groups>
23
+ </wordpress>
24
+ </sections>
25
+ </config>
app/code/community/Fishpig/WpCustomerSynch/license.txt ADDED
File without changes
app/code/community/Fishpig/WpCustomerSynch/notes.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##
2
+ # Fishpig's WordPress Customer Synchronisation for Magento eCommerce
3
+ #
4
+ # Author: FishPig Limited
5
+ # Documentation: http://fishpig.co.uk/wordpress-customer-synchronisation/
6
+ # Email Support: help@fishpig.co.uk
7
+ # Bug Reports: help@fishpig.co.uk
8
+ # Installation: http://fishpig.co.uk/wordpress-integration-setup/
9
+ ##
10
+
11
+ 23/07/2011 - v0.9.0
12
+ - Extension created and released
13
+ - Added backwards compatibility for old WordPress installations that use md5 password hashing
app/etc/modules/Fishpig_WpCustomerSynch.xml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Fishpig_WpCustomerSynch>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Fishpig_Wordpress />
9
+ <Mage_Customer />
10
+ <Mage_Core />
11
+ </depends>
12
+ </Fishpig_WpCustomerSynch>
13
+ </modules>
14
+ </config>
package.xml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Fishpigs_WpCustomerSync</name>
4
+ <version>0.9.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://fishpig.co.uk/license.txt">fishpig EULA</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Synchronise your Magento customers and WordPress users for a fully integrated experience.</summary>
10
+ <description>Synchronise your Magento customers and WordPress users for a fully integrated experience. Provide a single sign-on experience for your users to make their experience even better.&#xD;
11
+ &#xD;
12
+ This extension requires FishPig's WordPress Integration be installed.</description>
13
+ <notes>Extension created and released&#xD;
14
+ Added backwards compatibility for old WordPress installations that use md5 password hashing</notes>
15
+ <authors><author><name>fishpig</name><user>auto-converted</user><email>ben@fishpig.co.uk</email></author></authors>
16
+ <date>2011-07-23</date>
17
+ <time>17:59:03</time>
18
+ <contents><target name="magecommunity"><dir name="Fishpig"><dir name="WpCustomerSynch"><dir name="Helper"><file name="Data.php" hash="88df18cc6eebdc087e75e4732ec49154"/></dir><dir name="Model"><file name="Observer.php" hash="c420ee37206b4fa4ee62e3ee54834cd1"/></dir><dir name="etc"><file name="config.xml" hash="7f53a3831b31a2b1f21e9ff24a49e5c9"/><file name="system.xml" hash="24f254baf012a779d9d819af866992b5"/></dir><file name="license.txt" hash="d41d8cd98f00b204e9800998ecf8427e"/><file name="notes.txt" hash="a39aa097584e75b282a110de06b0ca4d"/></dir></dir></target><target name="mage"><dir name="app"><dir name="etc"><dir name="modules"><file name="Fishpig_WpCustomerSynch.xml" hash="158bd0cd37c974931d657d1b4f4437d5"/></dir></dir></dir></target></contents>
19
+ <compatible/>
20
+ <dependencies/>
21
+ </package>