-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookiewow-wordpress.php
201 lines (173 loc) · 5.92 KB
/
cookiewow-wordpress.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
<?php
/**
* Cookie Wow Banner
*
* @package cookiewow-banner
*
* @wordpress-plugin
* Plugin Name: Cookie Wow Banner
* Plugin URI: https://github.com/datawowio/cookiewow-wordpress
* Description: An easy way to manage cookie consent on web pages.
* Version: 1.1.7
* Author: Cookie Wow
* Author URI: https://cookiewow.com/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
define( 'WP_COOKIEWOW_SLUG', 'cookiewow-settings' );
define( 'WP_COOKIEWOW_FILE', __FILE__ );
/**
* Initialize admin settings.
*/
function cookiewow_admin_init() {
$option_group = 'cookiewow_settings_fields';
$option_name = 'cookiewow_option';
$args = array( 'sanitize_callback' => 'cookiewow_sanitize_settings_fields' );
register_setting( $option_group, $option_name, $args );
$id = 'cookiewow_setting_section_id';
$title = '';
$callback = 'cookiewow_settings_section_description';
add_settings_section( $id, $title, $callback, WP_COOKIEWOW_SLUG );
$id = 'cookiewow_token';
$title = 'Cookie Banner ID';
$callback = 'cookiewow_settings_fields';
$section = 'cookiewow_setting_section_id';
$args = null;
add_settings_field( $id, $title, $callback, WP_COOKIEWOW_SLUG, $section, $args );
}
/**
* Add admin menu.
*/
function cookiewow_admin_menu() {
$parent_slug = 'options-general.php';
$page_title = 'Cookie Wow Settings';
$menu_title = 'Cookie Wow';
$capability = 'manage_options';
$function = 'cookiewow_settings_page';
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, WP_COOKIEWOW_SLUG, $function );
}
/**
* Admin menu icon.
*/
function cookiewow_admin_menu_icon() {
$file_contents = file_get_contents( plugin_dir_path( __FILE__ ) . 'static/images/icon-cookiewow.b64' );
return "data:image/svg+xml;base64,$file_contents";
}
/**
* Display a notification when an error occurred in updating settings.
*/
function cookiewow_admin_notices() {
settings_errors( WP_COOKIEWOW_SLUG );
}
/**
* Display admin settings fields.
*/
function cookiewow_settings_fields() {
$option = get_option( 'cookiewow_option' );
printf(
'<input type="text" id="cookiewow_token" name="cookiewow_option[cookiewow_token]" class="regular-text" value="%s" />',
isset( $option['cookiewow_token'] ) ? esc_attr( $option['cookiewow_token'] ) : ''
);
}
/**
* Link to the configuration page of the plugin & documentation
*
* @param string[] $actions An array of plugin action links.
*/
function cookiewow_settings_action_links( $actions ) {
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', 'https://help.cookiewow.com/th/?utm_source=wp_plugin&utm_medium=wp_cookiewow', __( 'Docs', 'cookiewow' ) ) );
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=' . WP_COOKIEWOW_SLUG ), __( 'Settings', 'cookiewow' ) ) );
return $actions;
}
/**
* Display admin settings form.
*/
function cookiewow_settings_page() {
require_once plugin_dir_path( __FILE__ ) . 'settings.php';
}
/**
* Sanitize the setting fields before updating.
*
* @param string[] $fields The admin settings fields.
*/
function cookiewow_sanitize_settings_fields( $fields ) {
$sanitized_fields = array();
if ( isset( $fields['cookiewow_token'] ) ) {
$sanitized_fields['cookiewow_token'] = sanitize_text_field( ( $fields['cookiewow_token'] ) );
}
return $sanitized_fields;
}
/**
* Display description for admin settings form.
*/
function cookiewow_settings_section_description() {
}
/**
* Uninstall the plugin.
*/
function cookiewow_uninstall() {
delete_option( 'cookiewow_option' );
}
/**
* Enqueue scripts to WordPress.
*/
function cookiewow_enqueue_scripts() {
$option = get_option( 'cookiewow_option' );
if ( ! isset( $option['cookiewow_token'] )
|| '' === trim( $option['cookiewow_token'] ) ) {
return;
}
$script_name = 'cookiewow_script';
$src = 'https://cookiecdn.com/cwc.js';
$dependencies = array();
$version = null;
$in_footer = false;
wp_enqueue_script( $script_name, $src, $dependencies, $version, $in_footer );
$token = esc_attr( $option['cookiewow_token'] );
$script_name = 'cookiewow_configs_script';
$src = "https://cookiecdn.com/configs/{$token}";
$dependencies = array();
$version = null;
$in_footer = false;
wp_enqueue_script( $script_name, $src, $dependencies, $version, $in_footer );
}
/**
* To replace the default ID that generated by WordPress when enqueued script
* and to add data-cwcid.
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $script_name The script's name that registered handle.
* @param string $src The script's source URL..
* @return string The $tag that has been modified.
*/
function cookiewow_script_loader_tag( $tag, $script_name, $src ) {
if ( 'cookiewow_configs_script' === $script_name ) {
$option = get_option( 'cookiewow_option' );
$token = esc_attr( $option['cookiewow_token'] );
$replace_at_position = strpos( $tag, 'src' );
$replace_to_position = -1;
return substr_replace(
$tag,
"id='cookieWow' type='text/javascript' src='$src' data-cwcid='$token' data-cfasync='false'></script>",
$replace_at_position,
$replace_to_position
);
} elseif ( 'cookiewow_script' === $script_name ) {
$replace_at_position = strpos( $tag, 'src' );
$replace_to_position = -1;
return substr_replace(
$tag,
"src='$src' data-cfasync='false'></script>",
$replace_at_position,
$replace_to_position
);
}
return $tag;
}
add_action( 'admin_init', 'cookiewow_admin_init' );
add_action( 'admin_menu', 'cookiewow_admin_menu' );
add_action( 'admin_notices', 'cookiewow_admin_notices' );
add_action( 'wp_enqueue_scripts', 'cookiewow_enqueue_scripts' );
add_filter( 'plugin_action_links_' . plugin_basename( WP_COOKIEWOW_FILE ), 'cookiewow_settings_action_links' );
add_filter( 'script_loader_tag', 'cookiewow_script_loader_tag', $priority = 1, $accepted_args = 3 );
register_uninstall_hook( __FILE__, 'cookiewow_uninstall' );