Skip to content

Commit

Permalink
Merge pull request #1 from ADmad/master
Browse files Browse the repository at this point in the history
Misc.
  • Loading branch information
jadb committed Aug 5, 2015
2 parents 00adc8f + cb25c59 commit 1272de2
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 58 deletions.
35 changes: 28 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
#This Travis config template file was taken from https://github.com/FriendsOfCake/travis
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0

before_script:
- phpenv rehash
sudo: false

env:
global:
- DEFAULT=1

matrix:
fast_finish: true

include:
- php: 7.0
env: PHPCS=1 DEFAULT=0

- php: 5.6
env: COVERALLS=1 DEFAULT=0

install:
- composer self-update
- composer install --dev
- composer require cakephp/cakephp-codesniffer:dev-master
- composer self-update
- composer install --prefer-dist --no-interaction --dev

before_script:
- sh -c "if [ '$PHPCS' = '1' ]; then composer require cakephp/cakephp-codesniffer:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

script:
- phpunit
# - ./vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests
- sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then ./vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"

notifications:
email: false
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Social Meta

[![Build Status](https://travis-ci.org/gourmet/social-meta.svg?branch=master)](https://travis-ci.org/gourmet/social-meta)
[![Total Downloads](https://poser.pugx.org/gourmet/social-meta/downloads.svg)](https://packagist.org/packages/gourmet/social-meta)
[![License](https://poser.pugx.org/gourmet/social-meta/license.svg)](https://packagist.org/packages/gourmet/social-meta)
[![Build Status](https://img.shields.io/travis/gourmet/social-meta/master.svg?style=flat-square)](https://travis-ci.org/gourmet/social-meta)
[![Total Downloads](https://img.shields.io/packagist/dt/gourmet/social-meta.svg?style=flat-square)](https://packagist.org/packages/gourmet/social-meta)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt)

Adds [Facebook Open Graph][fbog] and [Twitter Cards][twcards] support to [CakePHP 3].

Expand All @@ -27,35 +27,36 @@ You then need to load the plugin. In `boostrap.php`, something like:

## Usage

First, include the helpers in your `AppController`, specific `Controller` or `AppView`. Example in
`AppController`:
Include the helpers in your `AppView`:

```php
public $helpers = [
'Gourmet\SocialMeta.Card',
'Gourmet\SocialMeta.OpenGraph'
];
public function initialize(array $config)
{
$this->loadHelper('Gourmet\SocialMeta.Card');
$this->loadHelper('Gourmet\SocialMeta.OpenGraph');
}
```

Keep in mind that certain configuration option are made available to you. For example:

```php
public $helpers = [
'Gourmet\SocialMeta.Card' => [
public function initialize(array $config)
{
$this->loadHelper('Gourmet\SocialMeta.Card', [
'card' => 'photo',
'tags' => ['twitter' => [
'description' => 'Some default description'
]]
],
'Gourmet\SocialMeta.OpenGraph' => [
]);
$this->loadHelper('Gourmet\SocialMeta.OpenGraph', [
'app_id' => 'xxx'
]
];
]);
}
```

You are now ready to use the helpers in your view.
You are now ready to use the helpers in your view / layout.

For the [Facebook OpenGraph][fbog], you will need to use the helper's `html()` method as it
For the [Facebook OpenGraph][fbog], you will need to use the helper's `html()` method as it
will include the defined namespaces:

```php
Expand Down
62 changes: 31 additions & 31 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "gourmet/social-meta"
, "description": "Adds Facebook Open Graph and Twitter Cards support to CakePHP 3."
, "type": "cakephp-plugin"
, "keywords": [
"cakephp"
, "gourmet"
]
, "homepage": "https://github.com/gourmet/social-meta"
, "license": "MIT"
, "authors": [
"name": "gourmet/social-meta",
"description": "Adds Facebook Open Graph and Twitter Cards support to CakePHP 3.x",
"type": "cakephp-plugin",
"keywords": [
"cakephp",
"gourmet",
"social",
"facebook",
"twitter"
],
"homepage": "https://github.com/gourmet/social-meta",
"license": "MIT",
"authors": [
{
"name": "Jad Bitar"
, "homepage": "http://jadb.io"
, "role": "Author"
}
, {
"name": "Others"
, "homepage": "https://github.com/gourmet/social-meta/graphs/contributors"
"name": "Jad Bitar",
"homepage": "http://jadb.io",
"role": "Author"
},
{
"name": "Others",
"homepage": "https://github.com/gourmet/social-meta/graphs/contributors"
}
]
, "support": {
"issues": "https://github.com/gourmet/social-meta/issues"
, "source": "https://github.com/gourmet/social-meta"
}
, "require": {
"cakephp/cakephp": "3.0.*-dev"
}
, "require-dev": {
"phpunit/phpunit": "4.1.*"
}
, "autoload": {
],
"support": {
"issues": "https://github.com/gourmet/social-meta/issues",
"source": "https://github.com/gourmet/social-meta"
},
"require": {
"cakephp/cakephp": "~3.0"
},
"autoload": {
"psr-4": {
"Gourmet\\SocialMeta\\": "src"
}
}
, "autoload-dev": {
},
"autoload-dev": {
"psr-4": {
"Gourmet\\SocialMeta\\Test\\": "tests"
}
Expand Down
27 changes: 27 additions & 0 deletions src/View/Helper/CardHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class CardHelper extends Helper
'tags' => []
];

/**
* Constructor
*
* @param \Cake\View\View $View View instance.
* @param array $config Config
*/
public function __construct(View $View, array $config = [])
{
$this->_defaultConfig['url'] = $View->request->here;
Expand All @@ -29,17 +35,38 @@ public function __construct(View $View, array $config = [])
$this->setTitle($View->fetch('title'));
}

/**
* Add tag
*
* @param string $tag Tag name
* @param string $value Tag value
* @param array $options Options
* @return $this
*/
public function addTag($tag, $value, array $options = [])
{
$this->config("tags.twitter.$tag", $options ? [$value, $options] : $value);
return $this;
}

/**
* Set URL.
*
* @param string|array $value URL
* @return $this
*/
public function setUrl($value)
{
return $this->addTag('url', Router::url($value, true));
}

/**
* Magic method to handle calls to "set<Foo>" methods.
*
* @param string $tag Tag name
* @param array $args Arguments
* @return mixed
*/
public function __call($tag, $args)
{
if (strpos($tag, 'set') !== 0) {
Expand Down
8 changes: 7 additions & 1 deletion src/View/Helper/MetaTagAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

trait MetaTagAwareTrait
{

/**
* Render view block.
*
* @return string
*/
public function render()
{
$block = $this->config('viewBlockName');

foreach ((array) $this->config('tags') as $namespace => $values) {
foreach ((array)$this->config('tags') as $namespace => $values) {
foreach ($values as $tag => $content) {
$property = "$namespace:$tag";

Expand Down
70 changes: 68 additions & 2 deletions src/View/Helper/OpenGraphHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ class OpenGraphHelper extends Helper
{
use MetaTagAwareTrait;

/**
* Helpers used by this helper.
*
* @var array
*/
public $helpers = ['Html'];

/**
* Default config.
*
* @var array
*/
protected $_defaultConfig = [
'viewBlockName' => 'smOpenGraph',
'app_id' => null,
Expand All @@ -20,6 +30,13 @@ class OpenGraphHelper extends Helper
'tags' => [],
];

/**
* Generate HTML tag.
*
* @param array $options Options
* @param array $namespaces Namespaces
* @return string
*/
public function html(array $options = [], array $namespaces = [])
{
$this->addNamespace('og', 'http://ogp.me/ns#');
Expand All @@ -34,14 +51,21 @@ public function html(array $options = [], array $namespaces = [])
$this->setType($this->config('type'));
$this->setUri($this->request->here);
$this->setTitle($this->_View->fetch('title'));

if ($appId = $this->config('app_id')) {
$this->setAppId($appId);
}

return $this->Html->tag('html', null, $this->config('namespaces') + $options);
}

/**
* Add namespace
*
* @param string $namespace Namespace
* @param string|array $url URL
* @return $this
*/
public function addNamespace($namespace, $url)
{
if (strpos($namespace, 'xmlns') !== 0) {
Expand All @@ -52,17 +76,38 @@ public function addNamespace($namespace, $url)
return $this;
}

/**
* Add tag
*
* @param string $namespace Namespace
* @param string $tag Tag name
* @param string $value Tag value
* @param array $options Options
* @return $this
*/
public function addTag($namespace, $tag, $value, array $options = [])
{
$this->config("tags.$namespace.$tag", $options ? [$value, $options] : $value);
return $this;
}

/**
* Set App Id
*
* @param string $id Facebook App ID
* @return $this
*/
public function setAppId($id)
{
return $this->addTag('fb', 'app_id', $id);
}

/**
* Set Admins
*
* @param string|array $id Admin user IDs
* @return $this
*/
public function setAdmins($id)
{
if (is_array($id)) {
Expand All @@ -72,9 +117,16 @@ public function setAdmins($id)
return $this->addTag('fb', 'admins', $id);
}

/**
* Set locale
*
* @param string|array $value Locale(s)
* @param string $namespace Namespace. Defaults to "og"
* @return $this
*/
public function setLocale($value, $namespace = 'og')
{
$value = array_unique((array) $value);
$value = array_unique((array)$value);

foreach ($value as &$v) {
if (strpos($v, '-') !== false) {
Expand All @@ -93,11 +145,25 @@ public function setLocale($value, $namespace = 'og')
return $this->addTag($namespace, 'locale', $locale, $options);
}

/**
* Set URL.
*
* @param string|array $value URL
* @param string $namespace Namespace. Defaults to "og"
* @return $this
*/
public function setUri($value, $namespace = 'og')
{
return $this->addTag($namespace, 'uri', Router::url($value, true));
}

/**
* Magic method to handle calls to "set<Foo>" methods.
*
* @param string $tag Tag name
* @param array $args Arguments
* @return mixed
*/
public function __call($tag, $args)
{
if (strpos($tag, 'set') !== 0) {
Expand Down

0 comments on commit 1272de2

Please sign in to comment.