-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cookie-wall.php
200 lines (177 loc) · 6.73 KB
/
class.cookie-wall.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
if (! defined('ABSPATH')) {
exit;
} // Exit if accessed directly
class CookieWall
{
private static $cookieName = "wp-tropical-cookie-wall";
private static $initiated = false;
private static $useragent = '';
private static $requestscheme = '';
private static $httphost = '';
private static $redirecturl = '';
private static $scripts = array();
private static $styles = array();
public static function init()
{
if (! self::$initiated) {
self::$initiated = true;
self::init_hooks();
self::createVars();
self::createCookieWall();
}
}
/**
* Initializes Plugin
*/
private static function init_hooks()
{
add_shortcode('accept_button', array('CookieWall', 'shrt_cookieAccept'));
add_shortcode('readmore', array('CookieWall', 'shrt_readmore'));
add_shortcode('page_edit_date', array('CookieWall', 'shrt_pageEdit'));
load_plugin_textdomain(COOKIE_WALL_TEXT_DOMAIN, false, COOKIE_WALL_PLUGIN_DIR.'/translations/');
}
public static function createCookieWall()
{
if (self::checkCookieWall()) {
$post = get_post(self::getCookiePageID());
self::enqueueScripts();
include('templates/cookiewall.php');
exit;
}
}
private static function checkCookieWall()
{
if (self::get_blocked_agents()) {
if (!self::checkCookie() && !self::isCookiePage() && !is_admin()) {
wp_redirect(self::getCookiePageUri()."?u=".base64_encode(self::$requestscheme."://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]));
exit;
} elseif (isset($_GET['a'])) {
if ($_GET['a'] == 'y') {
setcookie(self::$cookieName, 1, time()+31556926, '/');
wp_redirect(base64_decode($_GET['u']));
exit;
}
} elseif (self::isCookiePage()) {
return true;
}
return false;
}
return false;
}
public static function enqueueScripts()
{
array_push(self::$styles, array("Cookie-wall-style", COOKIE_WALL_PLUGIN_URI .'assets/css/style.css', array(), 1));
array_push(self::$scripts, array("jquery", get_site_url().'/wp-includes/js/jquery/jquery.js', array(), 1));
array_push(self::$scripts, array("cookie-wall-scripts", COOKIE_WALL_PLUGIN_URI.'assets/js/scripts.js', array(), 1));
}
private static function checkCookie()
{
if (!isset($_COOKIE[SELF::$cookieName])) {
return false;
}
return true;
}
private static function getCookiePageUri()
{
return get_site_url()."/".get_page_uri(self::getCookiePageID())."/";
}
private static function getCurrentUri()
{
return self::$requestscheme."://".self::$httphost.self::$redirecturl;
}
private static function isCookiePage()
{
if (self::getCookiePageUri() === self::getCurrentUri()) {
return true;
}
return false;
}
private static function getCookiePageID()
{
$options = get_option('tropical_cookie_wall_options');
return $options['content_page'];
}
private static function getStyles()
{
foreach (self::$styles as $style) {
$html .= "<link id=\"{$style[0]}\" href=\"{$style[1]}\" media=\"all\" rel=\"stylesheet\">".PHP_EOL;
}
return $html;
}
private static function getScripts()
{
foreach (self::$scripts as $script) {
$html .= "<script id=\"{$script[0]}\" src=\"{$script[1]}\" type='text/javascript'></script> ".PHP_EOL;
}
return $html;
}
private static function getPageBgImage()
{
$options = get_option('tropical_cookie_wall_options');
return wp_get_attachment_url($options['background_url']);
}
private static function getPageBgColor()
{
$options = get_option('tropical_cookie_wall_options');
return $options['background_color'];
}
private static function getBGBlur()
{
$options = get_option('tropical_cookie_wall_options');
return $options['background_blur'];
}
private static function getLogoUrl()
{
$options = get_option('tropical_cookie_wall_options');
return wp_get_attachment_url($options['content_logo']);
}
private static function getTrackingCode()
{
$options = get_option('tropical_cookie_wall_options');
return "<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{$options['trackingID']}', 'auto');
ga('set', 'anonymizeIp', true);
ga('set', 'displayFeaturesTask', null);
ga('send', 'pageview');
</script>;";
}
private static function get_blocked_agents()
{
$blocked_agents = array('Internet\ Explorer', 'MSIE', 'Chrome', 'Safari', 'Firefox', 'Windows', 'Opera', 'iphone', 'ipad', 'android', 'blackberry');
foreach ($blocked_agents as $agent) {
if (stristr(self::$useragent, $agent) !== false) {
return true;
}
}
return false;
}
public static function createVars()
{
self::$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : getenv('HTTP_USER_AGENT');
self::$requestscheme = isset($_SERVER["REQUEST_SCHEME"]) ? $_SERVER['REQUEST_SCHEME'] : getenv('REQUEST_SCHEME');
self::$httphost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : getenv('HTTP_HOST');
self::$redirecturl = isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : getenv('REDIRECT_URL');
}
public function shrt_cookieAccept($atts)
{
$url = self::$requestscheme."://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."&a=y";
return "<a class=\"btn btn__accept\" href=\"{$url}\" id=\"accept_koe\">".__('Accept', COOKIE_WALL_TEXT_DOMAIN)."</a>";
}
public function shrt_pageEdit($atts)
{
$post = get_post(self::getCookiePageID());
return date_i18n(get_option('date_format'), strtotime($post->post_modified));
}
public function shrt_readmore($atts, $content = "")
{
$content = do_shortcode($content);
return "<a href=\"#\" id=\"readmore\">".__('Read more', COOKIE_WALL_TEXT_DOMAIN)."</a>
<div id=\"readmore_content\">$content</div>
";
}
}