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 3
/
class_commentpress_db.php
3902 lines (2291 loc) · 79 KB
/
class_commentpress_db.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 /*
===============================================================
Class CommentPressDatabase Version 1.0
===============================================================
AUTHOR : Christian Wach <[email protected]>
---------------------------------------------------------------
NOTES
=====
This class is a wrapper for the majority of database operations.
---------------------------------------------------------------
*/
/*
===============================================================
Class Name
===============================================================
*/
class CommentPressDatabase {
/*
===============================================================
Properties
===============================================================
*/
// parent object reference
var $parent_obj;
// options
var $cp_options = array();
// paragraph-level comments
var $para_comments_enabled = 1;
// TOC content ('post' or 'page')
var $toc_content = 'page';
// TOC chapters are pages by default
var $toc_chapter_is_page = 1;
// Show extended TOC content for posts lists
var $show_extended_toc = 1;
// TOC shows subpages by default
var $show_subpages = 1;
// show page titles by default
var $title_visibility = 'show';
// hide page meta by default
var $page_meta_visibility = 'hide';
// default editor (tinyMCE)
var $comment_editor = 1;
// promote reading (1) or commenting (0)
var $promote_reading = 0;
// allow sidebar to be minimised
var $minimise_sidebar = 1;
// default excerpt length
var $excerpt_length = 55;
// default header background colour (hex, same as in layout.css)
var $header_bg_colour = '819565';
// default scroll speed (ms)
var $js_scroll_speed = '800';
// default type of blog - eg, array('0' => 'Poetry','1' => 'Prose')
var $blog_type = false;
// default blog workflow (like translation, for example), off by default
var $blog_workflow = 0;
// default sidebar
var $sidebar_default = 'comments';
// default minimum page width (px)
var $min_page_width = '447';
// prevent save_post hook firing more than once
var $saved_post = false;
/**
* @description: initialises this object
* @param object $parent_obj a reference to the parent object
* @return object
* @todo:
*
*/
function __construct( $parent_obj ) {
// store reference to parent
$this->parent_obj = $parent_obj;
// init
$this->_init();
// --<
return $this;
}
/**
* @description: PHP 4 constructor
*/
function CommentPressDatabase( $parent_obj ) {
// 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
* @param integer $blog_id the ID of the blog - default null
* @todo:
*
*/
function activate( $blog_id = null ) {
// update db schema
$this->schema_update();
// test if we have our version
if ( !$this->option_wp_get( 'cp_version' ) ) {
// store Commentpress version
$this->option_wp_set( 'cp_version', CP_VERSION );
}
// test that we aren't reactivating
if ( !$this->option_wp_get( 'cp_options' ) ) {
// add options with default values
$this->options_create();
// enable comment threading (should this be an option?)
//$this->_store_wordpress_option( 'thread_comments', '1' );
// finally, turn comment paging option off
$this->_cancel_comment_paging();
}
// always create special pages
$this->create_special_pages();
// override widgets
$this->_clear_widgets();
}
/**
* @description: upgrade Commentpress plugin from 3.1 options to latest set
* @return boolean $result
* @todo:
*
*/
function upgrade() {
// database object
global $wpdb;
// init return
$result = false;
// if we have a commentpress install (or we're forcing)
if ( $this->check_upgrade() ) {
// are we missing the cp_options option?
if ( !$this->option_wp_exists( 'cp_options' ) ) {
// upgrade to the single array
$this->options_upgrade();
}
// checkboxes send no value if not checked, so use a default
$cp_blog_workflow = $this->blog_workflow;
// default blog type
$cp_blog_type = $this->blog_type;
// get variables
extract( $_POST );
// New in CP 3.3.3 - changed the way the welcome page works
if ( $this->option_exists( 'cp_special_pages' ) ) {
// do we have the cp_welcome_page option?
if ( $this->option_exists( 'cp_welcome_page' ) ) {
// get it
$page_id = $this->option_get( 'cp_welcome_page' );
// retrieve data on special pages
$special_pages = $this->option_get( 'cp_special_pages' );
// is it in our special pages array?
if ( in_array( $page_id, $special_pages ) ) {
// remove page id from array
$special_pages = array_diff( $special_pages, array( $page_id ) );
// reset option
$this->option_set( 'cp_special_pages', $special_pages );
}
}
}
// New in CP 3.3.3 - are we missing the cp_sidebar_default option?
if ( !$this->option_exists( 'cp_sidebar_default' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_sidebar_default );
// add chosen cp_page_meta_visibility option
$this->option_set( 'cp_sidebar_default', $_choice );
}
// New in CP 3.3.2 - are we missing the cp_page_meta_visibility option?
if ( !$this->option_exists( 'cp_page_meta_visibility' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_page_meta_visibility );
// add chosen cp_page_meta_visibility option
$this->option_set( 'cp_page_meta_visibility', $_choice );
}
// New in CP 3.3.1 - are we missing the cp_blog_workflow option?
if ( !$this->option_exists( 'cp_blog_workflow' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_blog_workflow );
// add chosen cp_blog_workflow option
$this->option_set( 'cp_blog_workflow', $_choice );
}
// New in CP 3.3.1 - are we missing the cp_blog_type option?
if ( !$this->option_exists( 'cp_blog_type' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_blog_type );
// add chosen cp_blog_type option
$this->option_set( 'cp_blog_type', $_choice );
}
// New in CP 3.3 - are we missing the cp_show_extended_toc option?
if ( !$this->option_exists( 'cp_show_extended_toc' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_show_extended_toc );
// add chosen cp_show_extended_toc option
$this->option_set( 'cp_show_extended_toc', $_choice );
}
// are we missing the cp_comment_editor option?
if ( !$this->option_exists( 'cp_comment_editor' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_comment_editor );
// add chosen cp_comment_editor option
$this->option_set( 'cp_comment_editor', $_choice );
}
// are we missing the cp_promote_reading option?
if ( !$this->option_exists( 'cp_promote_reading' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_promote_reading );
// add chosen cp_promote_reading option
$this->option_set( 'cp_promote_reading', $_choice );
}
// are we missing the cp_title_visibility option?
if ( !$this->option_exists( 'cp_title_visibility' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_title_visibility );
// add chosen cp_title_visibility option
$this->option_set( 'cp_title_visibility', $_choice );
}
// are we missing the cp_header_bg_colour option?
if ( !$this->option_exists( 'cp_header_bg_colour' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_header_bg_colour );
// strip our rgb #
if ( stristr( $_choice, '#' ) ) {
$_choice = substr( $_choice, 1 );
}
// reset to default if blank
if ( $_choice == '' ) {
$_choice = $this->header_bg_colour;
}
// add chosen cp_header_bg_colour option
$this->option_set( 'cp_header_bg_colour', $_choice );
}
// are we missing the cp_js_scroll_speed option?
if ( !$this->option_exists( 'cp_js_scroll_speed' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_js_scroll_speed );
// add chosen cp_js_scroll_speed option
$this->option_set( 'cp_js_scroll_speed', $_choice );
}
// are we missing the cp_min_page_width option?
if ( !$this->option_exists( 'cp_min_page_width' ) ) {
// get choice
$_choice = $wpdb->escape( $cp_min_page_width );
// add chosen cp_min_page_width option
$this->option_set( 'cp_min_page_width', $_choice );
}
// do we still have the legacy cp_allow_users_to_minimize option?
if ( $this->option_exists( 'cp_allow_users_to_minimize' ) ) {
// delete old cp_allow_users_to_minimize option
$this->option_delete( 'cp_allow_users_to_minimize' );
}
// do we have special pages?
if ( $this->option_exists( 'cp_special_pages' ) ) {
// if we don't have the toc page...
if ( !$this->option_exists( 'cp_toc_page' ) ) {
// get special pages array
$special_pages = $this->option_get( 'cp_special_pages' );
// create TOC page -> a convenience, let's us define a logo as attachment
$special_pages[] = $this->_create_toc_page();
// store the array of page IDs that were created
$this->option_set( 'cp_special_pages', $special_pages );
}
}
// save new Commentpress options
$this->options_save();
// store new Commentpress version
$this->option_wp_set( 'cp_version', CP_VERSION );
}
// --<
return $result;
}
/**
* @description: reset Wordpress to prior state, but retain options
* @todo:
*
*/
function deactivate() {
// reset comment threading
//$this->_reset_wordpress_option( 'thread_comments' );
// reset comment paging option
$this->_reset_comment_paging();
// restore widgets
$this->_reset_widgets();
// always remove special pages
$this->delete_special_pages();
}
/**
* @description: uninstalls pages, options and database modifications
* @todo:
*
*/
function uninstall() {
// remove special pages
$this->delete_special_pages();
// delete options
$this->options_delete();
// restore database schema
// NOTE: we will lose all our submitted comment text signatures
$this->schema_restore();
}
//#################################################################
/*
===============================================================
PUBLIC METHODS
===============================================================
*/
/**
* @description: update Wordpress database schema
* @return boolean $result
* @todo:
*
*/
function schema_update() {
// database object
global $wpdb;
// include Wordpress upgrade script
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// add the column, if not already there
$result = maybe_add_column(
$wpdb->comments,
'comment_text_signature',
"ALTER TABLE `$wpdb->comments` ADD `comment_text_signature` VARCHAR(255) NULL;"
);
// --<
return $result;
}
/**
* @description: restore Wordpress database schema
* @return boolean $result
* @todo:
*
*/
function schema_restore() {
// database object
global $wpdb;
// include Wordpress install helper script
require_once( ABSPATH . 'wp-admin/install-helper.php' );
// drop the column, if already there
$result = maybe_drop_column(
$wpdb->comments,
'comment_text_signature',
"ALTER TABLE `$wpdb->comments` DROP `comment_text_signature`;"
);
// --<
return $result;
}
/**
* @description: do we have the comment_text_signature field?
* @return boolean $result
* @todo:
*
*/
function db_is_modified() {
// database object
global $wpdb;
// init
$result = false;
// define query
$query = "DESCRIBE $wpdb->comments";
// get columns
$cols = $wpdb->get_results( $query );
// loop
foreach( $cols AS $col ) {
// is it comment_text_signature?
if ( $col->Field == 'comment_text_signature' ) {
// we got it
$result = true;
break;
}
}
// --<
return $result;
}
/**
* @description: check for plugin upgrade
* @return boolean $result
* @todo:
*
*/
function check_upgrade() {
// init
$result = false;
// get installed version cast as string
$_version = (string) $this->option_wp_get( 'cp_version' );
// if we have a commentpress install and it's lower than this one
if ( $_version !== false AND version_compare( CP_VERSION, $_version, '>' ) ) {
// override
$result = true;
}
// --<
return $result;
}
/**
* @description: create all basic Commentpress options
* @todo: store plugin options in a single array
*
*/
function options_create() {
// init options array --> TO DO
$this->cp_options = array(
'cp_para_comments_enabled' => $this->para_comments_enabled,
'cp_show_posts_or_pages_in_toc' => $this->toc_content,
'cp_toc_chapter_is_page' => $this->toc_chapter_is_page,
'cp_show_subpages' => $this->show_subpages,
'cp_show_extended_toc' => $this->show_extended_toc,
'cp_title_visibility' => $this->title_visibility,
'cp_page_meta_visibility' => $this->page_meta_visibility,
'cp_header_bg_colour' => $this->header_bg_colour,
'cp_js_scroll_speed' => $this->js_scroll_speed,
'cp_min_page_width' => $this->min_page_width,
'cp_comment_editor' => $this->comment_editor,
'cp_promote_reading' => $this->promote_reading,
'cp_minimise_sidebar' => $this->minimise_sidebar,
'cp_excerpt_length' => $this->excerpt_length,
'cp_blog_type' => $this->blog_type,
'cp_blog_workflow' => $this->blog_workflow,
'cp_sidebar_default' => $this->sidebar_default
);
// Paragraph-level comments enabled by default
add_option( 'cp_options', $this->cp_options );
}
/**
* @description: delete all Commentpress options
* @todo:
*
*/
function options_delete() {
// delete Commentpress version
delete_option( 'cp_version' );
// delete Commentpress options
delete_option( 'cp_options' );
}
/**
* @description: save the settings set by the administrator
* @return boolean success or failure
* @todo: do more error checking?
*
*/
function options_update() {
// database object
global $wpdb;
// init result
$result = false;
// was the form submitted?
if( isset( $_POST['cp_submit'] ) ) {
// check that we trust the source of the data
check_admin_referer( 'cp_admin_action', 'cp_nonce' );
// init vars
$cp_install = '';
$cp_uninstall = '';
$cp_upgrade = '';
$cp_reset = '';
$cp_create_pages = '';
$cp_delete_pages = '';
$cp_para_comments_live = '';
$cp_show_subpages = 0;
$cp_minimise_sidebar = 0;
$cp_para_comments_enabled = 0;
$cp_show_extended_toc = 0;
$cp_blog_type = 0;
$cp_blog_workflow = 0;
$cp_sidebar_default = 'comments';
// get variables
extract( $_POST );
// did we ask to install Commentpress?
if ( $cp_install == '1' ) {
// add database modifications
$this->schema_update();
// --<
return true;
}
// did we ask to uninstall Commentpress?
if ( $cp_uninstall == '1' ) {
// remove database modifications
$this->uninstall();
// --<
return true;
}
// did we ask to upgrade Commentpress?
if ( $cp_upgrade == '1' ) {
// do upgrade
$this->upgrade();
// --<
return true;
}
// did we ask to reset?
if ( $cp_reset == '1' ) {
// reset theme options
$this->options_reset_theme();
// --<
return true;
}
// did we ask to auto-create special pages?
if ( $cp_create_pages == '1' ) {
// remove any existing special pages
$this->delete_special_pages();
// create special pages
$this->create_special_pages();
}
// did we ask to delete special pages?
if ( $cp_delete_pages == '1' ) {
// remove special pages
$this->delete_special_pages();
}
// Commentpress Theme params
// individual special pages
//$cp_welcome_page = $wpdb->escape( $cp_welcome_page );
//$cp_blog_page = $wpdb->escape( $cp_blog_page );
//$cp_general_comments_page = $wpdb->escape( $cp_general_comments_page );
//$cp_all_comments_page = $wpdb->escape( $cp_all_comments_page );
//$cp_comments_by_page = $wpdb->escape( $cp_comments_by_page );
//$this->option_set( 'cp_welcome_page', $cp_welcome_page );
//$this->option_set( 'cp_blog_page', $cp_blog_page );
//$this->option_set( 'cp_general_comments_page', $cp_general_comments_page );
//$this->option_set( 'cp_all_comments_page', $cp_all_comments_page );
//$this->option_set( 'cp_comments_by_page', $cp_comments_by_page );
// comments enabled
$cp_para_comments_enabled = $wpdb->escape( $cp_para_comments_enabled );
$this->option_set( 'cp_para_comments_enabled', ( $cp_para_comments_enabled ? 1 : 0 ) );
// TOC content
$cp_show_posts_or_pages_in_toc = $wpdb->escape( $cp_show_posts_or_pages_in_toc );
$this->option_set( 'cp_show_posts_or_pages_in_toc', $cp_show_posts_or_pages_in_toc );
// if we have pages in TOC and a value for the next param...
if ( $cp_show_posts_or_pages_in_toc == 'page' AND isset( $cp_toc_chapter_is_page ) ) {
$cp_toc_chapter_is_page = $wpdb->escape( $cp_toc_chapter_is_page );
$this->option_set( 'cp_toc_chapter_is_page', $cp_toc_chapter_is_page );
// if chapters are not pages and we have a value for the next param...
if ( $cp_toc_chapter_is_page == '0' ) {
$cp_show_subpages = $wpdb->escape( $cp_show_subpages );
$this->option_set( 'cp_show_subpages', ( $cp_show_subpages ? 1 : 0 ) );
} else {
// always set to show subpages
$this->option_set( 'cp_show_subpages', 1 );
}
}
// extended or vanilla posts TOC
if ( $cp_show_posts_or_pages_in_toc == 'post' ) {
$cp_show_extended_toc = $wpdb->escape( $cp_show_extended_toc );
$this->option_set( 'cp_show_extended_toc', ( $cp_show_extended_toc ? 1 : 0 ) );
}
// excerpt length
$this->option_set( 'cp_excerpt_length', $cp_excerpt_length );
$cp_excerpt_length = $wpdb->escape( $cp_excerpt_length );
// comment editor
$cp_comment_editor = $wpdb->escape( $cp_comment_editor );
$this->option_set( 'cp_comment_editor', ( $cp_comment_editor ? 1 : 0 ) );
// is the AJAX-commenting plugin enabled?
if ( function_exists( 'cpac_enable_plugin' ) ) {
// live commenting
$cp_para_comments_live = $wpdb->escape( $cp_para_comments_live );
update_option( 'cp_para_comments_live', ( $cp_para_comments_live ? 1 : 0 ) );
}
// behaviour
$cp_promote_reading = $wpdb->escape( $cp_promote_reading );
$this->option_set( 'cp_promote_reading', ( $cp_promote_reading ? 1 : 0 ) );
// title visibility
$cp_title_visibility = $wpdb->escape( $cp_title_visibility );
$this->option_set( 'cp_title_visibility', $cp_title_visibility );
// page meta visibility
$cp_page_meta_visibility = $wpdb->escape( $cp_page_meta_visibility );
$this->option_set( 'cp_page_meta_visibility', $cp_page_meta_visibility );
// header background colour
// strip our rgb #
if ( stristr( $cp_header_bg_colour, '#' ) ) {
$cp_header_bg_colour = substr( $cp_header_bg_colour, 1 );
}
// reset to default if blank
if ( $cp_header_bg_colour == '' ) {
$cp_header_bg_colour = $this->header_bg_colour;
}
// save it
$cp_header_bg_colour = $wpdb->escape( $cp_header_bg_colour );
$this->option_set( 'cp_header_bg_colour', $cp_header_bg_colour );
// save scroll speed
$cp_js_scroll_speed = $wpdb->escape( $cp_js_scroll_speed );
$this->option_set( 'cp_js_scroll_speed', $cp_js_scroll_speed );
// save min page width
$cp_min_page_width = $wpdb->escape( $cp_min_page_width );
$this->option_set( 'cp_min_page_width', $cp_min_page_width );
// minimise sidebar
$cp_minimise_sidebar = $wpdb->escape( $cp_minimise_sidebar );
$this->option_set( 'cp_minimise_sidebar', ( $cp_minimise_sidebar ? 1 : 0 ) );
// save workflow
$cp_blog_workflow = $wpdb->escape( $cp_blog_workflow );