-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathThreeWP_Broadcast.php
3441 lines (2919 loc) · 114 KB
/
ThreeWP_Broadcast.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
/*
Author: edward_plainview
Author Email: [email protected]
Author URI: http://www.plainview.se
Description: Broadcast / multipost a post, with attachments, custom fields, tags and other taxonomies to other blogs in the network.
Plugin Name: ThreeWP Broadcast
Plugin URI: http://plainview.se/wordpress/threewp-broadcast/
Version: 7
*/
namespace threewp_broadcast;
if ( ! class_exists( '\\threewp_broadcast\\base' ) ) require_once( __DIR__ . '/ThreeWP_Broadcast_Base.php' );
require_once( 'vendor/autoload.php' );
use \plainview\sdk\collections\collection;
use \threewp_broadcast\broadcast_data\blog;
use \plainview\sdk\html\div;
use \stdClass;
class ThreeWP_Broadcast
extends \threewp_broadcast\ThreeWP_Broadcast_Base
{
use \plainview\sdk\wordpress\traits\debug;
/**
@brief Broadcasting stack.
@details
An array of broadcasting_data objects, the latest being at the end.
@since 20131120
**/
private $broadcasting = [];
/**
@brief Public property used during the broadcast process.
@see include/Broadcasting_Data.php
@since 20130530
@var $broadcasting_data
**/
public $broadcasting_data = null;
/**
@brief Display Broadcast completely, including menus and post overview columns.
@since 20131015
@var $display_broadcast
**/
public $display_broadcast = true;
/**
@brief Display the Broadcast columns in the post overview.
@details Disabling this will prevent the user from unlinking posts.
@since 20131015
@var $display_broadcast_columns
**/
public $display_broadcast_columns = true;
/**
@brief Display the Broadcast menu
@since 20131015
@var $display_broadcast_menu
**/
public $display_broadcast_menu = true;
/**
@brief Add the meta box in the post editor?
@details Standard is null, which means the plugin(s) should work it out first.
@since 20131015
@var $display_broadcast_meta_box
**/
public $display_broadcast_meta_box = true;
/**
@brief Display information in the menu about the premium pack?
@see threewp_broadcast_premium_pack_info()
@since 20131004
@var $display_premium_pack_info
**/
public $display_premium_pack_info = true;
/**
@brief Caches permalinks looked up during this page view.
@see post_link()
@since 20130923
**/
public $permalink_cache;
public $plugin_version = 7;
// 20140501 when debug trait is moved to SDK.
protected $sdk_version_required = 20130505; // add_action / add_filter
protected $site_options = array(
'blogs_to_hide' => 5, // How many blogs to auto-hide
'broadcast_internal_custom_fields' => true, // Broadcast internal custom fields?
'canonical_url' => true, // Override the canonical URLs with the parent post's.
'clear_post' => true, // Clear the post before broadcasting.
'custom_field_whitelist' => '_wp_page_template _wplp_ _aioseop_', // Internal custom fields that should be broadcasted.
'custom_field_blacklist' => '', // Internal custom fields that should not be broadcasted.
'custom_field_protectlist' => '', // Internal custom fields that should not be overwritten on broadcast
'database_version' => 0, // Version of database and settings
'debug' => false, // Display debug information?
'debug_ips' => '', // List of IP addresses that can see debug information, when debug is enabled.
'save_post_priority' => 640, // Priority of save_post action. Higher = lets other plugins do their stuff first
'override_child_permalinks' => false, // Make the child's permalinks link back to the parent item?
'post_types' => 'post page', // Custom post types which use broadcasting
'existing_attachments' => 'use', // What to do with existing attachments: use, overwrite, randomize
'role_broadcast' => 'super_admin', // Role required to use broadcast function
'role_link' => 'super_admin', // Role required to use the link function
'role_broadcast_as_draft' => 'super_admin', // Role required to broadcast posts as templates
'role_broadcast_scheduled_posts' => 'super_admin', // Role required to broadcast scheduled, future posts
'role_taxonomies' => 'super_admin', // Role required to broadcast the taxonomies
'role_custom_fields' => 'super_admin', // Role required to broadcast the custom fields
);
public function _construct()
{
if ( ! $this->is_network )
wp_die( $this->_( 'Broadcast requires a Wordpress network to function.' ) );
$this->add_action( 'add_meta_boxes' );
$this->add_action( 'admin_menu' );
$this->add_action( 'admin_print_styles' );
if ( $this->get_site_option( 'override_child_permalinks' ) )
{
$this->add_filter( 'post_link', 10, 3 );
$this->add_filter( 'post_type_link', 'post_link', 10, 3 );
}
$this->add_filter( 'threewp_broadcast_add_meta_box' );
$this->add_filter( 'threewp_broadcast_admin_menu', 100 );
$this->add_filter( 'threewp_broadcast_broadcast_post' );
$this->add_filter( 'threewp_broadcast_get_user_writable_blogs', 11 ); // Allow other plugins to do this first.
$this->add_filter( 'threewp_broadcast_get_post_types', 9 ); // Add our custom post types to the array of broadcastable post types.
$this->add_action( 'threewp_broadcast_manage_posts_custom_column', 9 ); // Just before the standard 10.
$this->add_action( 'threewp_broadcast_maybe_clear_post', 11 );
$this->add_action( 'threewp_broadcast_menu', 9 );
$this->add_action( 'threewp_broadcast_menu', 'threewp_broadcast_menu_final', 100 );
$this->add_action( 'threewp_broadcast_prepare_broadcasting_data' );
$this->add_filter( 'threewp_broadcast_prepare_meta_box', 9 );
$this->add_filter( 'threewp_broadcast_prepare_meta_box', 'threewp_broadcast_prepared_meta_box', 100 );
$this->add_action( 'threewp_broadcast_wp_insert_term', 9 );
$this->add_action( 'threewp_broadcast_wp_update_term', 9 );
if ( $this->get_site_option( 'canonical_url' ) )
$this->add_action( 'wp_head', 1 );
$this->permalink_cache = new \stdClass;
}
public function admin_menu()
{
$this->load_language();
$action = new actions\admin_menu;
$action->apply();
$action = new actions\menu;
$action->broadcast = $this;
$action->apply();
// Hook into save_post, no matter is the meta box is displayed or not.
$this->add_action( 'save_post', intval( $this->get_site_option( 'save_post_priority' ) ) );
}
public function admin_print_styles()
{
$load = false;
foreach(array( 'post-new.php', 'post.php' ) as $string)
$load |= strpos( $_SERVER[ 'SCRIPT_FILENAME' ], $string) !== false;
if ( !$load )
return;
$this->enqueue_js();
wp_enqueue_style( 'threewp_broadcast', $this->paths[ 'url' ] . '/css/css.scss.min.css', '', $this->plugin_version );
}
// --------------------------------------------------------------------------------------------
// ----------------------------------------- Activate / Deactivate
// --------------------------------------------------------------------------------------------
public function activate()
{
if ( !$this->is_network )
wp_die("This plugin requires a Wordpress Network installation.");
$db_ver = $this->get_site_option( 'database_version', 0 );
if ( $db_ver < 1 )
{
// Remove old options
$this->delete_site_option( 'requirewhenbroadcasting' );
// Removed 1.5
$this->delete_site_option( 'activity_monitor_broadcasts' );
$this->delete_site_option( 'activity_monitor_group_changes' );
$this->delete_site_option( 'activity_monitor_unlinks' );
$this->query("CREATE TABLE IF NOT EXISTS `".$this->wpdb->base_prefix."_3wp_broadcast` (
`user_id` int(11) NOT NULL COMMENT 'User ID',
`data` text NOT NULL COMMENT 'User''s data',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Contains the group settings for all the users';
");
$this->query("CREATE TABLE IF NOT EXISTS `". $this->broadcast_data_table() . "` (
`blog_id` int(11) NOT NULL COMMENT 'Blog ID',
`post_id` int(11) NOT NULL COMMENT 'Post ID',
`data` text NOT NULL COMMENT 'Serialized BroadcastData',
KEY `blog_id` (`blog_id`,`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
");
// Cats and tags replaced by taxonomy support. Version 1.5
$this->delete_site_option( 'role_categories' );
$this->delete_site_option( 'role_categories_create' );
$this->delete_site_option( 'role_tags' );
$this->delete_site_option( 'role_tags_create' );
$db_ver = 1;
}
if ( $db_ver < 2 )
{
// Convert the array site options to strings.
foreach( [ 'custom_field_exceptions', 'post_types' ] as $key )
{
$value = $this->get_site_option( $key, '' );
if ( is_array( $value ) )
{
$value = array_filter( $value );
$value = implode( ' ', $value );
}
$this->update_site_option( $key, $value );
}
$db_ver = 2;
}
if ( $db_ver < 3 )
{
$this->delete_site_option( 'always_use_required_list' );
$this->delete_site_option( 'blacklist' );
$this->delete_site_option( 'requiredlist' );
$this->delete_site_option( 'role_taxonomies_create' );
$this->delete_site_option( 'role_groups' );
$db_ver = 3;
}
if ( $db_ver < 4 )
{
$exceptions = $this->get_site_option( 'custom_field_exceptions', '' );
$this->delete_site_option( 'custom_field_exceptions' );
$whitelist = $this->get_site_option( 'custom_field_whitelist', $exceptions );
$db_ver = 4;
}
if ( $db_ver < 5 )
{
$this->create_broadcast_data_id_column();
$db_ver = 5;
}
$this->update_site_option( 'database_version', $db_ver );
}
public function uninstall()
{
$this->query("DROP TABLE `".$this->wpdb->base_prefix."_3wp_broadcast`");
$query = sprintf( "DROP TABLE `%s`", $this->broadcast_data_table() );
$this->query( $query );
}
// --------------------------------------------------------------------------------------------
// ----------------------------------------- Admin
// --------------------------------------------------------------------------------------------
/**
@brief Show maintenance options.
@since 20131107
**/
public function admin_menu_maintenance()
{
$maintenance = new maintenance\controller;
echo $maintenance;
}
public function admin_menu_post_types()
{
$form = $this->form2();
$r = '';
$post_types = $this->get_site_option( 'post_types' );
$post_types = str_replace( ' ', "\n", $post_types );
$post_types_input = $form->textarea( 'post_types' )
->cols( 20, 10 )
->label( 'Custom post types to broadcast' )
->value( $post_types );
$label = $this->_( 'A list of custom post types that have broadcasting enabled. The default value is %s.', '<code>post<br/>page</code>' );
$post_types_input->description->set_unfiltered_label( $label );
$form->primary_button( 'save_post_types' )
->value( $this->_( 'Save the broadcastable custom post types' ) );
if ( $form->is_posting() )
{
$form->post()->use_post_values();
$post_types = $form->input( 'post_types' )->get_value();
$post_types = $this->lines_to_string( $post_types );
$this->update_site_option( 'post_types', $post_types);
$this->message( 'Custom post types saved!' );
}
$r .= $this->p_( 'Custom post types must be specified using their internal Wordpress names on a new line each. It is not possible to automatically make a list of available post types on the whole network because of a limitation within Wordpress (the current blog knows only of its own custom post types).' );
$blog_post_types = get_post_types();
unset( $blog_post_types[ 'nav_menu_item' ] );
$blog_post_types = array_keys( $blog_post_types );
$r .= $this->p_( 'The custom post types registered on <em>this</em> blog are: <code>%s</code>', implode( ', ', $blog_post_types ) );
$r .= $form->open_tag();
$r .= $form->display_form_table();
$r .= $form->close_tag();
echo $r;
}
public function admin_menu_premium_pack_info()
{
$r = '';
$r .= $this->html_css();
$contents = file_get_contents( __DIR__ . '/html/premium_pack_info.html' );
$r .= $this->wrap( $contents, $this->_( 'ThreeWP Broadcast Premium Pack info' ) );
echo $r;
}
public function admin_menu_settings()
{
$this->enqueue_js();
$form = $this->form2();
$form->id( 'broadcast_settings' );
$r = '';
$roles = $this->roles_as_options();
$roles = array_flip( $roles );
$fs = $form->fieldset( 'roles' );
$fs->legend->label_( 'Roles' );
$role_broadcast = $fs->select( 'role_broadcast' )
->value( $this->get_site_option( 'role_broadcast' ) )
->description_( 'The broadcast access role is the user role required to use the broadcast function at all.' )
->label_( 'Broadcast' )
->options( $roles );
$role_link = $fs->select( 'role_link' )
->value( $this->get_site_option( 'role_link' ) )
->description_( 'When a post is linked with broadcasted posts, the child posts are updated / deleted when the parent is updated.' )
->label_( 'Link to child posts' )
->options( $roles );
$role_custom_fields = $fs->select( 'role_custom_fields' )
->value( $this->get_site_option( 'role_custom_fields' ) )
->description_( 'Which role is needed to allow custom field broadcasting?' )
->label_( 'Broadcast custom fields' )
->options( $roles );
$role_taxonomies = $fs->select( 'role_taxonomies' )
->value( $this->get_site_option( 'role_taxonomies' ) )
->description_( 'Which role is needed to allow taxonomy broadcasting? The taxonomies must have the same slug on all blogs.' )
->label_( 'Broadcast taxonomies' )
->options( $roles );
$role_broadcast_as_draft = $fs->select( 'role_broadcast_as_draft' )
->value( $this->get_site_option( 'role_broadcast_as_draft' ) )
->description_( 'Which role is needed to broadcast drafts?' )
->label_( 'Broadcast as draft' )
->options( $roles );
$role_broadcast_scheduled_posts = $fs->select( 'role_broadcast_scheduled_posts' )
->value( $this->get_site_option( 'role_broadcast_scheduled_posts' ) )
->description_( 'Which role is needed to broadcast scheduled (future) posts?' )
->label_( 'Broadcast scheduled posts' )
->options( $roles );
$fs = $form->fieldset( 'seo' );
$fs->legend->label_( 'SEO' );
$override_child_permalinks = $fs->checkbox( 'override_child_permalinks' )
->checked( $this->get_site_option( 'override_child_permalinks' ) )
->description_( "Use the parent post's permalink for the children. If checked, child posts will link back to the parent post." )
->label_( "Use parent permalink" );
$canonical_url = $fs->checkbox( 'canonical_url' )
->checked( $this->get_site_option( 'canonical_url' ) )
->description_( "Child posts have their canonical URLs pointed to the URL of the parent post. This automatically disables the canonical URL from Yoast's Wordpress SEO plugin." )
->label_( 'Canonical URL' );
$fs = $form->fieldset( 'custom_field_handling' );
$fs->legend->label_( 'Custom field handling' );
$fs->markup( 'internal_field_info' )
->p_( 'Some custom fields start with underscores. They are generally Wordpress internal fields and therefore not broadcasted. Some plugins store their information as underscored custom fields. If you wish them, or some of them, to be broadcasted, use either of the options below.' );
$broadcast_internal_custom_fields = $fs->checkbox( 'broadcast_internal_custom_fields' )
->checked( $this->get_site_option( 'broadcast_internal_custom_fields' ) )
->description_( 'Broadcast all fields, including those beginning with underscores.' )
->label_( 'Broadcast internal custom fields' );
$blacklist = $this->get_site_option( 'custom_field_blacklist' );
$blacklist = str_replace( ' ', "\n", $blacklist );
$custom_field_blacklist = $fs->textarea( 'custom_field_blacklist' )
->cols( 40, 10 )
->description_( 'When broadcasting internal custom fields, override and do not broadcast these fields.' )
->label_( 'Internal field blacklist' )
->trim()
->value( $blacklist );
$protectlist = $this->get_site_option( 'custom_field_protectlist' );
$protectlist = str_replace( ' ', "\n", $protectlist );
$custom_field_protectlist = $fs->textarea( 'custom_field_protectlist' )
->cols( 40, 10 )
->description_( 'When broadcasting internal custom fields, do not overwrite the following fields on the child blogs.' )
->label_( 'Internal field protectlist' )
->trim()
->value( $protectlist );
$whitelist = $this->get_site_option( 'custom_field_whitelist' );
$whitelist = str_replace( ' ', "\n", $whitelist );
$custom_field_whitelist = $fs->textarea( 'custom_field_whitelist' )
->cols( 40, 10 )
->description_( 'When not broadcasting internal custom fields, override and broadcast these fields.' )
->label_( 'Internal field whitelist' )
->trim()
->value( $whitelist );
$fs->markup( 'whitelist_defaults' )
->p_( 'The default whitelist is: %s', "<code>\n_wp_page_template\n_wplp_\n_aioseop_</code>" );
$fs = $form->fieldset( 'misc' );
$fs->legend->label_( 'Miscellaneous' );
$clear_post = $fs->checkbox( 'clear_post' )
->description_( 'The POST PHP variable is data sent when updating posts. Most plugins are fine if the POST is cleared before broadcasting, while others require that the data remains intact. Uncheck this setting if you notice that child posts are not being treated the same on the child blogs as they are on the parent blog.' )
->label_( 'Clear POST' )
->checked( $this->get_site_option( 'clear_post' ) );
$save_post_priority = $fs->number( 'save_post_priority' )
->description_( 'The priority for the save_post hook. Should be after all other plugins have finished modifying the post. Default is 640.' )
->label_( 'save_post priority' )
->min( 1 )
->required()
->size( 5, 5 )
->value( $this->get_site_option( 'save_post_priority' ) );
$blogs_to_hide = $fs->number( 'blogs_to_hide' )
->description_( 'In the broadcast meta box, after how many blogs the list should be auto-hidden.' )
->label_( 'Blogs to hide' )
->min( 1 )
->required()
->size( 3, 3 )
->value( $this->get_site_option( 'blogs_to_hide' ) );
$existing_attachments = $fs->select( 'existing_attachments' )
->description_( 'Action to take when attachments with the same filename already exist on the child blog.' )
->label_( 'Existing attachments' )
->option( 'Use the existing attachment on the child blog', 'use' )
->option( 'Overwrite the attachment', 'overwrite' )
->option( 'Create a new attachment with a randomized suffix', 'randomize' )
->required()
->value( $this->get_site_option( 'existing_attachments', 'use' ) );
$this->add_debug_settings_to_form( $form );
$save = $form->primary_button( 'save' )
->value_( 'Save settings' );
if ( $form->is_posting() )
{
$form->post();
$form->use_post_values();
$this->update_site_option( 'role_broadcast', $role_broadcast->get_post_value() );
$this->update_site_option( 'role_link', $role_link->get_post_value() );
$this->update_site_option( 'role_taxonomies', $role_taxonomies->get_post_value() );
$this->update_site_option( 'role_custom_fields', $role_custom_fields->get_post_value() );
$this->update_site_option( 'role_broadcast_as_draft', $role_broadcast_as_draft->get_post_value() );
$this->update_site_option( 'role_broadcast_scheduled_posts', $role_broadcast_scheduled_posts->get_post_value() );
$this->update_site_option( 'override_child_permalinks', $override_child_permalinks->is_checked() );
$this->update_site_option( 'canonical_url', $canonical_url->is_checked() );
$this->update_site_option( 'broadcast_internal_custom_fields', $broadcast_internal_custom_fields->is_checked() );
$blacklist = $custom_field_blacklist->get_post_value();
$blacklist = $this->lines_to_string( $blacklist );
$this->update_site_option( 'custom_field_blacklist', $blacklist );
$whitelist = $custom_field_whitelist->get_post_value();
$whitelist = $this->lines_to_string( $whitelist );
$this->update_site_option( 'custom_field_whitelist', $whitelist );
$protectlist = $custom_field_protectlist->get_post_value();
$protectlist = $this->lines_to_string( $protectlist );
$this->update_site_option( 'custom_field_protectlist', $protectlist );
$this->update_site_option( 'clear_post', $clear_post->is_checked() );
$this->update_site_option( 'save_post_priority', $save_post_priority->get_post_value() );
$this->update_site_option( 'blogs_to_hide', $blogs_to_hide->get_post_value() );
$this->update_site_option( 'existing_attachments', $existing_attachments->get_post_value() );
$this->save_debug_settings_from_form( $form );
$this->message( 'Options saved!' );
}
$r .= $form->open_tag();
$r .= $form->display_form_table();
$r .= $form->close_tag();
echo $r;
}
public function admin_menu_tabs()
{
$this->load_language();
$tabs = $this->tabs();
$tabs->tab( 'settings' ) ->callback_this( 'admin_menu_settings' ) ->name_( 'Settings' );
$tabs->tab( 'maintenance' ) ->callback_this( 'admin_menu_maintenance' ) ->name_( 'Maintenance' );
$tabs->tab( 'post_types' ) ->callback_this( 'admin_menu_post_types' ) ->name_( 'Custom post types' );
$tabs->tab( 'uninstall' ) ->callback_this( 'admin_uninstall' ) ->name_( 'Uninstall' );
echo $tabs;
}
public function broadcast_menu_tabs()
{
$this->load_language();
$tabs = $this->tabs()
->default_tab( 'user_broadcast_info' )
->get_key( 'action' );
if ( isset( $_GET[ 'action' ] ) )
{
switch( $_GET[ 'action' ] )
{
case 'user_delete':
$tabs->tab( 'user_delete' )
->heading_( 'Delete the child post' )
->name_( 'Delete child' );
break;
case 'user_delete_all':
$tabs->tab( 'user_delete_all' )
->heading_( 'Delete all child posts' )
->name_( 'Delete all children' );
break;
case 'user_find_orphans':
$tabs->tab( 'user_find_orphans' )
->heading_( 'Find orphans' )
->name_( 'Find orphans' );
break;
case 'user_restore':
$tabs->tab( 'user_restore' )
->heading_( 'Restore the child post from the trash' )
->name_( 'Restore child' );
break;
case 'user_restore_all':
$tabs->tab( 'user_restore_all' )
->heading_( 'Restore all of the child posts from the trash' )
->name_( 'Restore all' );
break;
case 'user_trash':
$tabs->tab( 'user_trash' )
->heading_( 'Trash the child post' )
->name_( 'Trash child' );
break;
case 'user_trash_all':
$tabs->tab( 'user_trash_all' )
->heading_( 'Trash all child posts' )
->name_( 'Trash all children' );
break;
case 'user_unlink':
$tabs->tab( 'user_unlink' )
->heading_( 'Unlink the child post' )
->name_( 'Unlink child' );
break;
case 'user_unlink_all':
$tab = $tabs->tab( 'user_unlink_all' )
->callback_this( 'user_unlink' )
->heading_( 'Unlink all child posts' )
->name_( 'Unlink all children' );
break;
}
}
$tabs->tab( 'user_broadcast_info' )->name_( 'Broadcast information' );
$action = new actions\broadcast_menu_tabs();
$action->tabs = $tabs;
$action->apply();
echo $tabs;
}
/**
Deletes a broadcasted post.
**/
public function user_delete()
{
// Nonce check
global $blog_id;
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
$child_blog_id = $_GET[ 'child' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_delete';
$nonce_key .= '_' . $child_blog_id;
$nonce_key .= '_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )
die( __method__ . " security check failed." );
$broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id );
switch_to_blog( $child_blog_id );
$broadcasted_post_id = $broadcast_data->get_linked_child_on_this_blog();
if ( $broadcasted_post_id === null )
wp_die( 'No broadcasted child post found on this blog!' );
wp_delete_post( $broadcasted_post_id, true );
$broadcast_data->remove_linked_child( $child_blog_id );
restore_current_blog();
$broadcast_data = $this->set_post_broadcast_data( $blog_id, $post_id, $broadcast_data );
$message = $this->_( 'The child post has been deleted.' );
echo $this->message( $message);
echo sprintf( '<p><a href="%s">%s</a></p>',
wp_get_referer(),
$this->_( 'Back to post overview' )
);
}
/**
@brief Deletes all of a post's children.
@since 20131031
**/
public function user_delete_all()
{
// Nonce check
global $blog_id;
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_delete_all';
$nonce_key .= '_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )
die( __method__ . " security check failed." );
$broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id );
foreach( $broadcast_data->get_linked_children() as $child_blog_id => $child_post_id )
{
switch_to_blog( $child_blog_id );
wp_delete_post( $child_post_id, true );
$broadcast_data->remove_linked_child( $child_blog_id );
restore_current_blog();
}
$broadcast_data = $this->set_post_broadcast_data( $blog_id, $post_id, $broadcast_data );
$message = $this->_( "All of the child posts have been deleted." );
echo $this->message( $message);
echo sprintf( '<p><a href="%s">%s</a></p>',
wp_get_referer(),
$this->_( 'Back to post overview' )
);
}
/**
Finds orphans for a specific post.
**/
public function user_find_orphans()
{
$current_blog_id = get_current_blog_id();
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_find_orphans_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )
die( __method__ . " security check failed." );
$form = $this->form2();
$post = get_post( $post_id );
$r = '';
$table = $this->table();
$row = $table->head()->row();
$table->bulk_actions()
->form( $form )
->add( $this->_( 'Create link' ), 'create_link' )
->cb( $row );
$row->th()->text_( 'Domain' );
$broadcast_data = $this->get_post_broadcast_data( $current_blog_id, $post_id );
// Get a list of blogs that this user can link to.
$filter = new filters\get_user_writable_blogs( $this->user_id() );
$blogs = $filter->apply()->blogs;
$orphans = [];
foreach( $blogs as $blog )
{
if ( $blog->id == $current_blog_id )
continue;
if ( $broadcast_data->has_linked_child_on_this_blog( $blog->id ) )
continue;
$blog->switch_to();
$args = array(
'cache_results' => false,
'name' => $post->post_name,
'numberposts' => 2,
'post_type'=> $post->post_type,
'post_status' => $post->post_status,
);
$posts = get_posts( $args );
if ( count( $posts ) == 1 )
{
$orphan = reset( $posts );
$orphan->permalink = get_permalink( $orphan->ID );
$orphans[ $blog->id ] = $orphan;
}
$blog->switch_from();
}
if ( $form->is_posting() )
{
$form->post();
if ( $table->bulk_actions()->pressed() )
{
switch ( $table->bulk_actions()->get_action() )
{
case 'create_link':
$ids = $table->bulk_actions()->get_rows();
foreach( $orphans as $blog_id => $orphan )
{
$bulk_id = sprintf( '%s_%s', $blog_id, $orphan->ID );
if ( ! in_array( $bulk_id, $ids ) )
continue;
$broadcast_data->add_linked_child( $blog_id, $orphan->ID );
unset( $orphans[ $blog_id ] ); // There can only be one orphan per blog, so we're not interested in the blog anymore.
// Update the child's broadcast data.
$child_broadcast_data = $this->get_post_broadcast_data( $blog_id, $orphan->ID );
$child_broadcast_data->set_linked_parent( $current_blog_id, $post_id );
$this->set_post_broadcast_data( $blog_id, $orphan->ID, $child_broadcast_data );
}
// Update the broadcast data for the parent post.
$this->set_post_broadcast_data( $current_blog_id, $post_id, $broadcast_data );
echo $this->message_( 'The selected children were linked!' );
break;
}
}
}
if ( count( $orphans ) < 1 )
{
$r .= $this->_( 'No possible child posts were found on the other blogs you have write access to. Either there are no posts with the same title as this one, or all possible orphans have already been linked.' );
}
else
{
foreach( $orphans as $blog_id => $orphan )
{
$row = $table->body()->row();
$bulk_id = sprintf( '%s_%s', $blog_id, $orphan->ID );
$table->bulk_actions()->cb( $row, $bulk_id );
$row->td()->text( '<a href="' . $orphan->permalink . '">' . $blogs[ $blog_id ]->blogname . '</a>' );
}
$r .= $form->open_tag();
$r .= $table;
$r .= $form->close_tag();
}
echo $r;
echo '<p><a href="edit.php?post_type='.$post->post_type.'">Back to post overview</a></p>';
}
public function user_broadcast_info()
{
if ( ! is_super_admin() )
{
echo $this->p( 'No information available.' );
return;
}
$table = $this->table();
$table->caption()->text( 'Information' );
$row = $table->head()->row();
$row->th()->text( 'Key' );
$row->th()->text( 'Value' );
if ( $this->debugging() )
{
// Debug
$row = $table->body()->row();
$row->td()->text( 'Debugging' );
$row->td()->text( 'Enabled' );
}
// Broadcast version
$row = $table->body()->row();
$row->td()->text( 'Broadcast version' );
$row->td()->text( $this->plugin_version );
// Broadcast file checksum
$row = $table->body()->row();
$row->td()->text( 'Broadcast file checksum' );
$text = md5( file_get_contents( __FILE__ ) );
$row->td()->text( $text );
// PHP version
$row = $table->body()->row();
$row->td()->text( 'PHP version' );
$row->td()->text( phpversion() );
// SDK version
$row = $table->body()->row();
$text = sprintf( '%sPlainview Wordpress SDK%s',
'<a href="https://github.com/the-plainview/sdk">',
'</a>'
);
$row->td()->text( $text );
$row->td()->text( $this->sdk_version );
// SDK version required
$row = $table->body()->row();
$row->td()->text( 'SDK version required' );
$row->td()->text( $this->sdk_version_required );
// SDK path
$row = $table->body()->row();
$row->td()->text( 'Plainview Wordpress SDK path' );
$object = new \ReflectionObject( new \plainview\sdk\wordpress\base );
$row->td()->text( $object->getFilename() );
// WP upload path
$row = $table->body()->row();
$row->td()->text( 'Wordpress upload directory array' );
$row->td()->text( '<pre>' . var_export( wp_upload_dir(), true ) . '</pre>' );
// PHP maximum execution time
$row = $table->body()->row();
$row->td()->text( 'PHP maximum execution time' );
$text = sprintf( '%s seconds', ini_get ( 'max_execution_time' ) );
$row->td()->text( $text );
// PHP maximum memory limit
$row = $table->body()->row();
$row->td()->text( 'PHP memory limit' );
$text = ini_get( 'memory_limit' );
$row->td()->text( $text );
// WP maximum memory limit
$row = $table->body()->row();
$row->td()->text( 'Wordpress memory limit' );
$text = $this->p( WP_MEMORY_LIMIT . "
This can be increased by adding the following to your wp-config.php:
<code>define('WP_MEMORY_LIMIT', '512M');</code>
" );
$row->td()->text( $text );
// Debug info
$row = $table->body()->row();
$row->td()->text( 'Debug code' );
$text = WP_MEMORY_LIMIT;
$text = $this->p( "Add the following lines to your wp-config.php to help find out why errors or blank screens are occurring:
<code>ini_set('display_errors','On');</code>
<code>define('WP_DEBUG', true);</code>
" );
$row->td()->text( $text );
echo $table;
}
/**
@brief Restores a trashed post.
@since 20131031
**/
public function user_restore()
{
// Nonce check
global $blog_id;
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
$child_blog_id = $_GET[ 'child' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_restore';
$nonce_key .= '_' . $child_blog_id;
$nonce_key .= '_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )
die( __method__ . " security check failed." );
$broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id );
switch_to_blog( $child_blog_id );
$child_post_id = $broadcast_data->get_linked_child_on_this_blog();
wp_publish_post( $child_post_id );
restore_current_blog();
$message = $this->_( 'The child post has been restored.' );
echo $this->message( $message);
echo sprintf( '<p><a href="%s">%s</a></p>',
wp_get_referer(),
$this->_( 'Back to post overview' )
);
}
/**
@brief Restores all of the children from the trash.
@since 20131031
**/
public function user_restore_all()
{
// Nonce check
global $blog_id;
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_restore_all';
$nonce_key .= '_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )
die( __method__ . " security check failed." );
$broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id );
foreach( $broadcast_data->get_linked_children() as $child_blog_id => $child_post_id )
{
switch_to_blog( $child_blog_id );
wp_publish_post( $child_post_id );
restore_current_blog();
}
$message = $this->_( 'The child posts have been restored.' );
echo $this->message( $message);
echo sprintf( '<p><a href="%s">%s</a></p>',
wp_get_referer(),
$this->_( 'Back to post overview' )
);
}
/**
Trashes a broadcasted post.
**/
public function user_trash()
{
// Nonce check
global $blog_id;
$nonce = $_GET[ '_wpnonce' ];
$post_id = $_GET[ 'post' ];
$child_blog_id = $_GET[ 'child' ];
// Generate the nonce key to check against.
$nonce_key = 'broadcast_trash';
$nonce_key .= '_' . $child_blog_id;
$nonce_key .= '_' . $post_id;
if ( ! wp_verify_nonce( $nonce, $nonce_key) )