Roots Discourse

All of the sudden App.php Controller stops passing data to templates


#1

I’m getting undefined variable errors throughout my site that once were not there. Here is my App.php file:

    <?php

namespace App\Controllers;

use Sober\Controller\Controller;

class App extends Controller
{
    public $userId;
    public $gamertag;
    public $avatar;

    public $playerGt;
    public $playerWpId;
    public $playerAvatar;

    /*
     * stores a bunch of variables to be used throughout the class
     */

    protected function initVars()
    {
        $this->userId = get_current_user_id();
        $this->gamertag = get_user_meta($this->userId, 'msa_gt') ?? null;
        $this->avatar = get_avatar($this->userId);

        // set the players data to the users data by default, but then change it if there is a if the gamertag query param exists

        $this->playerGt = get_query_var('gamertag') ?? $this->gamertag;
        $this->playerWpId = $this->userId;
        $this->playerAvatar = $this->avatar;

        if($this->playerGt != $this->gamertag) :
            $args = array(
                'meta_key' => 'msa_gt',
                'meta_value' => $this->playerGt,
            );
            $user = get_users($args);
            $this->playerWpId = $user[0]->ID;
            $this->playerAvatar = get_avatar($this->playerWpId);
        endif;

    }

    /*
     * gets the site name
     * @return string the name of the site
     * @access public
     */

    public function siteName()
    {
        return get_bloginfo('name');
    }

    /*
     * gets the site url
     * @return string the site url including http(s)
     * @access public
     */

    public function siteUrl()
    {
        return get_site_url();
    }

    /*
     * gets the logout url
     * @return string the logout url value (built in with Wordpress)
     * @access public
     */
    public function wpLogoutUrl()
    {
        return wp_logout_url();
    }

    /*
     * gets the user ID
     * @return int current user's ID, or 0 if no user is logged in.
     * @access public
     */
    public function userId()
    {
        return $this->userId;
    }

    /*
     * is the user logged in?
     * @return True if user is logged in, false if not logged in.
     * @access public static
     */

    public function isUserLoggedIn()
    {
        return is_user_logged_in();
    }

    /*
     * gets the gamertag
     * @return string gamertag
     * @access public
     */

    public function gamertag()
    {
        return get_user_meta($this->userId, 'msa_gt');
    }

    /*
     * the gamertag of the player being searched for (can sometimes be seen in the url as a query param)
     * @return string desired players gamertag
     * @access public static
     */

    public function playerGt()
    {
        return $this->playerGt;
    }

    /*
     * the userId of the player
     * @return string desired players userId
     * @access public static
     */

    public function playerWpId()
    {
        return $this->playerWpId;
    }

    /*
     * the avatar of the player which is to be selected to display
     * @return string desired players avatar
     * @access public static
     */

    public function playerAvatar()
    {
        return $this->playerAvatar;
    }

    /*
     * gets the id of the clan that the user is associated with
     * @return int clan id
     * @access public
     */

    public function clanAssoc()
    {
        return get_user_meta($this->userId, 'clan_assoc');
    }



    public static function title()
    {
        if (is_home()) {
            if ($home = get_option('page_for_posts', true)) {
                return get_the_title($home);
            }
            return __('Latest Posts', 'sage');
        }
        if (is_archive()) {
            return get_the_archive_title();
        }
        if (is_search()) {
            return sprintf(__('Search Results for %s', 'sage'), get_search_query());
        }
        if (is_404()) {
            return __('Not Found', 'sage');
        }
        return get_the_title();
    }

    /*
   * Get the specific template name for a given post.
   * @return string Page template filename. Returns an empty string when the default page template is in use. Returns false if the post does not exist.
   * @access public static
   */

    public static function pageTemplateSlug()
    {
        return get_page_template_slug();
    }

    /*
   * gets the avatar
   * @return string url of the avatar
   * @param int $userID
   * @access public static
   */

    public static function avatar($userId)
    {
        return !empty($userId) ? get_avatar($userId) : get_avatar(self::$userId);
    }
}

the body class is: “home page-template-default page page-id-7 logged-in admin-bar app-data index-data singular-data page-data page-7-data page-home-data front-page-data customize-support”


#2
  • What are some of the specific errors you’re seeing?
  • What change with your site between when it last worked and when the errors started appearing?

Unless there’s some part of your class that you haven’t posted, I’m not sure how initVars() ever gets run, and if it’s never run then all the properties it’s meant to set are never set. Normally I’d think you’d call it from __construct() but this class doesn’t appear to have __construct() defined.


closed #3

This topic was automatically closed after 42 days. New replies are no longer allowed.