1+ <?php
2+
3+ /*
4+ * To change this license header, choose License Headers in Project Properties.
5+ * To change this template file, choose Tools | Templates
6+ * and open the template in the editor.
7+ */
8+
9+ namespace daxslab \taggedview ;
10+
11+ use Yii ;
12+ use yii \web \View as BaseView ;
13+
14+ /**
15+ * Description of SeoTags
16+ *
17+ * @author glpz
18+ */
19+ class View extends BaseView
20+ {
21+
22+ // toogle tags to register
23+ public $ registerStandardTags = true ;
24+ public $ registerOpenGraphTags = true ;
25+ public $ registerTwitterCardTags = true ;
26+ // toogle translations
27+ public $ translate = ['title ' , 'site_name ' , 'description ' , 'author ' , 'keywords ' ];
28+ //meta properties
29+ public $ author ;
30+ public $ site_name ;
31+ public $ url ;
32+ public $ description ;
33+ public $ type ;
34+ public $ locale ;
35+ public $ image ;
36+ public $ robots ;
37+ public $ keywords = [];
38+ public $ creator ;
39+ public $ generator ;
40+ public $ date ;
41+ public $ data_type ;
42+ public $ card ;
43+ public $ site ;
44+ public $ label1 ;
45+ public $ data1 ;
46+ public $ label2 ;
47+ public $ data2 ;
48+
49+ private $ updated_time ;
50+
51+ public function init ()
52+ {
53+ if ($ this ->site_name == null ) {
54+ $ this ->site_name = Yii::$ app ->name ;
55+ }
56+ if ($ this ->url == null ) {
57+ $ this ->url = Yii::$ app ->request ->absoluteUrl ;
58+ }
59+
60+ $ this ->translateProperties ();
61+ }
62+
63+ protected function renderHeadHtml ()
64+ {
65+ if ($ this ->locale == null ) {
66+ $ this ->locale = str_replace ('- ' , '_ ' , Yii::$ app ->language );
67+ }
68+
69+ if ($ this ->registerStandardTags ) {
70+ $ this ->registerStandardMetaTags ();
71+ }
72+
73+ if ($ this ->registerOpenGraphTags ) {
74+ $ this ->registerOpenGraphMetaTags ();
75+ }
76+
77+ if ($ this ->registerTwitterCardTags ) {
78+ $ this ->registerTwitterCardMetaTags ();
79+ }
80+
81+ array_multisort ($ this ->metaTags );
82+ return parent ::renderHeadHtml ();
83+ }
84+
85+ protected function registerStandardMetaTags ()
86+ {
87+
88+ foreach ($ this ->keywords as $ keyword ) {
89+ $ this ->registerMetaTag (['name ' => 'article:tag ' , 'content ' => trim ($ keyword )]);
90+ }
91+
92+ $ this ->keywords = empty ($ this ->keywords ) ? null : join (', ' , $ this ->keywords );
93+ foreach (['author ' , 'description ' , 'robots ' , 'keywords ' , 'generator ' ] as $ property ) {
94+ if ($ this ->$ property ) {
95+ $ this ->registerMetaTag (['name ' => $ property , 'content ' => $ this ->$ property ]);
96+ }
97+ }
98+
99+ $ this ->registerLinkTag (['rel ' => 'canonical ' , 'href ' => $ this ->url ]);
100+
101+ }
102+
103+ protected function registerOpenGraphMetaTags ()
104+ {
105+
106+ $ this ->updated_time = $ this ->date ;
107+
108+ foreach (['title ' , 'url ' , 'site_name ' , 'type ' , 'description ' , 'locale ' , 'updated_time ' ] as $ property ) {
109+ if ($ this ->$ property ) {
110+ $ this ->registerMetaTag (['property ' => "og: " . $ property , 'content ' => $ this ->$ property ]);
111+ }
112+
113+ }
114+
115+ if ($ this ->image !== null ) {
116+ if (is_array ($ this ->image )) {
117+ foreach ($ this ->image as $ key => $ value ) {
118+ $ this ->registerMetaTag (['property ' => 'og:image ' , 'content ' => $ value ], 'og:image ' . $ key );
119+ }
120+ } else {
121+ $ this ->registerMetaTag (['property ' => 'og:image ' , 'content ' => $ this ->image ], 'og:image ' );
122+ }
123+
124+ }
125+ }
126+
127+
128+ protected function registerTwitterCardMetaTags ()
129+ {
130+ foreach ([
131+ 'card ' ,
132+ 'title ' ,
133+ 'url ' ,
134+ 'creator ' ,
135+ 'site ' ,
136+ 'type ' ,
137+ 'description ' ,
138+ 'label1 ' ,
139+ 'data1 ' ,
140+ 'label2 ' ,
141+ 'data2 '
142+ ] as $ property ) {
143+ if ($ this ->$ property ) {
144+ $ this ->registerMetaTag (['name ' => "twitter: " . $ property , 'content ' => $ this ->$ property ]);
145+ }
146+ }
147+
148+ if ($ this ->image !== null ) {
149+ if (is_array ($ this ->image )) {
150+ $ this ->registerMetaTag (['name ' => 'twitter:image ' , 'content ' => $ this ->image [0 ]], 'twitter:image ' );
151+ } else {
152+ $ this ->registerMetaTag (['name ' => 'twitter:image ' , 'content ' => $ this ->image ], 'twitter:image ' );
153+ }
154+ }
155+ }
156+
157+ protected function translateProperties ()
158+ {
159+ foreach ($ this ->translate as $ property ) {
160+ if ($ this ->$ property != null ) {
161+ if (is_array ($ this ->$ property )) {
162+ $ the_array = $ this ->$ property ;
163+ foreach ($ the_array as $ i => $ word ) {
164+ $ the_array [$ i ] = Yii::t ('app ' , $ word );
165+ }
166+ $ this ->$ property = $ the_array ;
167+ } else {
168+ $ this ->$ property = Yii::t ('app ' , $ this ->$ property );
169+ }
170+ }
171+ }
172+ }
173+
174+ }
0 commit comments