Skip to content

Commit 1a110be

Browse files
committed
Simpify seo and og_image bugfix
1 parent 34a1472 commit 1a110be

2 files changed

Lines changed: 52 additions & 199 deletions

File tree

src/Dtos/ContentDto.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,18 @@ public function getSeo(?string $locale = null)
222222
$result = $seo->get($locale);
223223

224224
// Using fallback locale
225-
if (! $result) {
226-
if (($fallbackLocale = $this->getFallbackLocale()) && ($fallbackSeo = $seo->get($fallbackLocale))) {
227-
$fallbackSeo->locale = $fallbackLocale;
228-
$result = $fallbackSeo;
229-
}
230-
} else {
231-
$result->locale = $locale;
225+
if (! $result &&
226+
($fallbackLocale = $this->getFallbackLocale()) &&
227+
($fallbackSeo = $seo->get($fallbackLocale))
228+
) {
229+
$result = $fallbackSeo;
232230
}
233231

234232
// Using default SEO data if not found
235233
if (! $result) {
236-
$result = SeoDto::fromArray([
237-
'locale' => $locale,
238-
]);
234+
$result = SeoDto::fromArray([]);
239235
}
240-
236+
241237
return $result;
242238
}
243239

src/Dtos/SeoDto.php

Lines changed: 45 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\HtmlString;
88
use SolutionForest\InspireCms\InspireCmsConfig;
99
use SolutionForest\InspireCms\Support\Base\Dtos\BaseDto;
10+
use SolutionForest\InspireCms\Support\Dtos\MediaAssetDto;
1011

1112
class SeoDto extends BaseDto
1213
{
@@ -73,83 +74,13 @@ class SeoDto extends BaseDto
7374
*/
7475
public $siteName;
7576

76-
/**
77-
* The locale of the content.
78-
*
79-
* @var string
80-
*/
81-
public $locale;
82-
83-
/**
84-
* The published time of the content.
85-
*
86-
* @var string
87-
*/
88-
public $publishedTime;
89-
90-
/**
91-
* The last modified time of the content.
92-
*
93-
* @var string
94-
*/
95-
public $modifiedTime;
96-
97-
/**
98-
* The author of the content.
99-
*
100-
* @var string
101-
*/
102-
public $author;
103-
104-
/**
105-
* The section of the site where the content is located.
106-
*
107-
* @var string
108-
*/
109-
public $section;
110-
111-
/**
112-
* The tag associated with the content.
113-
*
114-
* @var string
115-
*/
116-
public $tag;
117-
118-
/**
119-
* The category of the content.
120-
*
121-
* @var string
122-
*/
123-
public $category;
124-
12577
/**
12678
* The canonical URL of the content.
12779
*
12880
* @var string
12981
*/
13082
public $canonical;
13183

132-
/**
133-
* The alternate URL of the content.
134-
*
135-
* @var string
136-
*/
137-
public $alternate;
138-
139-
/**
140-
* The AMP HTML version of the content.
141-
*
142-
* @var string
143-
*/
144-
public $ampHtml;
145-
146-
/**
147-
* The AMP version of the content.
148-
*
149-
* @var string
150-
*/
151-
public $amp;
152-
15384
/**
15485
* Indicates whether the content should not be indexed by search engines.
15586
*
@@ -164,55 +95,6 @@ class SeoDto extends BaseDto
16495
*/
16596
public $noFollow;
16697

167-
/**
168-
* Indicates whether the content should not be archived by search engines.
169-
*
170-
* @var string
171-
*/
172-
public $noArchive;
173-
174-
/**
175-
* Indicates whether search engines should not show a snippet of the content.
176-
*
177-
* @var string
178-
*/
179-
public $noSnippet;
180-
181-
/**
182-
* Indicates whether the content should not be included in the Open Directory Project.
183-
*
184-
* @var string
185-
*/
186-
public $noOdp;
187-
188-
/**
189-
* Indicates whether the content should not be included in Yahoo Directory.
190-
*
191-
* @var string
192-
*/
193-
public $noYdir;
194-
195-
/**
196-
* Indicates whether images on the content should not be indexed by search engines.
197-
*
198-
* @var string
199-
*/
200-
public $noImageIndex;
201-
202-
/**
203-
* Indicates whether the content should not be translated by search engines.
204-
*
205-
* @var string
206-
*/
207-
public $noTranslate;
208-
209-
/**
210-
* Indicates whether the content should not be cached by search engines.
211-
*
212-
* @var string
213-
*/
214-
public $noCache;
215-
21698
/**
21799
* @var Collection<self>|null
218100
*/
@@ -229,10 +111,6 @@ public static function fromArray(array $parameters)
229111
'og_image' => 'ogImage',
230112
'noindex' => 'noIndex',
231113
'nofollow' => 'noFollow',
232-
'noarchive' => 'noArchive',
233-
'nosnippet' => 'noSnippet',
234-
'noodp' => 'noOdp',
235-
'noydir' => 'noYdir',
236114
];
237115

238116
foreach ($parameters as $key => $value) {
@@ -287,7 +165,7 @@ public static function fromArray(array $parameters)
287165
}
288166

289167
/**
290-
* @param self[]|Collection<slef>|null $ancestors
168+
* @param self[]|Collection<self>|null $ancestors
291169
* @return self
292170
*/
293171
public function setAncestors($ancestors)
@@ -341,8 +219,8 @@ public function __toString(): string
341219
$html .= "<meta property=\"og:description\" content=\"{$ogDescription}\">\n";
342220
}
343221

344-
if ($this->ogImage && ($mediaAssetUrl = inspirecms_asset()->findByKeys($this->ogImage)->first()?->getUrl())) {
345-
$html .= "<meta property=\"og:image\" content=\"{$mediaAssetUrl}\">\n";
222+
if ($odImageUrl = $this->transformImage($this->ogImage)) {
223+
$html .= "<meta property=\"og:image\" content=\"{$odImageUrl}\">\n";
346224
}
347225

348226
if ($this->url) {
@@ -357,86 +235,65 @@ public function __toString(): string
357235
$html .= "<meta property=\"og:site_name\" content=\"{$this->siteName}\">\n";
358236
}
359237

360-
if ($this->locale) {
361-
$html .= "<meta property=\"og:locale\" content=\"{$this->locale}\">\n";
362-
}
363-
364-
if ($this->publishedTime) {
365-
$html .= "<meta property=\"article:published_time\" content=\"{$this->publishedTime}\">\n";
366-
}
367-
368-
if ($this->modifiedTime) {
369-
$html .= "<meta property=\"article:modified_time\" content=\"{$this->modifiedTime}\">\n";
370-
}
371-
372-
if ($this->author) {
373-
$html .= "<meta property=\"article:author\" content=\"{$this->author}\">\n";
374-
}
375-
376-
if ($this->section) {
377-
$html .= "<meta property=\"article:section\" content=\"{$this->section}\">\n";
378-
}
379-
380-
if ($this->tag) {
381-
$html .= "<meta property=\"article:tag\" content=\"{$this->tag}\">\n";
382-
}
383-
384-
if ($this->category) {
385-
$html .= "<meta property=\"article:category\" content=\"{$this->category}\">\n";
386-
}
387-
388238
if ($this->canonical) {
389239
$html .= "<link rel=\"canonical\" href=\"{$this->canonical}\">\n";
390240
}
391241

392-
if ($this->alternate) {
393-
$html .= "<link rel=\"alternate\" href=\"{$this->alternate}\">\n";
394-
}
395-
396-
if ($this->ampHtml) {
397-
$html .= "<link rel=\"amphtml\" href=\"{$this->ampHtml}\">\n";
398-
}
399-
400-
if ($this->amp) {
401-
$html .= "<link rel=\"amp\" href=\"{$this->amp}\">\n";
402-
}
403-
242+
$robotsContent = [];
404243
if ($this->noIndex) {
405-
$html .= "<meta name=\"robots\" content=\"noindex\">\n";
244+
$robotsContent[] = 'noindex';
406245
}
407-
408246
if ($this->noFollow) {
409-
$html .= "<meta name=\"robots\" content=\"nofollow\">\n";
247+
$robotsContent[] = 'nofollow';
410248
}
411-
412-
if ($this->noArchive) {
413-
$html .= "<meta name=\"robots\" content=\"noarchive\">\n";
249+
if (!empty($robotsContent)) {
250+
$html .= "<meta name=\"robots\" content=\"" . implode(', ', $robotsContent) . "\">\n";
414251
}
415252

416-
if ($this->noSnippet) {
417-
$html .= "<meta name=\"robots\" content=\"nosnippet\">\n";
418-
}
253+
return $html;
254+
}
419255

420-
if ($this->noOdp) {
421-
$html .= "<meta name=\"robots\" content=\"noodp\">\n";
256+
protected function transformImage($value)
257+
{
258+
// If the value is empty or null, return null
259+
if (empty($value)) {
260+
return null;
422261
}
423262

424-
if ($this->noYdir) {
425-
$html .= "<meta name=\"robots\" content=\"noydir\">\n";
426-
}
263+
// If the value is a string, check if it is a valid URL
264+
if (is_string($value)) {
427265

428-
if ($this->noImageIndex) {
429-
$html .= "<meta name=\"robots\" content=\"noimageindex\">\n";
430-
}
266+
if (filter_var($value, FILTER_VALIDATE_URL)) {
267+
return $value;
268+
}
269+
270+
// If not, check if it is a relative URL (e.g., "/storage/images/example.jpg")
271+
if (str_starts_with($value, '/')) {
272+
// Assuming the base URL is defined in your configuration
273+
$baseUrl = config('app.url', 'http://localhost');
274+
return rtrim($baseUrl, '/') . $value;
275+
}
431276

432-
if ($this->noTranslate) {
433-
$html .= "<meta name=\"robots\" content=\"notranslate\">\n";
277+
return null;
434278
}
435279

436-
if ($this->noCache) {
437-
$html .= "<meta name=\"robots\" content=\"nocache\">\n";
280+
// If the value is an array (from MediaPicker)
281+
if (is_array($value)) {
282+
283+
try {
284+
$dto = MediaAssetDto::fromArray($value);
285+
// Handle the case where the src is a string
286+
if (filled($dto->src)) {
287+
return $this->transformImage($dto->src);
288+
}
289+
} catch (\Throwable $th) {
290+
//
291+
}
292+
293+
return null;
438294
}
439295

440-
return $html;
296+
// If the value is not a string or an array, return null
297+
return null;
441298
}
442299
}

0 commit comments

Comments
 (0)