XF 2.1 How to add new PHP conditional if certain domain is used?

rdn

Well-known member
So I have this code from an addon (coded by @TickTackk 2 years ago I think).
PHP:
if ((!$visitor->user_id || $visitor->user_state !== 'valid') && !$this->internalRenderedLink && $linkInfo['type'] !== 'internal')
From what I understand, skip if the visitors is not logged or not valid and also if internal links.

I want to add more condition, if the domain is equal to google.com then skip the condition.
What would be the exact code to use?

Many Thanks!
 
The provided snippet is not large enough to give you any code samples that could work. We don't even know what variable the URL is stored in.
 
Thanks, this is the whole Code.
PHP:
<?php

namespace TickTackk\HideLinksFromGuest\XF\BbCode\Renderer;

/**
 * Class Html
 *
 * @package TickTackk\HideLinksFromGuest\XF\BbCode\Renderer
 */
class Html extends XFCP_Html
{
    /**
     * @var null|bool
     */
    protected $internalRenderedLink;

    /**
     * @param string $text
     * @param string $url
     * @param array  $options
     *
     * @return string|\XF\Phrase
     */
    protected function getRenderedLink($text, $url, array $options)
    {
        $linkInfo = $this->formatter->getLinkClassTarget($url);
        $visitor = \XF::visitor();
        if ((!$visitor->user_id || $visitor->user_state !== 'valid') && !$this->internalRenderedLink && $linkInfo['type'] !== 'internal')
        {
            $router = \XF::app()->router('public');
            $this->internalRenderedLink = true;
            try
            {
                return \XF::phrase('tckHideLinksFromGuest_you_must_x_or_y_to_view_this_link', [
                    'register_link' => $this->getRenderedLink(\XF::phrase('tckHideLinksFromGuest_register'), $router->buildLink('register'), []),
                    'login_link' => $this->getRenderedLink(\XF::phrase('tckHideLinksFromGuest_login'), $router->buildLink('login'), [])
                ])->render('raw');
            }
            finally
            {
                $this->internalRenderedLink = false;
            }
        }

        return parent::getRenderedLink($text, $url, $options);
    }
}
 
I think you'd have to parse the URL to get this information:

PHP:
$host = parse_url($url, PHP_URL_HOST);

This will give the full host, including any subdomains. To match sub-domains too you'd need to use a regular expression:

PHP:
if (preg_match('#(^|\.)google\.com$#i', $host)) {
    // ...
}
 
Top Bottom