XF 2.2 Where to find XF.php Documentation?

DKGE

Member
Hello

I would like to learn more about interacting with XF.php
But so far i am unable to find documentation of what is available.
For example once you have the user as $user what are the options you can use. (example: $user->username)
I figured some stuff out by just testing and trying different things.
But i would love to know where i need to look for these options and know exactly how to use it.
Am i just overlooking a documentation somewhere or was it never documented?

My current search is to see if the member is banned, so far no luck.

Here is my test script to find these options so far.
PHP:
<?php
$dir = '/path/to/xenforo';
require($dir . '/src/XF.php');
\XF::start($dir);
$app = \XF::setupApp('XF\Pub\App');
$app->start(); // !! Needed for $visitor !!
$session = $app->session();
$uid = $session->get('userId');
$visitor = \XF::visitor();  // !! Needed for $visitor !!

// If user is not logged in, return guest
if ($uid){
    $user = $app->finder('XF:User')->where('user_id', $uid)->fetchOne();

    // User Name:
    print_r("Username: " . $user->username . "<br>");

    // User Admin Status:
    if ($user->is_admin){
        print_r("User is admin<br>");
    } else {
        print_r("User is not admin<br>");
    }

    // User ID:
    print_r("User ID: " . $user->user_id . "<br>");

    // Print all custom fields starting with test:
    foreach ($user->Profile->custom_fields as $key => $value) {
        if (strpos($key, 'test') === 0){
            print_r($key . " : " . $value . "<br>");
        }
    }

    // User Last Login:
    print_r("Last Login: " . date("d-m-Y H:i:s", $user->Admin->last_login) . "<br>");

    // User Groups:
    print_r("Primary Group: " . $user->user_group_id . "<br>");
    print_r("Secondary Groups: " . implode(', ', $user->secondary_group_ids) . "<br>");

    // User Avatar URL:
    $avatarUrl = $visitor->getAvatarUrl('m', null, true); // s, m, l, o => avatar sizes
    print_r("Avatar: " . "<a href='" . $avatarUrl . "'>" . $avatarUrl . "</a><br>");
    print_r("<img src='" . $avatarUrl . "'>");

    // remove value from custom field
    // $user->Profile->custom_fields->set('test1', "", 'admin_edit');
    // $user->Profile->save();

    // Add user to group 31
    // $user->set('secondary_group_ids', array_merge($user->secondary_group_ids, [31]));
    // $user->save();

    // remove user from secondary group 31
    // $user->set('secondary_group_ids', array_diff($user->secondary_group_ids, [31]));
    // $user->save();

}else{
    echo 'guest';
}
 
Solution

Though the "real" documentation are the PHP files (PHPdoc, your IDE should be able to use this automatically).

\XF::visitor() returns an instance of \XF\Entity\User wihch has

PHP:
/**
 * COLUMNS
 * @property int|null $user_id
 * @property string $username
 * @property int $username_date
 * @property int $username_date_visible
 * @property string $email
 * @property int $style_id
 * @property int $language_id
 * @property...

Though the "real" documentation are the PHP files (PHPdoc, your IDE should be able to use this automatically).

\XF::visitor() returns an instance of \XF\Entity\User wihch has

PHP:
/**
 * COLUMNS
 * @property int|null $user_id
 * @property string $username
 * @property int $username_date
 * @property int $username_date_visible
 * @property string $email
 * @property int $style_id
 * @property int $language_id
 * @property string $timezone
 * @property bool $visible
 * @property bool $activity_visible
 * @property int $user_group_id
 * @property array $secondary_group_ids
 * @property int $display_style_group_id
 * @property int $permission_combination_id_
 * @property int $message_count
 * @property int $question_solution_count
 * @property int $alerts_unviewed
 * @property int $alerts_unread
 * @property int $conversations_unread
 * @property int $register_date
 * @property int $last_activity_
 * @property int|null $last_summary_email_date
 * @property int $trophy_points
 * @property int $avatar_date
 * @property int $avatar_width
 * @property int $avatar_height
 * @property bool $avatar_highdpi
 * @property string $gravatar
 * @property string $user_state
 * @property string $security_lock
 * @property bool $is_moderator
 * @property bool $is_admin
 * @property bool $is_staff
 * @property bool $is_banned
 * @property int $reaction_score
 * @property int $vote_score
 * @property string $custom_title
 * @property int $warning_points
 * @property string $secret_key
 * @property int $privacy_policy_accepted
 * @property int $terms_accepted
 *
 * GETTERS
 * @property \XF\PermissionSet $PermissionSet
 * @property int $permission_combination_id
 * @property bool $is_super_admin
 * @property int $last_activity
 * @property string $email_confirm_key
 * @property int $warning_count
 * @property int $next_allowed_username_change
 *
 * RELATIONS
 * @property \XF\Entity\Admin $Admin
 * @property \XF\Entity\UserAuth $Auth
 * @property \XF\Mvc\Entity\AbstractCollection|\XF\Entity\UserConnectedAccount[] $ConnectedAccounts
 * @property \XF\Entity\UserOption $Option
 * @property \XF\Entity\PermissionCombination $PermissionCombination
 * @property \XF\Entity\UserProfile $Profile
 * @property \XF\Entity\UserPrivacy $Privacy
 * @property \XF\Entity\UserBan $Ban
 * @property \XF\Entity\UserReject $Reject
 * @property \XF\Entity\SessionActivity $Activity
 * @property \XF\Entity\ApprovalQueue $ApprovalQueue
 * @property \XF\Mvc\Entity\AbstractCollection|\XF\Entity\UserFollow[] $Following
 * @property \XF\Entity\UsernameChange $PendingUsernameChange
 * @property \XF\Entity\PreRegAction $PreRegAction
 */
and a dozen methods to do various things (like many methods to check permissions).

Your code could be simplified
PHP:
<?php
$dir = '/path/to/xenforo';
require($dir . '/src/XF.php');
\XF::start($dir);
$app = \XF::setupApp('XF\Pub\App');
$app->start(); // !! Needed for $visitor !!
$visitor = \XF::visitor();  // !! Needed for $visitor !!

// If user is not logged in, return guest
if ($visitor->user_id)
{
    // use $visitor instead of $user from now or: $user = $visitor;

PHP:
 // User Last Login:
    print_r("Last Login: " . date("d-m-Y H:i:s", $user->Admin->last_login) . "<br>");
This will only work for Admins, for normal users it will result in an error as $user->Admin would be null:
Besides that, it's not the Last login but the last login to the admin control panel by that user.
XenForo does not save a timestamp of the last login for users.
 
Solution
Back
Top Bottom