AJAX
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Current_Resource.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
Settings.php
418 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Utils\String_Util; |
| 6 | |
| 7 | class Settings |
| 8 | { |
| 9 | public function __construct() |
| 10 | { |
| 11 | add_action('admin_init', [$this, 'register_settings']); |
| 12 | add_action('admin_init', [$this, 'register_view_counter_settings']); |
| 13 | add_action('admin_init', [$this, 'register_blocked_ip_settings']); |
| 14 | add_action('admin_init', [$this, 'register_block_by_role_settings']); |
| 15 | |
| 16 | if (iawp_is_pro()) { |
| 17 | add_action('admin_init', [$this, 'register_email_report_settings']); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return array |
| 23 | */ |
| 24 | private function get_editable_roles(): array |
| 25 | { |
| 26 | $editable_roles = []; |
| 27 | $wp_roles = wp_roles()->roles; |
| 28 | |
| 29 | array_walk($wp_roles, function ($role, $role_key) use (&$editable_roles) { |
| 30 | if ($role_key === 'administrator') { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $read_only_access = $role['capabilities']['iawp_read_only_access'] ?? false; |
| 35 | $full_access = $role['capabilities']['iawp_full_access'] ?? false; |
| 36 | |
| 37 | $editable_roles[] = [ |
| 38 | 'key' => $role_key, |
| 39 | 'name' => $role['name'], |
| 40 | 'iawp_no_access' => !$read_only_access && !$full_access, |
| 41 | 'iawp_read_only_access' => $read_only_access, |
| 42 | 'iawp_full_access' => $full_access, |
| 43 | ]; |
| 44 | }); |
| 45 | |
| 46 | return $editable_roles; |
| 47 | } |
| 48 | |
| 49 | public function render_settings() |
| 50 | { |
| 51 | echo iawp()->templates()->render('settings/index'); |
| 52 | |
| 53 | if (iawp_is_pro()) { |
| 54 | echo iawp()->templates()->render('settings/email_reports', [ |
| 55 | 'time' => iawp()->get_option('iawp_email_report_time', 9), |
| 56 | 'message' => iawp()->get_option('iawp_email_report_message', esc_html("Please find the performance report attached to this email. It includes last month's views & visitors, as well as the top pages, referrers, and countries.", 'iawp')), |
| 57 | 'emails' => iawp()->get_option('iawp_email_report_email_addresses', []), |
| 58 | ]); |
| 59 | } |
| 60 | echo iawp()->templates()->render('settings/block_ips', [ |
| 61 | 'ips' => iawp()->get_option('iawp_blocked_ips', []), |
| 62 | ]); |
| 63 | echo iawp()->templates()->render('settings/block_by_role', [ |
| 64 | 'roles' => wp_roles()->roles, |
| 65 | 'blocked' => iawp()->get_option('iawp_blocked_roles', []), |
| 66 | ]); |
| 67 | echo iawp()->templates()->render('settings/capabilities', [ |
| 68 | 'editable_roles' => $this->get_editable_roles(), |
| 69 | 'capabilities' => Capability_Manager::all_capabilities(), |
| 70 | ]); |
| 71 | echo iawp()->templates()->render('settings/view_counter'); |
| 72 | echo iawp()->templates()->render('settings/export'); |
| 73 | echo iawp()->templates()->render('settings/delete', [ |
| 74 | 'site_name' => get_bloginfo('name'), |
| 75 | 'site_url' => site_url(), |
| 76 | ]); |
| 77 | } |
| 78 | |
| 79 | public function register_settings() |
| 80 | { |
| 81 | add_settings_section('iawp-settings-section', esc_html__('Basic Settings', 'iawp'), '', 'independent-analytics-settings'); |
| 82 | |
| 83 | $args = [ |
| 84 | 'type' => 'boolean', |
| 85 | 'default' => false, |
| 86 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 87 | ]; |
| 88 | register_setting('iawp_settings', 'iawp_dark_mode', $args); |
| 89 | add_settings_field('iawp_dark_mode', esc_html__('Dark mode', 'iawp'), [$this, 'dark_mode_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dark-mode']); |
| 90 | |
| 91 | register_setting('iawp_settings', 'iawp_track_authenticated_users', $args); |
| 92 | add_settings_field('iawp_track_authenticated_users', esc_html__('Track logged in users', 'iawp'), [$this, 'track_authenticated_users_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'logged-in']); |
| 93 | |
| 94 | $args = [ |
| 95 | 'type' => 'integer', |
| 96 | 'default' => 0, |
| 97 | 'sanitize_callback' => 'absint', |
| 98 | ]; |
| 99 | register_setting('iawp_settings', 'iawp_dow', $args); |
| 100 | add_settings_field('iawp_dow', esc_html__('First day of week', 'iawp'), [$this, 'starting_dow_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dow']); |
| 101 | |
| 102 | $args = [ |
| 103 | 'type' => 'boolean', |
| 104 | 'default' => false, |
| 105 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 106 | ]; |
| 107 | register_setting('iawp_settings', 'iawp_need_clear_cache', $args); |
| 108 | } |
| 109 | |
| 110 | public function dark_mode_callback() |
| 111 | { |
| 112 | echo iawp()->templates()->render('settings/dark_mode', [ |
| 113 | 'dark_mode' => iawp()->get_option('iawp_dark_mode', false), |
| 114 | ]); |
| 115 | } |
| 116 | |
| 117 | public function track_authenticated_users_callback() |
| 118 | { |
| 119 | echo iawp()->templates()->render('settings/track_authenticated_users', [ |
| 120 | 'track_authenticated_users' => iawp()->get_option('iawp_track_authenticated_users', false), |
| 121 | ]); |
| 122 | } |
| 123 | |
| 124 | public function starting_dow_callback() |
| 125 | { |
| 126 | echo iawp()->templates()->render('settings/first_day_of_week', [ |
| 127 | 'day_of_week' => iawp()->get_option('iawp_dow', 0), |
| 128 | 'days' => [ |
| 129 | 0 => esc_html__('Sunday', 'iawp'), |
| 130 | 1 => esc_html__('Monday', 'iawp'), |
| 131 | 2 => esc_html__('Tuesday', 'iawp'), |
| 132 | 3 => esc_html__('Wednesday', 'iawp'), |
| 133 | 4 => esc_html__('Thursday', 'iawp'), |
| 134 | 5 => esc_html__('Friday', 'iawp'), |
| 135 | 6 => esc_html__('Saturday', 'iawp'), |
| 136 | ], |
| 137 | ]); |
| 138 | } |
| 139 | |
| 140 | public function register_view_counter_settings() |
| 141 | { |
| 142 | add_settings_section('iawp-view-counter-settings-section', esc_html__('Public View Counter', 'iawp'), '', 'independent-analytics-view-counter-settings'); |
| 143 | |
| 144 | $args = [ |
| 145 | 'type' => 'boolean', |
| 146 | 'default' => false, |
| 147 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 148 | ]; |
| 149 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_enable', $args); |
| 150 | add_settings_field('iawp_view_counter_enable', esc_html__('Enable the view counter', 'iawp'), [$this, 'view_counter_enable_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'enable']); |
| 151 | |
| 152 | $args = [ |
| 153 | 'type' => 'array', |
| 154 | 'default' => [], |
| 155 | 'sanitize_callback' => [$this, 'sanitize_view_counter_post_types'], |
| 156 | ]; |
| 157 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_post_types', $args); |
| 158 | add_settings_field('iawp_view_counter_post_types', esc_html__('Display on these post types', 'iawp'), [$this, 'view_counter_post_types_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'post-types']); |
| 159 | |
| 160 | $args = [ |
| 161 | 'type' => 'string', |
| 162 | 'default' => 'after', |
| 163 | 'sanitize_callback' => [$this, 'sanitize_view_counter_position'], |
| 164 | ]; |
| 165 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_position', $args); |
| 166 | add_settings_field('iawp_view_counter_position', esc_html__('Show it in this location', 'iawp'), [$this, 'view_counter_position_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'position']); |
| 167 | |
| 168 | $args = [ |
| 169 | 'type' => 'string', |
| 170 | 'default' => '', |
| 171 | 'sanitize_callback' => [$this, 'sanitize_view_counter_exclude'], |
| 172 | ]; |
| 173 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_exclude', $args); |
| 174 | add_settings_field('iawp_view_counter_exclude', esc_html__('Exclude these pages', 'iawp'), [$this, 'view_counter_exclude_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'exclude']); |
| 175 | |
| 176 | $default = function_exists('pll__') ? pll__('Views:', 'iawp') : __('Views:', 'iawp'); |
| 177 | $args = [ |
| 178 | 'type' => 'string', |
| 179 | 'default' => $default, |
| 180 | 'sanitize_callback' => 'sanitize_text_field', |
| 181 | ]; |
| 182 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_label', $args); |
| 183 | add_settings_field('iawp_view_counter_label', esc_html__('Edit the label', 'iawp'), [$this, 'view_counter_label_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'counter-label']); |
| 184 | |
| 185 | $args = [ |
| 186 | 'type' => 'boolean', |
| 187 | 'default' => true, |
| 188 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 189 | ]; |
| 190 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_icon', $args); |
| 191 | add_settings_field('iawp_view_counter_icon', esc_html__('Display the icon', 'iawp'), [$this, 'view_counter_icon_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'icon']); |
| 192 | } |
| 193 | |
| 194 | public function view_counter_enable_callback() |
| 195 | { |
| 196 | echo iawp()->templates()->render('settings/view_counter/enable', [ |
| 197 | 'enable' => iawp()->get_option('iawp_view_counter_enable', false), |
| 198 | ]); |
| 199 | } |
| 200 | |
| 201 | public function view_counter_post_types_callback() |
| 202 | { |
| 203 | $site_post_types = get_post_types(['public' => true], 'objects'); |
| 204 | $counter = 0; |
| 205 | foreach ($site_post_types as $post_type) { |
| 206 | echo iawp()->templates()->render('settings/view_counter/post_types', [ |
| 207 | 'counter' => $counter, |
| 208 | 'post_type' => $post_type, |
| 209 | 'saved' => iawp()->get_option('iawp_view_counter_post_types', []), |
| 210 | ]); |
| 211 | $counter++; |
| 212 | } ?> |
| 213 | <p class="description"><?php esc_html_e('Uncheck all boxes to only show view count manually. See shortcode documentation below for details.', 'iawp'); ?></p> |
| 214 | <?php |
| 215 | } |
| 216 | |
| 217 | public function view_counter_position_callback() |
| 218 | { |
| 219 | echo iawp()->templates()->render('settings/view_counter/position', [ |
| 220 | 'position' => iawp()->get_option('iawp_view_counter_position', 'after'), |
| 221 | ]); |
| 222 | } |
| 223 | |
| 224 | public function view_counter_exclude_callback() |
| 225 | { |
| 226 | echo iawp()->templates()->render('settings/view_counter/exclude', [ |
| 227 | 'exclude' => iawp()->get_option('iawp_view_counter_exclude', ''), |
| 228 | ]); |
| 229 | } |
| 230 | |
| 231 | public function view_counter_label_callback() |
| 232 | { |
| 233 | $default = function_exists('pll__') ? pll__('Views:', 'iawp') : __('Views:', 'iawp'); |
| 234 | echo iawp()->templates()->render('settings/view_counter/label', [ |
| 235 | 'label' => iawp()->get_option('iawp_view_counter_label', $default) |
| 236 | ]); |
| 237 | } |
| 238 | |
| 239 | public function view_counter_icon_callback() |
| 240 | { |
| 241 | echo iawp()->templates()->render('settings/view_counter/icon', [ |
| 242 | 'icon' => get_option('iawp_view_counter_icon', true), |
| 243 | ]); |
| 244 | } |
| 245 | |
| 246 | public function register_blocked_ip_settings() |
| 247 | { |
| 248 | add_settings_section('iawp-blocked-ips-settings-section', esc_html__('Block IP Addresses', 'iawp'), '', 'iawp-blocked-ips-settings'); |
| 249 | |
| 250 | $args = [ |
| 251 | 'type' => 'array', |
| 252 | 'default' => [], |
| 253 | 'sanitize_callback' => [$this, 'sanitize_blocked_ips'], |
| 254 | ]; |
| 255 | register_setting('iawp_blocked_ip_settings', 'iawp_blocked_ips', $args); |
| 256 | } |
| 257 | |
| 258 | public function register_user_permission_settings() |
| 259 | { |
| 260 | add_settings_section('iawp-user-permission-settings-section', esc_html__('User Permissions', 'iawp'), '', 'iawp-user-permission-settings'); |
| 261 | |
| 262 | $args = [ |
| 263 | 'type' => 'bool', |
| 264 | 'default' => false, |
| 265 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 266 | ]; |
| 267 | register_setting('iawp_user_permission_settings', 'iawp_white_label', $args); |
| 268 | } |
| 269 | |
| 270 | public function register_email_report_settings() |
| 271 | { |
| 272 | add_settings_section('iawp-email-report-settings-section', esc_html__('Scheduled Email Reports', 'iawp'), '', 'iawp-email-report-settings'); |
| 273 | |
| 274 | $args = [ |
| 275 | 'type' => 'number', |
| 276 | 'default' => 9, |
| 277 | 'sanitize_callback' => [$this, 'sanitize_email_report_time'], |
| 278 | ]; |
| 279 | register_setting('iawp_email_report_settings', 'iawp_email_report_time', $args); |
| 280 | |
| 281 | $args = [ |
| 282 | 'type' => 'string', |
| 283 | 'default' => esc_html("Please find the performance report attached to this email. It includes last month's views & visitors, as well as the top pages, referrers, and countries.", 'iawp'), |
| 284 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 285 | ]; |
| 286 | register_setting('iawp_email_report_settings', 'iawp_email_report_message', $args); |
| 287 | |
| 288 | $args = [ |
| 289 | 'type' => 'array', |
| 290 | 'default' => [], |
| 291 | 'sanitize_callback' => [$this, 'sanitize_email_addresses'], |
| 292 | ]; |
| 293 | register_setting('iawp_email_report_settings', 'iawp_email_report_email_addresses', $args); |
| 294 | } |
| 295 | |
| 296 | public function register_block_by_role_settings() |
| 297 | { |
| 298 | add_settings_section('iawp-block-by-role-settings-section', esc_html__('Block by User Role', 'iawp'), '', 'iawp-block-by-role-settings'); |
| 299 | |
| 300 | $args = [ |
| 301 | 'type' => 'array', |
| 302 | 'default' => [], |
| 303 | 'sanitize_callback' => [$this, 'sanitize_blocked_roles'], |
| 304 | ]; |
| 305 | register_setting('iawp_block_by_role_settings', 'iawp_blocked_roles', $args); |
| 306 | } |
| 307 | |
| 308 | public function sanitize_view_counter_post_types($user_input) |
| 309 | { |
| 310 | $site_post_types = get_post_types(['public' => true]); |
| 311 | $to_save = []; |
| 312 | foreach ($user_input as $post_type) { |
| 313 | if (in_array($post_type, $site_post_types)) { |
| 314 | $to_save[] = $post_type; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return $to_save; |
| 319 | } |
| 320 | |
| 321 | public function sanitize_view_counter_position($user_input) |
| 322 | { |
| 323 | if (in_array($user_input, ['before', 'after', 'both'])) { |
| 324 | return $user_input; |
| 325 | } else { |
| 326 | return 'after'; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | public function sanitize_view_counter_exclude($user_input) |
| 331 | { |
| 332 | $user_input = explode(',', $user_input); |
| 333 | $to_save = []; |
| 334 | |
| 335 | foreach ($user_input as $id) { |
| 336 | $save = absint($id); |
| 337 | if ($save != 0) { |
| 338 | $to_save[] = $save; |
| 339 | } |
| 340 | } |
| 341 | $to_save = implode(',', $to_save); |
| 342 | |
| 343 | return $to_save; |
| 344 | } |
| 345 | |
| 346 | public function sanitize_blocked_ips($user_input) |
| 347 | { |
| 348 | $to_save = []; |
| 349 | foreach ($user_input as $ip) { |
| 350 | if (!String_Util::str_contains($ip, '*')) { |
| 351 | if (filter_var($ip, FILTER_VALIDATE_IP)) { |
| 352 | $to_save[] = $ip; |
| 353 | } |
| 354 | } else { |
| 355 | $subs = explode('.', $ip); |
| 356 | $built_ip = []; |
| 357 | |
| 358 | for ($i = 0; $i < count($subs); $i++) { |
| 359 | if ($subs[$i] == '*') { |
| 360 | $built_ip[] = $subs[$i]; |
| 361 | } else { |
| 362 | $int = absint($subs[$i]); |
| 363 | if ($int < 0 || $int > 255) { |
| 364 | break; |
| 365 | } |
| 366 | $built_ip[] = $int; |
| 367 | } |
| 368 | if ($i == count($subs) - 1) { |
| 369 | $to_save[] = implode('.', $built_ip); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return $to_save; |
| 376 | } |
| 377 | |
| 378 | public function sanitize_email_addresses($emails) |
| 379 | { |
| 380 | $to_save = []; |
| 381 | foreach ($emails as $email) { |
| 382 | $cleaned = sanitize_email($email); |
| 383 | if (is_email($cleaned)) { |
| 384 | $to_save[] = $cleaned; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return $to_save; |
| 389 | } |
| 390 | |
| 391 | public function sanitize_email_report_time($user_time) |
| 392 | { |
| 393 | $accepted_times = []; |
| 394 | for ($i = 0; $i < 24; $i++) { |
| 395 | $accepted_times[] = $i; |
| 396 | } |
| 397 | if (in_array($user_time, $accepted_times)) { |
| 398 | return $user_time; |
| 399 | } else { |
| 400 | return 9; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | public function sanitize_blocked_roles($blocked_roles) |
| 405 | { |
| 406 | $to_save = []; |
| 407 | $user_roles = array_keys(wp_roles()->roles); |
| 408 | |
| 409 | foreach ($blocked_roles as $blocked) { |
| 410 | if (in_array($blocked, $user_roles)) { |
| 411 | $to_save[] = $blocked; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return $to_save; |
| 416 | } |
| 417 | } |
| 418 |