Search code examples
phphtmlwordpresswordpress-theming

Wordpress PHP - Empty menu links generated by old Artisteer theme


I am helping recover a compromised website, which was using a custom theme generated by Artisteer v4.3.0.60745

You can view the archived site here.

I am trying to get it all running over a fresh local copy of Wordpress 6.6.2 (no plugins) and PHP 8.2.20. The problem I have is that the theme's header and sidebar menus do not populate with titles and links.

The old, archived site generated the following header menu:

My local copy only creates null hrefs with no titles as seen here:

<ul class="art-hmenu menu-6">
    <li class="menu-item-"><a href="#"></a>
    </li>
    <li class="menu-item-"><a href="#"></a>
    </li>
    <li class="menu-item-"><a href="#"></a>
    </li>
    <li class="menu-item-"><a href="#"></a>
    </li>
    <li class="menu-item-"><a href="#"></a>
    </li>
</ul>

After some debugging, I can see that $sorted_menu_items are not being passed into the theme_MenuItem class successfully, but I have no idea why.

See here for VS Code debug window ($el contains the correct data, but $items[] are all null).

Here are the code snippets responsible:

wp-content/themes/osp5/library/navigation.php line 35

/* custom menu */

function theme_get_list_menu($args = array()) {
    global $wp_query;
    $menu_items = wp_get_nav_menu_items($args['menu']->term_id);
    if (empty($menu_items))
        return '';
    $home_page_id = (int) get_option('page_for_posts');
    $queried_object = $wp_query->get_queried_object();
    $queried_object_id = (int) $wp_query->queried_object_id;
    $active_ID = null;

    $IdToKey = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $IdToKey[$menu_item->ID] = $key;
        if ($menu_item->object_id == $queried_object_id &&
                (
                (!empty($home_page_id) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
                ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
                ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ))
                )
        ) {
            $active_ID = $menu_item->ID;
        } elseif ('custom' == $menu_item->object) {
            if (theme_is_current_url($menu_item->url)) {
                $active_ID = $menu_item->ID;
            }
        }
    }

    $current_ID = $active_ID;
    while ($current_ID && isset($IdToKey[$current_ID])) {
        $activeIDs[] = $current_ID;
        $current_item = &$menu_items[$IdToKey[$current_ID]];
        $current_item->classes[] = 'active';
        $current_ID = $current_item->menu_item_parent;
    }

    $sorted_menu_items = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item);
    }

    $items = array();
    foreach ($sorted_menu_items as $el) {
        $id = $el->db_id;
        $title = $el->title;
        $classes = empty($el->classes) ? array() : (array) $el->classes;
        $active = in_array('active', $classes);
        $items[] = new theme_MenuItem(array(
                    'id' => $id,
                    'active' => $active,
                    'attr' => array(
                        'title' => strip_tags(empty($el->attr_title) ? $title : $el->attr_title),
                        'target' => $el->target,
                        'rel' => $el->xfn,
                        'href' => $el->url,
                        'class' => join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $el))
                    ),
                    'title' => $title,
                    'parent' => $el->menu_item_parent
                ));
    }

    $walker = new theme_MenuWalker();
    $items = apply_filters('wp_nav_menu_objects', $items, $args);
    $items = $walker->walk($items, $args);
    $items = apply_filters('wp_nav_menu_items', $items, $args);
    return apply_filters('wp_nav_menu', $items, $args);
}

line 246, the menuItem class

/* menu item */

class theme_MenuItem {

    var $id;
    var $active;
    var $parent;
    var $attr;
    var $title;

    function theme_MenuItem($args = '') {
        $args = wp_parse_args($args, array(
            'id' => '',
            'active' => false,
            'parent' => 0,
            'attr' => '',
            'title' => '',
                )
        );
        $this->id = $args['id'];
        $this->active = $args['active'];
        $this->parent = $args['parent'];
        $this->attr = $args['attr'];
        $this->title = $args['title'];
    }

    function get_start($level) {
        $class = theme_get_array_value($this->attr, 'class', '');
        $class = 'menu-item-' . $this->id . (strlen($class) > 0 ? ' ' : '') . $class;
        $this->attr['class'] = ($this->active ? 'active' : null);
        $title = apply_filters('the_title', $this->title, $this->id);
        if (theme_get_option('theme_menu_trim_title')) {
            $title = theme_trim_long_str(strip_tags($title), theme_get_option($level == 0 ? 'theme_menu_trim_len' : 'theme_submenu_trim_len'));
        }
        return str_repeat("\t", $level + 1)
                . '<li' . theme_prepare_attr(array('class' => $class)) . '>'
                . '<a' . theme_prepare_attr($this->attr) . '>'
                . $title
                . '</a>' . "\n";
    }

    function get_end($level) {
        return str_repeat("\t", $level + 1) . '</li>' . "\n";
    }

}

Since this was working on whatever old version of Wordpress and PHP was being used, I am guessing there are some syntax changes I need to make. When turning on WP_DEBUG, there are some deprecation warnings that pop up inside the menu items, but I think those are from further along the trace (when these nulls get passed along):

Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /Applications/MAMP/htdocs/osprzemien/wp-includes/formatting.php on line 2476

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /Applications/MAMP/htdocs/osprzemien/wp-includes/class-wp-hook.php on line 326

Solution

  • I downgraded all the way to PHP 5.6.40 and the menu worked, but WP_DEBUG showed the following warning:

    Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; theme_MenuItem has a deprecated constructor in /Applications/MAMP/htdocs/osprzemien584/wp-content/themes/osp5/library/navigation.php on line 248

    Changing the offending function theme_MenuItem into function __construct resolved the issue and the menu now works on PHP 8.2.20.