Skip to content

Commit

Permalink
Misc performance improvements (#304)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Leite <jesseleite@gmail.com>
  • Loading branch information
ryanmitchell and jesseleite authored Dec 6, 2023
1 parent 957faa8 commit c96d53f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion resources/lang/nl/fieldsets/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
'change_frequency' => 'Sitemap: Updatefrequentie',
'change_frequency_instruct' => 'Een instructie voor zoekmachines over hoe vaak de pagina waarschijnlijk geüpdatet zal worden.',

];
];
2 changes: 1 addition & 1 deletion resources/lang/nl/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@
'three_segment_urls_site_warning' => ':count pagina met meer dan 3 segmenten in de URL.|:count pagina\'s met meer dan 3 segmenten in hun URL\'s.',
],

];
];
24 changes: 17 additions & 7 deletions src/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Statamic\SeoPro;

use Statamic\Assets\Asset;
use Statamic\Facades\Blink;
use Statamic\Statamic;

class Fields
{
Expand Down Expand Up @@ -241,15 +243,23 @@ public function getConfig()
*/
protected function getPlaceholder($handle)
{
$cascade = (new Cascade)->with(SiteDefaults::load()->all());

if ($this->data) {
$cascade = $cascade
->with($this->getSectionDefaults($this->data))
->with($this->data->value('seo', []))
->withCurrent($this->data);
if (! Statamic::isCpRoute()) {
return null;
}

$cascade = Blink::once('seo-pro::placeholder.cascade', function () {
$cascade = (new Cascade)->with(SiteDefaults::load()->all());

if ($this->data) {
$cascade = $cascade
->with($this->getSectionDefaults($this->data))
->with($this->data->value('seo', []))
->withCurrent($this->data);
}

return $cascade;
});

$placeholder = $cascade->value($handle);

if (is_array($placeholder)) {
Expand Down
8 changes: 5 additions & 3 deletions src/Fieldtypes/SeoProFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ protected function fields()

protected function fieldConfig()
{
if ($this->field()->parent() instanceof Entry || $this->field()->parent() instanceof Term) {
$parent = $this->field()->parent();
$parent = $this->field()->parent();

if (! ($parent instanceof Entry || $parent instanceof Term)) {
$parent = null;
}

return SeoProFields::new($parent ?? null)->getConfig();
}

public function augment($data)
{
if (! is_iterable($data)) {
if (empty($data) || ! is_iterable($data)) {
return $data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Fieldtypes/SourceFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function augment($data)
return $data;
}

$fieldtype = $this->sourceField()->fieldtype();
$fieldtype = $this->fieldtype();

if ($data === false) {
$data = $fieldtype->defaultValue();
Expand All @@ -105,7 +105,7 @@ public function augment($data)
protected function sourceField()
{
if (! $config = $this->config('field')) {
return null;
return;
}

return new Field(null, $config);
Expand Down
20 changes: 11 additions & 9 deletions src/GetsSectionDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public function getAugmentedSectionDefaults($current)
return [];
}

return Blueprint::make()
->setContents([
'fields' => Fields::new()->getConfig(),
])
->fields()
->addValues($seo = $parent->cascade('seo') ?: [])
->augment()
->values()
->only(array_keys($seo));
return Blink::once($this->getCacheKey($parent).'.augmented', function () use ($parent) {
return Blueprint::make()
->setContents([
'fields' => Fields::new()->getConfig(),
])
->fields()
->addValues($seo = $parent->cascade('seo') ?: [])
->augment()
->values()
->only(array_keys($seo));
});
}

protected function getCacheKey($parent)
Expand Down
11 changes: 8 additions & 3 deletions src/SiteDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\SeoPro;

use Illuminate\Support\Collection;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\File;
use Statamic\Facades\YAML;
Expand Down Expand Up @@ -73,6 +74,8 @@ public function save()
File::put($this->path(), YAML::dump($this->items));

SeoProSiteDefaultsSaved::dispatch($this);

Blink::forget('seo-pro::defaults');
}

/**
Expand All @@ -82,9 +85,11 @@ public function save()
*/
protected function getDefaults()
{
return collect(YAML::file(__DIR__.'/../content/seo.yaml')->parse())
->merge(YAML::file($this->path())->parse())
->all();
return Blink::once('seo-pro::defaults', function () {
return collect(YAML::file(__DIR__.'/../content/seo.yaml')->parse())
->merge(YAML::file($this->path())->parse())
->all();
});
}

/**
Expand Down

0 comments on commit c96d53f

Please sign in to comment.