/
breadcrumbs.php
124 lines (106 loc) · 2.69 KB
/
breadcrumbs.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
<?php
/**
* AnsPress breadcrumbs widget
*
* @package AnsPress
* @author Rahul Aryan <rah12@live.com>
* @license GPL 2+ GNU GPL licence above 2+
* @link https://anspress.net
* @since 2.0.0
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* AnsPress breadcrumbs widget.
*/
class AnsPress_Breadcrumbs_Widget extends WP_Widget {
/**
* Initialize the class
*/
public function __construct() {
parent::__construct(
'ap_breadcrumbs_widget',
__( '(AnsPress) Breadcrumbs', 'anspress-question-answer' ),
array( 'description' => __( 'Show current anspress page navigation', 'anspress-question-answer' ) )
);
}
/**
* Get breadcrumbs array
*
* @return array
*/
public static function get_breadcrumbs() {
$current_page = ap_current_page();
$title = ap_page_title();
$a = array();
$a['base'] = array(
'title' => ap_opt( 'base_page_title' ),
'link' => ap_base_page_link(),
'order' => 0,
);
$current_page = $current_page ? $current_page : '';
if ( is_question() ) {
$a['page'] = array(
'title' => $title,
'link' => get_permalink( get_question_id() ),
'order' => 10,
);
} elseif ( 'base' !== $current_page && '' !== $current_page ) {
$a['page'] = array(
'title' => $title,
'link' => $current_page,
'order' => 10,
);
}
$a = apply_filters( 'ap_breadcrumbs', $a );
return is_array( $a ) ? ap_sort_array_by_order( $a ) : array();
}
/**
* Output AnsPress breadcrumbs.
*
* @return void
*/
public static function breadcrumbs() {
$navs = self::get_breadcrumbs();
echo '<ul class="ap-breadcrumbs clearfix">';
echo '<li class="ap-breadcrumbs-home"><a href="' . esc_url( home_url( '/' ) ) . '" class="apicon-home"></a></li>';
echo '<li><i class="apicon-chevron-right"></i></li>';
$i = 1;
$total_nav = count( $navs );
foreach ( $navs as $k => $nav ) {
if ( ! empty( $nav ) ) {
echo '<li>';
echo '<a href="' . esc_url( $nav['link'] ) . '">' . esc_attr( $nav['title'] ) . '</a>';
echo '</li>';
if ( $total_nav !== $i ) {
echo '<li><i class="apicon-chevron-right"></i></li>';
}
}
++$i;
}
echo '</ul>';
}
/**
* Output widget.
*
* @param array $args Widget arguments.
* @param array $instance Widget instance.
* @return void
*/
public function widget( $args, $instance ) {
echo wp_kses_post( $args['before_widget'] );
self::breadcrumbs();
echo wp_kses_post( $args['after_widget'] );
}
}
/**
* Register breadcrumbs widget.
*
* @return void
*/
function register_anspress_breadcrumbs() {
register_widget( 'AnsPress_Breadcrumbs_Widget' );
}
add_action( 'widgets_init', 'register_anspress_breadcrumbs' );