-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSiteDefaults.php
More file actions
97 lines (78 loc) · 3.08 KB
/
Copy pathSiteDefaults.php
File metadata and controls
97 lines (78 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Statamic\SeoPro\SiteDefaults;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Statamic\Facades\Addon;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
class SiteDefaults
{
public static function get(): Collection
{
return Blink::once('seo-pro::site-defaults', function () {
$data = Arr::get(Addon::get('statamic/seo-pro')->settings()->raw(), 'site_defaults', []);
return Site::all()->map(function ($site) use ($data) {
$values = Arr::get($data, Site::multiEnabled() ? $site->handle() : null);
// When there are no values set, and it's a root localization, use defaults
// from the blueprint as initial values.
if (empty($values) && self::origins()->get($site->handle()) === null) {
$values = self::defaultValues();
}
return new LocalizedSiteDefaults($site->handle(), collect($values));
});
});
}
public static function origins($origins = null): Collection|bool
{
if (func_num_args() === 0) {
return Blink::once('seo-pro::site-defaults-origins', function () {
return Site::all()
->mapWithKeys(fn ($site) => [$site->handle() => null])
->merge(Addon::get('statamic/seo-pro')->settings()->get('site_defaults_sites', []))
->map(fn ($origin) => empty($origin) ? null : $origin);
});
}
Addon::get('statamic/seo-pro')->settings()->set('site_defaults_sites', $origins)->save();
Blink::forget('seo-pro::site-defaults');
Blink::forget('seo-pro::site-defaults-origins');
return true;
}
public static function in(string $locale): ?LocalizedSiteDefaults
{
if (! self::get()->has($locale)) {
return null;
}
return self::get()->get($locale);
}
public static function save(LocalizedSiteDefaults $localized): bool
{
$data = Arr::get(Addon::get('statamic/seo-pro')->settings()->raw(), 'site_defaults', []);
if (Site::multiEnabled()) {
$data[$localized->locale()] = $localized->all();
} else {
$data = $localized->all();
}
Addon::get('statamic/seo-pro')->settings()->set('site_defaults', $data)->save();
Blink::forget('seo-pro::site-defaults');
Blink::forget("seo-pro::site-defaults.augmented.{$localized->locale()}");
return true;
}
public static function blueprint(): \Statamic\Fields\Blueprint
{
return Blueprint::get();
}
private static function defaultValues(): array
{
return [
'site_name' => '{{ config:app:name }}',
'site_name_position' => 'after',
'site_name_separator' => '|',
'title' => '@seo:title',
'description' => '@seo:content',
'canonical_url' => '@seo:permalink',
'og_type' => 'website',
'priority' => 0.5,
'change_frequency' => 'monthly',
];
}
}