'avada_vertical_menu',
'description' => __( 'This widget replaces the Side Navigation Template.', 'Avada' ),
];
$control_ops = [
'id_base' => 'avada-vertical-menu-widget',
];
parent::__construct( 'avada-vertical-menu-widget', __( 'Avada: Vertical Menu', 'Avada' ), $widget_ops, $control_ops );
$this->enqueue_script();
}
/**
* Echoes the widget content.
*
* @access public
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance The settings for the particular instance of the widget.
*/
public function widget( $args, $instance ) {
extract( $args ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput
if ( $title ) {
echo $before_title . $title . $after_title; // phpcs:ignore WordPress.Security.EscapeOutput
}
$widget_id_escaped = is_numeric( $args['widget_id'] ) ? 'avada-vertical-menu-widget-' . esc_attr( $args['widget_id'] ) . '-nav' : esc_attr( $args['widget_id'] ) . '-nav'; // phpcs:ignore WordPress.Security.EscapeOutput
// Dynamic Styles.
$style = '';
echo $style; // phpcs:ignore WordPress.Security.EscapeOutput
$nav_type = $instance['nav_type'];
if ( ! isset( $instance['fusion_divider_color'] ) ) {
if ( isset( $instance['border_color'] ) ) {
$instance['fusion_divider_color'] = $instance['border_color'];
} else {
$instance['fusion_divider_color'] = '';
}
}
$widget_border_class = ( '' === $instance['fusion_divider_color'] ? 'no-border' : '' );
if ( 'custom_menu' === $nav_type ) {
// Get menu.
$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
if ( ! $nav_menu ) {
echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput
return;
}
$link_before = '';
$link_after = '';
if ( ( 'left' === $instance['layout'] && ! is_rtl() ) || ( 'right' === $instance['layout'] && is_rtl() ) ) {
$link_before = '';
$link_after = '';
}
$nav_menu_args = [
'fallback_cb' => '',
'menu' => $nav_menu,
'container_class' => 'fusion-vertical-menu-widget', // Do not delete, needed for menu icons.
'container' => false,
'item_spacing' => 'discard',
'link_before' => $link_before,
'link_after' => $link_after,
'echo' => false,
];
$nav_menu_args = apply_filters( 'avada_vertical_menu_widget_custom_args', $nav_menu_args );
$aria_label = __( 'Secondary navigation', 'Avada' );
if ( isset( $instance['title'] ) ) {
/* translators: The widget name. */
$aria_label = sprintf( __( 'Secondary Navigation: %s', 'Avada' ), $instance['title'] );
}
echo '';
} elseif ( 'vertical_menu' === $nav_type ) {
// Get page.
$parent_page = ( ! empty( $instance['parent_page'] ) || '0' != $instance['parent_page'] ) ? $instance['parent_page'] : false; // phpcs:ignore WordPress.PHP.StrictComparisons
if ( ! $parent_page ) {
if (
( function_exists( 'fusion_doing_ajax' ) && fusion_doing_ajax() ) || // Widget loaded from a change in the live editor.
( function_exists( 'fusion_is_preview_frame' ) && fusion_is_preview_frame() ) // Initial load on the live-editor.
) {
echo '
';
esc_html_e( 'For the vertical layout to work, the page needs to be set as part of the WordPress parent/child hierarchy.', 'Avada' );
echo '
';
}
echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput
return;
}
$html = '';
$html = str_replace( 'fusion-prefix-', '', $html );
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
}
echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput
}
/**
* Updates a particular instance of a widget.
*
* This function should check that `$new_instance` is set correctly. The newly-calculated
* value of `$instance` should be returned. If false is returned, the instance won't be
* saved/updated.
*
* @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 Settings to save or bool false to cancel saving.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = isset( $new_instance['title'] ) ? $new_instance['title'] : '';
$instance['nav_type'] = isset( $new_instance['nav_type'] ) ? $new_instance['nav_type'] : '';
$instance['nav_menu'] = isset( $new_instance['nav_menu'] ) ? $new_instance['nav_menu'] : '';
$instance['parent_page'] = isset( $new_instance['parent_page'] ) ? $new_instance['parent_page'] : '';
$instance['behavior'] = isset( $new_instance['behavior'] ) ? $new_instance['behavior'] : '';
$instance['layout'] = isset( $new_instance['layout'] ) ? $new_instance['layout'] : '';
$instance['font_size'] = isset( $new_instance['font_size'] ) ? $new_instance['font_size'] : '';
unset( $instance['border_color'] );
return $instance;
}
/**
* Get array of pages which have got children.
*
* @access public
* @return array Array of all pages which have got chidlren.
*/
public function get_pages_with_children() {
$args = [
'parent' => -1,
'post_type' => 'page',
'post_status' => 'publish',
];
$pages = get_pages( $args );
$pages = array_filter( $pages, [ $this, 'exclude_parents' ] );
$parents = [];
foreach ( $pages as $page ) {
$parents[ $page->post_parent ] = get_the_title( $page->post_parent );
}
return $parents;
}
/**
* Callback function for array_filter in get_pages_with_children method.
*
* This method chcecks if current pages array index has got parent set.
*
* @access public
* @param array $element Current array element.
* @return bool whether got parent or not.
*/
public function exclude_parents( $element ) {
return isset( $element->post_parent ) && 0 !== $element->post_parent;
}
/**
* Enqueues script.
*
* @access public
* @return void
*/
public function enqueue_script() {
global $fusion_library_latest_version;
Fusion_Dynamic_JS::enqueue_script(
'awb-vertical-menu-widget',
Fusion_Scripts::$js_folder_url . '/general/awb-vertical-menu-widget.js',
Fusion_Scripts::$js_folder_path . '/general/awb-vertical-menu-widget.js',
[ 'jquery' ],
$fusion_library_latest_version,
true
);
}
/**
* Outputs the settings update form.
*
* @access public
* @param array $instance Current settings.
* @return void
*/
public function form( $instance ) {
$defaults = [
'title' => '',
'nav_type' => 'custom',
'nav_menu' => '',
'parent_page' => '',
'behavior' => 'hover',
'layout' => 'left',
'font_size' => '14px',
];
$instance = wp_parse_args( (array) $instance, $defaults );
// Get menus.
$menus = wp_get_nav_menus();
$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
// Get pages.
$pages = $this->get_pages_with_children();
$parent_page = isset( $instance['parent_page'] ) ? $instance['parent_page'] : '';
?>
fusion_megamenu_icon ) && ! empty( $item->fusion_megamenu_icon ) );
// Build the icon's markup.
$icon = ( $has_icon ) ? '' : '';
// In RTL languages append the icon, otherwise append it.
return ( is_rtl() ) ? $title . ' ' . $icon : $icon . ' ' . $title;
}
}
/* Omit closing PHP tag to avoid "Headers already sent" issues. */