|
1 <?php if (!defined('SOCIAL_UPGRADE')) { die('Direct script access not allowed.'); } |
|
2 /** |
|
3 * Upgrades Social to 2.0. |
|
4 */ |
|
5 |
|
6 // Force the lock |
|
7 $semaphore = Social_Semaphore::factory(); |
|
8 $semaphore->lock(); |
|
9 |
|
10 // Find old social_notify and update to _social_notify. |
|
11 $meta_keys = array( |
|
12 'social_aggregated_replies', |
|
13 'social_broadcast_error', |
|
14 'social_broadcast_accounts', |
|
15 'social_broadcasted_ids', |
|
16 'social_aggregation_log', |
|
17 'social_twitter_content', |
|
18 'social_notify_twitter', |
|
19 'social_facebook_content', |
|
20 'social_notify_facebook', |
|
21 'social_notify', |
|
22 'social_broadcasted' |
|
23 ); |
|
24 if (count($meta_keys)) { |
|
25 foreach ($meta_keys as $key) { |
|
26 $new = '_'.$key; |
|
27 if ($key == 'social_aggregated_replies') { |
|
28 $new = '_social_aggregated_ids'; |
|
29 } |
|
30 $wpdb->query(" |
|
31 UPDATE $wpdb->postmeta |
|
32 SET meta_key = '$new' |
|
33 WHERE meta_key = '$key' |
|
34 "); |
|
35 } |
|
36 } |
|
37 |
|
38 // Delete old useless meta |
|
39 $meta_keys = array( |
|
40 '_social_broadcasted' |
|
41 ); |
|
42 if (count($meta_keys)) { |
|
43 foreach ($meta_keys as $key) { |
|
44 $wpdb->query(" |
|
45 DELETE |
|
46 FROM $wpdb->postmeta |
|
47 WHERE meta_key = '$key' |
|
48 "); |
|
49 } |
|
50 } |
|
51 |
|
52 // De-auth Facebook accounts for new permissions. |
|
53 if (version_compare(Social::option('installed_version'), '2.0', '<')) { |
|
54 // Fix aggregated IDs |
|
55 $results = $wpdb->get_results(" |
|
56 SELECT post_id, meta_value |
|
57 FROM $wpdb->postmeta |
|
58 WHERE meta_key = '_social_aggregated_ids' |
|
59 "); |
|
60 if (is_array($results)) { |
|
61 foreach ($results as $result) { |
|
62 $result->meta_value = maybe_unserialize($result->meta_value); |
|
63 if (is_array($result->meta_value)) { |
|
64 $meta_value = array(); |
|
65 foreach ($result->meta_value as $id) { |
|
66 if (is_string($id)) { |
|
67 if (strpos($id, '_') !== false) { |
|
68 if (!isset($meta_value['facebook'])) { |
|
69 $meta_value['facebook'] = array(); |
|
70 } |
|
71 |
|
72 $meta_value['facebook'][] = $id; |
|
73 } |
|
74 else { |
|
75 if (!isset($meta_value['twitter'])) { |
|
76 $meta_value['twitter'] = array(); |
|
77 } |
|
78 |
|
79 $meta_value['twitter'][] = $id; |
|
80 } |
|
81 } |
|
82 } |
|
83 |
|
84 if (!empty($meta_value)) { |
|
85 update_post_meta($result->post_id, '_social_aggregated_ids', $meta_value); |
|
86 } |
|
87 else { |
|
88 delete_post_meta($result->post_id, '_social_aggregated_ids'); |
|
89 } |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 // Global accounts |
|
95 $set_meta = false; |
|
96 $accounts = get_option('social_accounts', array()); |
|
97 if (count($accounts)) { |
|
98 if (isset($accounts['facebook'])) { |
|
99 $set_meta = true; |
|
100 $accounts['facebook'] = array(); |
|
101 } |
|
102 |
|
103 if (isset($accounts['twitter'])) { |
|
104 foreach ($accounts['twitter'] as $account_id => $account) { |
|
105 if (!isset($account->universal)) { |
|
106 $accounts['twitter'][$account_id]->universal = true; |
|
107 } |
|
108 } |
|
109 } |
|
110 |
|
111 update_option('social_accounts', $accounts); |
|
112 } |
|
113 |
|
114 $results = $wpdb->get_results(" |
|
115 SELECT user_id, meta_value |
|
116 FROM $wpdb->usermeta |
|
117 WHERE meta_key = 'social_accounts' |
|
118 "); |
|
119 if (is_array($results)) { |
|
120 foreach ($results as $result) { |
|
121 $accounts = maybe_unserialize($result->meta_value); |
|
122 if (is_array($accounts)) { |
|
123 if (isset($accounts['facebook'])) { |
|
124 $set_meta = true; |
|
125 $accounts['facebook'] = array(); |
|
126 } |
|
127 |
|
128 if (isset($accounts['twitter'])) { |
|
129 foreach ($accounts['twitter'] as $account_id => $account) { |
|
130 if (!isset($account->personal)) { |
|
131 $accounts['twitter'][$account_id]->personal = true; |
|
132 } |
|
133 } |
|
134 } |
|
135 |
|
136 update_user_meta($result->user_id, 'social_accounts', $accounts); |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 if ($set_meta) { |
|
142 $results = $wpdb->get_results(" |
|
143 SELECT ID |
|
144 FROM $wpdb->users |
|
145 "); |
|
146 if (is_array($results)) { |
|
147 foreach ($results as $result) { |
|
148 update_user_meta($result->ID, 'social_2.0_upgrade', true); |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 // Upgrade system_cron to fetch_comments |
|
154 $fetch = $wpdb->get_var(" |
|
155 SELECT option_value |
|
156 FROM $wpdb->options |
|
157 WHERE option_name = 'social_system_crons' |
|
158 "); |
|
159 |
|
160 if (empty($fetch)) { |
|
161 $fetch = '1'; |
|
162 } |
|
163 |
|
164 $wpdb->query(" |
|
165 INSERT |
|
166 INTO $wpdb->options (option_name, option_value) |
|
167 VALUES('social_fetch_comments', '$fetch') |
|
168 ON DUPLICATE KEY UPDATE option_id = option_id |
|
169 "); |
|
170 |
|
171 // Update all comment types |
|
172 $keys = array(); |
|
173 foreach (Social::instance()->services() as $service) { |
|
174 $keys[] = $service->key(); |
|
175 if ($service->key() == 'facebook') { |
|
176 $keys[] = 'facebook-like'; |
|
177 } |
|
178 } |
|
179 |
|
180 foreach ($keys as $key) { |
|
181 $query = $wpdb->query(" |
|
182 UPDATE $wpdb->comments |
|
183 SET comment_type = 'social-$key' |
|
184 WHERE comment_type = '$key' |
|
185 "); |
|
186 } |
|
187 |
|
188 // Make sure all commenter accounts have the commenter flag |
|
189 $results = $wpdb->get_results(" |
|
190 SELECT m.user_id |
|
191 FROM $wpdb->users AS u |
|
192 JOIN $wpdb->usermeta AS m |
|
193 ON m.user_id = u.ID |
|
194 WHERE m.meta_key = 'social_accounts' |
|
195 AND u.user_email LIKE '%@example.com' |
|
196 "); |
|
197 if (count($results)) { |
|
198 foreach ($results as $result) { |
|
199 update_user_meta($result->user_id, 'social_commenter', 'true'); |
|
200 } |
|
201 } |
|
202 |
|
203 // Rename the XMLRPC option |
|
204 $wpdb->query(" |
|
205 UPDATE $wpdb->options |
|
206 SET option_name = 'social_default_accounts' |
|
207 WHERE option_name = 'social_xmlrpc_accounts' |
|
208 "); |
|
209 |
|
210 // Fix the broadcasted IDs format |
|
211 $results = $wpdb->get_results(" |
|
212 SELECT pm.meta_value, pm.post_id, p.post_content |
|
213 FROM $wpdb->postmeta AS pm |
|
214 JOIN $wpdb->posts AS p |
|
215 ON pm.post_id = p.ID |
|
216 WHERE meta_key = '_social_broadcasted_ids' |
|
217 "); |
|
218 if (is_array($results)) { |
|
219 foreach ($results as $result) { |
|
220 $meta_value = maybe_unserialize($result->meta_value); |
|
221 if (is_array($meta_value)) { |
|
222 Social::log('Old meta value for post #:post_id: :meta_value', array( |
|
223 'post_id' => $result->post_id, |
|
224 'meta_value' => print_r($meta_value, true) |
|
225 )); |
|
226 $_meta_value = array(); |
|
227 foreach ($meta_value as $service_key => $accounts) { |
|
228 if (!isset($_meta_value[$service_key])) { |
|
229 $_meta_value[$service_key] = array(); |
|
230 } |
|
231 |
|
232 foreach ($accounts as $account_id => $broadcasted) { |
|
233 Social::log('Checking account #:account_id (:service).', array( |
|
234 'account_id' => $account_id, |
|
235 'service' => $service_key |
|
236 )); |
|
237 if (!isset($_meta_value[$service_key][$account_id])) { |
|
238 $_meta_value[$service_key][$account_id] = array(); |
|
239 } |
|
240 |
|
241 if (is_array($broadcasted)) { |
|
242 foreach ($broadcasted as $id => $data) { |
|
243 Social::log('Current Meta Value: :meta_value', array( |
|
244 'meta_value' => print_r($_meta_value, true) |
|
245 )); |
|
246 |
|
247 if (is_scalar($data)) { |
|
248 $_meta_value[$service_key][$account_id][$data] = array( |
|
249 'message' => '' |
|
250 ); |
|
251 } |
|
252 else { |
|
253 $_meta_value[$service_key][$account_id][$id] = $data; |
|
254 } |
|
255 } |
|
256 } |
|
257 else { |
|
258 $_meta_value[$service_key][$account_id][$broadcasted] = array( |
|
259 'message' => '' |
|
260 ); |
|
261 } |
|
262 } |
|
263 } |
|
264 |
|
265 if (!empty($_meta_value)) { |
|
266 update_post_meta($result->post_id, '_social_broadcasted_ids', $_meta_value); |
|
267 } |
|
268 |
|
269 Social::log('New meta value for post #:post_id: :meta_value', array( |
|
270 'post_id' => $result->post_id, |
|
271 'meta_value' => print_r($_meta_value, true) |
|
272 )); |
|
273 } |
|
274 } |
|
275 } |
|
276 |
|
277 // Add broadcast by default |
|
278 Social::option('broadcast_by_default', '0'); |
|
279 |
|
280 // Reschedule posts for aggregation |
|
281 $results = $wpdb->get_results(" |
|
282 SELECT post_id |
|
283 FROM $wpdb->postmeta |
|
284 WHERE meta_key = '_social_broadcasted_ids' |
|
285 ORDER BY post_id DESC |
|
286 LIMIT 50 |
|
287 "); |
|
288 if ($results !== null) { |
|
289 $queue = Social_Aggregation_Queue::factory(); |
|
290 foreach ($results as $result) { |
|
291 if (!$queue->find($result->post_id)) { |
|
292 $queue->add($result->post_id); |
|
293 } |
|
294 } |
|
295 $queue->save(); |
|
296 } |
|
297 |
|
298 // Fix comment author urls for Facebook comments... |
|
299 $results = $wpdb->get_results(" |
|
300 SELECT comment_ID, comment_author_url |
|
301 FROM $wpdb->comments |
|
302 WHERE comment_type = 'social-facebook' |
|
303 AND comment_author_url LIKE 'http://graph.facebook.com/%' |
|
304 "); |
|
305 foreach ($results as $result) { |
|
306 $url = explode('http://graph.facebook.com/', $result->comment_author_url); |
|
307 $id = explode('/', $url[1]); |
|
308 |
|
309 $wpdb->query($wpdb->prepare(" |
|
310 UPDATE $wpdb->comments |
|
311 SET comment_author_url = %s |
|
312 WHERE comment_ID = %s |
|
313 ", 'http://facebook.com/profile.php?id='.$id[1], $result->comment_ID)); |
|
314 } |
|
315 |
|
316 // Remove old CRONs |
|
317 if (($timestamp = wp_next_scheduled('social_cron_60_init')) !== false) { |
|
318 wp_unschedule_event($timestamp, 'social_cron_60_init'); |
|
319 } |
|
320 if (($timestamp = wp_next_scheduled('social_cron_60_core')) !== false) { |
|
321 wp_unschedule_event($timestamp, 'social_cron_60_core'); |
|
322 } |
|
323 } |
|
324 |
|
325 // Flush the cache |
|
326 wp_cache_flush(); |
|
327 |
|
328 // Decrement the semaphore and unlock |
|
329 $semaphore->unlock(); |