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\services; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Component; |
16
|
|
|
use craft\elements\Asset; |
17
|
|
|
use craft\elements\db\MatrixBlockQuery; |
18
|
|
|
use craft\elements\db\TagQuery; |
19
|
|
|
use craft\helpers\Template; |
20
|
|
|
use craft\web\twig\variables\Paginate; |
21
|
|
|
use nystudio107\seomatic\base\InheritableSettingsModel; |
22
|
|
|
use nystudio107\seomatic\helpers\DynamicMeta as DynamicMetaHelper; |
23
|
|
|
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper; |
24
|
|
|
use nystudio107\seomatic\helpers\Schema as SchemaHelper; |
25
|
|
|
use nystudio107\seomatic\helpers\Text as TextHelper; |
26
|
|
|
use nystudio107\seomatic\helpers\UrlHelper; |
27
|
|
|
use nystudio107\seomatic\models\MetaBundle; |
28
|
|
|
use nystudio107\seomatic\Seomatic; |
29
|
|
|
use yii\base\Exception; |
30
|
|
|
use yii\base\InvalidConfigException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @author nystudio107 |
|
|
|
|
34
|
|
|
* @package Seomatic |
35
|
|
|
* @since 3.0.0 |
36
|
|
|
*/ |
37
|
|
|
class Helper extends Component |
38
|
|
|
{ |
39
|
|
|
// Constants |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
const TWITTER_TRANSFORM_MAP = [ |
43
|
|
|
'summary' => 'twitter-summary', |
44
|
|
|
'summary_large_image' => 'twitter-large', |
45
|
|
|
'app' => 'twitter-large', |
46
|
|
|
'player' => 'twitter-large', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
// Public Methods |
50
|
|
|
// ========================================================================= |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sanitize user input by decoding any HTML Entities, URL decoding the text, |
54
|
|
|
* then removing any newlines, stripping tags, stripping Twig tags, and changing |
55
|
|
|
* single {}'s into ()'s |
56
|
|
|
* |
57
|
|
|
* @param $str |
|
|
|
|
58
|
|
|
* @return string |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public static function sanitizeUserInput($str): string |
61
|
|
|
{ |
62
|
|
|
return TextHelper::sanitizeUserInput($str); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return the appropriate Twitter Transform based on the current $metaGlobalVars->twitterCard |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public static function twitterTransform(): string |
71
|
|
|
{ |
72
|
|
|
$transform = 'twitter-summary'; |
73
|
|
|
$metaGlobalVars = Seomatic::$plugin->metaContainers->metaGlobalVars; |
74
|
|
|
if ($metaGlobalVars) { |
|
|
|
|
75
|
|
|
$transform = self::TWITTER_TRANSFORM_MAP[$metaGlobalVars->twitterCard] ?? $transform; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $transform; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the proper content for the `query-input` JSON-LD property |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function siteLinksQueryInput(): string |
87
|
|
|
{ |
88
|
|
|
$result = ''; |
89
|
|
|
|
90
|
|
|
$metaSiteVars = Seomatic::$plugin->metaContainers->metaSiteVars; |
91
|
|
|
if ($metaSiteVars && !empty($metaSiteVars->siteLinksQueryInput)) { |
92
|
|
|
$result = 'required name='.$metaSiteVars->siteLinksQueryInput; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return whether this is a preview request of any kind |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public static function isPreview(): bool |
104
|
|
|
{ |
105
|
|
|
$isPreview = false; |
106
|
|
|
$request = Craft::$app->getRequest(); |
107
|
|
|
if (Seomatic::$craft32) { |
108
|
|
|
$isPreview = $request->getIsPreview(); |
109
|
|
|
} |
110
|
|
|
$isLivePreview = $request->getIsLivePreview(); |
111
|
|
|
|
112
|
|
|
return ($isPreview || $isLivePreview); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return the Same As Links info as an array or null |
117
|
|
|
* |
118
|
|
|
* @param string $handle |
|
|
|
|
119
|
|
|
* @return array|null |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public static function sameAsByHandle(string $handle) { |
|
|
|
|
122
|
|
|
$result = null; |
123
|
|
|
|
124
|
|
|
$sameAs = Seomatic::$plugin->metaContainers->metaSiteVars->sameAsLinks; |
125
|
|
|
if (!empty($sameAs) && !empty($handle)) { |
126
|
|
|
foreach ($sameAs as $sameAsInfo) { |
127
|
|
|
if (!empty($sameAsInfo) && is_array($sameAsInfo) && !empty($sameAsInfo['handle'])) { |
128
|
|
|
if ($sameAsInfo['handle'] === $handle) { |
129
|
|
|
return $sameAsInfo; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return the canonical URL for the request, with the query string stripped |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public static function safeCanonicalUrl(): string |
144
|
|
|
{ |
145
|
|
|
$url = ''; |
|
|
|
|
146
|
|
|
try { |
147
|
|
|
$url = Craft::$app->getRequest()->getPathInfo(); |
148
|
|
|
} catch (InvalidConfigException $e) { |
149
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
150
|
|
|
} |
151
|
|
|
$url = DynamicMetaHelper::sanitizeUrl($url); |
152
|
|
|
|
153
|
|
|
return UrlHelper::absoluteUrlWithProtocol($url); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return the site URL for a given URL. This gives SEOmatic a chance to override it |
158
|
|
|
* |
159
|
|
|
* @param string $path |
|
|
|
|
160
|
|
|
* @param array|string|null $params |
|
|
|
|
161
|
|
|
* @param string|null $scheme |
|
|
|
|
162
|
|
|
* @param int|null $siteId |
|
|
|
|
163
|
|
|
* @return string |
|
|
|
|
164
|
|
|
* @throws Exception if|null $siteId is invalid |
|
|
|
|
165
|
|
|
*/ |
166
|
|
|
public static function siteUrl(string $path = '', $params = null, string $scheme = null, int $siteId = null): string |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
return UrlHelper::siteUrl($path); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Paginate based on the passed in Paginate variable as returned from the |
173
|
|
|
* Twig {% paginate %} tag: |
174
|
|
|
* https://docs.craftcms.com/v3/templating/tags/paginate.html#the-pageInfo-variable |
175
|
|
|
* |
176
|
|
|
* @param Paginate $pageInfo |
|
|
|
|
177
|
|
|
*/ |
178
|
|
|
public static function paginate(Paginate $pageInfo) |
179
|
|
|
{ |
180
|
|
|
DynamicMetaHelper::paginate($pageInfo); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Truncates the string to a given length. If $substring is provided, and |
185
|
|
|
* truncating occurs, the string is further truncated so that the substring |
186
|
|
|
* may be appended without exceeding the desired length. |
187
|
|
|
* |
188
|
|
|
* @param string $string The string to truncate |
189
|
|
|
* @param int $length Desired length of the truncated string |
190
|
|
|
* @param string $substring The substring to append if it can fit |
191
|
|
|
* |
192
|
|
|
* @return string with the resulting $str after truncating |
193
|
|
|
*/ |
194
|
|
|
public static function truncate($string, $length, $substring = '…'): string |
195
|
|
|
{ |
196
|
|
|
return TextHelper::truncate($string, $length, $substring); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Truncates the string to a given length, while ensuring that it does not |
201
|
|
|
* split words. If $substring is provided, and truncating occurs, the |
202
|
|
|
* string is further truncated so that the substring may be appended without |
203
|
|
|
* exceeding the desired length. |
204
|
|
|
* |
205
|
|
|
* @param string $string The string to truncate |
206
|
|
|
* @param int $length Desired length of the truncated string |
207
|
|
|
* @param string $substring The substring to append if it can fit |
208
|
|
|
* |
209
|
|
|
* @return string with the resulting $str after truncating |
210
|
|
|
*/ |
211
|
|
|
public static function truncateOnWord($string, $length, $substring = '…'): string |
212
|
|
|
{ |
213
|
|
|
return TextHelper::truncateOnWord($string, $length, $substring); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return a list of localized URLs that are in the current site's group |
218
|
|
|
* The current URI is used if $uri is null. Similarly, the current site is |
219
|
|
|
* used if $siteId is null. |
220
|
|
|
* The resulting array of arrays has `id`, `language`, `ogLanguage`, |
221
|
|
|
* `hreflangLanguage`, and `url` as keys. |
222
|
|
|
* |
223
|
|
|
* @param string|null $uri |
|
|
|
|
224
|
|
|
* @param int|null $siteId |
|
|
|
|
225
|
|
|
* |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public static function getLocalizedUrls(string $uri = null, int $siteId = null): array |
229
|
|
|
{ |
230
|
|
|
return DynamicMetaHelper::getLocalizedUrls($uri, $siteId); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Allow setting the X-Robots-Tag and Link headers on static files as per: |
235
|
|
|
* https://moz.com/blog/how-to-advanced-relcanonical-http-headers |
236
|
|
|
* |
237
|
|
|
* @param $url |
|
|
|
|
238
|
|
|
* @param string $robots |
|
|
|
|
239
|
|
|
* @param string $canonical |
|
|
|
|
240
|
|
|
* @param bool $inline |
|
|
|
|
241
|
|
|
* |
242
|
|
|
* @return \Twig\Markup |
243
|
|
|
* @throws \yii\base\Exception |
244
|
|
|
*/ |
245
|
|
|
public static function seoFileLink($url, $robots = '', $canonical = '', $inline = true): \Twig\Markup |
246
|
|
|
{ |
247
|
|
|
// Get the file name |
248
|
|
|
$path = parse_url($url, PHP_URL_PATH); |
249
|
|
|
$fileName = pathinfo($path, PATHINFO_BASENAME); |
250
|
|
|
// Set some defaults |
251
|
|
|
$robots = empty($robots) ? 'all' : $robots; |
252
|
|
|
$canonical = empty($canonical) ? $url : $canonical; |
253
|
|
|
$inlineStr = $inline === true ? '1' : '0'; |
254
|
|
|
// Compose the base64 encoded URL |
255
|
|
|
$seoFileLink = 'seomatic/seo-file-link/' |
256
|
|
|
.base64_encode($url) |
257
|
|
|
.'/' |
258
|
|
|
.base64_encode($robots) |
259
|
|
|
.'/' |
260
|
|
|
.base64_encode($canonical) |
261
|
|
|
.'/' |
262
|
|
|
.$inlineStr |
263
|
|
|
.'/' |
264
|
|
|
.$fileName; |
|
|
|
|
265
|
|
|
|
266
|
|
|
return Template::raw(UrlHelper::siteUrl($seoFileLink)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Load the appropriate meta containers for the given $uri and optional |
271
|
|
|
* $siteId |
272
|
|
|
* |
273
|
|
|
* @param string $uri |
|
|
|
|
274
|
|
|
* @param int|null $siteId |
|
|
|
|
275
|
|
|
*/ |
276
|
|
|
public static function loadMetadataForUri(string $uri = '', int $siteId = null) |
277
|
|
|
{ |
278
|
|
|
Seomatic::$plugin->metaContainers->loadMetaContainers($uri, $siteId); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the URL to the $siteId's sitemap index |
283
|
|
|
* |
284
|
|
|
* @param int|null $siteId |
|
|
|
|
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public static function sitemapIndexForSiteId(int $siteId = null): string |
289
|
|
|
{ |
290
|
|
|
return Seomatic::$plugin->sitemaps->sitemapIndexUrlForSiteId($siteId); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Return a sitemap for each site in the same site group |
295
|
|
|
* |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
public static function sitemapIndex(): string |
299
|
|
|
{ |
300
|
|
|
return Seomatic::$plugin->sitemaps->sitemapIndex(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Return a sitemap for each site in the same site group |
305
|
|
|
* |
306
|
|
|
* @deprecated use sitemapIndex() instead |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
public static function siteGroupSitemaps(): string |
310
|
|
|
{ |
311
|
|
|
return Seomatic::$plugin->sitemaps->sitemapIndex(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $sourceType |
|
|
|
|
316
|
|
|
* @param string $sourceHandle |
|
|
|
|
317
|
|
|
* @param int|null $siteId |
|
|
|
|
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public static function sitemapUrlForBundle(string $sourceType, string $sourceHandle, int $siteId = null): string |
322
|
|
|
{ |
323
|
|
|
return Seomatic::$plugin->sitemaps->sitemapUrlForBundle($sourceType, $sourceHandle, $siteId); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Extract plain old text from a field |
328
|
|
|
* |
329
|
|
|
* @param $field |
|
|
|
|
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
public static function extractTextFromField($field = null): string |
334
|
|
|
{ |
335
|
|
|
return TextHelper::extractTextFromField($field); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Extract concatenated text from all of the tags in the $tagElement and |
340
|
|
|
* return as a comma-delimited string |
341
|
|
|
* |
342
|
|
|
* @param TagQuery $tagQuery |
|
|
|
|
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public static function extractTextFromTags($tagQuery = null): string |
347
|
|
|
{ |
348
|
|
|
return TextHelper::extractTextFromTags($tagQuery); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Extract text from all of the blocks in a matrix field, concatenating it |
353
|
|
|
* together. |
354
|
|
|
* |
355
|
|
|
* @param MatrixBlockQuery $matrixQuery |
|
|
|
|
356
|
|
|
* @param string $fieldHandle |
|
|
|
|
357
|
|
|
* |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public static function extractTextFromMatrix($matrixQuery = null, $fieldHandle = ''): string |
361
|
|
|
{ |
362
|
|
|
return TextHelper::extractTextFromMatrix($matrixQuery, $fieldHandle); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Return the most important keywords extracted from the text as a comma- |
367
|
|
|
* delimited string |
368
|
|
|
* |
369
|
|
|
* @param string $text |
|
|
|
|
370
|
|
|
* @param int $limit |
|
|
|
|
371
|
|
|
* @param bool $useStopWords |
|
|
|
|
372
|
|
|
* |
373
|
|
|
* @return string |
374
|
|
|
*/ |
375
|
|
|
public static function extractKeywords($text = '', $limit = 15, $useStopWords = true): string |
376
|
|
|
{ |
377
|
|
|
return TextHelper::extractKeywords($text, $limit, $useStopWords); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Extract a summary consisting of the 3 most important sentences from the |
382
|
|
|
* text |
383
|
|
|
* |
384
|
|
|
* @param string $text |
|
|
|
|
385
|
|
|
* @param bool $useStopWords |
|
|
|
|
386
|
|
|
* |
387
|
|
|
* @return string |
388
|
|
|
*/ |
389
|
|
|
public static function extractSummary($text = '', $useStopWords = true): string |
390
|
|
|
{ |
391
|
|
|
return TextHelper::extractSummary($text, $useStopWords); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Return a period-delimited schema.org path from the $settings |
396
|
|
|
* |
397
|
|
|
* @param $settings |
|
|
|
|
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
public static function getEntityPath($settings): string |
402
|
|
|
{ |
403
|
|
|
return SchemaHelper::getEntityPath($settings); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Return a flattened, indented menu of the given $path |
408
|
|
|
* |
409
|
|
|
* @param string $path |
|
|
|
|
410
|
|
|
* |
411
|
|
|
* @return array |
412
|
|
|
*/ |
413
|
|
|
public static function getTypeMenu($path): array |
414
|
|
|
{ |
415
|
|
|
return SchemaHelper::getTypeMenu($path); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Return a single menu of schemas starting at $path |
420
|
|
|
* |
421
|
|
|
* @param string $path |
|
|
|
|
422
|
|
|
* |
423
|
|
|
* @return array |
424
|
|
|
*/ |
425
|
|
|
public static function getSingleTypeMenu($path): array |
426
|
|
|
{ |
427
|
|
|
return SchemaHelper::getSingleTypeMenu($path); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Transform the $asset for social media sites in $transformName and |
432
|
|
|
* optional $siteId |
433
|
|
|
* |
434
|
|
|
* @param int|Asset $asset the Asset or Asset ID |
435
|
|
|
* @param string $transformName the name of the transform to apply |
436
|
|
|
* @param int|null $siteId |
|
|
|
|
437
|
|
|
* @param string $transformMode |
|
|
|
|
438
|
|
|
* |
439
|
|
|
* @return string URL to the transformed image |
440
|
|
|
*/ |
441
|
|
|
public function socialTransform($asset, $transformName = '', $siteId = null, $transformMode = null): string |
442
|
|
|
{ |
443
|
|
|
return ImageTransformHelper::socialTransform($asset, $transformName, $siteId, $transformMode); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Get the width of the transformed social image for $transformName and |
448
|
|
|
* optional $siteId |
449
|
|
|
* |
450
|
|
|
* @param int|Asset $asset the Asset or Asset ID |
451
|
|
|
* @param string $transformName the name of the transform to apply |
452
|
|
|
* @param int|null $siteId |
|
|
|
|
453
|
|
|
* @param string $transformMode |
|
|
|
|
454
|
|
|
* |
455
|
|
|
* @return string URL to the transformed image |
456
|
|
|
*/ |
457
|
|
|
public function socialTransformWidth( |
458
|
|
|
$asset, |
459
|
|
|
$transformName = '', |
460
|
|
|
$siteId = null, |
461
|
|
|
$transformMode = null |
462
|
|
|
): string { |
463
|
|
|
return ImageTransformHelper::socialTransformWidth($asset, $transformName, $siteId, $transformMode); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Get the height of the transformed social image for $transformName and |
468
|
|
|
* optional $siteId |
469
|
|
|
* |
470
|
|
|
* @param int|Asset $asset the Asset or Asset ID |
471
|
|
|
* @param string $transformName the name of the transform to apply |
472
|
|
|
* @param int|null $siteId |
|
|
|
|
473
|
|
|
* @param string $transformMode |
|
|
|
|
474
|
|
|
* |
475
|
|
|
* @return string URL to the transformed image |
476
|
|
|
*/ |
477
|
|
|
public function socialTransformHeight( |
478
|
|
|
$asset, |
479
|
|
|
$transformName = '', |
480
|
|
|
$siteId = null, |
481
|
|
|
$transformMode = null |
482
|
|
|
): string { |
483
|
|
|
return ImageTransformHelper::socialTransformHeight($asset, $transformName, $siteId, $transformMode); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Return whether we are running Craft 3.1 or later |
488
|
|
|
* |
489
|
|
|
* @return bool |
490
|
|
|
*/ |
491
|
|
|
public function craft31(): bool |
492
|
|
|
{ |
493
|
|
|
return Seomatic::$craft31; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Return whether we are running Craft 3.2 or later |
498
|
|
|
* |
499
|
|
|
* @return bool |
500
|
|
|
*/ |
501
|
|
|
public function craft32(): bool |
502
|
|
|
{ |
503
|
|
|
return Seomatic::$craft32; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Return whether we are running Craft 3.3 or later |
508
|
|
|
* |
509
|
|
|
* @return bool |
510
|
|
|
*/ |
511
|
|
|
public function craft33(): bool |
512
|
|
|
{ |
513
|
|
|
return Seomatic::$craft33; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Given a list of meta bundles in order of descending distance, return the bundle that has inheritable value. |
518
|
|
|
* |
519
|
|
|
* @param array $inheritedValues |
|
|
|
|
520
|
|
|
* @param string $settingName |
|
|
|
|
521
|
|
|
* @param string $collectionName The name off the collection to search |
|
|
|
|
522
|
|
|
* @return MetaBundle|null |
|
|
|
|
523
|
|
|
* @since 3.4.0 |
|
|
|
|
524
|
|
|
*/ |
525
|
|
|
public function findInheritableBundle(array $inheritedValues, string $settingName, string $collectionName = "metaGlobalVars") |
526
|
|
|
{ |
527
|
|
|
if (in_array($collectionName, ['metaGlobalVars', 'metaSitemapVars'], true)) { |
528
|
|
|
foreach ($inheritedValues as $bundle) { |
529
|
|
|
/** @var $bundle MetaBundle */ |
|
|
|
|
530
|
|
|
if (isset($bundle->{$collectionName}[$settingName])) { |
531
|
|
|
if (is_bool($bundle->{$collectionName}[$settingName]) || !empty($bundle->{$collectionName}[$settingName])) { |
532
|
|
|
return $bundle; |
533
|
|
|
} |
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
return null; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Return true if a setting is inherited. |
543
|
|
|
* |
544
|
|
|
* @param InheritableSettingsModel $settingCollection |
|
|
|
|
545
|
|
|
* @param $settingName |
|
|
|
|
546
|
|
|
* @return bool |
|
|
|
|
547
|
|
|
* @since 3.4.0 |
|
|
|
|
548
|
|
|
*/ |
549
|
|
|
public function isInherited(InheritableSettingsModel $settingCollection, $settingName) |
550
|
|
|
{ |
551
|
|
|
$explicitInherit = array_key_exists($settingName, $settingCollection->inherited); |
552
|
|
|
$explicitOverride = array_key_exists($settingName, $settingCollection->overrides); |
553
|
|
|
|
554
|
|
|
if ($explicitInherit || $explicitOverride) { |
555
|
|
|
return $explicitInherit && !$explicitOverride; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
return empty($settingCollection->{$settingName}); |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|