Skip to content

Commit

Permalink
Refactor conditional fields (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Nov 14, 2022
1 parent 92aba1b commit 0f744c5
Show file tree
Hide file tree
Showing 30 changed files with 377 additions and 90 deletions.
4 changes: 4 additions & 0 deletions content/content.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
seo_site_name_position: end
seo_generate_social_images: false
seo_og_title: '@auto'
seo_og_description: '@auto'
seo_twitter_card: summary
seo_twitter_title: '@auto'
seo_twitter_description: '@auto'
seo_canonical_type: current
seo_noindex: false
seo_nofollow: false
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"production": "mix --production"
},
"dependencies": {
"vue": "^2.6.14"
"vue": "^2.6.14",
"marked": "^4.0.10"
},
"devDependencies": {
"autoprefixer": "^10.4.5",
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/js/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/cp.js": "/js/cp.js?id=a111bc589aba1b73cd9741c2cac99989",
"/js/cp.js": "/js/cp.js?id=ad0deeb3d85b3f2bec4c7418fb07f730",
"/css/cp.css": "/css/cp.css?id=8061e1c7006144a94600e6bb047fcff1"
}
9 changes: 9 additions & 0 deletions resources/js/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import DefaultsPublishForm from './components/DefaultsPublishForm'
import SocialImageFieldtype from './components/SocialImageFieldtype'
import SourceFieldtype from './components/SourceFieldtype'

Statamic.booting(() => {
Statamic.component('defaults-publish-form', DefaultsPublishForm)
Statamic.component('social_image-fieldtype', SocialImageFieldtype)
Statamic.component('seo_source-fieldtype', SourceFieldtype)
})
8 changes: 7 additions & 1 deletion resources/js/components/DefaultsPublishForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@
</style>

<script>
import HasHiddenFields from '../../../vendor/statamic/cms/resources/js/components/data-list/HasHiddenFields';
export default {
mixins: [
HasHiddenFields,
],
props: {
publishContainer: String,
initialReference: String,
Expand Down Expand Up @@ -199,7 +205,7 @@ export default {
this.saving = true;
this.clearErrors();
const payload = { ...this.values, ...{
const payload = { ...this.visibleValues, ...{
blueprint: this.fieldset.handle,
_localized: this.localizedFields,
}};
Expand Down
19 changes: 19 additions & 0 deletions resources/js/conditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Fetch the conditions after Statamic has booted.
Statamic.booted(() => {
Statamic.$store.dispatch("publish/advancedSeo/fetchConditions")
})

// Fetch the conditions when the user changes the site in the sidebar.
Statamic.$store.watch(state => state.publish?.base?.site, () => {
Statamic.$store.dispatch("publish/advancedSeo/fetchConditions")
})

// Add the showSitemapFields condition.
Statamic.$conditions.add('showSitemapFields', ({ store }) => {
return store.state.publish.advancedSeo.conditions?.showSitemapFields
});

// Add the showSocialImagesGeneratorFields condition.
Statamic.$conditions.add('showSocialImagesGeneratorFields', ({ store }) => {
return store.state.publish.advancedSeo.conditions?.showSocialImagesGeneratorFields
});
12 changes: 3 additions & 9 deletions resources/js/cp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import DefaultsPublishForm from './components/DefaultsPublishForm'
import SocialImageFieldtype from './components/SocialImageFieldtype'
import SourceFieldtype from './components/SourceFieldtype'

Statamic.booting(() => {
Statamic.component('defaults-publish-form', DefaultsPublishForm)
Statamic.component('social_image-fieldtype', SocialImageFieldtype)
Statamic.component('seo_source-fieldtype', SourceFieldtype)
})
import './components'
import './conditions'
import './store'
33 changes: 33 additions & 0 deletions resources/js/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Statamic.$store.registerModule(['publish', 'advancedSeo'], {

namespaced: true,

state: {
conditions: null,
},

getters: {
conditions: state => state.conditions,
},

actions: {
fetchConditions({ commit }) {
return Statamic.$request.post(`/!/advanced-seo/conditions`, {
url: window.location,
id: Statamic.$store.state.publish?.base?.values?.id,
site: Statamic.$store.state.publish.base.site,
})
.then(response => commit('setConditions', response.data))
.catch(function (error) {
console.log(error);
});
},
},

mutations: {
setConditions(state, conditions) {
state.conditions = conditions;
},
}

})
2 changes: 2 additions & 0 deletions routes/actions.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Aerni\AdvancedSeo\Http\Controllers\Cp\ConditionsController;
use Aerni\AdvancedSeo\Http\Controllers\Web\SocialImagesController;
use Illuminate\Support\Facades\Route;

Route::name('advanced-seo.')->group(function () {
Route::get('/social-images/{theme}/{type}/{id}', [SocialImagesController::class, 'show'])->name('social_images.show');
Route::post('/conditions', ConditionsController::class);
});
2 changes: 2 additions & 0 deletions src/Actions/EvaluateModelHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Illuminate\Support\Str;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
Expand All @@ -16,6 +17,7 @@ class EvaluateModelHandle
public static function handle(mixed $model): ?string
{
return match (true) {
($model instanceof SeoDefaultSet) => $model->handle(),
($model instanceof Collection) => $model->handle(),
($model instanceof Entry) => $model->collection()->handle(),
($model instanceof EntryBlueprintFound) => Str::after($model->blueprint->namespace(), '.'),
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/EvaluateModelLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Taxonomies\Taxonomy;
Expand All @@ -18,6 +19,7 @@ class EvaluateModelLocale
public static function handle(mixed $model): ?string
{
return match (true) {
($model instanceof SeoDefaultSet) => request()->get('site') ?? $model->selectedSite(),
($model instanceof Collection) => Statamic::isCpRoute() ? basename(request()->path()) : Site::current()->handle(),
($model instanceof Entry) => $model->locale(),
($model instanceof EntryBlueprintFound) => Statamic::isCpRoute() ? basename(request()->path()) : Site::current()->handle(),
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/EvaluateModelParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Illuminate\Support\Str;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
Expand All @@ -20,6 +21,7 @@ class EvaluateModelParent
public static function handle(mixed $data): mixed
{
return match (true) {
($data instanceof SeoDefaultSet) => $data,
($data instanceof Collection) => $data,
($data instanceof Entry) => $data->collection(),
($data instanceof EntryBlueprintFound) => CollectionApi::find(Str::after($data->blueprint->namespace(), '.')),
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/EvaluateModelSites.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Illuminate\Support\Collection as LaravelCollection;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Taxonomies\Taxonomy;
Expand All @@ -12,6 +13,7 @@ class EvaluateModelSites
public static function handle(mixed $model): ?LaravelCollection
{
return match (true) {
($model instanceof SeoDefaultSet) => $model->sites(),
($model instanceof Collection) => $model->sites(),
($model instanceof Taxonomy) => $model->sites(),
($model instanceof DefaultsData) => $model->sites,
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/EvaluateModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Data\SeoDefaultSet;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Taxonomies\Taxonomy;
Expand All @@ -15,6 +16,7 @@ class EvaluateModelType
public static function handle(mixed $model): ?string
{
return match (true) {
($model instanceof SeoDefaultSet) => $model->type(),
($model instanceof Collection) => 'collections',
($model instanceof Entry) => 'collections',
($model instanceof EntryBlueprintFound) => 'collections',
Expand Down
9 changes: 8 additions & 1 deletion src/Actions/ShouldGenerateSocialImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ public static function handle(Entry $entry): bool
// Don't generate if we're first localiting an entry.
if (str_contains(request()->path(), 'localize')) {
return false;
};
}

// Shouldn't generate if the generator was disabled in the config.
if (! config('advanced-seo.social_images.generator.enabled', false)) {
return false;
}

$disabled = config('advanced-seo.disabled.collections', []);

// Check if the collection is set to be disabled globally.
if (in_array($entry->collectionHandle(), $disabled)) {
return false;
}

// Get the collections that are allowed to generate social images.
$enabledCollections = Seo::find('site', 'social_media')
?->in($entry->site()->handle())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

namespace Aerni\AdvancedSeo\Actions;
namespace Aerni\AdvancedSeo\Conditions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Facades\Seo;

class ShouldDisplaySitemapSettings
class ShowSitemapFields
{
public static function handle(DefaultsData $data): bool
{
if (! config('advanced-seo.sitemap.enabled', true)) {
return false;
}

$disabled = config("advanced-seo.disabled.{$data->type}", []);

// Check if the collection/taxonomy is set to be disabled globally.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Aerni\AdvancedSeo\Actions;
namespace Aerni\AdvancedSeo\Conditions;

use Aerni\AdvancedSeo\Data\DefaultsData;
use Aerni\AdvancedSeo\Facades\Seo;
use Statamic\Facades\Site;

class ShouldDisplaySocialImagesGenerator
class ShowSocialImagesGeneratorFields
{
public static function handle(DefaultsData $data): bool
{
Expand All @@ -15,13 +14,20 @@ public static function handle(DefaultsData $data): bool
return false;
}

$disabled = config("advanced-seo.disabled.{$data->type}", []);

// Check if the collection/taxonomy is set to be disabled globally.
if (in_array($data->handle, $disabled)) {
return false;
}

// Terms are not yet supported.
if ($data->type === 'taxonomies') {
return false;
}

$enabledCollections = Seo::find('site', 'social_media')
?->in(Site::selected()->handle())
?->in($data->locale)
?->value('social_images_generator_collections') ?? [];

// Don't show the generator section if the collection is not configured.
Expand Down
6 changes: 4 additions & 2 deletions src/Contracts/SeoDefaultsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ interface SeoDefaultsRepository
{
public function make(): SeoDefaultSet;

public function find(string $type, string $id): ?SeoDefaultSet;
public function find(string $type, string $handle): ?SeoDefaultSet;

public function findOrMake(string $type, string $id): SeoDefaultSet;
public function findById(string $id): ?SeoDefaultSet;

public function findOrMake(string $type, string $handle): SeoDefaultSet;

public function all(): Collection;

Expand Down
Loading

0 comments on commit 0f744c5

Please sign in to comment.