This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
class_commentpress_mu.php
714 lines (395 loc) · 12.5 KB
/
class_commentpress_mu.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
<?php /*
===============================================================
Class CommentPressMultiSite Version 1.0
===============================================================
AUTHOR : Christian Wach <[email protected]>
LAST MODIFIED : 19/03/2012
---------------------------------------------------------------
NOTES
=====
This class encapsulates all Multisite compatibility
---------------------------------------------------------------
*/
/*
===============================================================
Class Name
===============================================================
*/
class CommentPressMultiSite {
/*
===============================================================
Properties
===============================================================
*/
// parent object reference
var $parent_obj;
// admin object reference
var $db;
// context array
var $context;
// buddypress flag
var $buddypress;
/**
* @description: initialises this object
* @param object $parent_obj a reference to the parent object
* @return object
* @todo:
*
*/
function __construct( $parent_obj = null ) {
// store reference to "parent" (calling obj, not OOP parent)
$this->parent_obj = $parent_obj;
// store reference to database wrapper (child of calling obj)
$this->db = $this->parent_obj->db;
// init
$this->_init();
// --<
return $this;
}
/**
* PHP 4 constructor
*/
function CommentPressMultiSite( $parent_obj = null ) {
// is this php5?
if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
// call php5 constructor
$this->__construct( $parent_obj );
}
// --<
return $this;
}
/**
* @description: set up all items associated with this object
* @todo:
*
*/
function initialise() {
}
/**
* @description: if needed, destroys all items associated with this object
* @todo:
*
*/
function destroy() {
}
//#################################################################
/*
===============================================================
PUBLIC METHODS
===============================================================
*/
/**
* @description: add an admin page for this plugin
* @todo:
*
*/
function add_admin_menu() {
// we must be network admin
if ( !is_super_admin() ) { return false; }
// try and update options
$saved = $this->db->options_update();
// always add the admin page to the Settings menu
$page = add_submenu_page(
'settings.php',
__( 'Commentpress Network', 'cp-multisite' ),
__( 'Commentpress Network', 'cp-multisite' ),
'manage_options',
'cpmu_admin_page',
array( &$this, 'admin_page' )
);
// add styles only on our admin page, see:
// http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
add_action( 'admin_print_styles-'.$page, array( &$this, 'add_admin_styles' ) );
}
/**
* @description: enqueue any styles and scripts needed by our admin page
* @todo:
*
*/
function add_admin_styles() {
/*
// EXAMPLES:
// add css
wp_enqueue_style('cpmu-admin-style', CPMU_PLUGIN_URL . 'css/admin.css');
// add javascripts
wp_enqueue_script( 'cpmu-admin-js', CPMU_PLUGIN_URL . 'js/admin.js' );
*/
}
/**
* @description: show our admin page
* @todo:
*
*/
function admin_page() {
// only allow network admins through
if( is_super_admin() == false ) {
// disallow
wp_die( __( 'You do not have permission to access this page.', 'cp-multisite' ) );
}
// sanitise admin page url
$url = $_SERVER['REQUEST_URI'];
$url_array = explode( '&', $url );
if ( is_array( $url_array ) ) { $url = $url_array[0]; }
// show a message for now
$msg = '<p>Holding page. No options are set here as yet.</p>';
// define admin page - needs translation cap
$admin_page = '
<div class="icon32" id="icon-options-general"><br/></div>
<h2>Commentpress for Multisite</h2>
<form method="post" action="'.htmlentities($url.'&updated=true').'">
'.wp_nonce_field( 'cpmu_admin_action', 'cpmu_nonce', true, false ).'
'.wp_referer_field( false ).'
<p style="padding-top: 30px;">Checking environment...</p>
'.$msg.'
<p class="submit">
<input type="submit" name="cpmu_submit" value="Save Changes" class="button-primary" />
</p>
</form>'."\n\n\n\n";
// done
echo $admin_page;
}
/**
* @description: enqueue any styles and scripts needed by our public pages
* @todo:
*
*/
function add_frontend_styles() {
/*
// EXAMPLES:
// add javascripts
wp_enqueue_script(
'cpmu-admin-js',
CPMU_PLUGIN_URL . 'js/admin.js'
);
*/
// add css for signup form
wp_enqueue_style(
'cpmu-signup-style',
CPMU_PLUGIN_URL . 'css/signup.css'
);
}
/**
* @description: hook into the group blog signup form
* @todo:
*
*/
function signup_blogform( $errors ) {
// only apply to wordpress signup form (not the BuddyPress one)
// TODO: make sure this method never fires when BP is active
if ( !isset( $GLOBALS['bp'] ) ) {
// define title
$title = __( 'CommentPress:', 'cp-multisite' );
// define text
$text = __( 'Do you want to make the new site a Commentpress document?', 'cp-multisite' );
// define enable label
$enable_label = __( 'Enable CommentPress', 'cp-multisite' );
// off by default
$has_workflow = false;
// init output
$workflow_html = '';
// allow overrides
$has_workflow = apply_filters( 'cp_blog_workflow_exists', $has_workflow );
// if we have workflow enabled, by a plugin, say...
if ( $has_workflow !== false ) {
// define workflow label
$workflow_label = __( 'Enable Custom Workflow', 'cp-multisite' );
// allow overrides
$workflow_label = apply_filters( 'cp_blog_workflow_label', $workflow_label );
// show it
$workflow_html = '
<div class="checkbox">
<label for="cp_blog_workflow"><input type="checkbox" value="1" id="cp_blog_workflow" name="cp_blog_workflow" /> '.$workflow_label.'</label>
</div>
';
}
// assume no types
$types = array();
// init output
$type_html = '';
// but allow overrides for plugins to supply some
$types = apply_filters( 'cp_blog_type_options', $types );
// if we got any, use them
if ( !empty( $types ) ) {
// define blog type label
$type_label = __( 'Document Type', 'cp-multisite' );
// allow overrides
$type_label = apply_filters( 'cp_blog_type_label', $type_label );
// construct options
$type_option_list = array();
$n = 0;
foreach( $types AS $type ) {
$type_option_list[] = '<option value="'.$n.'">'.$type.'</option>';
$n++;
}
$type_options = implode( "\n", $type_option_list );
// show it
$type_html = '
<div class="dropdown">
<label for="cp_blog_type">'.$type_label.'</label> <select id="cp_blog_type" name="cp_blog_type">
'.$type_options.'
</select>
</div>
';
}
// construct form
$form = '
<br />
<div id="cp-multisite-options">
<h3>'.$title.'</h3>
<p>'.$text.'</p>
<div class="checkbox">
<label for="cpmu-new-blog"><input type="checkbox" value="1" id="cpmu-new-blog" name="cpmu-new-blog" /> '.$enable_label.'</label>
</div>
'.$workflow_html.'
'.$type_html.'
</div>
';
echo $form;
}
}
/**
* @description: hook into wpmu_new_blog and target plugins to be activated
* @todo:
*
*/
function wpmu_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
// test for presence of our checkbox variable in _POST
if ( isset( $_POST['cpmu-new-blog'] ) AND $_POST['cpmu-new-blog'] == '1' ) {
// hand off to private method
$this->_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta );
}
}
//#################################################################
/*
===============================================================
PRIVATE METHODS
===============================================================
*/
/**
* @description: object initialisation
* @todo:
*
*/
function _init() {
// register hooks
$this->_register_hooks();
}
/**
* @description: register Wordpress hooks
* @todo:
*
*/
function _register_hooks() {
// add form elements to signup form
add_action( 'signup_blogform', array( &$this, 'signup_blogform' ) );
// activate blog-specific CommentPress plugin
add_action('wpmu_new_blog', array( &$this, 'wpmu_new_blog' ), 12, 6); // includes/ms-functions.php
// is this the back end?
if ( is_admin() ) {
// add menu to Network submenu
add_action( 'network_admin_menu', array( &$this, 'add_admin_menu' ), 30 );
} else {
// register any public styles
add_action('wp_enqueue_scripts', array( &$this, 'add_frontend_styles' ), 20);
}
}
/**
* @description: create a blog
* @todo:
*
*/
function _create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
// wpmu_new_blog calls this *after* restore_current_blog, so we need to do it again
switch_to_blog( $blog_id );
// ----------------------
// Activate CommentPress
// ----------------------
// get Commentpress plugin
$path_to_plugin = cpmu_find_plugin_by_name( 'Commentpress' );
// if we got Commentpress...
if ( false !== $path_to_plugin ) {
// activate it in its buffered sandbox
cpmu_activate_plugin( $path_to_plugin, true );
// do post install
$this->_do_blog_post_install();
}
// get CP Ajaxified
$path_to_plugin = cpmu_find_plugin_by_name( 'Commentpress Ajaxified' );
// if we got it...
if ( false !== $path_to_plugin ) {
// activate it in its buffered sandbox
cpmu_activate_plugin( $path_to_plugin, true );
}
// switch back
restore_current_blog();
}
/**
* @description: Commentpress initialisation
* @todo:
*
*/
function _do_blog_post_install() {
global $commentpress_obj, $wpdb;
// post-activation configuration
if ( is_null( $commentpress_obj ) ) {
// create it
$commentpress_obj = new CommentPress;
}
/*
------------------------------------------------------------------------
Configure Commentpress based on admin page settings
------------------------------------------------------------------------
*/
// TODO: create admin page settings
// TOC = posts
//$commentpress_obj->db->option_set( 'cp_show_posts_or_pages_in_toc', 'post' );
// TOC show extended posts
//$commentpress_obj->db->option_set( 'cp_show_extended_toc', 1 );
/*
------------------------------------------------------------------------
Further Commentpress plugins may define Blog Workflows and Type and
enable them to be set in the blog signup form.
------------------------------------------------------------------------
*/
// check for (translation) workflow (checkbox)
$cp_blog_workflow = 0;
if ( isset( $_POST['cp_blog_workflow'] ) ) {
// ensure boolean
$cp_blog_workflow = ( $_POST['cp_blog_workflow'] == '1' ) ? 1 : 0;
}
// set workflow
$commentpress_obj->db->option_set( 'cp_blog_workflow', $cp_blog_workflow );
// check for blog type (dropdown)
$cp_blog_type = 0;
if ( isset( $_POST['cp_blog_type'] ) ) {
$cp_blog_type = intval( $_POST['cp_blog_type'] );
}
// set blog type
$commentpress_obj->db->option_set( 'cp_blog_type', $cp_blog_type );
// save
$commentpress_obj->db->options_save();
/*
------------------------------------------------------------------------
WordPress Internal Configuration
------------------------------------------------------------------------
*/
/*
// allow anonymous commenting (may be overridden)
$anon_comments = 0;
// allow plugin overrides
$anon_comments = apply_filters( 'cp_require_comment_registration', $anon_comments );
// update wp option
update_option( 'comment_registration', $anon_comments );
// add Lorem Ipsum to "Sample Page" if the Network setting is empty?
$first_page = get_site_option( 'first_page' );
// is it empty?
if ( $first_page == '' ) {
// get it & update content, or perhaps delete?
}
*/
// reset all widgets
update_option( 'sidebars_widgets', null );
}
//#################################################################
} // class ends
?>