Skip to content

Commit

Permalink
Merge pull request #32 from dalholm/master
Browse files Browse the repository at this point in the history
Add Open Graph product support
  • Loading branch information
eusonlito authored Sep 20, 2021
2 parents 4748499 + ca1b969 commit c65b2a8
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 9 deletions.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,47 @@ Or you can use Blade directives:
</html>
```

### 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

<head>
...
{!! Meta::tag('type') !!} // this is needed for Meta Product to change the og:type to og:product
{!! Meta::tag('product') !!}
</head>

```

Add your product data from your controller

```php
<?php namespace App\Http\Controllers;

use Meta;

class ProductController extends Controller
{
public function show()
{
# Add product meta
Meta::set('product', [
'price' => 100,
'currency' => 'EUR',
]);

# if multiple currencies just add more product metas
Meta::set('product', [
'price' => 100,
'currency' => 'USD',
]);

return view('index');
}
}
```

### Config

```php
Expand Down Expand Up @@ -245,7 +286,7 @@ return [
|
*/

'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard'],
'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard'],
];
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 30 additions & 3 deletions src/Eusonlito/LaravelMeta/Meta.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Eusonlito\LaravelMeta;

use Eusonlito\LaravelMeta\Tags\MetaProduct;

class Meta
{
use FixesTrait;
Expand All @@ -27,7 +29,7 @@ class Meta
'title_limit' => 70,
'description_limit' => 200,
'image_limit' => 5,
'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard']
'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard']
];

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Eusonlito/LaravelMeta/Tags/MetaName.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class MetaName extends TagAbstract
{
protected static $specials = ['canonical'];
protected static $specials = ['canonical', 'product'];

public static function tagDefault($key, $value)
{
Expand Down
43 changes: 43 additions & 0 deletions src/Eusonlito/LaravelMeta/Tags/MetaProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Eusonlito\LaravelMeta\Tags;

class MetaProduct extends TagAbstract
{
protected static $available = [
'price', 'currency',
];

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 .= '<meta property="'.self::propertyTag($key).'" content="'.$value.'" />';
}
}

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;
}
}
2 changes: 1 addition & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
|
*/

'tags' => ['Tag', 'MetaName', 'MetaProperty', 'TwitterCard'],
'tags' => ['Tag', 'MetaName', 'MetaProperty', 'MetaProduct', 'TwitterCard'],
];
17 changes: 15 additions & 2 deletions tests/Tests.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
use Eusonlito\LaravelMeta\Meta;

class Tests extends PHPUnit_Framework_TestCase
class Tests extends PHPUnit\Framework\TestCase
{
protected static $title;
protected $Meta;

public function setUp()
public function setUp(): void
{
self::$title = self::text(20);

Expand Down Expand Up @@ -173,4 +173,17 @@ public function testTagImageDefault()
$this->assertTrue(mb_substr_count($tag, '<image>') === 0);
$this->assertTrue(mb_substr_count($tag, '<link rel="image_src"') === 4);
}

public function testTagProduct()
{

$this->Meta->set('product', [
'price' => 100,
'currency' => 'EUR'
]);

$tag = $this->Meta->tag('product');
$this->assertTrue(mb_substr_count($tag, '<meta property="product:price:amount"') === 1);
$this->assertTrue(mb_substr_count($tag, '<meta property="product:price:currency"') === 1);
}
}

0 comments on commit c65b2a8

Please sign in to comment.