Making WordPress.org


Ignore:
Timestamp:
06/11/2019 08:52:54 PM (5 years ago)
Author:
coffee2code
Message:

Handbook plugin: Fix navigation links for menu-based handbooks.

Previous check was insufficient for sites that had multiple handbooks where some had menu-based ordering and some were menu_order-based.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php

    r7723 r8941  
    99
    1010    /**
    11      * Is the handbook table of contents produced by the handbook pages widget?
    12      *
    13      * @access private
    14      * @var bool
    15      */
    16     private static $using_pages_widget = false;
    17 
    18     /**
    1911     * Initializes handbook navigation.
    2012     */
     
    2719     */
    2820    public static function do_init() {
    29         // Note if the WPorg_Handbook_Pages_Widget widget is in use.
    30         if ( is_active_widget( false, false, WPorg_Handbook_Pages_Widget::get_widget_id_base(), true ) ) {
    31             self::$using_pages_widget = true;
    32         }
     21        // Determine how handbook sidebar menu is defined
     22        add_action( 'wp', [ __CLASS__, 'determine_handbook_sidebar_menu_source' ] );
    3323
    3424        // Override o2 navigation defaults.
    3525        add_filter( 'o2_post_fragment', array( __CLASS__, 'o2_post_fragment' ), 10, 2 );
     26    }
     27
     28    /**
     29     * Determines the source for the handbook sidebar menu.
     30     *
     31     * @return string|false The handbook sidebar menu source. Either 'menu_widget',
     32     *                      'handbook_pages_widget'. False if not on a handbook
     33     *                      page. Empty string if no sidebar menu of its of an
     34     *                      unrecognized source.
     35     */
     36    public static function determine_handbook_sidebar_menu_source() {
     37        // Bail early if not a handbook.
     38        if ( ! $post_type = wporg_get_current_handbook() ) {
     39            return false;
     40        }
     41
     42        $menu_source = '';
     43
     44        $sidebars = wp_get_sidebars_widgets();
     45        if ( $sidebars && isset( $sidebars[ $post_type ] ) ) {
     46            foreach ( $sidebars[ $post_type ] as $widget ) {
     47                $widget_base = _get_widget_id_base( $widget );
     48                if ( 'nav_menu' === $widget_base ) {
     49                    $menu_source = 'menu_widget';
     50                } elseif( 'handbook_pages' === $widget_base ) {
     51                    $menu_source = 'handbook_pages_widget';
     52                }
     53                if ( $menu_source ) {
     54                    break;
     55                }
     56            }
     57        }
     58
     59        return $menu_source;
    3660    }
    3761
     
    5377        }
    5478
    55         if ( self::$using_pages_widget ) {
     79        if ( 'handbook_pages_widget' === self::determine_handbook_sidebar_menu_source() ) {
    5680            $adjacent = self::get_adjacent_posts_via_handbook_pages_widget( $post_id );
    5781        } else {
     
    94118        $prev = $next = false;
    95119
    96         if ( self::$using_pages_widget ) {
     120        if ( 'handbook_pages_widget' === self::determine_handbook_sidebar_menu_source() ) {
    97121            $adjacent = self::get_adjacent_posts_via_handbook_pages_widget();
    98122        } else {
Note: See TracChangeset for help on using the changeset viewer.