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