forked from Doap/wp-social
-
Notifications
You must be signed in to change notification settings - Fork 0
/
social.php
executable file
·2264 lines (2047 loc) · 66.6 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 discussions using integrations with Twitter and Facebook. Brought to you by <a href="http://mailchimp.com">MailChimp</a>.
Version: 2.7
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 URL of the API
*/
public static $api_url = 'https://sopresto.socialize-this.com/';
/**
* @var string version number
*/
public static $version = '2.7';
/**
* @var string CRON lock directory.
*/
public static $cron_lock_dir = null;
/**
* @var string plugins URL
*/
public static $plugins_url = '';
/**
* @var string plugins file path
*/
public static $plugins_path = '';
/**
* @var bool loaded by theme?
*/
public static $loaded_by_theme = false;
/**
* @var string duplicate comment message
*/
public static $duplicate_comment_message = 'duplicate comment';
/**
* @var Social_Log logger
*/
private static $log = null;
/**
* @var array default options
*/
protected static $options = array(
'debug' => false,
'install_date' => 0,
'installed_version' => 0,
'broadcast_format' => '{title}: {content} {url}',
'comment_broadcast_format' => '{content} {url}',
'system_cron_api_key' => null,
'cron' => '1',
'aggregate_comments' => '1',
'broadcast_by_default' => '0',
'use_standard_comments' => '0',
);
/**
* @var Social instance of Social
*/
public static $instance = null;
/**
* Loads the instance of Social.
*
* @static
* @return Social
*/
public static function instance() {
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Handles the auto loading of classes.
*
* @static
*
* @param string $class
*
* @return bool
*/
public static function auto_load($class) {
if (substr($class, 0, 7) == 'Social_' or substr($class, 0, 7) == 'Kohana_') {
try {
$file = Social::$plugins_path.'lib/'.str_replace('_', '/', strtolower($class)).'.php';
$file = apply_filters('social_auto_load_file', $file, $class);
if (file_exists($file)) {
require $file;
return true;
}
return false;
}
catch (Exception $e) {
Social::log(sprintf(__('Failed to auto load class %s.', 'social'), $class));
}
}
return true;
}
/**
* Returns the broadcast format tokens.
*
* Format:
*
* {key} => __('Description', 'social')
*
* @static
* @return array
*/
public static function broadcast_tokens() {
$query = new WP_Query(array(
'posts_per_page' => 1
));
if (count($query->posts) and $post = $query->posts[0]) {
$url = wp_get_shortlink($post->ID);
$date = get_date_from_gmt($post->post_date_gmt);
}
else {
$url = home_url('?p=123');
$date = get_date_from_gmt(current_time('mysql', true));
}
$defaults = array(
'{url}' => sprintf(__('Example: %s', 'social'), $url),
'{title}' => '',
'{content}' => '',
'{date}' => sprintf(__('Example: %s', 'social'), $date),
'{author}' => '',
);
return apply_filters('social_broadcast_tokens', $defaults);
}
/**
* Returns the comment broadcast format tokens.
*
* Format:
*
* {key} => __('Description', 'social')
*
* @static
* @return mixed
*/
public static function comment_broadcast_tokens() {
$defaults = array(
'{content}' => '',
'{url}' => '',
);
return apply_filters('social_comment_broadcast_tokens', $defaults);
}
/**
* Sets or gets an option based on the key defined.
*
* Get Format:
*
* Running Social::option('option_name') will load "social_option_name" using get_option()
*
* Set Format:
*
* Running Social::option('option_name', 'new_value') will update "social_option_name" to "new_value"
* using update_option().
*
* @static
* @param string $key option key
* @param mixed $value option value
* @return bool|mixed
* @uses get_option()
* @uses update_option()
*/
public static function option($key, $value = null) {
if ($value === null) {
$default = null;
if (isset(Social::$options[$key])) {
$default = Social::$options[$key];
}
return get_option('social_'.$key, $default);
}
update_option('social_'.$key, $value);
return false;
}
/**
* Add a message to the log.
*
* @static
* @param string $message message to add to the log
* @param array $args arguments to pass to the writer
* @param string $context context of the log message
* @param bool $backtrace show the backtrace
* @return void
*/
public static function log($message, array $args = null, $context = null, $backtrace = false) {
Social::$log->write($message, $args, $context, $backtrace);
}
/**
* Sets the loaded by theme.
*
* @static
* @return void
*/
public static function social_loaded_by_theme() {
self::$loaded_by_theme = true;
}
/**
* Sets the customer die handler.
*
* @static
* @param string $handler
* @return array
*/
public static function wp_die_handler($handler) {
return array('Social', 'wp_comment_die_handler');
}
/**
* Don't actually die for aggregation runs.
*
* @static
* @param string $message
* @param string $title
* @param array $args
* @return mixed
*/
public static function wp_comment_die_handler($message, $title, $args) {
if ($message == __('Duplicate comment detected; it looks as though you’ve already said that!')) {
// Keep going
throw new Exception(Social::$duplicate_comment_message);
}
}
/**
* @var bool is Social enabled?
*/
private $_enabled = null;
/**
* Returns an array of all of the services.
*
* Format of the data returned:
*
* $services = array(
* 'twitter' => Social_Service_Twitter,
* 'facebook' => Social_Service_Facebook,
* // ... any other services registered
* )
*
* @return array
*/
public function services() {
return $this->load_services();
}
/**
* Returns a service by access key.
*
* Loading a service:
*
* $twitter = Social::instance()->service('twitter');
*
* @param string $key service key
* @return mixed Social_Service|Social_Service_Twitter|Social_Service_Facebook|false
*/
public function service($key) {
$services = $this->load_services();
if (!isset($services[$key])) {
return false;
}
return $services[$key];
}
/**
* Returns a service by comment type.
*
* Loading a service:
*
* $twitter = Social::instance()->service_for_comment_type('social-twitter-rt');
*
* @param string $key service key
* @return mixed Social_Service|Social_Service_Twitter|Social_Service_Facebook|false
*/
public function service_for_comment_type($comment_type) {
$services = $this->load_services();
foreach ($services as $service) {
if (in_array($comment_type, $service->comment_types())) {
return $service;
}
}
return false;
}
/**
* Initializes Social.
*
* @wp-action init
* @return void
*/
public function init() {
// Load the language translations
if (Social::$loaded_by_theme) {
$path = trailingslashit(Social::$plugins_path).'lang';
load_theme_textdomain('social', $path);
}
else {
$plugin_dir = basename(dirname(SOCIAL_FILE)).'/lang';
load_plugin_textdomain('social', false, $plugin_dir);
}
if (version_compare(PHP_VERSION, '5.2.4', '<')) {
deactivate_plugins(basename(__FILE__)); // Deactivate ourself
wp_die(__("Sorry, Social requires PHP 5.2.4 or higher. Ask your host how to enable PHP 5 as the default on your servers.", 'social'));
}
// Just activated?
if (!Social::option('install_date')) {
Social::option('install_date', current_time('timestamp', 1));
Social::option('system_cron_api_key', wp_generate_password(16, false));
}
// Plugins URL
$url = plugins_url('', SOCIAL_FILE);
Social::$plugins_url = trailingslashit(apply_filters('social_plugins_url', $url));
Social::$plugins_path = trailingslashit(apply_filters('social_plugins_path', SOCIAL_PATH));
// Set the logger
Social::$log = Social_Log::factory();
// Require Facebook and Twitter by default.
require Social::$plugins_path.'social-twitter.php';
require Social::$plugins_path.'social-facebook.php';
}
/**
* Auth Cookie expiration for API users.
*
* @return int
*/
public function auth_cookie_expiration() {
return 31536000; // 1 Year
}
/**
* Enqueues the assets for Social.
*
* @wp-action wp_enqueue_scripts
* @wp-action load-post-new.php
* @wp-action load-post.php
* @wp-action load-profile.php
* @wp-action load-settings_page_social
* @return void
*/
public function enqueue_assets() {
if (Social::option('use_standard_comments') == '1') {
return;
}
// JS/CSS
if (!defined('SOCIAL_COMMENTS_JS')) {
define('SOCIAL_COMMENTS_JS', Social::$plugins_url.'assets/social.js');
}
if (SOCIAL_COMMENTS_JS !== false) {
wp_enqueue_script('jquery');
wp_enqueue_script('social_js', SOCIAL_COMMENTS_JS, array('jquery'), Social::$version, true);
wp_localize_script('social_js', 'Sociali18n', array(
'commentReplyTitle' => __('Post a Reply', 'social'),
));
}
if (!is_admin()) {
if (!defined('SOCIAL_COMMENTS_CSS')) {
define('SOCIAL_COMMENTS_CSS', Social::$plugins_url.'assets/comments.css');
}
if (SOCIAL_COMMENTS_CSS !== false) {
wp_enqueue_style('social_comments', SOCIAL_COMMENTS_CSS, array(), Social::$version, 'screen');
}
}
}
/**
* Enqueues the assets for Social.
*
* @wp-action admin_enqueue_scripts
* @return void
*/
public function admin_enqueue_assets() {
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 (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);
$data = apply_filters('social_admin_js_strings', array(
'protectedTweet' => __('Protected Tweet', 'social'),
'invalidUrl' => __('Invalid URL', 'social'),
));
wp_localize_script('social_admin', 'socialAdminL10n', $data);
}
}
/**
* Loads the services on every page if the user is an admin.
*
* @wp-action admin_init
* @return void
*/
public function admin_init() {
if (current_user_can('manage_options') or current_user_can('publish_posts')) {
// Trigger upgrade?
if (isset($_GET['page']) and $_GET['page'] == basename(SOCIAL_FILE)) {
global $wpdb;
// First check for the semaphore options, they need to be added before the upgrade starts.
$results = $wpdb->get_results("
SELECT option_id
FROM $wpdb->options
WHERE option_name IN ('social_locked', 'social_unlocked')
");
if (!count($results)) {
update_option('social_unlocked', '1');
update_option('social_last_lock_time', current_time('mysql', 1));
update_option('social_semaphore', '0');
}
if (version_compare(Social::option('installed_version'), Social::$version, '<')) {
$this->_enabled = false;
$this->upgrade();
}
}
if ($this->_enabled === null) {
$this->load_services();
}
}
// Redirect to the home_url() if the user is a commenter.
if (!current_user_can('publish_posts')) {
$commenter = get_user_meta(get_current_user_id(), 'social_commenter', true);
if (!empty($commenter) and $commenter == 'true') {
wp_redirect(trailingslashit(home_url()));
}
}
}
/**
* Checks to see if system crons are disabled.
*
* @wp-action load-settings_page_social
* @return void
*/
public function check_system_cron() {
Social::log('Checking system CRON');
// Schedule CRONs
if (Social::option('cron') == '1') {
if (wp_next_scheduled('social_cron_15_init') === false) {
Social::log('Adding Social 15 CRON schedule');
wp_schedule_event(time() + 900, 'every15min', 'social_cron_15_init');
}
wp_remote_get(
admin_url('options_general.php?'.http_build_query(array(
'social_controller' => 'cron',
'social_action' => 'check_crons',
'social_api_key' => Social::option('system_cron_api_key')
), null, '&')),
array(
'timeout' => 0.01,
'blocking' => false,
'sslverify' => apply_filters('https_local_ssl_verify', true),
)
);
}
}
/**
* Handlers requests.
*
* @wp-action init
* @return void
*/
public function request_handler() {
if (isset($_GET['social_controller'])) {
Social_Request::factory()->execute();
}
}
/**
* Adds a link to the "Settings" menu in WP-Admin.
*
* @wp-action admin_menu
* @return void
*/
public function admin_menu() {
add_options_page(
__('Social Options', 'social'),
__('Social', 'social'),
'manage_options',
basename(SOCIAL_FILE),
array(
$this,
'admin_options_form'
)
);
}
/**
* Add Settings link to plugins - code from GD Star Ratings
*
* @wp-filter plugin_action_links
* @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="'.esc_url(admin_url('options-general.php?page=social.php')).'">'.__('Settings', 'social').'</a>';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* Handles the display of different messages for admin notices.
*
* @wp-action admin_notices
* @action admin_notices
*/
public function admin_notices() {
if (current_user_can('manage_options') or current_user_can('publish_posts')) {
// Upgrade notice
if (version_compare(Social::option('installed_version'), Social::$version, '<')) {
$message = sprintf(__('Social is shiny and new! Please <a href="%s">verify and save your settings</a> to complete the upgrade.', 'social'), esc_url(Social::settings_url()));
echo '<div class="error"><p>'.$message.'</p></div>';
}
$suppress_no_accounts_notice = get_user_meta(get_current_user_id(), 'social_suppress_no_accounts_notice', true);
if (!$this->_enabled and (!isset($_GET['page']) or $_GET['page'] != basename(SOCIAL_FILE)) and empty($suppress_no_accounts_notice)) {
$dismiss = sprintf(__('<a href="%s" class="social_dismiss">[Dismiss]</a>', 'social'), esc_url(admin_url('options-general.php?social_controller=settings&social_action=suppress_no_accounts_notice')));
$message = sprintf(__('To start using Social, please <a href="%s">add an account</a>.', 'social'), esc_url(Social::settings_url()));
echo '<div class="error"><p>'.$message.' '.$dismiss.'</p></div>';
}
if (isset($_GET['page']) and $_GET['page'] == basename(SOCIAL_FILE)) {
// CRON Lock
if (Social::option('cron_lock_error') !== null) {
$upload_dir = wp_upload_dir();
if (is_writeable(Social::$plugins_path) or (isset($upload_dir['basedir']) and is_writeable($upload_dir['basedir']))) {
delete_option('social_cron_lock_error');
}
else {
if (isset($upload_dir['basedir'])) {
$message = sprintf(__('Social requires that either %s or %s be writable for CRON jobs.', 'social'), esc_html(Social::$plugins_path), esc_html($upload_dir['basedir']));
}
else {
$message = sprintf(__('Social requires that %s is writable for CRON jobs.', 'social'), esc_html(Social::$plugins_path));
}
echo '<div class="error"><p>'.esc_html($message).'</p></div>';
}
}
// Enable notice?
$suppress_enable_notice = get_user_meta(get_current_user_id(), 'social_suppress_enable_notice', true);
if (empty($suppress_enable_notice)) {
$message = __('When you enable Social, users will be created when they log in with Facebook or Twitter to comment. These users are created without a role and will be prevented from accessing the admin side of WordPress until an administrator edits the user to give them a role.', 'social');
$dismiss = sprintf(__('<a href="%s" class="social_dismiss">[Dismiss]</a>', 'social'), esc_url(admin_url('options-general.php?social_controller=settings&social_action=suppress_enable_notice')));
echo '<div class="updated"><p>'.$message.' '.$dismiss.'</p></div>';
}
}
// Log write error
$error = Social::option('log_write_error');
if ($error == '1') {
echo '<div class="error"><p>'.
sprintf(__('%s needs to be writable for Social\'s logging. <a href="%" class="social_dismiss">[Dismiss]</a>', 'social'), esc_html(Social::$plugins_path), esc_url(admin_url('options-general.php?social_controller=settings&social_action=clear_log_write_error'))).
'</p></div>';
}
}
// Deauthed accounts
$deauthed = Social::option('deauthed');
if (!empty($deauthed)) {
foreach ($deauthed as $service => $data) {
foreach ($data as $id => $message) {
$dismiss = sprintf(__('<a href="%s" class="%s">[Dismiss]</a>', 'social'), esc_url(admin_url('options-general.php?social_controller=settings&social_action=clear_deauth&id='.$id.'&service='.$service)), 'social_dismiss');
echo '<div class="error"><p>'.esc_html($message).' '.$dismiss.'</p></div>';
}
}
}
// Errored broadcasting?
global $post;
if (isset($post->ID)) {
$error_accounts = get_post_meta($post->ID, '_social_broadcast_error', true);
if (!empty($error_accounts)) {
$message = Social_View::factory('wp-admin/post/broadcast/error/notice', array(
'social' => $this,
'accounts' => $error_accounts,
'post' => $post,
));
echo '<div class="error" id="social-broadcast-error">'.$message.'</div>';
delete_post_meta($post->ID, '_social_broadcast_error');
}
}
// 2.0 Upgrade?
$upgrade_2_0 = get_user_meta(get_current_user_id(), 'social_2.0_upgrade', true);
if (!empty($upgrade_2_0)) {
if (current_user_can('manage_options')) {
$output = __('Social needs to re-authorize your Facebook account(s). Please re-connect your <a href="%s">global</a> and <a href="%s">personal</a> accounts.', 'social');
$output = sprintf($output, esc_url(Social::settings_url()), esc_url(admin_url('profile.php#social-accounts')));
}
else {
$output = __('Social needs to re-authorize your Facebook account(s).. Please re-connect your <a href="%s">personal</a> accounts.', 'social');
$output = sprintf($output, esc_url(admin_url('profile.php#social-networks')));
}
$dismiss = sprintf(__('<a href="%s" class="%s">[Dismiss]</a>', 'social'), esc_url(admin_url('options-general.php?social_controller=settings&social_action=clear_2_0_upgrade')), 'social_dismiss');
echo '<div class="error"><p>'.$output.' '.$dismiss.'</p></div>';
}
}
/**
* Displays the admin options form.
*
* @return void
*/
public function admin_options_form() {
Social_Request::factory('settings/index')->execute();
}
/**
* Shows the user's social network accounts.
*
* @wp-action show_user_profile
* @param object $profileuser
* @return void
*/
public function show_user_profile($profileuser) {
$default_accounts = get_user_meta($profileuser->ID, 'social_default_accounts', true);
if (empty($default_accounts)) {
$default_accounts = array();
}
$accounts = array();
foreach ($this->services() as $key => $service) {
if (!isset($accounts[$key])) {
$accounts[$key] = array();
}
foreach ($service->accounts() as $account) {
if ($account->personal()) {
$accounts[$key][] = $account->id();
}
}
}
echo Social_View::factory('wp-admin/profile', array(
'defaults' => $default_accounts,
'services' => $this->services(),
'accounts' => $accounts,
));
}
/**
* Saves the default accounts for the user.
*
* @wp-action personal_options_update
* @param int $user_id
* @return void
*/
public function personal_options_update($user_id) {
// Store the default accounts
$accounts = array();
if (isset($_POST['social_default_accounts']) and is_array($_POST['social_default_accounts'])) {
foreach ($_POST['social_default_accounts'] as $account) {
$account = explode('|', $account);
$accounts[$account[0]][] = $account[1];
}
}
// TODO abstract this to the facebook plugin
if (isset($_POST['social_default_pages']) and is_array($_POST['social_default_pages'])) {
if (!isset($accounts['facebook'])) {
$accounts['facebook'] = array(
'pages' => array()
);
}
$accounts['facebook']['pages'] = $_POST['social_default_pages'];
}
if (count($accounts)) {
update_user_meta($user_id, 'social_default_accounts', $accounts);
}
else {
delete_user_meta($user_id, 'social_default_accounts');
}
// Save Enabled child accounts
$is_profile = true;
$enabled_child_accounts = is_array($_POST['social_enabled_child_accounts']) ? $_POST['social_enabled_child_accounts'] : array();
foreach ($this->services() as $service_key => $service) {
$updated_accounts = array();
foreach ($service->accounts() as $account) {
//default service to empty array in case it is not set
$enabled_child_accounts[$service_key] = isset($enabled_child_accounts[$service_key]) ? $enabled_child_accounts[$service_key] : array();
$account->update_enabled_child_accounts($enabled_child_accounts[$service_key]);
$updated_accounts[$account->id()] = $account->as_object();
}
$service->accounts($updated_accounts)->save($is_profile);
}
}
/**
* Return array of enabled social broadcasting post types
*
* @static
* @return array
*/
public static function broadcasting_enabled_post_types() {
return apply_filters('social_broadcasting_enabled_post_types', get_post_types(array(
'public' => true,
'hierarchical' => false
)));
}
/**
* Check if a post type has broadcasting enabled
*
* @static
* @param string $post_type post type to check for
* @return bool
*/
public static function broadcasting_enabled_for_post_type($post_type = null) {
return (bool) in_array($post_type, self::broadcasting_enabled_post_types());
}
/**
* Add Meta Boxes
*
* @wp-action do_meta_boxes
* @return void
*/
public function do_meta_boxes() {
global $post;
if ($post !== null && Social::option('disable_broadcasting') != 1) {
foreach (self::broadcasting_enabled_post_types() as $post_type) {
add_meta_box('social_meta_broadcast', __('Social Broadcasting', 'social'), array(
$this,
'add_meta_box_broadcast'
), $post_type, 'side', 'high');
$fetch = Social::option('aggregate_comments');
if ($this->_enabled
&& !empty($fetch)
&& $post->post_status == 'publish'
&& post_type_supports($post->post_type, 'comments')
&& Social::option('aggregate_comments'))
{
add_meta_box('social_meta_aggregation_log', __('Social Comments', 'social'), array(
$this,
'add_meta_box_log'
), $post_type, 'normal', 'core');
}
}
}
}
/**
* Adds the broadcasting meta box.
*
* @return void
*/
public function add_meta_box_broadcast() {
global $post;
$broadcasted = '';
$broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true);
if (!empty($broadcasted_ids)) {
$broadcasted = Social_View::factory('wp-admin/post/meta/broadcast/parts/broadcasted', array(
'services' => $this->services(),
'ids' => $broadcasted_ids,
'post' => $post,
));
}
$show_broadcast = false;
foreach ($this->services() as $service) {
if (count($service->accounts())) {
$show_broadcast = true;
break;
}
}
// Content
$button = '';
$content = '';
if ($show_broadcast) {
if ($post->post_status != 'private') {
switch ($post->post_status) {
case 'pending':
$button = 'Edit';
$accounts = get_post_meta($post->ID, '_social_broadcast_accounts', true);
$content = Social_View::factory('wp-admin/post/meta/broadcast/pending', array(
'accounts' => $accounts,
'services' => $this->services(),
));
break;
case 'future':
$button = 'Edit';
$accounts = get_post_meta($post->ID, '_social_broadcast_accounts', true);
$content = Social_View::factory('wp-admin/post/meta/broadcast/scheduled', array(
'services' => $this->services(),
'accounts' => $accounts,
));
break;
case 'publish':
$button = 'Broadcast';
break;
default:
if ($post->post_status == 'draft' and !empty($broadcasted_ids)) {
$content = '';
}
else {
$notify = false;
if (get_post_meta($post->ID, '_social_notify', true) == '1') {
$notify = true;
}
else if (Social::option('broadcast_by_default') == '1') {
$notify = true;
}
$content = Social_View::factory('wp-admin/post/meta/broadcast/default', array(
'post' => $post,
'notify' => $notify,
));
}
break;
}
}
else {
$content = Social_View::factory('wp-admin/post/meta/broadcast/private');
}
// Button
if (!empty($button)) {
$button = Social_View::factory('wp-admin/post/meta/broadcast/parts/button', array(
'broadcasted' => $broadcasted,
'button_text' => $button,
));
}
}
echo Social_View::factory('wp-admin/post/meta/broadcast/shell', array(
'post' => $post,
'content' => $content,
'broadcasted' => $broadcasted,
'button' => $button
));
}
/**
* Adds the aggregation log meta box.
*
* @return void
*/
public function add_meta_box_log() {
global $post;
$next_run = get_post_meta($post->ID, '_social_aggregation_next_run', true);
if (empty($next_run)) {
$next_run = __('Not Scheduled', 'social');
}
else {
$next_run = Social_Aggregation_Queue::next_run($next_run);
}
echo Social_View::factory('wp-admin/post/meta/log/shell', array(
'post' => $post,
'next_run' => $next_run,
));
}
/**
* Show the broadcast options if publishing.
*
* @wp-filter redirect_post_location
* @param string $location default post-publish location
* @param int $post_id post ID
* @return string|void
*/
public function redirect_post_location($location, $post_id) {
if ((isset($_POST['social_notify']) and $_POST['social_notify'] == '1') and
(isset($_POST['visibility']) and $_POST['visibility'] !== 'private')
) {
update_post_meta($post_id, '_social_notify', '1');
if (isset($_POST['publish']) or isset($_POST['social_broadcast'])) {
Social_Request::factory('broadcast/options')->post(array(
'post_ID' => $post_id,
'location' => $location,
))->execute();
}
}
else {
delete_post_meta($post_id, '_social_notify');
}
return $location;
}
/**
* Removes post meta if the post is going to private.
*
* @wp-action transition_post_status
* @param string $new
* @param string $old
* @param object $post
* @return void
*/
public function transition_post_status($new, $old, $post) {
if ($new == 'private') {
delete_post_meta($post->ID, '_social_notify');
delete_post_meta($post->ID, '_social_broadcast_accounts');
foreach ($this->services() as $key => $service) {
delete_post_meta($post->ID, '_social_'.$key.'_content');
}
}
else {
$xmlrpc = false;
if ($new == 'publish') {
if ( ( defined('XMLRPC_REQUEST') or defined('SOCIAL_MAIL_PUBLISH') ) and $old != 'publish') {
$xmlrpc = true;
$this->xmlrpc_publish_post($post);
}
if (self::broadcasting_enabled_for_post_type($post->post_type)) {
Social_Aggregation_Queue::factory()->add($post->ID)->save();
}
}
// Sends previously saved broadcast information
if ($xmlrpc or ($old == 'future' and !in_array($new, array('future', 'draft')))) {
Social_Request::factory('broadcast/run')->query(array(
'post_ID' => $post->ID
))->execute();
}
}
}
/**
* Broadcasts the post on XML RPC requests.
*
* @param object $post
* @return void
*/
public function xmlrpc_publish_post($post) {
if ($post and Social::option('broadcast_by_default') == '1') {
Social::log('Broadcasting triggered by XML-RPC.');
$broadcast_accounts = array();