Instantly share code, notes, and snippets.
Created
October 23, 2023 11:56
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save webmandesign/22a29649a3220d60ab230cae10991924 to your computer and use it in GitHub Desktop.
WebMan Sub Navigation Widget plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined( 'WPINC' ) ) exit; | |
/** | |
* Plugin Name: WebMan Sub Navigation Widget | |
* Description: Extraction and modification of code from WebMan Amplifier plugin. | |
* Version: 1.0.0 | |
* Author: WebMan Design - Oliver Juhas | |
* Author URI: https://www.webmandesign.eu | |
* License: GNU General Public License v3 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt | |
* Requires at least: 6.1 | |
* Tested up to: 6.3 | |
*/ | |
/** | |
* Widget class | |
* | |
* @since 1.0.9.9 | |
* @version 1.4 | |
* | |
* Contents: | |
* | |
* 0) Init | |
* 10) Output | |
* 20) Options | |
*/ | |
class WM_Subnav extends WP_Widget { | |
/** | |
* 0) Init | |
*/ | |
/** | |
* Constructor | |
* | |
* @since 1.0.9.9 | |
* @version 1.3.10 | |
*/ | |
function __construct() { | |
// Helper variables | |
$atts = array(); | |
$atts['id'] = 'wm-subnav'; | |
$atts['name'] = esc_html_x( 'WebMan Sub Navigation Widget', 'Widget name.', 'webman-subnav-widget' ); | |
$atts['widget_ops'] = array( | |
'classname' => 'wm-subnav', | |
'description' => esc_html_x( 'List of subpages', 'Widget description.', 'webman-subnav-widget' ), | |
'customize_selective_refresh' => true, | |
); | |
$atts['control_ops'] = array(); | |
// Processing | |
parent::__construct( $atts['id'], $atts['name'], $atts['widget_ops'], $atts['control_ops'] ); | |
} // /__construct | |
/** | |
* 10) Output | |
*/ | |
/** | |
* Output HTML | |
* | |
* @since 1.0.9.9 | |
* @version 1.4.3 | |
*/ | |
function widget( $args, $instance ) { | |
// Requirements check | |
$post_types = get_post_types( array( 'hierarchical' => true ) ); | |
if ( | |
! is_singular( $post_types ) | |
|| apply_filters( 'wmhook_widgets_wm_subnav_disabled', false, $args, $instance ) | |
) { | |
return; | |
} | |
// Helper variables | |
global $post; | |
$output = ''; | |
$instance = wp_parse_args( $instance, array( | |
'depth' => 3, | |
'order' => 'menu_order', | |
'parent' => '', | |
'title' => '', | |
) ); | |
$post = ( is_home() ) ? ( get_post( get_option( 'page_for_posts' ) ) ) : ( $post ); | |
$parents = get_ancestors( $post->ID, get_post_type( $post ) ); | |
// Get the direct parent or the highest level parent | |
if ( $instance['parent'] && ! empty( $parents ) ) { | |
$grandparent = $parents[0]; | |
} elseif ( ! $instance['parent'] && ! empty( $parents ) ) { | |
$grandparent = end( $parents ); | |
} else { | |
$grandparent = ''; | |
} | |
// Set the parent page title as a widget title when it was left empty | |
if ( ! trim( $instance['title'] ) ) { | |
if ( $grandparent ) { | |
$args['before_title'] = $args['before_title'] . '<a href="' . esc_url( get_permalink( $grandparent ) ) . '">'; | |
$instance['title'] = get_the_title( $grandparent ); | |
$args['after_title'] = '</a>' . $args['after_title']; | |
} else { | |
$args['before_title'] = $args['before_title'] . '<a href="' . esc_url( get_permalink( $post->ID ) ) . '">'; | |
$instance['title'] = get_the_title( $post->ID ); | |
$args['after_title'] = '</a>' . $args['after_title']; | |
} | |
$instance['title'] = apply_filters( 'wmhook_widgets_wm_subnav_title_auto', $instance['title'], $args, $instance ); | |
} | |
// Subpages or siblings | |
$args_children = array( | |
'post_type' => get_post_type( $post ), | |
'title_li' => '', | |
'depth' => absint( $instance['depth'] ), | |
'sort_column' => $instance['order'], | |
'echo' => false, | |
'child_of' => $post->ID, | |
); | |
if ( $grandparent ) { | |
$args_children['child_of'] = $grandparent; | |
} | |
$children = wp_list_pages( (array) apply_filters( 'wmhook_widgets_wm_subnav_wp_list_pages_args', $args_children, $args, $instance ) ); | |
// If there are no pages, don't display the widget | |
if ( empty( $children ) ) { | |
return; | |
} | |
// Processing | |
// Before widget | |
$output .= $args['before_widget']; | |
// Title | |
if ( trim( $instance['title'] ) ) { | |
$output .= $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base, $args ) . $args['after_title']; | |
} | |
$output .= '<ul class="sub-nav">' . $children . '</ul>'; | |
// After widget | |
$output .= $args['after_widget']; | |
// Output | |
echo apply_filters( 'wmhook_widgets_wm_subnav_output', $output, $args, $instance ); | |
} // /widget | |
/** | |
* 20) Options | |
*/ | |
/** | |
* Options form | |
* | |
* @since 1.0.9.9 | |
* @version 1.4 | |
*/ | |
function form( $instance ) { | |
// Helper variables | |
$instance = wp_parse_args( $instance, array( | |
'depth' => 3, | |
'order' => 'menu_order', | |
'parent' => '', | |
'title' => '', | |
) ); | |
// Output | |
?> | |
<p class="wm-desc"> | |
<?php echo esc_html_x( 'Displays a hierarchical list of child and sibling pages for the current page.', 'Widget description.', 'webman-subnav-widget' ) ?> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"> | |
<?php esc_html_e( 'Title:', 'webman-subnav-widget' ) ?> | |
</label> | |
<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" /> | |
<small> | |
<?php esc_html_e( 'If you leave blank, the main parent page title will be displayed.', 'webman-subnav-widget' ) ?> | |
</small> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'depth' ); ?>"> | |
<?php esc_html_e( 'Child pages depth level:', 'webman-subnav-widget' ); ?> | |
</label> | |
<select name="<?php echo $this->get_field_name( 'depth' ); ?>" id="<?php echo $this->get_field_id( 'depth' ); ?>" class="widefat"> | |
<?php | |
for ( $i = 1; $i < 10; $i++ ) { | |
echo '<option value="' . esc_attr( $i ) . '" ' . selected( esc_attr( $instance['depth'] ), esc_attr( $i ), false ) . '>' . esc_html( $i ) . '</option>'; | |
} | |
?> | |
</select> | |
</p> | |
<p> | |
<input type="checkbox" name="<?php echo $this->get_field_name( 'parent' ); ?>" id="<?php echo $this->get_field_id( 'parent' ); ?>" <?php checked( $instance['parent'], 'on' ); ?>/> | |
<label for="<?php echo $this->get_field_id( 'parent' ); ?>"> | |
<?php esc_html_e( 'Direct parent and its child pages only', 'webman-subnav-widget' ); ?> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'order' ); ?>"> | |
<?php esc_html_e( 'List order:', 'webman-subnav-widget' ); ?> | |
</label> | |
<select name="<?php echo $this->get_field_name( 'order' ); ?>" id="<?php echo $this->get_field_id( 'order' ); ?>" class="widefat"> | |
<?php | |
$options = apply_filters( 'wmhook_widgets_wm_subnav_form_order', array( | |
'post_title' => esc_html_x( 'By name', 'List order method.', 'webman-subnav-widget' ), | |
'post_date' => esc_html_x( 'By date', 'List order method.', 'webman-subnav-widget' ), | |
'menu_order' => esc_html_x( 'Menu order', 'List order method.', 'webman-subnav-widget' ), | |
) ); | |
foreach ( $options as $value => $name ) { | |
echo '<option value="' . esc_attr( $value ) . '" ' . selected( esc_attr( $instance['order'] ), $value, false ) . '>' . esc_html( $name ) . '</option>'; | |
} | |
?> | |
</select> | |
</p> | |
<?php | |
do_action( 'wmhook_widgets_wm_subnav_form', $instance ); | |
} // /form | |
/** | |
* Save the options | |
* | |
* @since 1.0.9.9 | |
* @version 1.4 | |
*/ | |
function update( $new_instance, $old_instance ) { | |
// Helper variables | |
$instance = $old_instance; | |
// Processing | |
$instance['depth'] = $new_instance['depth']; | |
$instance['order'] = $new_instance['order']; | |
$instance['parent'] = $new_instance['parent']; | |
$instance['title'] = $new_instance['title']; | |
// Output | |
return apply_filters( 'wmhook_widgets_wm_subnav_instance', $instance, $new_instance, $old_instance ); | |
} // /update | |
} // /WM_Subnav | |
/** | |
* Widget registration | |
* | |
* @since 1.0.9.9 | |
* @version 1.2.8 | |
*/ | |
function wm_subnav_registration() { | |
// Processing | |
register_widget( 'WM_Subnav' ); | |
} // /wm_subnav_registration | |
add_action( 'widgets_init', 'wm_subnav_registration' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment