Commit 3fafe55630a7886fa8ff5f2c4ab6bc4a0c87de94

Authored by alex
1 parent cfb947e6

Микроразметка для ФБ в хедере + подчистил свою микродату

frontend/controllers/NewController.php deleted
1   -<?php
2   -namespace frontend\controllers;
3   -
4   -use phpDocumentor\Reflection\Types\Object_;
5   -use yii\web\Controller;
6   -use frontend\microdata\MicrodataFabric;
7   -use yii\helpers\VarDumper as d;
8   -use frontend\microdata\test2;
9   -
10   -class NewController extends Controller
11   -{
12   - public function actionIndex()
13   - {
14   -
15   -
16   -
17   -
18   - $microdata=new MicrodataFabric();
19   - print_r($microdata::createJsonFromArticle($productArray)->toJson());
20   - die;
21   - }
22   -}
23 0 \ No newline at end of file
frontend/controllers/ServiceController.php
... ... @@ -31,6 +31,7 @@
31 31 }
32 32  
33 33  
  34 + # список цен для микроданных
34 35 $prices=[];
35 36 foreach ($model->prices as $kry=>$price)
36 37 {
... ... @@ -56,21 +57,11 @@
56 57 :[
57 58 'type'=>'Product',
58 59 'name'=> "'{$model->language->attributes['title']}'",
59   - 'image'=> [
60   - 'https://example.com/photos/1x1/photo.jpg',
61   - 'https://example.com/photos/4x3/photo.jpg',
62   - 'https://example.com/photos/16x9/photo.jpg'
63   - ],
64 60 'offers'=> [
65 61 '@type'=> 'Offer',
66 62 'priceCurrency'=> 'UAH',
67 63 'Price'=> "'".max($prices)."'",
68   - 'priceValidUntil'=> '2020-11-05',
69   - 'itemCondition'=> 'http://schema.org/UsedCondition',
70   - 'availability'=> 'http://schema.org/InStock',
71   - 'seller'=> [
72   - 'type'=> 'Organization',
73   - 'name'=>'Executive Objects']
  64 +
74 65 ]
75 66 ];
76 67  
... ...
frontend/microdata/ArticleMicrodata.php
... ... @@ -20,15 +20,9 @@ class ArticleMicrodata extends Microdata
20 20 # обрезаю входящие свойства, работаем только с реально существующими
21 21 foreach ($config as $key=>$value){
22 22 if(!property_exists(self::class,$key))
23   - {
24 23 unset($config[$key]);
25   - }
26 24 elseif($value==null || $value===false || $value=='')
27   - {
28 25 unset($config[$key]);
29   - }
30   -
31   -
32 26  
33 27 }
34 28  
... ... @@ -37,13 +31,6 @@ class ArticleMicrodata extends Microdata
37 31 parent::__construct($config);
38 32 }
39 33  
40   -
41   - public function createJson()
42   - {
43   -
44   - }
45   -
46   -
47 34 public function toJson(): string
48 35 {
49 36 $resultJson=" <script type=\"application/ld+json\">";
... ... @@ -62,85 +49,6 @@ class ArticleMicrodata extends Microdata
62 49 }
63 50  
64 51  
65   - function add($key,$value)
66   - {
67   - if ($value!==null)
68   - $this->constructArray[$key]=$value;
69   -
70   - }
71   -
72   -
73   -
74   - /**
75   -
76   - <script type="application/ld+json">
77   - {
78   - "@context": "http://schema.org",
79   - "@type": "Article",
80   - "author": "SEO-специалист",
81   - "interactionStatistic": [
82   - {
83   - "@type": "InteractionCounter",
84   - "interactionService":
85   - {
86   - "@type": "Website",
87   - "name": "Twitter",
88   - "url": "http://www.twitter.com"
89   - },
90   - "interactionType": "http://schema.org/ShareAction",
91   - "userInteractionCount": "20"
92   - },
93   - {
94   - "@type": "InteractionCounter",
95   - "interactionType": "http://schema.org/CommentAction",
96   - "userInteractionCount": "10"
97   - }
98   - ],
99   - "name": "JSON-LD"
100   - }
101   - </script>
102   -
103   -
104   -
105   -
106   -
107   -
108   - <script type="application/ld+json">
109   - {
110   - "@context": "http://schema.org",
111   - "@type": "NewsArticle",
112   - "mainEntityOfPage": {
113   - "@type": "WebPage",
114   - "@id": "https://google.com/article"
115   - },
116   - "headline": "Article headline",
117   - "image": [
118   - "https://example.com/photos/1x1/photo.jpg",
119   - "https://example.com/photos/4x3/photo.jpg",
120   - "https://example.com/photos/16x9/photo.jpg"
121   - ],
122   - "datePublished": "2015-02-05T08:00:00+08:00",
123   - "dateModified": "2015-02-05T09:20:00+08:00",
124   - "author": {
125   - "@type": "Person",
126   - "name": "John Doe"
127   - },
128   - "publisher": {
129   - "@type": "Organization",
130   - "name": "Google",
131   - "logo": {
132   - "@type": "ImageObject",
133   - "url": "https://google.com/logo.jpg"
134   - }
135   - },
136   - "description": "A most wonderful article"
137   - }
138   - </script>
139   -
140   -
141   -
142   - */
143   -
144 52  
145 53 }
146 54  
... ...
frontend/microdata/Microdata.php
... ... @@ -13,7 +13,6 @@ class Microdata extends BaseObject
13 13 public $name=null;
14 14 public $type=null;
15 15 public $image=null;
16   - public $test=null;
17 16 public $offers=null;
18 17  
19 18 }
20 19 \ No newline at end of file
... ...
frontend/microdata/ProductMicrodata.php
... ... @@ -7,9 +7,7 @@ use yii\base\BaseObject;
7 7 class ProductMicrodata extends Microdata
8 8 {
9 9  
10   - public $type=null;
11   - public $name=null;
12   - public $image=null;
  10 +
13 11 public $brand=null;
14 12 public $aggregateRating=null;
15 13 public $offers=null;
... ... @@ -45,32 +43,27 @@ class ProductMicrodata extends Microdata
45 43 parent::__construct($config);
46 44 }
47 45  
48   -
49   - public function createJson()
50   - {
51   -
52   - }
53   -
54   -
55 46 public function toJson(): string
56 47 {
57 48 $resultJson=" <script type=\"application/ld+json\">";
58 49  
59   - $test=[];
  50 + $finalArray=[];
60 51 foreach ($this->constructArray as $key=>$value)
61 52 {
62   - if($key=='type')$key='@'.$key;
63   - $test[$key]=$value;
  53 + if($key=='type' || $key=='context')$key='@'.$key;
  54 + $finalArray[$key]=$value;
64 55 }
65 56  
66   - $resultJson1=json_encode($test,JSON_UNESCAPED_UNICODE);
  57 + $resultJson1=json_encode($finalArray,JSON_UNESCAPED_UNICODE);
67 58 $resultJson1=str_replace('\/','/',$resultJson1);
68 59 $resultJson.=$resultJson1;
69 60 $resultJson.='</script>';
  61 +
70 62 return $resultJson;
71 63 }
72 64  
73 65  
  66 +
74 67 function add($key,$value)
75 68 {
76 69 if ($value!==null)
... ... @@ -79,81 +72,6 @@ class ProductMicrodata extends Microdata
79 72 }
80 73  
81 74  
82   - public function test()
83   - {
84   -
85   -
86   - #Для страниц услуг где представлена только одна цена
87   - $productArray=[
88   - 'type'=>'Product',
89   - 'name'=> 'Executive Anvil',
90   - 'image'=> [
91   - 'https://example.com/photos/1x1/photo.jpg',
92   - 'https://example.com/photos/4x3/photo.jpg',
93   - 'https://example.com/photos/16x9/photo.jpg'
94   - ],
95   - 'description'=>'Sleeker than ACME\'s Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.',
96   - 'mpn'=>'925872',
97   - 'brand'=>[
98   - 'type'=> 'Thing',
99   - 'name'=> 'ACME'
100   - ],
101   - 'aggregateRating'=>
102   - [
103   - 'type'=> 'AggregateRating',
104   - 'ratingValue'=> '4.4',
105   - 'reviewCount'=> '89'
106   - ],
107   - 'offers'=> [
108   - '@type'=> 'Offer',
109   - 'priceCurrency'=> 'USD',
110   - 'Price'=> '199.99',
111   - 'priceValidUntil'=> '2020-11-05',
112   - 'itemCondition'=> 'http://schema.org/UsedCondition',
113   - 'availability'=> 'http://schema.org/InStock',
114   - 'seller'=> [
115   - 'type'=> 'Organization',
116   - 'name'=>'Executive Objects']
117   - ]
118   - ];
119   -
120   -
121   - #Для страниц услуг где представлены 2 и больше цен
122   - $layout2=
123   - [
124   - '@context'=> 'http://schema.org/',
125   - '@type'=> 'Product',
126   - 'name'=> 'Executive Anvil',
127   - 'image'=>
128   - [
129   - 'https://example.com/photos/1x1/photo.jpg',
130   - 'https://example.com/photos/4x3/photo.jpg',
131   - 'https://example.com/photos/16x9/photo.jpg'
132   - ],
133   - 'brand'=>
134   - [
135   - '@type'=> 'Thing',
136   - 'name'=> 'ACME'
137   - ],
138   - 'aggregateRating'=>
139   - [
140   - '@type'=> 'AggregateRating',
141   - 'ratingValue'=> '4.4',
142   - 'ratingCount'=> '89'
143   - ],
144   - 'offers' =>
145   - [
146   - '@type'=> 'AggregateOffer',
147   - 'lowPrice'=> '119.99',
148   - 'highPrice'=> '199.99',
149   - 'priceCurrency'=> 'USD'
150   - ]
151   - ];
152   -
153   -
154   - }
155   -
156   -
157 75  
158 76  
159 77  
... ...
frontend/microdata/settings/settings.php
... ... @@ -4,4 +4,5 @@
4 4  
5 5 return [
6 6 '`context'=>"http://schema.org",
  7 +
7 8 ];
8 9 \ No newline at end of file
... ...
frontend/views/layouts/main.php
... ... @@ -55,15 +55,64 @@
55 55 ]
56 56 )->orderBy('sort')
57 57 ->all();
58   -
59   - $this->registerMetaTag(
60   - [
61   - 'name' => 'description',
62   - 'content' => $seo->desc,
63   - ]
64   - );
65   -
66   -
  58 +
  59 +
  60 +
  61 +//-----------------------------------------------------------------------------------------------------------------------
  62 +// Блок с микроразметкой для FB
  63 +
  64 + # $property нужно для property="og:type" FB
  65 + switch (\Yii::$app->controller->id)
  66 + {
  67 + case "site":
  68 + $property='website';
  69 + break;
  70 + case "blog":
  71 + $property='article';
  72 + break;
  73 + case 'service':
  74 + $property='product';
  75 + break;
  76 + default:
  77 + $property='website';
  78 + break;
  79 + }
  80 +
  81 +$this->registerMetaTag(
  82 + [
  83 + 'property'=>'og:description',
  84 + 'content' => $seo->desc,
  85 + ]
  86 +);
  87 +$this->registerMetaTag(
  88 + [
  89 + 'property'=>"og:type",
  90 + 'content' => $property,
  91 + ]
  92 +);
  93 +$this->registerMetaTag(
  94 + [
  95 + 'property'=>"og:url",
  96 + 'content' => Url::current(),
  97 + ]
  98 +);
  99 +$this->registerMetaTag(
  100 + [
  101 + 'property'=>"og:title",
  102 + 'content' =>$seo->title,
  103 + ]
  104 +);
  105 +$this->registerMetaTag(
  106 + [
  107 + 'name' => 'description',
  108 + 'property'=>"og:image",
  109 + 'content' => \Yii::$app->homeUrl."/storage/logo/".$settings->logo,
  110 + ]
  111 +);
  112 +
  113 +//----------------------------------------------------------------------------------------------------------------------
  114 +
  115 +
67 116 $this->registerMetaTag(
68 117 [
69 118 'name' => 'robots',
... ...