From b4ff742dac502df01578e48b767d22c0a9771ba1 Mon Sep 17 00:00:00 2001 From: Mikael Dalholm Date: Mon, 30 Nov 2020 16:17:53 +0100 Subject: [PATCH 1/6] Add Open Graph product support --- src/Eusonlito/LaravelMeta/Meta.php | 33 +++++++++++++-- src/Eusonlito/LaravelMeta/Tags/MetaName.php | 2 +- .../LaravelMeta/Tags/MetaProduct.php | 40 +++++++++++++++++++ src/config/config.php | 2 +- tests/Tests.php | 13 ++++++ 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/Eusonlito/LaravelMeta/Tags/MetaProduct.php diff --git a/src/Eusonlito/LaravelMeta/Meta.php b/src/Eusonlito/LaravelMeta/Meta.php index 53333e7..f417ae5 100644 --- a/src/Eusonlito/LaravelMeta/Meta.php +++ b/src/Eusonlito/LaravelMeta/Meta.php @@ -1,6 +1,8 @@ 70, 'description_limit' => 200, 'image_limit' => 5, - 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard'] + 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard'] ]; /** @@ -106,13 +108,15 @@ public function title($title = null) */ public function set($key, $value) { - $value = $this->plain($value); + if (!is_array($value)) { + $value = $this->plain($value); + } + $method = 'set'.$key; if (method_exists($this, $method)) { return $this->$method($value); } - return $this->metas[$key] = self::cut($value, $key); } @@ -175,6 +179,20 @@ protected function removeImage() $this->metas['image'] = []; } + /** + * @param string $value + * + * @return string + */ + protected function setProduct($value) + { + $this->metas['product'][] = $value; + + $this->set('type', 'og:product'); + + return $value; + } + /** * @param string $key * @param string|array $default = '' @@ -211,6 +229,15 @@ public function getImage($default) return array_slice(array_merge($this->metas['image'], $default), 0, $this->config['image_limit']); } + /** + * @param string|array $default + * + * @return array + */ + public function getProduct () + { + return $this->metas['product']; + } /** * @param string $key diff --git a/src/Eusonlito/LaravelMeta/Tags/MetaName.php b/src/Eusonlito/LaravelMeta/Tags/MetaName.php index 263424b..7e4b9c1 100644 --- a/src/Eusonlito/LaravelMeta/Tags/MetaName.php +++ b/src/Eusonlito/LaravelMeta/Tags/MetaName.php @@ -3,7 +3,7 @@ class MetaName extends TagAbstract { - protected static $specials = ['canonical']; + protected static $specials = ['canonical', 'product']; public static function tagDefault($key, $value) { diff --git a/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php new file mode 100644 index 0000000..f8bb1a0 --- /dev/null +++ b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php @@ -0,0 +1,40 @@ + $value) { + if (in_array($key, self::$available, true)) { + $html .= ''; + } + } + + return $html; + + } + + public static function propertyTag ($key) + { + $tag = 'product:'; + switch ($key) { + case 'price': + $tag .= 'price:amount'; + break; + case 'currency': + $tag .= 'price:currency'; + break; + } + + return $tag; + } +} diff --git a/src/config/config.php b/src/config/config.php index 6065c31..e1c206f 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -42,5 +42,5 @@ | */ - 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard'], + 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard'], ]; diff --git a/tests/Tests.php b/tests/Tests.php index 5037193..b522ae9 100644 --- a/tests/Tests.php +++ b/tests/Tests.php @@ -173,4 +173,17 @@ public function testTagImageDefault() $this->assertTrue(mb_substr_count($tag, '') === 0); $this->assertTrue(mb_substr_count($tag, 'Meta->set('product', [ + 'price' => 100, + 'currency' => 'EUR' + ]); + + $tag = $this->Meta->tag('product'); + $this->assertTrue(mb_substr_count($tag, 'assertTrue(mb_substr_count($tag, ' Date: Mon, 30 Nov 2020 16:27:29 +0100 Subject: [PATCH 2/6] Update readme --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eb57ac5..b6a102d 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,47 @@ Or you can use Blade directives: ``` +### MetaProduct / og:product +This will allow you to add product data to your meta data. See [Open Graph product object](https://developers.facebook.com/docs/payments/product/) +```php +// resources/views/html.php + + + ... + {!! Meta::tag('type') !!} // this is needed for Meta Product to change the og:type to og:product + {!! Meta::tag('product') !!} + + +``` + +Add your product data from your controller + +```php + 100, + 'currency' => 'EUR', + ]); + + # if multiple currencies just add more product metas + Meta::set('product', [ + 'price' => 100, + 'currency' => 'USD', + ]); + + return view('index'); + } +} +``` + ### Config ```php @@ -245,7 +286,7 @@ return [ | */ - 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard'], + 'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard'], ]; ``` From 270621f7fb5e15870a8f898be0903b39e69f4435 Mon Sep 17 00:00:00 2001 From: Mikael Dalholm Date: Mon, 30 Nov 2020 16:29:52 +0100 Subject: [PATCH 3/6] Upgrade PHPUnit --- composer.json | 2 +- tests/Tests.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 57c0555..bda829f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "php": ">=5.4" }, "require-dev": { - "phpunit/phpunit": "4.8.*" + "phpunit/phpunit": "^9.3" }, "autoload": { "psr-4": { diff --git a/tests/Tests.php b/tests/Tests.php index b522ae9..e19088f 100644 --- a/tests/Tests.php +++ b/tests/Tests.php @@ -1,12 +1,12 @@ Date: Mon, 20 Sep 2021 09:33:35 +0200 Subject: [PATCH 4/6] Updated MetProduct style --- src/Eusonlito/LaravelMeta/Tags/MetaProduct.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php index f8bb1a0..441acfc 100644 --- a/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php +++ b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php @@ -12,7 +12,9 @@ public static function tagDefault($key, $values) if (!is_array($values)) { return; } + $html = ''; + foreach($values as $key => $value) { if (in_array($key, self::$available, true)) { $html .= ''; @@ -20,19 +22,20 @@ public static function tagDefault($key, $values) } return $html; - } public static function propertyTag ($key) { $tag = 'product:'; + switch ($key) { + case 'amount': case 'price': $tag .= 'price:amount'; - break; + break; case 'currency': $tag .= 'price:currency'; - break; + break; } return $tag; From 6920608951d1b0e08973d956821d4e67ef511b75 Mon Sep 17 00:00:00 2001 From: Lito Date: Mon, 20 Sep 2021 09:36:07 +0200 Subject: [PATCH 5/6] Updated code style --- src/Eusonlito/LaravelMeta/Tags/MetaProduct.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php index 441acfc..642f388 100644 --- a/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php +++ b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php @@ -15,7 +15,7 @@ public static function tagDefault($key, $values) $html = ''; - foreach($values as $key => $value) { + foreach ($values as $key => $value) { if (in_array($key, self::$available, true)) { $html .= ''; } @@ -24,7 +24,7 @@ public static function tagDefault($key, $values) return $html; } - public static function propertyTag ($key) + public static function propertyTag($key) { $tag = 'product:'; From ca1b969a62399392fb944a3540b9f57c1a113e8a Mon Sep 17 00:00:00 2001 From: Lito Date: Mon, 20 Sep 2021 09:36:48 +0200 Subject: [PATCH 6/6] Updated code style --- src/Eusonlito/LaravelMeta/Meta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Eusonlito/LaravelMeta/Meta.php b/src/Eusonlito/LaravelMeta/Meta.php index f417ae5..c778b53 100644 --- a/src/Eusonlito/LaravelMeta/Meta.php +++ b/src/Eusonlito/LaravelMeta/Meta.php @@ -234,7 +234,7 @@ public function getImage($default) * * @return array */ - public function getProduct () + public function getProduct() { return $this->metas['product']; }