/
piskotki.php
301 lines (255 loc) · 8.5 KB
/
piskotki.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* @package Piškotki
* @version 1.2-beta
*/
/*
Plugin Name: Piškotki
Version: 1.2-beta
Author: Aljaž Jelen (Sibit d.o.o.)
Description: This is a custom cookie consent plugin for WordPress. Slovenian only.
*/
// This is used to easily turn on and off all "experimental" features.
global $debug;
$debug = false;
//$debug = true;
defined('ABSPATH') or die('<h1>One does not simply try to access plugin files directly.</h1>');
define('PISKOTKI_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PISKOTKI_PLUGIN_DIR', plugin_dir_path(__FILE__ ));
register_activation_hook(__FILE__, 'piskotki_create_page');
register_deactivation_hook(__FILE__, 'piskotki_remove_page');
function piskotki_add_options_page()
{
add_options_page('PIŠKOTKI', 'PIŠKOTKI', 'manage_options', 'piskotki', 'piskotki_settings_page');
}
add_action('admin_menu', 'piskotki_add_options_page');
function piskotki_create_page()
{
global $wpdb;
$page_title = 'Piškotki';
$page_name = 'piskotki';
delete_option('piskotki_page_title');
add_option('piskotki_page_title', $page_title, '', 'yes');
delete_option('piskotki_page_name');
add_option('piskotki_page_name', $page_name, '', 'yes');
delete_option('piskotki_page_id');
add_option('piskotki_page_id', '0', '', 'yes');
$page = get_page_by_title($page_title);
if (!$page)
{
$_p = array();
$_p['post_title'] = $page_title;
$_p['post_content'] = "To besedilo se lahko prepiše preko vtičnika. Ne urejajte ga tukaj!";
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$page_id = wp_insert_post($_p);
}
else
{
$page_id = $page->ID;
$page->post_status = 'publish';
$page_id = wp_update_post($page);
delete_option('piskotki_page_id');
add_option('piskotki_page_id', $page_id);
}
}
function piskotki_remove_page()
{
global $wpdb;
$page_title = get_option('piskotki_page_title');
$page_name = get_option('piskotki_page_name');
$page_id = get_option('piskotki_page_id');
if ($page_id)
{
wp_delete_post($page_id);
}
delete_option('piskotki_page_title');
delete_option('piskotki_page_name');
delete_option('piskotki_page_id');
}
function piskotki_get_id_by_slug($slug)
{
$page = get_page_by_path($slug);
if ($page) { return $page->ID; }
else { return NULL; }
}
// This function overrides the content of the generated page.
function create_page_content($content)
{
global $post;
if ($post->ID == piskotki_get_id_by_slug('piskotki'))
{
$entry = get_option('page');
$entry .= '<br><div class="cc-delete">Izbriši piškotke</div>';
$entry .= '<div class="cc-create">Ustvari piškotke</div>';
return $entry;
}
return $content;
}
add_filter('the_content', 'create_page_content');
function piskotki_query_parser($query)
{
$page_name = get_option('piskotki_page_name');
$page_id = get_option('piskotki_page_id');
$query_var = $query->query_vars;
if (!$query->did_permalink && (isset($query_var['page_id'])) && (intval($query_var['page_id']) == $page_id))
{
$query->set('piskotki_page_is_called', true);
return $query;
}
elseif (isset($query_var['pagename']) && (($query_var['pagename'] == $page_name) || ($_pos_found = strpos($query_var['pagename'], $page_name.'/') === 0)))
{
$query->set('piskotki_page_is_called', true);
return $query;
}
else
{
$query->set('piskotki_page_is_called', false);
return $query;
}
}
add_filter('parse_query', 'piskotki_query_parser');
function register_input_settings()
{
register_setting('piskotki_input', 'message');
register_setting('piskotki_input', 'dismiss');
register_setting('piskotki_input', 'learn_more');
//register_setting('piskotki_input', 'link');
register_setting('piskotki_input', 'theme');
register_setting('piskotki_input', 'delete');
register_setting('piskotki_input', 'page');
}
if (is_admin())
{
add_action('admin_init', 'register_input_settings');
}
function piskotki_settings_page()
{
global $debug;
?>
<div class="wrap">
<h2>Piškotki</h2>
<form action="options.php" method="POST">
<?php
settings_fields('piskotki_input');
do_settings_sections('piskotki_input');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">Sporočilo</th>
<td>
<textarea class="regular-text" name="message" rows="10" cols="50"><?php
echo esc_textarea(get_option('message'));
?></textarea>
</td>
</tr>
<tr valign="top">
<th scope="row">Opusti</th>
<td><input class="regular-text" type="text" name="dismiss" value="<?php echo esc_attr(get_option('dismiss')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Prikaži več</th>
<td><input class="regular-text" type="text" name="learn_more" value="<?php echo esc_attr(get_option('learn_more')); ?>" /></td>
</tr>
<!-- <tr valign="top">
<th scope="row">Povezava</th>
<td>
<input class="regular-text" type="text" name="link" value="<?php //echo esc_attr(get_option('link')); ?>" />
<p class="description">
<?php //_e('Povezava naj vodi do WordPress strani, ki vsebuje pogoje uporabe tega spletnega mesta.'); ?>
</p>
</td>
</tr> -->
<?php if ($debug == true): ?>
<tr valign="top">
<th scope="row">Izgled (CSS)</th>
<td><input class="regular-text" type="text" name="theme" value="<?php echo esc_attr(get_option('theme')); ?>" /></td>
</tr>
<?php endif; ?>
<tr valign="top">
<th scope="row">Sporočilo (izbris piškotkov) </th>
<td>
<input class="regular-text" type="text" name="delete" value="<?php echo esc_attr(get_option('delete')); ?>" />
<p class="description">
<?php _e('Ta tekst se prikaže, ko se klikne na ikono plugina na spletni strani.'); ?>
</p>
</td>
</tr>
<tr valign="top">
<th scope="row">Vsebina strani</th>
<td>
<textarea class="regular-text" name="page" rows="12" cols="60"><?php
echo esc_textarea(get_option('page'));
?></textarea>
<p class="description">
<?php _e('Ta tekst se prikaže na strani z informacijami o piškotkih.'); ?>
</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Frontend stuff
function echo_popup_box()
{
$id = piskotki_get_id_by_slug('piskotki');
$link = get_page_link($id);
$nl = "\n";
$popup = '<div class="cc-popup">' . $nl;
$popup .= ' <p>' . get_option('message') . '</p>' . $nl;
$popup .= ' <a href="' . $link . '">' . get_option('learn_more') . '</a>' . $nl;
$popup .= ' <div class="cc-dismiss">' . get_option('dismiss') . '</div>' . $nl;
$popup .= '</div>' . $nl;
echo $popup;
}
add_action('wp_footer', 'echo_popup_box');
function echo_settings_box()
{
$id = piskotki_get_id_by_slug('piskotki');
$link = get_page_link($id);
$nl = "\n";
$popup = '<div class="cc-settings">' . $nl;
$popup .= ' <p>' . get_option('delete') . '</p>' . $nl;
$popup .= ' <div class="cc-dismiss-no"><a href="' . $link . '">' . 'Nastavitve</a></div>' . $nl;
$popup .= '</div>' . $nl;
echo $popup;
}
add_action('wp_footer', 'echo_settings_box');
function echo_options()
{
$nl = "\n";
$options = '<div class="cc-options" onclick="OpenCookieOptions()"></div>' . $nl;
echo $options;
}
add_action('wp_footer', 'echo_options');
// End of frontend stuff
function register_scripts_and_styles()
{
wp_register_script(
'js_cookies',
plugins_url('js/js.cookie.js', __FILE__),
array('jquery'),
'2.0.3',
false
);
wp_register_script(
'conf',
plugins_url('js/cookies.consent.js', __FILE__),
array('jquery'),
'1.0',
false
);
wp_register_style(
'cookies',
plugins_url('styles/cookies.css', __FILE__)
);
wp_enqueue_script('js_cookies');
wp_enqueue_script('conf');
wp_enqueue_style('cookies');
}
add_action('wp_enqueue_scripts', 'register_scripts_and_styles');