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'], ]; ``` 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/src/Eusonlito/LaravelMeta/Meta.php b/src/Eusonlito/LaravelMeta/Meta.php index 53333e7..c778b53 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..642f388 --- /dev/null +++ b/src/Eusonlito/LaravelMeta/Tags/MetaProduct.php @@ -0,0 +1,43 @@ + $value) { + if (in_array($key, self::$available, true)) { + $html .= ''; + } + } + + return $html; + } + + public static function propertyTag($key) + { + $tag = 'product:'; + + switch ($key) { + case 'amount': + 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..e19088f 100644 --- a/tests/Tests.php +++ b/tests/Tests.php @@ -1,12 +1,12 @@ 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, '