Plugin Directory

Changeset 3418538


Ignore:
Timestamp:
12/12/2025 08:45:23 PM (2 months ago)
Author:
valerikluger
Message:

Release 1.0.5 - Fix menu item Visibility field not showing with custom admin walker

Location:
if-menu-visibility
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • if-menu-visibility/trunk/if-menu-visibility.php

    r3317885 r3418538  
    44Plugin Name: IF Menu Visibility
    55Description: Show menu items only for logged-in or logged-out users.
    6 Version: 1.0.4
     6Version: 1.0.5
    77Author: Valeri Kluger
    88Author URI: https://premium-plugin.com/
     
    1111Text Domain: if-menu-visibility
    1212*/
     13
     14// Damit "Visibility" in den Screen Options (oben rechts) erscheint
     15add_filter('manage_nav-menus_columns', function ($columns) {
     16    $columns['if-menu-visibility'] = __('Visibility', 'if-menu-visibility');
     17    return $columns;
     18}, 999);
     19
     20// Optional: nicht "von Grund auf" verstecken lassen (falls ein Plugin es in hidden-columns schiebt)
     21add_filter('get_user_option_managenav-menuscolumnshidden', function ($hidden) {
     22    if (!is_array($hidden)) return $hidden;
     23    return array_values(array_diff($hidden, ['if-menu-visibility']));
     24}, 999);
     25
     26// Fallback: Wenn ein Theme/Plugin den Admin-Menu-Walker ersetzt und
     27// wp_nav_menu_item_custom_fields NICHT mehr ausführt, injizieren wir das Feld trotzdem.
     28add_filter('wp_edit_nav_menu_walker', function ($walker) {
     29
     30    if (!is_admin()) return $walker;
     31
     32    // Walker-Klasse evtl. noch nicht geladen -> dann nichts ändern
     33    if (!class_exists($walker)) return $walker;
     34
     35    $new = 'IFMV_Walker_' . md5($walker);
     36    if (class_exists($new)) return $new;
     37
     38    $code = '
     39    class ' . $new . ' extends ' . $walker . ' {
     40
     41        private function ifmv_field_html($item_id, $current) {
     42            if (!current_user_can("edit_theme_options")) return "";
     43
     44            $html  = \'<p class="field-if-menu-visibility description description-wide">\';
     45            $html .= \'<label for="edit-menu-item-if-visibility-\' . esc_attr($item_id) . \'">\';
     46            $html .= esc_html__("Visibility:", "if-menu-visibility");
     47            $html .= \'<select id="edit-menu-item-if-visibility-\' . esc_attr($item_id) . \'" class="widefat" name="menu-item-if-visibility[\' . esc_attr($item_id) . \']">\';
     48            $html .= \'<option value="" \' . selected($current, "", false) . \'>\' . esc_html__("Always show", "if-menu-visibility") . \'</option>\';
     49            $html .= \'<option value="logged_in" \' . selected($current, "logged_in", false) . \'>\' . esc_html__("Only when logged in", "if-menu-visibility") . \'</option>\';
     50            $html .= \'<option value="logged_out" \' . selected($current, "logged_out", false) . \'>\' . esc_html__("Only when logged out", "if-menu-visibility") . \'</option>\';
     51            $html .= \'</select></label></p>\';
     52
     53            return $html;
     54        }
     55
     56        public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
     57
     58            $before_len = strlen($output);
     59            parent::start_el($output, $item, $depth, $args, $id);
     60
     61            // Nur den frisch hinzugefügten Teil bearbeiten
     62            $chunk = substr($output, $before_len);
     63
     64            // Wenn unser Feld schon drin ist (z.B. weil Action doch feuert) -> nichts tun
     65            if (strpos($chunk, "field-if-menu-visibility") !== false) {
     66                return;
     67            }
     68
     69            $current = get_post_meta($item->ID, "_if_menu_visibility", true);
     70            $field   = $this->ifmv_field_html($item->ID, $current);
     71
     72            if (!$field) return;
     73
     74            // 1) Core-Closing-Comment (häufig vorhanden)
     75            $new_chunk = preg_replace("/(\\s*<\\/div>\\s*<!--\\s*\\.menu-item-settings\\s*-->)/", $field . "$1", $chunk, 1, $count);
     76
     77            // 2) Fallback: vor "Move" Feld
     78            if (!$count) {
     79                $new_chunk = preg_replace("/(\\s*<p[^>]*class=\\"field-move\\b[^\\"]*\\"[^>]*>)/", $field . "$1", $chunk, 1, $count2);
     80                if (!$count2) {
     81                    // 3) Notfalls ans Ende hängen
     82                    $new_chunk = $chunk . $field;
     83                }
     84            }
     85
     86            $output = substr($output, 0, $before_len) . $new_chunk;
     87        }
     88    }';
     89
     90    eval($code);
     91    return $new;
     92
     93}, 999);
     94
     95
    1396
    1497// Menüpunkt erweitern (Backend)
     
    36119    $visibility = $item->if_menu_visibility ?? '';
    37120    ?>
    38     <p class="description description-wide">
     121    <p class="field-if-menu-visibility description description-wide">
    39122        <label for="edit-menu-item-if-visibility-<?php echo esc_attr($item_id); ?>">
    40123            <?php esc_html_e('Visibility:', 'if-menu-visibility'); ?>
  • if-menu-visibility/trunk/readme.txt

    r3317886 r3418538  
    44Tags: if menu, menu visibility, conditional menu, logged in, logged out
    55Requires at least: 5.0
    6 Tested up to: 6.8
    7 Stable tag: 1.0.4
     6Tested up to: 6.9
     7Stable tag: 1.0.5
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    3434== Changelog ==
    3535
     36= 1.0.5 =
     37* Fixed menu item “Visibility” field not showing when themes/plugins override the nav menu admin walker (fallback walker injection)
     38* Ensured the “Visibility” option appears in Screen Options and is not forcibly hidden by other plugins
     39
    3640= 1.0.4 =
    3741* Added ABSPATH check to prevent direct file access
Note: See TracChangeset for help on using the changeset viewer.