Skip to content

Commit 2dd25b3

Browse files
wip
1 parent 4088c43 commit 2dd25b3

14 files changed

+265
-89
lines changed

index.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head prefix="og: http://ogp.me/ns#">
4+
<meta charset="utf-8">
5+
<title>Minimum required properties</title>
6+
7+
<?php
8+
9+
require "vendor/autoload.php";
10+
11+
echo \Spatie\OpenGraph\Types\OpenGraphBook::create()
12+
->title('Test title')
13+
->addTag('test')
14+
->addTag('secondTag')
15+
->addImage('http://placehold.it/350x400')
16+
->addImage(\Spatie\OpenGraph\StructuredProperties\OpenGraphImage::create('http://placehold.it/400x200'))
17+
->getMetaTags();
18+
19+
?>
20+
</head>
21+
<body>
22+
<p>Minimum <a href="http://ogp.me/#metadata">required</a> properties:</p>
23+
<ul>
24+
<li>title</li>
25+
<li>image</li>
26+
<li>url</li>
27+
</ul>
28+
<p>The type property is also required but if absent the page will be treated as type website.</p>
29+
</body>
30+
</html>

src/OpenGraphImage.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/OpenGraphObject.php

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ abstract class OpenGraphObject
77
/** @var string */
88
protected $prefix = 'og';
99

10-
/** @var PropertyTag[] */
10+
/** @var \Spatie\OpenGraph\PropertyTag[] */
1111
protected $tags = [];
1212

13-
/** @var OpenGraphObject[] */
13+
/** @var \Spatie\OpenGraph\OpenGraphObject[] */
1414
protected $objects = [];
1515

1616
public function getMetaTags(): string
@@ -23,23 +23,77 @@ public function getMetaTags(): string
2323
return $html;
2424
}
2525

26-
protected function addTag(string $property, string $content, $prefix = null)
26+
public function getTagsArray(): array
2727
{
28-
if(empty($content))
29-
{
30-
return;
28+
$tagsArray = [];
29+
foreach ($this->getTags() as $tag) {
30+
$tagsArray[$tag->getProperty()] = $tag->getContent();
3131
}
3232

33-
$this->tags[] = PropertyTag::create($prefix ?? $this->prefix, $property, $content);
33+
return $tagsArray;
3434
}
3535

36+
/**
37+
* Get an array of PropertyTag objects for all properties in the current OpenGraphObject
38+
* and it's attached OpenGraphObjects.
39+
*
40+
* @return \Spatie\OpenGraph\PropertyTag[]
41+
*/
3642
protected function getTags(): array
3743
{
44+
$this->prepareTags();
45+
3846
$objectTags = [];
47+
3948
foreach($this->objects as $object) {
4049
$objectTags = array_merge($objectTags, $object->getTags());
4150
}
4251

4352
return array_merge($this->tags, $objectTags);
4453
}
54+
55+
protected function addObject(?OpenGraphObject $object)
56+
{
57+
if (! $object) {
58+
return;
59+
}
60+
61+
$this->objects[] = $object;
62+
}
63+
64+
protected function setProperty(string $property, ?string $content = null, ?string $prefix = null)
65+
{
66+
if (! $content) {
67+
return;
68+
}
69+
70+
if ($tag = $this->getProperty($property, $prefix)) {
71+
$tag->content = $content;
72+
73+
return;
74+
}
75+
76+
$this->addProperty($property, $content, $prefix);
77+
}
78+
79+
protected function addProperty(string $property, ?string $content = null, ?string $prefix = null)
80+
{
81+
$this->tags[] = PropertyTag::create($prefix ?? $this->prefix, $property, $content);
82+
}
83+
84+
85+
protected function getProperty(string $property, ?string $prefix)
86+
{
87+
foreach ($this->tags as $tag) {
88+
if ($tag->property === $property && $tag->prefix === $prefix ?? $this->prefix) {
89+
return $tag;
90+
}
91+
}
92+
93+
return null;
94+
}
95+
96+
protected function prepareTags() {
97+
//
98+
}
4599
}

src/PropertyTag.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ public static function create(string $prefix = 'og', string $property, string $c
2727

2828
public function renderHtml(): string
2929
{
30-
$propertySuffix = $this->property ? ":{$this->property}" : '';
31-
return "<meta property=\"{$this->prefix}{$propertySuffix}\" content=\"{$this->content}\">";
30+
return "<meta property=\"{$this->getProperty()}\" content=\"{$this->getContent()}\">";
31+
}
32+
33+
public function getProperty(): string
34+
{
35+
$property = $this->property ? ":{$this->property}" : '';
36+
return $this->prefix.$property;
37+
}
38+
39+
public function getContent(): string
40+
{
41+
return $this->content;
3242
}
3343
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Spatie\OpenGraph\StructuredProperties;
4+
5+
use Spatie\OpenGraph\OpenGraphObject;
6+
7+
class OpenGraphImage extends OpenGraphObject
8+
{
9+
/** @var string */
10+
protected $prefix = 'og:image';
11+
12+
public function __construct(string $url)
13+
{
14+
$this->url($url);
15+
}
16+
17+
public static function create(string $url)
18+
{
19+
return new static(...func_get_args());
20+
}
21+
22+
public function url(string $url) {
23+
$this->setProperty('', $url);
24+
25+
return $this;
26+
}
27+
28+
public function secure_url(string $secure_url) {
29+
$this->setProperty('secure_url', $secure_url);
30+
31+
return $this;
32+
}
33+
34+
public function type(string $type) {
35+
$this->setProperty('type', $type);
36+
37+
return $this;
38+
}
39+
40+
public function width(string $width) {
41+
$this->setProperty('width', $width);
42+
43+
return $this;
44+
}
45+
46+
public function height(string $height) {
47+
$this->setProperty('height', $height);
48+
49+
return $this;
50+
}
51+
52+
}

src/Types/OpenGraphBaseType.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,126 @@
33
namespace Spatie\OpenGraph\Types;
44

55

6-
use Spatie\OpenGraph\OpenGraphImage;
6+
use Spatie\OpenGraph\StructuredProperties\OpenGraphImage;
77
use Spatie\OpenGraph\OpenGraphObject;
88

99
abstract class OpenGraphBaseType extends OpenGraphObject
1010
{
1111
/** @var string */
1212
protected $type;
1313

14-
public function __construct(?string $title = '', ?string $url, $image = null)
14+
/** @var string */
15+
protected $prefix;
16+
17+
public function __construct(?string $title = null)
1518
{
1619
$this->type($this->type);
1720
$this->title($title);
18-
$this->url($url);
19-
$this->image($image);
2021
}
2122

2223
/**
23-
* Creates a new Open Graph object with the correct Open Graph object type.
24+
* Creates a new Open Graph object with the correct Open Graph object type.<br><br>
2425
*
25-
* This method's `title`, `url` and `image` parameters are required
26-
* properties for an Open Graph object.
27-
* You can set them here or use the methods on the returned OpenGraphBaseType
26+
* Please note that the `url` and `image` parameters are also required by Open Graph.<br>
27+
* You can set them using the `setUrl` and `addImage` methods on the returned OpenGraphBaseType
2828
* instance.
2929
*
3030
* @param null|string $title
31-
* @param null|string $url
32-
* @param null|string|\Spatie\OpenGraph\OpenGraphImage $image
3331
*
3432
* @return OpenGraphBaseType
3533
*/
36-
public static function create(?string $title = '', ?string $url, $image = null)
34+
public static function create(?string $title = null)
3735
{
38-
return new static(...func_get_args());
36+
return new static($title);
3937
}
4038

4139
public function title($title)
4240
{
43-
$this->addTag('title', $title, 'og');
41+
$this->setProperty('title', $title, 'og');
4442

4543
return $this;
4644
}
4745

4846
public function url($title)
4947
{
50-
$this->addTag('url', $title, 'og');
48+
$this->setProperty('url', $title, 'og');
5149

5250
return $this;
5351
}
5452

55-
public function image($image)
53+
/**
54+
* @param string|\Spatie\OpenGraph\StructuredProperties\OpenGraphImage $image
55+
*
56+
* @return $this
57+
*/
58+
public function addImage($image)
5659
{
5760
if (is_string($image)) {
5861
$image = new OpenGraphImage($image);
5962
}
6063

61-
$this->objects[] = $image;
64+
$this->addObject($image);
6265

6366
return $this;
6467
}
6568

6669
public function description(string $description)
6770
{
68-
$this->addTag('description', $description, 'og');
71+
$this->setProperty('description', $description, 'og');
6972

7073
return $this;
7174
}
7275

7376
public function determiner(string $determiner)
7477
{
75-
$this->addTag('determiner', $determiner, 'og');
78+
$this->setProperty('determiner', $determiner, 'og');
7679

7780
return $this;
7881
}
7982

8083
public function locale(string $locale = 'en_US')
8184
{
82-
$this->addTag('locale', $locale, 'og');
85+
$this->setProperty('locale', $locale, 'og');
8386

8487
return $this;
8588
}
8689

87-
public function alternateLocale(string $locale = 'en_US')
90+
public function addAlternateLocale(string $locale = 'en_US')
8891
{
89-
$this->addTag('locale:alternate', $locale, 'og');
92+
$this->addProperty('locale:alternate', $locale, 'og');
9093

9194
return $this;
9295
}
9396

9497
public function siteName(string $siteName)
9598
{
96-
$this->addTag('site_name', $siteName, 'og');
99+
$this->setProperty('site_name', $siteName, 'og');
97100

98101
return $this;
99102
}
100103

101104
protected function type(string $type)
102105
{
103-
$this->addTag('type', $type, 'og');
106+
$this->setProperty('type', $type, 'og');
104107

105108
return $this;
106109
}
110+
111+
protected function prepareTags()
112+
{
113+
if (empty($this->getProperty('url', 'og')->content)) {
114+
$this->setProperty('url', $this->getOrigin(), 'og');
115+
}
116+
}
117+
118+
protected function getOrigin(): string
119+
{
120+
$useSsl = (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
121+
122+
$serverProtocol = strtolower($_SERVER['SERVER_PROTOCOL']);
123+
$urlProtocol = substr($serverProtocol, 0, strpos($serverProtocol, '/')).(($useSsl) ? 's' : '');
124+
$url = "{$urlProtocol}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
125+
126+
return htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
127+
}
107128
}

0 commit comments

Comments
 (0)