forked from crowdfavorite/wp-social
/
social.php
2829 lines (2536 loc) · 85.9 KB
/
social.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Social
Plugin URI: http://mailchimp.com/social-plugin-for-wordpress/
Description: Broadcast newly published posts and pull in dicussions using integrations with Twitter and Facebook. Brought to you by <a href="http://mailchimp.com">MailChimp</a>.
Version: 1.0
Author: Crowd Favorite
Author URI: http://crowdfavorite.com/
*/
if (!class_exists('Social')) { // try to avoid double-loading...
/**
* Social Core
*
* @package Social
*/
final class Social {
/**
* @var string $api_url URL of the API
*/
public static $api_url = 'https://sopresto.mailchimp.com/';
/**
* @var string $plugins_url Path to the Social Plugin's directory. Set in constructor
*/
public static $plugins_url = '';
/**
* @var string $version plugin version
*/
public static $version = '1.0';
/**
* @var string $prefix prefix used to identify the plugin
*/
public static $prefix = 'social_';
/**
* @var string domain to use for i10n
*/
public static $i18n = 'social';
/**
* @var array services registered to Social
*/
public static $services = array();
/**
* @var array global services registered to Social
*/
public static $global_services = array();
/**
* @var array combined services
*/
public static $combined_services = array();
/**
* @var array commenter user accounts
*/
public static $commenters = array();
/**
* @var array default options
*/
protected static $options = array(
'debug' => 'false',
'install_date' => false,
'installed_version' => false,
'broadcast_format' => '{title}: {content} {url}',
'twitter_anywhere_api_key' => '',
'system_cron_api_key' => '',
'system_crons' => '0'
);
/**
* @var bool update plugin settings
*/
protected static $update = true;
/**
* @var bool upgrade the plugin
*/
protected static $upgrade = false;
/**
* @var string directory for the cron lock files
*/
private $cron_lock_dir = false;
/**
* @var bool broadcast meta set
*/
private $broadcast_meta_set = false;
/**
* Used to log debug data.
*
* @static
* @param string $content
* @return void
*/
public static function debug($content) {
$debug = get_option(Social::$prefix . 'debug', false);
if ($debug) {
$fh = fopen(Social::$plugins_url . 'log.txt', 'a');
fwrite($fh, $content . "\n");
fclose($fh);
}
}
/**
* Returns the broadcast format tokens.
*
* @static
* @return array
*/
public static function broadcast_tokens() {
$defaults = array(
'{url}' => __('Blog post\'s permalink'),
'{title}' => __('Blog post\'s title'),
'{content}' => __('Blog post\'s content'),
'{date}' => __('Blog post\'s date'),
'{author}' => __('Blog post\'s author'),
);
return apply_filters(Social::$prefix . 'broadcast_tokens', $defaults);
}
/**
* Returns the service object.
*
* @static
* @param string $service name of the service
* @param int $user_id custom user to load
* @param bool $global global service?
* @return Social_Facebook|Social_Twitter|bool
*/
public function service($service, $user_id = null, $global = false) {
if ($user_id !== null) {
if (!isset(Social::$services[$service])) {
return false;
}
if (!isset(Social::$commenters[$user_id])) {
$class = get_class(Social::$services[$service]);
Social::$commenters[$user_id] = new $class($user_id);
}
return Social::$commenters[$user_id];
}
if (!$global and isset(Social::$services[$service])) {
return Social::$services[$service];
}
else if ($global and isset(Social::$global_services[$service])) {
return Social::$global_services[$service];
}
return false;
}
/**
* Sets and returns the option.
*
* @static
* @param string $key option key
* @param string $value option value
* @param bool $update update system option?
* @return array|void
*/
public function option($key, $value = null, $update = false) {
if ($value === null) {
return (isset(Social::$options[$key]) ? Social::$options[$key] : null);
}
Social::$options[$key] = $value;
if ($update) {
update_option(Social::$prefix . $key, $value);
}
}
/**
* Sets the group of options
*
* @static
* @param array $options
* @return array|void
*/
public function options($options = null) {
if ($options === null) {
return Social::$options;
}
Social::$options = $options;
}
/**
* @return void
*/
public function install() {
if (version_compare(PHP_VERSION, '5.2.1', '<')) {
deactivate_plugins(basename(__FILE__)); // Deactivate ourself
wp_die(__("Sorry, Social requires PHP 5.2.1 or higher. Ask your host how to enable PHP 5 as the default on your servers.", Social::$i18n));
}
}
/**
* Remove the CRON unpon plugin deactivation.
*/
public function deactivate() {
wp_clear_scheduled_hook(Social::$prefix . 'aggregate_comments');
}
/**
* Add Settings link to plugins - code from GD Star Ratings
*
* @param array $links
* @param string $file
* @return array
*/
public function add_settings_link($links, $file) {
static $this_plugin;
if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
if ($file == $this_plugin){
$settings_link = '<a href="'.admin_url('options-general.php?page=social.php').'">'.__("Settings", "photosmash-galleries").'</a>';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* Adds the 15 minute interval.
*
* @param array $schedules
* @return array
*/
public function cron_schedules($schedules) {
$schedules['every15min'] = array(
'interval' => 900,
'display' => 'Every 15 minutes'
);
return $schedules;
}
/**
* Initializes the plugin.
*/
public function init() {
// Load the settings
$options = apply_filters(Social::$prefix . 'options', $this->options());
foreach ($options as $key => $default) {
$value = get_option(Social::$prefix . $key, $default);
if (empty($value)) {
switch ($key) {
case 'install_date':
$value = time();
break;
case 'installed_version':
$value = Social::$version;
break;
case 'system_cron_api_key':
$value = wp_generate_password(16, false);
break;
default:
$value = $default;
break;
}
update_option(Social::$prefix . $key, $value);
}
if ($key == 'installed_version' and (int)$value < (int)Social::$version) {
// Need to run an upgrade
Social::$upgrade = true;
}
$this->option($key, $value);
}
$url = plugins_url('', SOCIAL_FILE);
Social::$plugins_url = trailingslashit(apply_filters('social_plugins_url', $url));
$global_services = get_option(Social::$prefix . 'accounts');
$services = get_user_meta(get_current_user_id(), Social::$prefix . 'accounts', true);
if (!empty($global_services)) {
foreach ($global_services as $service => $accounts) {
if (!empty($accounts)) {
Social::$update = false;
break;
}
}
}
if (Social::$update and !empty($services)) {
foreach ($services as $service => $accounts) {
if (!empty($accounts)) {
Social::$update = false;
break;
}
}
}
if (!defined('SOCIAL_COMMENTS_JS')) {
define('SOCIAL_COMMENTS_JS', Social::$plugins_url . 'assets/social.js');
}
if (!defined('SOCIAL_ADMIN_JS')) {
define('SOCIAL_ADMIN_JS', Social::$plugins_url . 'assets/admin.js');
}
if (!defined('SOCIAL_ADMIN_CSS')) {
define('SOCIAL_ADMIN_CSS', Social::$plugins_url . 'assets/admin.css');
}
if (!defined('SOCIAL_COMMENTS_CSS')) {
define('SOCIAL_COMMENTS_CSS', Social::$plugins_url . 'assets/comments.css');
}
if (is_admin()) {
if (Social::$update) {
add_action('admin_notices', array($this, 'display_upgrade'));
}
// Deauthed accounts?
$deauthed = get_option(Social::$prefix . 'deauthed', array());
if (count($deauthed)) {
add_action('admin_notices', array($this, 'display_deauthed'));
}
if (SOCIAL_ADMIN_CSS !== false) {
wp_enqueue_style('social_admin', SOCIAL_ADMIN_CSS, array(), Social::$version, 'screen');
}
if (SOCIAL_ADMIN_JS !== false) {
wp_enqueue_script('social_admin', SOCIAL_ADMIN_JS, array(), Social::$version, true);
}
// Schedule the CRON?
if (isset($_GET['page']) and $_GET['page'] == 'social.php') {
if (Social::option('system_crons') != '1') {
if (wp_next_scheduled(Social::$prefix . 'cron_15_core') === false) {
wp_schedule_event(time() + 900, 'every15min', Social::$prefix . 'cron_15_core');
if (Social::option('debug') == '1') {
$this->log(basename(__FILE__).'.'.__LINE__.'.'.$_SERVER['REQUEST_URI'].' scheduling CRON 15');
}
}
if (wp_next_scheduled(Social::$prefix . 'cron_60_core') === false) {
wp_schedule_event(time() + 3600, 'hourly', Social::$prefix . 'cron_60_core');
if (Social::option('debug') == '1') {
$this->log(basename(__FILE__).'.'.__LINE__.'.'.$_SERVER['REQUEST_URI'].' scheduling CRON 60');
}
}
}
$url = str_replace('&', '&', wp_nonce_url(site_url('?' . Social::$prefix . 'action=check_crons')));
wp_remote_get($url, array(
'timeout' => 0.01,
'blocking' => false,
'sslverify' => apply_filters('https_local_ssl_verify', true)
));
}
// Fix for the helper
if (!defined('IS_PROFILE_PAGE')) {
define('IS_PROFILE_PAGE', false);
}
}
else {
if (SOCIAL_COMMENTS_CSS !== false) {
wp_enqueue_style('social_comments', SOCIAL_COMMENTS_CSS, array(), Social::$version, 'screen');
}
if (SOCIAL_COMMENTS_JS !== false) {
wp_enqueue_script('jquery');
}
}
if (SOCIAL_COMMENTS_JS !== false) {
wp_enqueue_script('social_js', SOCIAL_COMMENTS_JS, array(), Social::$version, true);
}
if (version_compare(PHP_VERSION, '5.2.1', '<')) {
wp_die(__("Sorry, Social requires PHP 5.2.1 or higher. Ask your host how to enable PHP 5 as the default on your servers.", Social::$i18n));
}
// Load Twitter/Facebook
require SOCIAL_PATH . 'lib/social/interface/service.php';
require SOCIAL_PATH . 'lib/social/helper.php';
require SOCIAL_PATH . 'lib/social/service.php';
require SOCIAL_PATH . 'social-facebook.php';
require SOCIAL_PATH . 'social-twitter.php';
// Register the Social services
Social::$services = apply_filters(Social::$prefix . 'register_service', Social::$services);
Social::$global_services = apply_filters(Social::$prefix . 'register_service', Social::$global_services);
// Load the user's accounts
if (!empty($services)) {
foreach ($services as $service => $accounts) {
if (!isset(Social::$services[$service]) or empty($accounts)) {
continue;
}
$this->service($service)->accounts($accounts);
}
}
// Load the global accounts
if (!empty($global_services)) {
foreach ($global_services as $service => $accounts) {
if (!isset(Social::$global_services[$service]) or empty($accounts)) {
continue;
}
$this->service($service, null, true)->accounts($accounts);
}
}
// Cache the global and user accounts.
$this->services();
// CRON Lock Location
if (is_writable(SOCIAL_PATH)) {
$this->cron_lock_dir = SOCIAL_PATH;
}
else {
$upload_dir = wp_upload_dir();
if (is_writable($upload_dir['basedir'])) {
$this->cron_lock_dir = $upload_dir['basedir'];
}
else if ($_GET['page'] == 'social.php') {
add_action('admin_notices', array($this, 'display_cron_lock_write_error'));
}
}
}
/**
* Displays the upgrade message.
*/
public function display_upgrade() {
if (current_user_can('manage_options') || current_user_can('publish_posts')) {
if (current_user_can('manage_options')) {
$url = Social_Helper::settings_url(null, true);
}
else {
$url = admin_url('profile.php#social-networks');
}
$message = sprintf(__('To broadcast to Twitter or Facebook, please update your <a href="%s">Social settings</a>.', Social::$i18n), $url);
echo '<div class="error"><p>' . $message . '</p></div>';
}
}
/**
* Displays warnings about deauthed accounts.
*/
public function display_deauthed() {
$deauthed = get_option(Social::$prefix . 'deauthed', array());
foreach ($deauthed as $service => $data) {
foreach ($data as $id => $message) {
echo '<div class="error"><p>' . $message . ' <a href="' . Social_Helper::settings_url(array('clear_deauth' => $id, 'service' => $service)) . '" class="' . Social::$prefix . 'deauth">[Dismiss]</a></p></div>';
}
}
}
/**
* Displays the CRON lock directory error.
*/
public function display_cron_lock_write_error() {
$upload_dir = wp_upload_dir();
if (isset($upload_dir['basedir'])) {
$message = sprintf(__('Social requires that either %s or %s be writable for CRON jobs.', Social::$i18n), SOCIAL_PATH, $upload_dir['basedir']);
}
else {
$message = sprintf(__('Social requires that %s is writable for CRON jobs.', Social::$i18n), SOCIAL_PATH);
}
echo '<div class="error"><p>' . $message . '</p></div>';
}
/**
* Handles the request.
*/
public function request_handler() {
if (isset($_GET[Social::$prefix . 'cron'])) {
$api_key_verified = false;
if (isset($_GET['api_key'])) {
if ($_GET['api_key'] == Social::option('system_cron_api_key')) {
$api_key_verified = true;
}
}
if (!$api_key_verified and !wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
$method = $_GET[Social::$prefix . 'cron'].'_core';
if (method_exists($this, $method)) {
switch ($method) {
case 'cron_15_core':
case 'cron_60_core':
$this->$method();
break;
}
}
}
else if (!empty($_POST[Social::$prefix . 'action'])) {
if (!wp_verify_nonce($_POST['_wpnonce'])) {
wp_die('Oops, please try again.');
}
switch ($_POST[Social::$prefix . 'action']) {
case 'broadcast_options':
$this->broadcast_options($_POST['post_ID'], $_POST['location']);
break;
case 'settings':
Social::option('broadcast_format', $_POST[Social::$prefix . 'broadcast_format'], true);
Social::option('debug', $_POST[Social::$prefix . 'debug'], true);
// Store the XML-RPC accounts
if (isset($_POST[Social::$prefix . 'xmlrpc_accounts'])) {
$accounts = array();
foreach ($_POST[Social::$prefix . 'xmlrpc_accounts'] as $account) {
$account = explode('|', $account);
$accounts[$account[0]][] = $account[1];
}
Social::option('xmlrpc_accounts', $accounts, true);
}
else {
delete_option(Social::$prefix . 'xmlrpc_accounts');
}
// Anywhere key
if (isset($_POST[Social::$prefix . 'twitter_anywhere_api_key'])) {
Social::option('twitter_anywhere_api_key', $_POST[Social::$prefix . 'twitter_anywhere_api_key'], true);
}
// System CRON
if (isset($_POST[Social::$prefix . 'system_crons'])) {
Social::option('system_crons', $_POST[Social::$prefix . 'system_crons'], true);
// Unschedule the CRONs
if (($timestamp = wp_next_scheduled(Social::$prefix . 'cron_15_core')) !== false) {
wp_unschedule_event($timestamp, Social::$prefix . 'cron_15_core');
}
if (($timestamp = wp_next_scheduled(Social::$prefix . 'cron_60_core')) !== false) {
wp_unschedule_event($timestamp, Social::$prefix . 'cron_60_core');
}
}
wp_redirect(Social_Helper::settings_url(array('saved' => 'true')));
exit;
break;
}
}
else if (!empty($_GET[Social::$prefix . 'action'])) {
$api_key_verified = false;
if (isset($_GET['api_key'])) {
if ($_GET['api_key'] == Social::option('system_cron_api_key')) {
$api_key_verified = true;
}
}
switch ($_GET[Social::$prefix . 'action']) {
case 'regenerate_api_key':
if (!wp_verify_nonce($_GET['_wpnonce'], 'regenerate_api_key')) {
wp_die('Oops, please try again.');
}
$key = wp_generate_password(16, false);
Social::option('system_cron_api_key', $key, true);
echo $key;
exit;
break;
case 'reload_form':
if (!is_user_logged_in()) {
echo json_encode(array(
'result' => 'error',
'html' => 'not logged in',
));
}
else {
$form = Social_Comment_Form::as_html(array(), $_GET['post_id'], false);
echo json_encode(array(
'result' => 'success',
'html' => $form,
'disconnect_url' => wp_loginout('', false)
));
}
exit;
break;
case 'cron_15':
if (!$api_key_verified and !wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
if ($this->cron_lock('cron_15_core')) {
do_action(Social::$prefix . 'cron_15');
$this->cron_unlock('cron_15_core');
}
break;
case 'cron_60':
if (!$api_key_verified and !wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
if ($this->cron_lock('cron_60_core')) {
do_action(Social::$prefix . 'cron_60');
$this->cron_unlock('cron_60_core');
}
break;
case 'aggregate_comments':
if (!$api_key_verified) {
wp_die('Oops, please try again.');
}
do_action(Social::$prefix . 'aggregate_comments');
break;
case 'retry_broadcast':
if (!$api_key_verified and !wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
$this->retry_broadcast();
break;
case 'run_aggregation':
if (!wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
if (Social::option('debug') == '1') {
$this->log('run_aggregation called manually.');
}
$this->run_aggregation($_GET['post_id']);
echo Social_Aggregate_Log::logs($_GET['post_id']);
exit;
break;
case 'import_from_url':
if (!wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
if (Social::option('debug') == '1') {
$this->log('import_from_url called manually.');
}
$services = $this->services();
if (isset($services['twitter']) and !empty($_GET['url'])) {
$services['twitter']->import_tweet($_GET['post_id'], $_GET['url']);
}
echo Social_Aggregate_Log::logs($_GET['post_id']);
exit;
break;
case 'check_crons':
if (!wp_verify_nonce($_GET['_wpnonce'])) {
wp_die('Oops, please try again.');
}
$crons = _get_cron_array();
$cron_60 = false;
$cron_15 = false;
foreach ($crons as $timestamp => $_crons) {
foreach ($_crons as $key => $cron) {
if ($key == Social::$prefix . 'cron_15_core') {
if ($cron_15) {
wp_unschedule_event($timestamp, Social::$prefix . 'cron_15_core');
}
else {
$cron_15 = true;
}
}
else if ($key == Social::$prefix . 'cron_60_core') {
if ($cron_60) {
wp_unschedule_event($timestamp, Social::$prefix . 'cron_60_core');
}
else {
$cron_60 = true;
}
}
}
}
break;
}
}
// Authorization complete?
else if (isset($_POST['data'])) {
$data = stripslashes($_POST['data']);
if (strpos($data, "\r") !== false) {
$data = str_replace(array("\r\n", "\r"), "\n", $data);
}
$data = json_decode($data);
$account = (object)array(
'keys' => $data->keys,
'user' => $data->user
);
// Add the account to the service.
if (IS_PROFILE_PAGE) {
$service = $this->service($data->service)->account($account);
}
else {
$service = $this->service($data->service, null, true)->account($account);
}
// Do we need to create a user?
if (!$service->loaded()) {
$service->create_user($account);
}
// Save the services
$service->save($account);
// Remove the service from the errors?
$deauthed = get_option(Social::$prefix . 'deauthed');
if (isset($deauthed[$service->service][$account->user->id])) {
unset($deauthed[$service->service][$account->user->id]);
update_option(Social::$prefix . 'deauthed', $deauthed);
// Remove from the global broadcast content as well.
$this->remove_from_xmlrpc($service->service, $account->user->id);
}
?>
<html>
<head>
<title>Authorized</title>
<?php wp_enqueue_script('jquery'); ?>
<?php wp_head(); ?>
</head>
<script type="text/javascript">
jQuery(function() {
window.opener.reloadSocialHTML();
window.close();
});
</script>
</html>
<?php
exit;
}
else if (isset($_GET[Social::$prefix . 'disconnect'])) {
if (IS_PROFILE_PAGE) {
$service = $this->service($_GET['service']);
}
else {
$service = Social::$global_services[$_GET['service']];
$this->remove_from_xmlrpc($_GET['service'], $_GET['id']);
}
$service->disconnect($_GET['id']);
if (is_admin()) {
wp_redirect(Social_Helper::settings_url());
}
else {
wp_logout();
wp_redirect($_GET['redirect_to']);
}
exit;
}
else if (isset($_GET['clear_deauth'])) {
$id = $_GET['clear_deauth'];
$service = $_GET['service'];
$deauthed = get_option(Social::$prefix . 'deauthed', array());
if (isset($deauthed[$service][$id])) {
unset($deauthed[$service][$id]);
update_option(Social::$prefix . 'deauthed', $deauthed);
$this->remove_from_xmlrpc($service, $id);
}
}
else if (isset($_GET['post']) and isset($_GET['action']) and $_GET['action'] == 'edit') {
$error = get_post_meta($_GET['post'], Social::$prefix . 'broadcast_error', true);
if ($error === 'true') {
add_action('admin_notices', array($this, 'display_failed_broadcast'));
delete_post_meta($_GET['post'], Social::$prefix . 'broadcast_error');
}
}
}
/**
* Removes an account from the XML-RPC stack.
*
* @param string $service
* @param int $service_id
* @return void
*/
private function remove_from_xmlrpc($service, $service_id) {
// Remove from the XML-RPC
$xmlrpc = get_option(Social::$prefix . 'xmlrpc_accounts', array());
if (isset($xmlrpc[$service])) {
$ids = array_values($xmlrpc[$service]);
if (in_array($service_id, $ids)) {
$_ids = array();
foreach ($ids as $id) {
if ($id != $_GET['id']) {
$_ids[] = $id;
}
}
$xmlrpc[$_GET['service']] = $_ids;
update_option(Social::$prefix . 'xmlrpc_accounts', $xmlrpc);
}
}
}
/**
* Handles the future to publish transition.
*
* @param object $post
* @return void
*/
public function publish_post($post) {
$this->broadcast($post);
}
/**
* Add Meta Boxes
*/
public function do_meta_boxes() {
global $post;
if (!Social::$update and $post !== null) {
foreach ($this->services() as $service) {
if (count($service->accounts())) {
add_meta_box(Social::$prefix . 'meta_broadcast', __('Social Broadcasting', Social::$i18n), array($this, 'add_meta_box'), 'post', 'side', 'core');
break;
}
}
if ($post->post_status == 'publish') {
add_meta_box(Social::$prefix . 'meta_aggregation_log', __('Social Comments', Social::$i18n), array($this, 'add_meta_log_box'), 'post', 'normal', 'core');
}
}
}
/**
* Adds the broadcasting meta box.
*/
public function add_meta_box() {
global $post;
$services = $this->services();
$broadcasted_ids = get_post_meta($post->ID, Social::$prefix . 'broadcasted_ids', true);
?>
<div class="social-meta-box" style="padding-top:6px;">
<?php
if ($post->post_status !== 'publish' or empty($broadcasted_ids))
{
?>
<h4 class="mar-top-none"><?php _e('Broadcast Post', Social::$i18n); ?></h4>
<p>Would you like to broadcast this post?</p>
<p>
<input type="radio" name="<?php echo Social::$prefix . 'notify'; ?>" id="<?php echo Social::$prefix . 'notify_yes'; ?>" class="social-toggle" value="1"/>
<label for="<?php echo Social::$prefix . 'notify_yes'; ?>" class="social-toggle-label"><?php _e('Yes', Social::$i18n); ?></label>
<input type="radio" name="<?php echo Social::$prefix . 'notify'; ?>" id="<?php echo Social::$prefix . 'notify_no'; ?>" class="social-toggle" value="0" checked="checked"/>
<label for="<?php echo Social::$prefix . 'notify_no'; ?>" class="social-toggle-label"><?php _e('No', Social::$i18n); ?></label>
</p>
<?php
}
else {
$header_shown = false;
foreach ($services as $key => $service) {
if (isset($broadcasted_ids[$key]) and count($broadcasted_ids[$key])) {
$accounts = $service->accounts();
if (!$header_shown) {
$header_shown = true;
?>
<p class="mar-top-none">This post has been broadcasted to the following accounts. You may broadcast to more accounts by clicking on the "Broadcast" button below.</p>
<input type="hidden" name="<?php echo Social::$prefix . 'notify'; ?>" value="1"/>
<?php
}
$accounts_output = '';
foreach ($broadcasted_ids[$key] as $user_id => $broadcast_id) {
if (isset($accounts[$user_id])) {
if (empty($accounts_output)) {
$accounts_output = '<h4>' . $service->title . '</h4><ul style="margin:0 0 25px 0;">';
}
$username = '';
if (isset($accounts[$user_id]->user->screen_name)) {
$username = $accounts[$user_id]->user->screen_name;
}
$accounts_output .= '<li style="clear:both;">';
$accounts_output .= '<img src="' . $service->profile_avatar($accounts[$user_id]) . '" width="24" height="24" style="float:left;" />';
$accounts_output .= '<span style="position:relative;top:5px;left:5px;">';
$accounts_output .= $service->profile_name($accounts[$user_id]);
$accounts_output .= ' <a href="' . $service->status_url($username, $broadcast_id) . '" target="_blank">View</a>';
$accounts_output .= '</span></li>';
}
}
if (!empty($accounts_output)) {
$accounts_output .= '</ul>';
echo $accounts_output;
}
}
}
}
if ($post->post_status == 'publish') {
?>
<p class="submit" style="clear:both;padding:0;margin:20px 0 0;">
<input type="submit" name="<?php echo Social::$prefix . 'broadcast'; ?>" value="<?php _e('Broadcast', Social::$i18n); ?>"/>
<a href="<?php echo admin_url('profile.php#social-networks'); ?>" style="float: right; padding-top: 8px;"><?php _e('My Accounts', Social::$i18n); ?></a>
</p>
<?php
}
?>
</div>
<?php
}
/**
* Adds the aggregation log meta box.
*
* @return void
*/
public function add_meta_log_box() {
global $post;
?>
<div class="social-meta-box-block">
<h4><?php _e('Add Tweet by URL', Social::$i18n); ?></h4>
<p><?php _e('Want to add a tweet? Enter the URL of the tweet here and Social will add it as a comment.', Social::$i18n); ?></p>
<p>
<input type="text" name="source_url" style="width:350px"/>
<span class="submit" style="float:none">
<a href="<?php echo wp_nonce_url(admin_url('?social_action=import_from_url&post_id=' . $post->ID)); ?>" id="import_from_url" class="button"><?php _e('Import Tweet', Social::$i18n); ?></a>
</span>
<img src="<?php echo admin_url('images/loading.gif'); ?>" style="position:relative;top:4px;left:0;display:none" id="import_from_url_loader"/>
</p>
</div><!-- .social-meta-box-block -->
<div class="social-meta-box-block cf-clearfix">
<h4><?php _e('Manual Refresh', Social::$i18n); ?></h4>
<p><?php _e('Manually run the comment aggregation and Social will look for mentions of this post on Facebook and Twitter.', Social::$i18n); ?></p>
<p class="submit" style="clear:both;float:none;padding:0;">
<a href="<?php echo wp_nonce_url(admin_url('?social_action=run_aggregation&post_id=' . $post->ID)); ?>" id="run_aggregation" class="button" style="float:left;margin-bottom:10px;"><?php _e('Find Social Comments', Social::$i18n); ?></a>
<img src="<?php echo admin_url('images/loading.gif'); ?>" style="float:left;position:relative;top:4px;left:5px;display:none;" id="run_aggregation_loader"/>
</p>
</div><!-- .social-meta-box-block -->
<div class="social-meta-box-block">
<h4><?php _e('Log', Social::$i18n); ?></h4>
<div id="aggregation_log">
<?php echo Social_Aggregate_Log::logs($post->ID); ?>
</div>
</div><!-- .social-meta-box-block -->
<?php
}
/**
* Show the broadcast options if publishing.
*
* @param string $location default post-publish location
* @param int $post_id post ID
* @return string|void
*/
public function redirect_post_location($location, $post_id) {
global $post;
if ((isset($_POST[Social::$prefix . 'notify']) and $post->post_status != 'publish') or isset($_POST[Social::$prefix . 'broadcast'])) {
$this->broadcast_options($post_id, $location);
}
return $location;
}
/**
* Adds a link to the "Settings" menu in WP-Admin.
*/
public function admin_menu() {
add_options_page(
__('Social Options', Social::$i18n),
__('Social', Social::$i18n),
'manage_options',
basename(__FILE__),
array($this, 'admin_options_form')
);
}
/**
* Displays the option form for the WP-Admin user.
*/
public function admin_options_form() {
?>
<form id="setup" class="social-view" method="post" action="<?php echo admin_url(); ?>">
<?php wp_nonce_field(); ?>
<input type="hidden" name="<?php echo Social::$prefix; ?>action" value="settings"/>
<?php if (isset($_GET['saved'])): ?>
<div id="message" class="updated">
<p><strong><?php _e('Social settings have been updated.', Social::$i18n); ?></strong></p>
</div>
<?php endif; ?>
<div class="wrap" id="social_options_page">