This repository has been archived by the owner on May 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
OpenGraph.php
65 lines (54 loc) · 2.16 KB
/
OpenGraph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace dragonjet\opengraph;
use Yii;
use yii\web\View;
class OpenGraph {
public $title;
public $site_name;
public $url;
public $description;
public $type;
public $locale;
public $image;
public function __construct(){
// Load default values
$this->title = Yii::$app->name;
$this->site_name = Yii::$app->name;
$this->url = Yii::$app->request->absoluteUrl;
$this->description = null;
$this->type = 'article';
$this->locale = str_replace('-','_',Yii::$app->language);
$this->image = null;
// Twitter Card
$this->twitter = new TwitterCard;
// Listed to Begin Page View event to start adding meta
Yii::$app->view->on(View::EVENT_BEGIN_PAGE, function(){
// Register required and easily determined open graph data
Yii::$app->controller->view->registerMetaTag(['property'=>'og:title', 'content'=>$this->title], 'og:title');
Yii::$app->controller->view->registerMetaTag(['property'=>'og:site_name', 'content'=>$this->site_name], 'og:site_name');
Yii::$app->controller->view->registerMetaTag(['property'=>'og:url', 'content'=>$this->url], 'og:url');
Yii::$app->controller->view->registerMetaTag(['property'=>'og:type', 'content'=>$this->type], 'og:type');
// Locale issafe to be specifued since it has default value on Yii applications
Yii::$app->controller->view->registerMetaTag(['property'=>'og:locale', 'content'=>$this->locale], 'og:locale');
// Only add a description meta if specified
if($this->description!==null){
Yii::$app->controller->view->registerMetaTag(['property'=>'og:description', 'content'=>$this->description], 'og:description');
}
// Only add an image meta if specified
if($this->image!==null){
Yii::$app->controller->view->registerMetaTag(['property'=>'og:image', 'content'=>$this->image], 'og:image');
}
$this->twitter->registerTags();
});
}
public function set($metas=[]){
// Massive assignment by array
foreach($metas as $property=>$content){
if($property=='twitter'){
$this->twitter->set($content);
}else if(property_exists($this, $property)){
$this->$property = $content;
}
}
}
}