-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdsenseWidget.php
155 lines (139 loc) · 4.49 KB
/
AdsenseWidget.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace geertw\Yii2\Adsense;
use Yii;
use yii\helpers\Html;
/**
* AdSense widget. This widget renders the Google AdSense code for showing AdSense banners.
*
* @package geertw\Yii2\Adsense
*/
class AdsenseWidget extends \yii\base\Widget {
/**
* @var string AdSense client ID
*/
public $client;
/**
* @var string AdSense slot ID
*/
public $slot;
/**
* @var bool Whether AdSense is enabled or not
*/
public $enabled;
/**
* @var bool Whether this banner is responsive
*/
public $responsive = true;
/**
* @var bool Whether this widget is visible at all
*/
public $visible = true;
/**
* List of all ad sizes Adsense has to offer (including regional formats).
* They are used to show whether a responsive block has a valid size.
* @var array
*/
private $adSizes = [
[300, 250],
[336, 280],
[728, 90],
[300, 600],
[320, 100],
[320, 50],
[468, 60],
[234, 60],
[120, 600],
[120, 240],
[160, 600],
[300, 1050],
[970, 90],
[970, 250],
[250, 250],
[200, 200],
[180, 150],
[125, 125],
[240, 400],
[980, 120],
[250, 360],
[930, 180],
[580, 400],
[750, 300],
[750, 200],
[750, 100],
];
public function init() {
parent::init();
if (empty($this->client)) {
// Try to load from params:
if (isset(Yii::$app->params['adsense']) && isset(Yii::$app->params['adsense']['client'])) {
$this->client = Yii::$app->params['adsense']['client'];
}
}
if (empty($this->slot)) {
if (isset(Yii::$app->params['adsense']) && isset(Yii::$app->params['adsense']['slot'])) {
$this->slot = Yii::$app->params['adsense']['slot'];
}
}
if (empty($this->enabled)) {
if (isset(Yii::$app->params['adsense']) && isset(Yii::$app->params['adsense']['enabled'])) {
$this->enabled = Yii::$app->params['adsense']['enabled'];
}
}
if (empty($this->client) || empty($this->slot)) {
$this->enabled = false;
}
}
/**
* Render the widget
* @return string
*/
public function run() {
if ($this->visible == false) {
return "<!-- Adsense widget not visible -->";
}
if ($this->enabled == true) {
// Return Google AdSense code
$this->view->registerJsFile('//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', ['async' => 'async']);
$code = "\n<ins class=\"adsbygoogle\"";
$code .= ' style="display:block"';
$code .= ' data-ad-client="' . Html::encode($this->client) . '"';
$code .= ' data-ad-slot="' . Html::encode($this->slot) . '"';
if ($this->responsive == true) {
$code .= ' data-ad-format="auto"';
}
$code .= "></ins>\n";
$code .= '<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>';
} else {
// AdSense can or should not be shown. Return dummy banner.
$id = uniqid('disabled_adsense_');
$code = '<ins class="adsbygoogle disabled" id="' . $id . '" style="background-color: #d4e926; color: #000; display: block; text-align: center; text-decoration: none">';
$code .= "Adsense disabled";
$code .= "</ins>\n";
if ($this->responsive == true) {
$adSizes = json_encode($this->adSizes);
$code .= <<<JSCODE
<script>
var disabled_banner = document.getElementById('{$id}');
var width = disabled_banner.clientWidth;
var height = disabled_banner.clientHeight;
var sizes = {$adSizes};
var validSize = false;
for (index = 0; index < sizes.length; ++index) {
if (sizes[index][0] == width && sizes[index][1] == height) {
disabled_banner.innerHTML = 'Adsense disabled<br><strong>' + width + 'x' + height + '</strong>';
validSize = true;
break;
}
}
if (validSize == false) {
disabled_banner.innerHTML = 'Adsense disabled<br><strong>Invalid size!</strong> (' + width + 'x' + height + ')';
disabled_banner.style.color = '#ffffff';
disabled_banner.style.backgroundColor = '#d50000';
}
</script>
JSCODE;
}
}
return $code;
}
}