Issues (257)

src/variables/SeomaticVariable.php (3 issues)

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\variables;
13
14
use Craft;
15
use craft\helpers\App;
16
use craft\helpers\UrlHelper;
17
use nystudio107\pluginvite\services\VitePluginService;
18
use nystudio107\seomatic\helpers\Environment as EnvironmentHelper;
19
use nystudio107\seomatic\models\MetaGlobalVars;
20
use nystudio107\seomatic\models\MetaSitemapVars;
21
use nystudio107\seomatic\models\MetaSiteVars;
22
use nystudio107\seomatic\models\Settings;
23
use nystudio107\seomatic\Seomatic;
24
use nystudio107\seomatic\services\Helper;
25
use nystudio107\seomatic\services\JsonLd;
26
use nystudio107\seomatic\services\Link;
27
use nystudio107\seomatic\services\MetaBundles;
28
use nystudio107\seomatic\services\MetaContainers;
29
use nystudio107\seomatic\services\Script;
30
use nystudio107\seomatic\services\Tag;
31
use nystudio107\seomatic\services\Title;
32
use yii\di\ServiceLocator;
33
34
/**
35
 * Seomatic defines the `seomatic` global template variable.
36
 *
37
 * @property Helper $helper
38
 * @property JsonLd $jsonLd
39
 * @property Link $link
40
 * @property Script $script
41
 * @property Tag $tag
42
 * @property Title $title
43
 * @property VitePluginService $vite
44
 *
45
 * @author    nystudio107
46
 * @package   Seomatic
47
 * @since     3.0.0
48
 */
49
class SeomaticVariable extends ServiceLocator
50
{
51
    // Properties
52
    // =========================================================================
53
54
    /**
55
     * @var MetaGlobalVars
56
     */
57
    public $meta;
58
59
    /**
60
     * @var MetaSiteVars
61
     */
62
    public $site;
63
64
    /**
65
     * @var MetaSitemapVars
66
     */
67
    public $sitemap;
68
69
    /**
70
     * @var Settings
71
     */
72
    public $config;
73
74
    /**
75
     * @var MetaContainers
76
     */
77
    public $containers;
78
79
    /**
80
     * @var MetaBundles
81
     */
82
    public $bundles;
83
84
    // Public Methods
85
    // =========================================================================
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function __construct($config = [])
91
    {
92
        /** @noinspection PhpDeprecationInspection */
93
        $components = [
94
            'helper' => Helper::class,
95
            'jsonLd' => JsonLd::class,
96
            'link' => Link::class,
97
            'script' => Script::class,
98
            'tag' => Tag::class,
99
            'title' => Title::class,
100
            // Register the vite service
101
            'vite' => Seomatic::$plugin->getVite(),
0 ignored issues
show
The method getVite() does not exist on null. ( Ignorable by Annotation )

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

101
            'vite' => Seomatic::$plugin->/** @scrutinizer ignore-call */ getVite(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        ];
103
104
        $config['components'] = $components;
105
106
        parent::__construct($config);
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public function init()
113
    {
114
        parent::init();
115
116
        $replaceVars = Seomatic::$loadingMetaContainers || Seomatic::$previewingMetaContainers;
117
        if ($this->meta === null || $replaceVars) {
118
            $this->meta = Seomatic::$plugin->metaContainers->metaGlobalVars;
119
        }
120
        if ($this->site === null || $replaceVars) {
121
            $this->site = Seomatic::$plugin->metaContainers->metaSiteVars;
122
        }
123
        if ($this->sitemap === null || $replaceVars) {
124
            $this->sitemap = Seomatic::$plugin->metaContainers->metaSitemapVars;
125
        }
126
        if ($this->containers === null || $replaceVars) {
127
            $this->containers = Seomatic::$plugin->metaContainers;
128
        }
129
        if ($this->bundles === null || $replaceVars) {
130
            $this->bundles = Seomatic::$plugin->metaBundles;
131
        }
132
        // Set the config settings, parsing the environment if its a frontend request
133
        $configSettings = Seomatic::$settings;
134
        if (!$replaceVars && !Craft::$app->getRequest()->getIsCpRequest()) {
135
            $configSettings->environment = Seomatic::$environment;
136
        }
137
        $this->config = $configSettings;
138
    }
139
140
    /**
141
     * Get the plugin's name
142
     *
143
     * @return null|string
144
     */
145
    public function getPluginName()
146
    {
147
        return Seomatic::$plugin->name;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getEnvironment()
154
    {
155
        return Craft::parseEnv(Seomatic::$environment);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

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

155
        return /** @scrutinizer ignore-deprecated */ Craft::parseEnv(Seomatic::$environment);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getEnvironmentMessage()
162
    {
163
        $result = '';
164
        /** @var Settings $settings */
165
        $settings = Seomatic::$plugin->getSettings();
166
        $settingsEnv = $settings->environment;
167
        $settingsEnv = Craft::parseEnv($settingsEnv);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

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

167
        $settingsEnv = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($settingsEnv);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
168
        $env = Seomatic::$environment;
169
        $settingsUrl = UrlHelper::cpUrl('seomatic/plugin');
170
        $general = Craft::$app->getConfig()->getGeneral();
171
        if (!$general->allowAdminChanges) {
172
            $settingsUrl = '';
173
        }
174
        // If they've manually overridden the environment, just return it
175
        if (EnvironmentHelper::environmentOverriddenByConfig()) {
176
            return Craft::t('seomatic', 'This is overridden by the `config/seomatic.php` config setting',
177
                []
178
            );
179
        }
180
        // If they have manually set the environment, just return it
181
        if (Seomatic::$settings->manuallySetEnvironment) {
182
            return Craft::t('seomatic', 'This is set manually by the "Manually Set SEOmatic Environment" plugin setting',
183
                []
184
            );
185
        }
186
        // If devMode is on, always force the environment to be 'local'
187
        if (Seomatic::$devMode) {
188
            return Craft::t('seomatic', 'The `{settingsEnv}` [SEOmatic Environment]({settingsUrl}) setting has been overriden to `{env}`, because the `devMode` config setting is enabled. Tracking scripts are disabled, and the `robots` tag is set to `none` to prevent search engine indexing.',
189
                ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl]
190
            );
191
        }
192
        // This can change depending on the user settings
193
        /** @phpstan-ignore-next-line */
194
        if (Seomatic::$settings->manuallySetEnvironment) {
195
            $envVar = $settingsEnv;
196
        } else {
197
            // Try to also check the `ENVIRONMENT` env var
198
            $envVar = App::env('ENVIRONMENT');
199
            $envVar = $envVar ?: App::env('CRAFT_ENVIRONMENT');
200
        }
201
        if (!empty($envVar)) {
202
            $env = EnvironmentHelper::determineEnvironment();
203
            switch ($env) {
204
                case EnvironmentHelper::SEOMATIC_DEV_ENV:
205
                    $additionalMessage = 'Tracking scripts are disabled, and the `robots` tag is set to `none` to prevent search engine indexing.';
206
                    break;
207
                case EnvironmentHelper::SEOMATIC_STAGING_ENV:
208
                    $additionalMessage = 'The `robots` tag is set to `none` to prevent search engine indexing.';
209
                    break;
210
                case EnvironmentHelper::SEOMATIC_PRODUCTION_ENV:
211
                    $additionalMessage = '';
212
                    break;
213
                default:
214
                    $additionalMessage = '';
215
                    break;
216
            }
217
            if ($settings->environment !== $env) {
218
                return Craft::t('seomatic', 'The `{settingsEnv}` [SEOmatic Environment]({settingsUrl}) setting has been overriden to `{env}`, because the `.env` setting `ENVIRONMENT` is set to `{envVar}`. {additionalMessage}',
219
                    ['env' => $env, 'settingsEnv' => $settingsEnv, 'settingsUrl' => $settingsUrl, 'envVar' => $envVar, 'additionalMessage' => $additionalMessage]
220
                );
221
            }
222
        }
223
224
        return $result;
225
    }
226
}
227