| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is the class that creates and handles the Accordion menu widget |
| 4 |
*/ |
| 5 |
|
| 6 |
// Exit if called directly |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
include_once( 'class-basepress-nav-walker.php' ); |
| 12 |
|
| 13 |
class Basepress_Nav_Widget extends WP_Widget { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Basepress_Nav_Widget constructor. |
| 18 |
* |
| 19 |
* @since 2.9.0 |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
parent::__construct( |
| 23 |
'basepress_nav_widget', // Base ID |
| 24 |
esc_html__( 'Knowledge Base - Accordion Menu', 'basepress' ), // Name |
| 25 |
array( 'description' => esc_html__( 'A single accordion menu to move inside the Knowledge Base', 'basepress' ) ) // Args |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Front-end display of widget. |
| 32 |
* |
| 33 |
* @since 2.9.0 |
| 34 |
* |
| 35 |
* @param array $args |
| 36 |
* @param array $instance |
| 37 |
*/ |
| 38 |
public function widget( $args, $instance ) { |
| 39 |
|
| 40 |
if( defined( 'REST_REQUEST' ) ){ |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
if( ! class_exists( 'Basepress') ){ |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 49 |
|
| 50 |
if ( ! empty( $instance['title'] ) ) { |
| 51 |
echo $args['before_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 52 |
echo esc_html( apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance ) ); |
| 53 |
echo $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 54 |
} |
| 55 |
|
| 56 |
$this->render( $instance ); |
| 57 |
|
| 58 |
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Back-end widget form. |
| 66 |
* |
| 67 |
* @since 2.9.0 |
| 68 |
* |
| 69 |
* @param array $instance |
| 70 |
* @return string|void |
| 71 |
*/ |
| 72 |
public function form( $instance ) { |
| 73 |
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; |
| 74 |
$link_sections = ! empty( $instance['link_sections'] ) ? $instance['link_sections'] : false; |
| 75 |
$articles_first = ! empty( $instance['articles_first'] ) ? $instance['articles_first'] : false; |
| 76 |
?> |
| 77 |
<p> |
| 78 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'basepress' ); ?></label> |
| 79 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> |
| 80 |
</p> |
| 81 |
|
| 82 |
<p> |
| 83 |
<input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'link_sections' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_sections' ) ); ?>" type="checkbox" value="1" <?php checked( $link_sections, 1 ); ?>> |
| 84 |
<label for="<?php echo esc_attr( $this->get_field_id( 'link_sections' ) ); ?>"><?php esc_attr_e( 'Add link to sections', 'basepress' ); ?></label> |
| 85 |
</p> |
| 86 |
|
| 87 |
<p> |
| 88 |
<input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'articles_first' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'articles_first' ) ); ?>" type="checkbox" value="1" <?php checked( $articles_first, 1 ); ?>> |
| 89 |
<label for="<?php echo esc_attr( $this->get_field_id( 'articles_first' ) ); ?>"><?php esc_attr_e( 'Display articles before sub-sections', 'basepress' ); ?></label> |
| 90 |
</p> |
| 91 |
<?php |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Sanitizes widget form values as they are saved. |
| 97 |
* |
| 98 |
* @since 2.9.0 |
| 99 |
* |
| 100 |
* @param array $new_instance |
| 101 |
* @param array $old_instance |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function update( $new_instance, $old_instance ) { |
| 105 |
$instance = array(); |
| 106 |
$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; |
| 107 |
$instance['link_sections'] = ! empty( $new_instance['link_sections'] ) ? $new_instance['link_sections'] : false; |
| 108 |
$instance['articles_first'] = ! empty( $new_instance['articles_first'] ) ? $new_instance['articles_first'] : false; |
| 109 |
|
| 110 |
return $instance; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Renders the widget using the BasePress_Nav_Walker |
| 116 |
* |
| 117 |
* @since 2.9.0 |
| 118 |
* |
| 119 |
* @param $instance |
| 120 |
*/ |
| 121 |
public function render( $instance ){ |
| 122 |
global $basepress_utils; |
| 123 |
|
| 124 |
$product = $basepress_utils->get_product(); |
| 125 |
|
| 126 |
$queried_object = get_queried_object(); |
| 127 |
|
| 128 |
$current_term_id = ''; |
| 129 |
$current_article_id = ''; |
| 130 |
$ancestor_terms = ''; |
| 131 |
$articles_first = ! empty( $instance['articles_first'] ) ? $instance['articles_first'] : false; |
| 132 |
$link_sections = ! empty( $instance['link_sections'] ) ? $instance['link_sections'] : false; |
| 133 |
|
| 134 |
if ( is_a( $queried_object, 'WP_Post' ) && 'knowledgebase' == $queried_object->post_type ) { |
| 135 |
$current_article_id = $queried_object->ID; |
| 136 |
$terms = get_the_terms( $queried_object->ID, 'knowledgebase_cat' ); |
| 137 |
if( ! empty( $terms ) ){ |
| 138 |
foreach( $terms as $term ){ |
| 139 |
if( 0 == $term->parent ){ |
| 140 |
continue; |
| 141 |
} |
| 142 |
$ancestor_terms = get_ancestors( $term->term_id, 'knowledgebase_cat', 'taxonomy' ); |
| 143 |
array_push( $ancestor_terms, $term->term_id ); |
| 144 |
} |
| 145 |
} |
| 146 |
} elseif ( is_a( $queried_object, 'WP_Term' ) ) { |
| 147 |
$current_term_id = $queried_object->term_id; |
| 148 |
$ancestor_terms = get_ancestors( $current_term_id, 'knowledgebase_cat', 'taxonomy' ); |
| 149 |
array_push( $ancestor_terms, $current_term_id ); |
| 150 |
} |
| 151 |
|
| 152 |
$args = array( |
| 153 |
'show_option_all' => '', |
| 154 |
'style' => 'list', |
| 155 |
'show_count' => 0, |
| 156 |
'hide_empty' => 1, |
| 157 |
'exclude' => '', |
| 158 |
'exclude_tree' => '', |
| 159 |
'include' => '', |
| 160 |
'hierarchical' => 1, |
| 161 |
'title_li' => '', |
| 162 |
'show_option_none' => '', |
| 163 |
'number' => null, |
| 164 |
'echo' => 1, |
| 165 |
'depth' => 0, |
| 166 |
'pad_counts' => 0, |
| 167 |
'taxonomy' => 'knowledgebase_cat', |
| 168 |
'child_of' => $product->id, |
| 169 |
'meta_query' => array( |
| 170 |
'relation' => 'OR', |
| 171 |
array( |
| 172 |
'key' => 'basepress_position', |
| 173 |
), |
| 174 |
array( |
| 175 |
'key' => 'basepress_position', |
| 176 |
'compare' => 'NOT EXISTS' |
| 177 |
) |
| 178 |
), |
| 179 |
'orderby' => 'meta_value_num', |
| 180 |
'order' => 'ASC', |
| 181 |
'open_categories' => $ancestor_terms, |
| 182 |
'current_category' => $current_term_id, |
| 183 |
'basepress_current_article' => $current_article_id, |
| 184 |
'articles_first' => $articles_first, |
| 185 |
'link_sections' => $link_sections, |
| 186 |
'walker' => new BasePress_Nav_Walker(), |
| 187 |
); |
| 188 |
|
| 189 |
/** |
| 190 |
* Filter arguments before generating the items list |
| 191 |
*/ |
| 192 |
$args = apply_filters( 'basepress_nav_term_args', $args ); |
| 193 |
|
| 194 |
echo '<ul class="bpress-nav-accordion">'; |
| 195 |
wp_list_categories( $args ); |
| 196 |
echo '</ul>'; |
| 197 |
} |
| 198 |
} |
| 199 |
?> |
| 200 |
|