'widget_pages',
'description' => __( 'A list of your site’s Pages.', 'benjamin' ),
'customize_selective_refresh' => true,
);
parent::__construct( 'pages', __( 'Pages', 'benjamin' ), $widget_ops );
}
private function menuStyleArgs($style = 'side_nav'){
if($style == 'side_nav'):
$class = 'usa-sidenav-list';
elseif($style == 'nav_list'):
$class = 'usa-unstyled-list';
else:
$class = '';
endif;
return $class;
}
public function dropdown_js($dropdown_id)
{
ob_start();
?>
id_base}-dropdown-{$this->number}";
$output = '';
$output .= '';
$output .= $this->dropdown_js($dropdown_id);
echo $output;
}
public function child_menu($children)
{
$output = '';
$output .= '
';
return $output;
}
public function menu($sortby, $exclude, $style, $show_children)
{
$style_args = $this->menuStyleArgs($style);
$class = $style_args ? 'class="'.$style_args.'"' : '';
$output = '';
$output .= '';
$pages = get_pages(array(
'sort_column' => $sortby,
'exclude' => $exclude,
'parent' => 0
));
$children = array();
if($show_children == '1'):
$children_q = get_pages(array(
'sort_column' => $sortby,
'exclude' => $exclude,
));
foreach($children_q as $child) {
if($child->post_parent > 0){
$children[$child->post_parent][] = $child;
}
}
endif;
$last_id = 0;
foreach($pages as $page){
$output .= '- ';
$output .= '';
$output .= $page->post_title;
$output .= '';
if(!empty($children[$page->ID]))
$output .= $this->child_menu($children[$page->ID]);
$output .= '
';
$last_id = $page->ID;
}
$output .= '
';
echo $output;
}
/**
* Outputs the content for the current Pages widget instance.
*
* @since 2.8.0
* @access public
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Pages widget instance.
*/
public function widget( $args, $instance ) {
/**
* Filters the widget title.
*
* @since 2.6.0
*
* @param string $title The widget title. Default 'Pages'.
* @param array $instance An array of the widget's settings.
* @param mixed $id_base The widget ID.
*/
$children = ! empty( $instance['children'] ) ? '1' : '0';
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? null : $instance['title'], $instance, $this->id_base );
$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
if ( $sortby == 'menu_order' )
$sortby = 'menu_order, post_title';
$style = ! empty( $instance['menu_style'] ) ? $instance['menu_style'] : 'side_nav';
/**
* Filters the arguments for the Pages widget.
*
* @since 2.8.0
*
* @see wp_list_pages()
*
* @param array $args An array of arguments to retrieve the pages list.
*/
$depth = ($children == '1') ? 2 : 1;
$out = wp_list_pages( apply_filters( 'widget_pages_args', array(
'title_li' => '',
'echo' => 0,
'sort_column' => $sortby,
'exclude' => $exclude,
'depth' => $depth
) ) );
// if no pages exists, just return false. this prevents further code
// execution as well as prevents WP's original nested conditionals.
if ( empty( $out ) )
return false;
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if($style == 'dropdown') {
$this->dropdown($sortby, $exclude, $children);
} elseif($style !== 'list') {
$this->menu($sortby, $exclude, $style, $children);
} else {
echo '';
}
echo $args['after_widget'];
}
/**
* Handles updating settings for the current Pages widget instance.
*
* @since 2.8.0
* @access public
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
$instance['sortby'] = $new_instance['sortby'];
} else {
$instance['sortby'] = 'menu_order';
}
$instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
$instance['children'] = !empty($new_instance['children']) ? 1 : 0;
if ( ! empty( $new_instance['menu_style'] ) ) {
$instance['menu_style'] = sanitize_text_field($new_instance['menu_style']);
}
return $instance;
}
/**
* Outputs the settings form for the Pages widget.
*
* @since 2.8.0
* @access public
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
$saved_style = isset( $instance['menu_style'] ) ? $instance['menu_style'] : '';
$children = isset( $instance['children'] ) ? (bool) $instance['children'] : false;
?>
/>