SeomaticVariable::getPluginName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
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\UrlHelper;
16
use nystudio107\pluginvite\services\VitePluginService;
17
use nystudio107\seomatic\helpers\Environment as EnvironmentHelper;
18
use nystudio107\seomatic\models\MetaGlobalVars;
19
use nystudio107\seomatic\models\MetaSitemapVars;
20
use nystudio107\seomatic\models\MetaSiteVars;
21
use nystudio107\seomatic\models\Settings;
22
use nystudio107\seomatic\Seomatic;
23
use nystudio107\seomatic\services\Helper;
24
use nystudio107\seomatic\services\JsonLd;
25
use nystudio107\seomatic\services\Link;
26
use nystudio107\seomatic\services\MetaBundles;
27
use nystudio107\seomatic\services\MetaContainers;
28
use nystudio107\seomatic\services\Script;
29
use nystudio107\seomatic\services\Tag;
30
use nystudio107\seomatic\services\Title;
31
use yii\di\ServiceLocator;
32
33
/**
34
 * Seomatic defines the `seomatic` global template variable.
35
 *
36
 * @property Helper $helper
37
 * @property JsonLd $jsonLd
38
 * @property Link $link
39
 * @property Script $script
40
 * @property Tag $tag
41
 * @property Title $title
42
 * @property VitePluginService $vite
43
 *
44
 * @author    nystudio107
45
 * @package   Seomatic
46
 * @since     3.0.0
47
 */
48
class SeomaticVariable extends ServiceLocator
49
{
50
    // Properties
51
    // =========================================================================
52
53
    /**
54
     * @var MetaGlobalVars
55
     */
56
    public $meta;
57
58
    /**
59
     * @var MetaSiteVars
60
     */
61
    public $site;
62
63
    /**
64
     * @var MetaSitemapVars
65
     */
66
    public $sitemap;
67
68
    /**
69
     * @var Settings
70
     */
71
    public $config;
72
73
    /**
74
     * @var MetaContainers
75
     */
76
    public $containers;
77
78
    /**
79
     * @var MetaBundles
80
     */
81
    public $bundles;
82
83
    // Public Methods
84
    // =========================================================================
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function __construct($config = [])
90
    {
91
        /** @noinspection PhpDeprecationInspection */
92
        $components = [
93
            'helper' => Helper::class,
94
            'jsonLd' => JsonLd::class,
95
            'link' => Link::class,
96
            'script' => Script::class,
97
            'tag' => Tag::class,
98
            'title' => Title::class,
99
            // Register the vite service
100
            'vite' => Seomatic::$plugin->getVite(),
101
        ];
102
103
        $config['components'] = $components;
104
105
        parent::__construct($config);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function init()
112
    {
113
        parent::init();
114
115
        $replaceVars = Seomatic::$loadingMetaContainers || Seomatic::$previewingMetaContainers;
116
        if ($this->meta === null || $replaceVars) {
117
            $this->meta = Seomatic::$plugin->metaContainers->metaGlobalVars;
118
        }
119
        if ($this->site === null || $replaceVars) {
120
            $this->site = Seomatic::$plugin->metaContainers->metaSiteVars;
121
        }
122
        if ($this->sitemap === null || $replaceVars) {
123
            $this->sitemap = Seomatic::$plugin->metaContainers->metaSitemapVars;
124
        }
125
        if ($this->containers === null || $replaceVars) {
126
            $this->containers = Seomatic::$plugin->metaContainers;
127
        }
128
        if ($this->bundles === null || $replaceVars) {
129
            $this->bundles = Seomatic::$plugin->metaBundles;
130
        }
131
        // Set the config settings, parsing the environment if its a frontend request
132
        $configSettings = Seomatic::$settings;
133
        if (!$replaceVars && !Craft::$app->getRequest()->getIsCpRequest()) {
134
            $configSettings->environment = Seomatic::$environment;
135
        }
136
        $this->config = $configSettings;
137
    }
138
139
    /**
140
     * Get the plugin's name
141
     *
142
     * @return null|string
143
     */
144
    public function getPluginName()
145
    {
146
        return Seomatic::$plugin->name;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getEnvironment()
153
    {
154
        return Craft::parseEnv(Seomatic::$environment);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::parseEnv(n...\Seomatic::environment) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
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

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