Issues (231)

src/models/Settings.php (1 issue)

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\models;
13
14
use craft\behaviors\EnvAttributeParserBehavior;
15
use craft\validators\ArrayValidator;
16
use nystudio107\seomatic\base\SeoElementInterface;
17
use nystudio107\seomatic\base\VarsModel;
18
use nystudio107\seomatic\Seomatic;
19
use yii\behaviors\AttributeTypecastBehavior;
20
21
/**
22
 * Plugin settings object, containing values for configuring the plugin
23
 *
24
 * @author    nystudio107
25
 * @package   Seomatic
26
 * @since     3.0.0
27
 */
28
class Settings extends VarsModel
29
{
30
    // Public Properties
31
    // =========================================================================
32
33
    /**
34
     * @var string The public-facing name of the plugin
35
     */
36
    public $pluginName = 'SEOmatic';
37
38
    /**
39
     * @var bool Should SEOmatic render metadata?
40
     */
41
    public $renderEnabled = true;
42
43
    /**
44
     * @var bool Should SEOmatic render frontend sitemaps?
45
     */
46
    public $sitemapsEnabled = true;
47
48
    /**
49
     * @deprecated This setting is no longer relevant with the paginated sitemap generation
50
     * @var bool Should sitemaps be regenerated automatically?
51
     */
52
    public $regenerateSitemapsAutomatically = true;
53
54
    /**
55
     * @var bool Should sitemaps be submitted to search engines automatically whenever there are changes?
56
     */
57
    public $submitSitemaps = true;
58
59
    /**
60
     * @var bool Should items where the entry URL doesn't match the canonical URL be excluded?
61
     */
62
    public $excludeNonCanonicalUrls = false;
63
64
    /**
65
     * @var bool Should the homepage be included in the generated Breadcrumbs JSON-LD?
66
     */
67
    public $includeHomepageInBreadcrumbs = true;
68
69
    /**
70
     * @var bool Should SEOmatic add to the http response headers?
71
     */
72
    public $headersEnabled = true;
73
74
    /**
75
     * @var bool Whether the environment should be manually set, or automatically determined
76
     */
77
    public $manuallySetEnvironment = false;
78
79
    /**
80
     * @var string The server environment, either `live`, `staging`, or `local`
81
     */
82
    public $environment = 'live';
83
84
    /**
85
     * @var bool Should SEOmatic display the SEO Preview sidebar?
86
     */
87
    public $displayPreviewSidebar = true;
88
89
    /**
90
     * @var bool Should SEOmatic add a Social Media Preview Target?
91
     */
92
    public $socialMediaPreviewTarget = true;
93
94
    /**
95
     * @var array The social media platforms that should be displayed in the SEO Preview sidebar
96
     */
97
    public $sidebarDisplayPreviewTypes = [
98
        'google',
99
        'twitter',
100
        'facebook',
101
    ];
102
103
    /**
104
     * @var bool Should SEOmatic display the SEO Analysis sidebar?
105
     */
106
    public $displayAnalysisSidebar = true;
107
108
    /**
109
     * @var string If `devMode` is on, prefix the <title> with this string
110
     */
111
    public $devModeTitlePrefix = '&#x1f6a7; ';
112
113
    /**
114
     * @var string Prefix the Control Panel <title> with this string
115
     */
116
    public $cpTitlePrefix = '&#x2699; ';
117
118
    /**
119
     * @var string If `devMode` is on, prefix the Control Panel <title> with this string
120
     */
121
    public $devModeCpTitlePrefix = '&#x1f6a7;&#x2699; ';
122
123
    /**
124
     * @var string The separator character to use for the `<title>` tag
125
     */
126
    public $separatorChar = '|';
127
128
    /**
129
     * @var int The max number of characters in the `<title>` tag
130
     */
131
    public $maxTitleLength = 70;
132
133
    /**
134
     * @var int The max number of characters in the `<meta name="description">` tag
135
     */
136
    public $maxDescriptionLength = 155;
137
138
    /**
139
     * @var bool Should Title tags be truncated at the max length, on word boundaries?
140
     */
141
    public $truncateTitleTags = true;
142
143
    /**
144
     * @var bool Should Description tags be truncated at the max length, on word boundaries?
145
     */
146
    public $truncateDescriptionTags = true;
147
148
    /**
149
     * @var bool Site Groups define logically separate sites
150
     */
151
    public $siteGroupsSeparate = true;
152
153
    /**
154
     * @var bool Whether to dynamically include the hreflang tags
155
     */
156
    public $addHrefLang = true;
157
158
    /**
159
     * @var bool Whether to dynamically include the `x-default` hreflang tags
160
     */
161
    public $addXDefaultHrefLang = true;
162
163
    /**
164
     * @var int The site to use for the `x-default` hreflang tag (0 defaults to the Primary site)
165
     */
166
    public $xDefaultSite = 0;
167
168
    /**
169
     * @var bool Whether to dynamically include hreflang tags on paginated pages
170
     */
171
    public $addPaginatedHreflang = true;
172
173
    /**
174
     * @var string Whether to include a Content Security Policy "nonce" for inline
175
     *      CSS or JavaScript. Valid values are 'header' or 'tag' for how the CSP
176
     *      should be included. c.f.:
177
     *      https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script
178
     */
179
    public $cspNonce = '';
180
181
    /**
182
     * @var array Fixed Content Security Policies to be added before any CSP nonces
183
     */
184
    public $cspScriptSrcPolicies = [
185
        0 => [
186
            'policy' => "'self'",
187
        ],
188
    ];
189
190
    /**
191
     * @var bool
192
     * SEO [best practices](https://www.searchenginejournal.com/google-dont-mix-noindex-relcanonical/262607)
193
     * are to have `canonical` links not appear on pages that are not intended to be indexed. SEOmatic does
194
     * this for you by default, but you can override that behavior with this setting
195
     */
196
    public $alwaysIncludeCanonicalUrls = false;
197
198
    /**
199
     * @var bool Should the Canonical URL be automatically lower-cased?
200
     */
201
    public $lowercaseCanonicalUrl = true;
202
203
    /**
204
     * @var bool Should the meta generator tag and X-Powered-By header be included?
205
     */
206
    public $generatorEnabled = true;
207
208
    /**
209
     * @var string|array
210
     * SEOmatic uses the Craft `siteUrl` to generate the external URLs.  If you
211
     * are using it in a non-standard environment, such as a headless GraphQL or
212
     * ElementAPI server, you can override what it uses for the `siteUrl` below.
213
     * This can be either a simple string, or an array of strings indexed by the site
214
     * handle, for multi-site setups. e.g.:
215
     * 'siteUrlOverride' => [
216
     *     'default' => 'http://example.com/',
217
     *     'spanish' => 'http://example.com/es/',
218
     * ],     */
219
    public $siteUrlOverride = '';
220
221
    /**
222
     * @var int|string|null
223
     * The duration of the SEOmatic meta cache in seconds.  Null means always cached until explicitly broken
224
     * If devMode is on, caches last 30 seconds.
225
     */
226
    public $metaCacheDuration = 0;
227
228
    /**
229
     * @var bool Determines whether the meta container endpoint should be enabled for anonymous frontend access
230
     */
231
    public $enableMetaContainerEndpoint = false;
232
233
    /**
234
     * @var bool Determines whether the JSON-LD endpoint should be enabled for anonymous frontend access
235
     */
236
    public $enableJsonLdEndpoint = false;
237
238
    /**
239
     * @var bool Determines whether the SEO File Link endpoint should be enabled for anonymous frontend access
240
     */
241
    public $enableSeoFileLinkEndpoint = false;
242
243
    /**
244
     * @var bool Determines whether the SEOmatic debug toolbar panel should be added to the Yii2 debug toolbar
245
     */
246
    public $enableDebugToolbarPanel = true;
247
248
    /**
249
     * @var SeoElementInterface[] The default SeoElement type classes
250
     */
251
    public $defaultSeoElementTypes = [
252
    ];
253
254
    /**
255
     * @var string[] URL params that are allowed to be considered part of the unique URL used for the metadata cache
256
     */
257
    public $allowedUrlParams = [
258
    ];
259
260
    /**
261
     * @var class-string[] Array of TwigExtension classes to instantiate and add to the SandboxView
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string[].
Loading history...
262
     */
263
    public $twigExtensionClasses = [
264
    ];
265
266
    // Public Methods
267
    // =========================================================================
268
269
    /**
270
     * @inerhitdoc
271
     */
272
    public function init()
273
    {
274
        // Normalize the metaCacheDuration to an integer
275
        if ($this->metaCacheDuration === null || $this->metaCacheDuration === 'null') {
276
            $this->metaCacheDuration = 0;
277
        }
278
    }
279
280
    /**
281
     * @inheritdoc
282
     */
283
    public function rules(): array
284
    {
285
        return [
286
            ['pluginName', 'string'],
287
            ['pluginName', 'default', 'value' => 'SEOmatic'],
288
            [
289
                [
290
                    'renderEnabled',
291
                    'sitemapsEnabled',
292
                    'regenerateSitemapsAutomatically',
293
                    'submitSitemaps',
294
                    'excludeNonCanonicalUrls',
295
                    'includeHomepageInBreadcrumbs',
296
                    'headersEnabled',
297
                    'generatorEnabled',
298
                    'addHrefLang',
299
                    'addXDefaultHrefLang',
300
                    'addPaginatedHreflang',
301
                    'manuallySetEnvironment',
302
                ],
303
                'boolean',
304
            ],
305
            ['xDefaultSite', 'integer'],
306
            ['xDefaultSite', 'default', 'value' => 0],
307
            ['cspNonce', 'string'],
308
            ['cspNonce', 'in', 'range' => [
309
                '',
310
                'header',
311
                'tag',
312
            ]],
313
            ['environment', 'string'],
314
            ['environment', 'default', 'value' => 'live'],
315
            [
316
                [
317
                    'displayPreviewSidebar',
318
                    'socialMediaPreviewTarget',
319
                    'displayAnalysisSidebar',
320
                    'enableMetaContainerEndpoint',
321
                    'enableJsonLdEndpoint',
322
                    'enableSeoFileLinkEndpoint',
323
                    'alwaysIncludeCanonicalUrls',
324
                    'lowercaseCanonicalUrl',
325
                    'enableDebugToolbarPanel',
326
                ],
327
                'boolean',
328
            ],
329
            [['devModeTitlePrefix', 'cpTitlePrefix', 'devModeCpTitlePrefix', 'truncateTitleTags', 'truncateDescriptionTags'], 'string'],
330
            ['separatorChar', 'string'],
331
            ['separatorChar', 'default', 'value' => '|'],
332
            ['maxTitleLength', 'integer', 'min' => 10],
333
            ['maxTitleLength', 'default', 'value' => 70],
334
            ['maxDescriptionLength', 'integer', 'min' => 10],
335
            ['maxDescriptionLength', 'default', 'value' => 155],
336
            ['siteUrlOverride', 'safe'],
337
            ['siteUrlOverride', 'default', 'value' => ''],
338
            [
339
                [
340
                    'sidebarDisplayPreviewTypes',
341
                    'defaultSeoElementTypes',
342
                    'cspScriptSrcPolicies',
343
                    'allowedUrlParams',
344
                    'twigExtensionClasses',
345
                ],
346
                ArrayValidator::class,
347
            ],
348
            ['metaCacheDuration', 'default', 'value' => 0],
349
            ['metaCacheDuration', 'integer'],
350
        ];
351
    }
352
353
    /**
354
     * @inheritdoc
355
     */
356
    public function behaviors()
357
    {
358
        // Keep any parent behaviors
359
        $behaviors = parent::behaviors();
360
        // Add in the AttributeTypecastBehavior
361
        $behaviors['typecast'] = [
362
            'class' => AttributeTypecastBehavior::class,
363
            // 'attributeTypes' will be composed automatically according to `rules()`
364
        ];
365
        // If we're running Craft 3.1 or later, add in the EnvAttributeParserBehavior
366
        if (Seomatic::$craft31) {
367
            $behaviors['parser'] = [
368
                'class' => EnvAttributeParserBehavior::class,
369
                'attributes' => [
370
                    'environment',
371
                ],
372
            ];
373
        }
374
375
        return $behaviors;
376
    }
377
}
378