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_nav.php
1413 lines (789 loc) · 23.2 KB
/
class_commentpress_nav.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 CommentPressNavigator Version 1.0
===============================================================
AUTHOR : Christian Wach <[email protected]>
---------------------------------------------------------------
NOTES
=====
This class is a wrapper for navigating pages in whatever hierarchy
or relationship they have been assigned
---------------------------------------------------------------
*/
/*
===============================================================
Class Name
===============================================================
*/
class CommentPressNavigator {
/*
===============================================================
Properties
===============================================================
*/
// parent object reference
var $parent_obj;
// next pages array
var $next_pages = array();
// previous pages array
var $previous_pages = array();
// next posts array
var $next_posts = array();
// previous posts array
var $previous_posts = array();
// page numbers array
var $page_numbers = array();
/**
* @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 CommentPressNavigator( $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
* @todo:
*
*/
function initialise() {
// init page lists
$this->_init_page_lists();
// init posts lists
$this->_init_posts_lists();
}
/**
* @description: if needed, destroys all items associated with this object
* @todo:
*
*/
function destroy() {
}
//#################################################################
/*
===============================================================
PUBLIC METHODS
===============================================================
*/
/**
* @description: get next page link
* @param boolean $with_comments requested page has comments - default false
* @return object $page_data if successful, boolean false if not
* @todo:
*
*/
function get_next_page( $with_comments = false ) {
// do we have any next pages?
if ( count( $this->next_pages ) > 0 ) {
// are we asking for comments?
if ( $with_comments ) {
// loop
foreach( $this->next_pages AS $next_page ) {
// does it have comments?
if ( $next_page->comment_count > 0 ) {
// --<
return $next_page;
}
}
} else {
// --<
return $this->next_pages[0];
}
}
// --<
return false;
}
/**
* @description: get previous page link
* @param boolean $with_comments requested page has comments - default false
* @return object $page_data if successful, boolean false if not
* @todo:
*
*/
function get_previous_page( $with_comments = false ) {
// do we have any previous pages?
if ( count( $this->previous_pages ) > 0 ) {
// are we asking for comments?
if ( $with_comments ) {
// loop
foreach( $this->previous_pages AS $previous_page ) {
// does it have comments?
if ( $previous_page->comment_count > 0 ) {
// --<
return $previous_page;
}
}
} else {
// --<
return $this->previous_pages[0];
}
}
// --<
return false;
}
/**
* @description: get next post link
* @param boolean $with_comments requested post has comments - default false
* @return object $post_data if successful, boolean false if not
* @todo:
*
*/
function get_next_post( $with_comments = false ) {
// do we have any next posts?
if ( count( $this->next_posts ) > 0 ) {
// are we asking for comments?
if ( $with_comments ) {
// loop
foreach( $this->next_posts AS $next_post ) {
// does it have comments?
if ( $next_post->comment_count > 0 ) {
// --<
return $next_post;
}
}
} else {
// --<
return $this->next_posts[0];
}
}
// --<
return false;
}
/**
* @description: get previous post link
* @param boolean $with_comments requested post has comments - default false
* @return object $post_data if successful, boolean false if not
* @todo:
*
*/
function get_previous_post( $with_comments = false ) {
// do we have any previous posts?
if ( count( $this->previous_posts ) > 0 ) {
// are we asking for comments?
if ( $with_comments ) {
// loop
foreach( $this->previous_posts AS $previous_post ) {
// does it have comments?
if ( $previous_post->comment_count > 0 ) {
// --<
return $previous_post;
}
}
} else {
// --<
return $this->previous_posts[0];
}
}
// --<
return false;
}
/**
* @description: get first viewable child page
* @param integer $page_id the page ID
* @return integer $first_child ID of the first child page (or false if not found)
* @todo:
*
*/
function get_first_child( $page_id ) {
// init to look for published pages
$defaults = array(
'post_parent' => $page_id,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC'
);
// get page children
$kids =& get_children( $defaults );
// do we have any?
if ( empty( $kids ) ) {
// no children
return false;
}
// we got some...
return $this->_get_first_child( $kids );
}
/**
* @description: get list of 'book' pages
* @param string $mode either 'structural' or 'readable'
* @return array $pages all 'book' pages
* @todo:
*
*/
function get_book_pages( $mode = 'readable' ) {
// init
$all_pages = array();
// do we have a nav menu enabled?
if ( has_nav_menu( 'toc' ) ) {
// YES - a custom menu disables "book" navigation
// --<
return $all_pages;
/*
// ------------------------------------------------------------------
// For posterity: here's what I considered when trying to tease out
// page lists from menu items. However, menus can contain anything,
// including external links, so the effort to parse the menu doesn't
// seem to be worthwhile.
// ------------------------------------------------------------------
// check menu locations
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
// get the menu object
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
// default args for reference
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false
);
// get the page references
$menu_items = wp_get_nav_menu_items( $menu->term_id, $args );
// init
$pages_to_get = array();
// if we get some
if ( $menu_items ) {
// convert to array of pages
foreach ( $menu_items AS $menu_item ) {
// is it a WP item?
if ( isset( $menu_item->object_id ) ) {
// construct array of WP pages in menu
$pages_to_get[] = $menu_item->object_id;
}
}
print_r( array( 'menu_items' => $menu_items, 'pages_to_get' => $pages_to_get ) ); die();
// set list pages defaults
$defaults = array(
'child_of' => 0,
'sort_order' => 'ASC',
'sort_column' => 'menu_order, post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => implode( ',', $pages_to_get ),
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'parent' => -1,
'exclude_tree' => ''
);
// get them
$all_pages = get_pages( $defaults );
}
}
*/
} else {
// -----------------------------------------------
// construct "book" navigation based on pages
// -----------------------------------------------
// default to no excludes
$excludes = '';
// get special pages
$special_pages = $this->parent_obj->db->option_get('cp_special_pages');
// are we in a BuddyPress scenario?
if ( $this->parent_obj->is_buddypress() ) {
// BuddyPress creates its own registration page at /register and
// redirects ordinary WP registration page requests to it. It also
// seems to exclude it from wp_list_pages(), see: $cp->display->list_pages()
// check if registration is allowed
if ( '1' == get_option('users_can_register') AND is_main_site() ) {
// find the registration page by its slug
$reg_page = get_page_by_path( 'register' );
// did we get one?
if ( is_object( $reg_page ) AND isset( $reg_page->ID ) ) {
// yes - exclude it as well
$special_pages[] = $reg_page->ID;
}
}
}
// are there any?
if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
// format them for the exclude param
$excludes = implode( ',', $special_pages );
}
// set list pages defaults
$defaults = array(
'child_of' => 0,
'sort_order' => 'ASC',
'sort_column' => 'menu_order, post_title',
'hierarchical' => 1,
'exclude' => $excludes,
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'parent' => -1,
'exclude_tree' => ''
);
// get them
$all_pages = get_pages( $defaults );
// if we have any pages...
if ( count( $all_pages ) > 0 ) {
// if chapters are not pages...
if ( $this->parent_obj->db->option_get( 'cp_toc_chapter_is_page' ) != '1' ) {
// do we want all readable pages?
if ( $mode == 'readable' ) {
// filter chapters out
$all_pages = $this->_filter_chapters( $all_pages );
}
}
// if Theme My Login is present...
if ( defined( 'TML_ABSPATH' ) ) {
// filter its page out
$all_pages = $this->_filter_theme_my_login_page( $all_pages );
}
}
} // end check for custom menu
//print_r( $all_pages ); die();
// --<
return $all_pages;
}
/**
* @description: get first readable 'book' page
* @return integer $id ID of the first page (or false if not found)
* @todo:
*
*/
function get_first_page() {
// init
$id = false;
// get all pages including chapters
$all_pages = $this->get_book_pages( 'structural' );
// if we have any pages...
if ( count( $all_pages ) > 0 ) {
// get first id
$id = $all_pages[0]->ID;
}
// --<
return $id;
}
/**
* @description: get page number
* @param integer $page_id the page ID
* @return integer $number number of the page
* @todo:
*
*/
function get_page_number( $page_id ) {
// init
$num = 0;
// access post
global $post;
// are parent pages viewable?
$viewable = ( $this->parent_obj->db->option_get( 'cp_toc_chapter_is_page' ) == '1' ) ? true : false;
// if they are...
if ( $viewable ) {
// get page number from array
$num = $this->_get_page_number( $page_id );
} else {
// get id of first viewable child
$first_child = $this->get_first_child( $post->ID );
// if this is a childless page
if ( !$first_child ) {
// get page number from array
$num = $this->_get_page_number( $page_id );
}
}
// --<
return $num;
}
/**
* @description: get page number
* @param integer $page_id the page ID
* @return integer $number number of the page
* @todo:
*
*/
function _get_page_number( $page_id ) {
// init
$num = 0;
// get from array
if ( array_key_exists( $page_id, $this->page_numbers ) ) {
// get it
$num = $this->page_numbers[ $page_id ];
}
// --<
return $num;
}
/**
* @description: redirect to child
* @todo:
*
*/
function redirect_to_child() {
// only on pages
if ( !is_page() ) { return; }
// access post object
global $post;
// do we have one?
if ( !is_object( $post ) ) {
// --<
die( 'no post object' );
}
// are parent pages viewable?
$viewable = ( $this->parent_obj->db->option_get( 'cp_toc_chapter_is_page' ) == '1' ) ? true : false;
// get id of first child
$first_child = $this->get_first_child( $post->ID );
// our conditions
if ( $first_child AND !$viewable ) {
// get link
$redirect = get_permalink( $first_child );
// do the redirect
//header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: $redirect" );
}
}
//#################################################################
/*
===============================================================
PRIVATE METHODS
===============================================================
*/
/**
* @description: object initialisation
* @todo:
*
*/
function _init() {
// is_page() and is_single() are not yet defined, so we init this object when
// wp_head() is fired - see initialise() above
}
/**
* @description: set up page list
* @todo:
*
*/
function _init_page_lists() {
// if we're navigating pages
if( is_page() ) {
// get all pages
$all_pages = $this->get_book_pages( 'readable' );
// if we have any pages...
if ( count( $all_pages ) > 0 ) {
// generate page numbers
$this->_generate_page_numbers( $all_pages );
// access post object
global $post;
// init the key we want
$page_key = false;
// loop
foreach( $all_pages AS $key => $page_obj ) {
// is it the currently viewed page?
if ( $page_obj->ID == $post->ID ) {
// set page key
$page_key = $key;
// kick out to preserve key
break;
}
}
// if we don't get a key...
if ( $page_key === false ) {
// the current page is a chapter and is not a page
$this->next_pages = array();
// --<
return;
}
// will there be a next array?
if ( isset( $all_pages[$key + 1] ) ) {
// get all subsequent pages
$this->next_pages = array_slice( $all_pages, $key + 1 );
}
// will there be a previous array?
if ( isset( $all_pages[$key - 1] ) ) {
// get all previous pages
$this->previous_pages = array_reverse( array_slice( $all_pages, 0, $key ) );
}
} // end have array check
} // end is_page() check
}
/**
* @description: set up posts list
* @todo:
*
*/
function _init_posts_lists() {
// if we're navigating posts
if( is_single() ) {
// set defaults
$defaults = array(
'numberposts' => -1,
'orderby' => 'date'
);
// get them
$all_posts = get_posts( $defaults );
// if we have any posts...
if ( count( $all_posts ) > 0 ) {
// access post object
global $post;
// loop
foreach( $all_posts AS $key => $post_obj ) {
// is it ours?
if ( $post_obj->ID == $post->ID ) {
// kick out to preserve key
break;
}
}
// will there be a next array?
if ( isset( $all_posts[$key + 1] ) ) {
// get all subsequent posts
$this->next_posts = array_slice( $all_posts, $key + 1 );
}
// will there be a previous array?
if ( isset( $all_posts[$key - 1] ) ) {
// get all previous posts
$this->previous_posts = array_reverse( array_slice( $all_posts, 0, $key ) );
}