Issues (57)

src/helpers/SocialHelper.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/social/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://dukt.net/craft/social/docs/license
6
 */
7
8
namespace dukt\social\helpers;
9
10
use Craft;
11
use craft\elements\User;
12
use craft\helpers\UrlHelper;
13
14
/**
15
 * Class SocialHelper
16
 *
17
 * @author Dukt <[email protected]>
18
 * @since  1.0
19
 */
20
class SocialHelper
21
{
22
    // Public Methods
23
    // =========================================================================
24
25
    /**
26
     * Returns a site action URL.
27
     *
28
     * @param string $path
29
     * @param array|string|null $params
30
     * @param string|null $protocol The protocol to use (e.g. http, https). If empty, the protocol used for the current
31
     *                                    request will be used.
32
     *
33
     * @return string
34
     */
35
    public static function siteActionUrl(string $path = '', $params = null, string $protocol = null): string
36
    {
37
        // Force `addTrailingSlashesToUrls` to `false` while we generate the redirectUri
38
        $addTrailingSlashesToUrls = Craft::$app->getConfig()->getGeneral()->addTrailingSlashesToUrls;
39
        Craft::$app->getConfig()->getGeneral()->addTrailingSlashesToUrls = false;
40
41
        $redirectUri = UrlHelper::actionUrl($path, $params, $protocol);
42
43
        // Set `addTrailingSlashesToUrls` back to its original value
44
        Craft::$app->getConfig()->getGeneral()->addTrailingSlashesToUrls = $addTrailingSlashesToUrls;
45
46
        // We don't want the CP trigger showing in the action URL.
47
        $redirectUri = str_replace(Craft::$app->getConfig()->getGeneral()->cpTrigger . '/', '', $redirectUri);
48
49
        return $redirectUri;
50
    }
51
52
    /**
53
     * @param User $newUser
54
     * @param      $attribute
55
     * @param      $template
56
     * @param      $profile
57
     */
58
    public static function fillUserAttribute(User $newUser, $attribute, $template, $profile)
59
    {
60
        if (array_key_exists($attribute, $newUser->getAttributes())) {
61
            try {
62
                $newUser->{$attribute} = Craft::$app->getView()->renderString($template, ['profile' => $profile]);
63
            } catch (\Exception $exception) {
64
                Craft::warning('Could not map:' . print_r([$attribute, $template, $profile, $exception->getMessage()], true), __METHOD__);
0 ignored issues
show
Are you sure print_r(array($attribute...n->getMessage()), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                Craft::warning('Could not map:' . /** @scrutinizer ignore-type */ print_r([$attribute, $template, $profile, $exception->getMessage()], true), __METHOD__);
Loading history...
65
            }
66
        }
67
    }
68
69
    /**
70
     * @param User $newUser
71
     * @param       $attribute
72
     * @param       $template
73
     * @param       $profile
74
     */
75
    public static function fillUserCustomFieldValue(User $newUser, $attribute, $template, $profile)
76
    {
77
        // Check to make sure custom field exists for user profile
78
        if (isset($newUser->{$attribute})) {
79
            try {
80
                $value = Craft::$app->getView()->renderString($template, ['profile' => $profile]);
81
                $newUser->setFieldValue($attribute, $value);
82
            } catch (\Exception $exception) {
83
                Craft::warning('Could not map:' . print_r([$template, $profile, $exception->getMessage()], true), __METHOD__);
0 ignored issues
show
Are you sure print_r(array($template,...n->getMessage()), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
                Craft::warning('Could not map:' . /** @scrutinizer ignore-type */ print_r([$template, $profile, $exception->getMessage()], true), __METHOD__);
Loading history...
84
            }
85
        }
86
    }
87
}
88