Skip to content

Commit

Permalink
optimize TDK for article, brand, catalog, category, page, product, tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yushine committed Jan 13, 2025
1 parent 2897d52 commit c7ecd57
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 17 deletions.
2 changes: 2 additions & 0 deletions database/seeders/ProductSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private function getProducts(): array
return [
[
'brand_id' => 1,
'spu_code' => 'galaxy-glow-evening-gown',
'slug' => 'galaxy-glow-evening-gown',
'translations' => [
[
Expand Down Expand Up @@ -122,6 +123,7 @@ private function getProducts(): array
],
[
'brand_id' => 1,
'spu_code' => 'urban-elite-suit-jacket',
'slug' => 'urban-elite-suit-jacket',
'translations' => [
[
Expand Down
100 changes: 100 additions & 0 deletions innopacks/common/src/Libraries/MetaInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright (c) Since 2024 InnoShop - All Rights Reserved
*
* @link https://www.innoshop.com
* @author InnoShop <[email protected]>
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace InnoShop\Common\Libraries;

use Illuminate\Support\Str;

class MetaInfo
{
private object $object;

private string $type;

/**
* @param $object
*/
public function __construct($object)
{
$this->object = $object;
$this->setType();
}

public static function getInstance($object): MetaInfo
{
return new self($object);
}

/**
* @return MetaInfo
*/
public function setType(): static
{
$this->type = Str::lower(class_basename($this->object));

return $this;
}

/**
* @return string
*/
public function getTitle(): string
{
$metaTitle = $this->object->translation->meta_title ?? '';
if ($metaTitle) {
return $metaTitle;
}

return $this->getName();
}

/**
* @return string
*/
public function getDescription(): string
{
$metaDescription = $this->object->translation->meta_description ?? '';
if ($metaDescription) {
return $metaDescription;
}

return $this->getName();
}

/**
* @return string
*/
public function getKeywords(): string
{
$metaKeywords = $this->object->translation->meta_keywords ?? '';
if ($metaKeywords) {
return $metaKeywords;
}

return $this->getName();
}

/**
* @return string
*/
public function getName(): string
{
$object = $this->object;
$type = $this->type;
if (in_array($type, ['category', 'product', 'tag'])) {
return $object->fallbackName('name');
} elseif (in_array($type, ['catalog', 'article', 'page'])) {
return $object->fallbackName('title');
} elseif ($type == 'brand') {
return $object->name;
}

return '';
}
}
4 changes: 2 additions & 2 deletions innopacks/common/src/Repositories/ProductRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public function handleProductData($data): array
}

return [
'spu_code' => $data['spu_code'],
'slug' => $data['slug'],
'spu_code' => $data['spu_code'] ?? null,
'slug' => $data['slug'] ?? null,
'brand_id' => $data['brand_id'] ?? 0,
'product_image_id' => $data['product_image_id'] ?? 0,
'product_video_id' => $data['product_video_id'] ?? 0,
Expand Down
5 changes: 4 additions & 1 deletion innopacks/front/resources/views/articles/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@extends('layouts.app')

@section('body-class', 'page-news-details')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($article)->getKeywords())

@section('content')

<x-front-breadcrumb type="article" :value="$article" />
Expand Down
4 changes: 4 additions & 0 deletions innopacks/front/resources/views/brands/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('layouts.app')
@section('body-class', 'page-categories')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($brand)->getKeywords())

@section('content')
<x-front-breadcrumb type="brand" :value="$brand" />

Expand Down
5 changes: 4 additions & 1 deletion innopacks/front/resources/views/catalogs/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@extends('layouts.app')

@section('body-class', 'page-news')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($catalog)->getKeywords())

@section('content')

<x-front-breadcrumb type="catalog" :value="$catalog"/>
Expand Down
4 changes: 4 additions & 0 deletions innopacks/front/resources/views/categories/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('layouts.app')
@section('body-class', 'page-categories')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($category)->getKeywords())

@section('content')
<x-front-breadcrumb type="category" :value="$category"/>

Expand Down
4 changes: 4 additions & 0 deletions innopacks/front/resources/views/pages/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@extends('layouts.app')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($page)->getKeywords())

@section('content')
@if($page->show_breadcrumb)
<x-front-breadcrumb type="page" :value="$page" />
Expand Down
15 changes: 3 additions & 12 deletions innopacks/front/resources/views/products/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
@extends('layouts.app')
@section('body-class', 'page-product')

@if($product->translation->meta_title ?? '')
@section('title', $product->translation->meta_title ?? '')
@endif

@if($product->translation->meta_description ?? '')
@section('description', $product->translation->meta_description ?? '')
@endif

@if($product->translation->meta_keywords ?? '')
@section('keywords', $product->translation->meta_keywords ?? '')
@endif

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($product)->getKeywords())

@push('header')
<script src="{{ asset('vendor/swiper/swiper-bundle.min.js') }}"></script>
Expand Down
5 changes: 4 additions & 1 deletion innopacks/front/resources/views/tags/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@extends('layouts.app')

@section('body-class', 'page-news')

@section('title', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getTitle())
@section('description', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getDescription())
@section('keywords', \InnoShop\Common\Libraries\MetaInfo::getInstance($tag)->getKeywords())

@section('content')

<x-front-breadcrumb type="tag" :value="$tag" />
Expand Down

0 comments on commit c7ecd57

Please sign in to comment.