AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
Settings.php
294 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Utils\Request; |
| 6 | use IAWP_SCOPED\IAWP\Utils\String_Util; |
| 7 | /** @internal */ |
| 8 | class Settings |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | \add_action('admin_init', [$this, 'register_settings']); |
| 13 | \add_action('admin_init', [$this, 'register_view_counter_settings']); |
| 14 | \add_action('admin_init', [$this, 'register_blocked_ip_settings']); |
| 15 | \add_action('admin_init', [$this, 'register_block_by_role_settings']); |
| 16 | if (\IAWP_SCOPED\iawp_is_pro()) { |
| 17 | \add_action('admin_init', [$this, 'register_email_report_settings']); |
| 18 | } |
| 19 | } |
| 20 | public function render_settings() |
| 21 | { |
| 22 | echo \IAWP_SCOPED\iawp_blade()->run('settings.index'); |
| 23 | if (\IAWP_SCOPED\iawp_is_pro()) { |
| 24 | echo \IAWP_SCOPED\iawp_blade()->run('settings.email-reports', ['time' => \IAWP_SCOPED\iawp()->get_option('iawp_email_report_time', 9), 'emails' => \IAWP_SCOPED\iawp()->get_option('iawp_email_report_email_addresses', []), 'saved_colors' => \IAWP_SCOPED\iawp()->get_option('iawp_email_report_colors', $this->email_report_colors()), 'default_colors' => $this->email_report_colors()]); |
| 25 | } |
| 26 | $ip = Request::ip(); |
| 27 | $ips = \IAWP_SCOPED\iawp()->get_option('iawp_blocked_ips', []); |
| 28 | $ip_is_blocked = \in_array($ip, $ips); |
| 29 | echo \IAWP_SCOPED\iawp_blade()->run('settings.block-ips', ['current_ip' => $ip, 'ip_is_blocked' => $ip_is_blocked, 'ips' => $ips]); |
| 30 | echo \IAWP_SCOPED\iawp_blade()->run('settings.block-by-role', ['roles' => \wp_roles()->roles, 'blocked' => \IAWP_SCOPED\iawp()->get_option('iawp_blocked_roles', [])]); |
| 31 | echo \IAWP_SCOPED\iawp_blade()->run('settings.capabilities', ['editable_roles' => $this->get_editable_roles(), 'capabilities' => Capability_Manager::all_capabilities()]); |
| 32 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter'); |
| 33 | echo \IAWP_SCOPED\iawp_blade()->run('settings.export-reports', ['report_finder' => new Report_Finder()]); |
| 34 | echo \IAWP_SCOPED\iawp_blade()->run('settings.export-data'); |
| 35 | echo \IAWP_SCOPED\iawp_blade()->run('settings.delete', ['site_name' => \get_bloginfo('name'), 'site_url' => \site_url()]); |
| 36 | } |
| 37 | public function register_settings() |
| 38 | { |
| 39 | \add_settings_section('iawp-settings-section', \esc_html__('Basic Settings', 'independent-analytics'), function () { |
| 40 | }, 'independent-analytics-settings'); |
| 41 | $args = ['type' => 'boolean', 'default' => \false, 'sanitize_callback' => 'rest_sanitize_boolean']; |
| 42 | \register_setting('iawp_settings', 'iawp_dark_mode', $args); |
| 43 | \add_settings_field('iawp_dark_mode', \esc_html__('Dark mode', 'independent-analytics'), [$this, 'dark_mode_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dark-mode']); |
| 44 | \register_setting('iawp_settings', 'iawp_track_authenticated_users', $args); |
| 45 | \add_settings_field('iawp_track_authenticated_users', \esc_html__('Track logged in users', 'independent-analytics'), [$this, 'track_authenticated_users_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'logged-in']); |
| 46 | \register_setting('iawp_settings', 'iawp_disable_admin_toolbar_analytics', $args); |
| 47 | \add_settings_field('iawp_disable_admin_toolbar_analytics', \esc_html__('Admin toolbar stats', 'independent-analytics'), [$this, 'disable_admin_toolbar_analytics_callback'], 'independent-analytics-settings', 'iawp-settings-section'); |
| 48 | \register_setting('iawp_settings', 'iawp_disable_widget', $args); |
| 49 | \add_settings_field('iawp_disable_widget', \esc_html__('Dashboard widget', 'independent-analytics'), [$this, 'disable_widget_callback'], 'independent-analytics-settings', 'iawp-settings-section'); |
| 50 | $args = ['type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint']; |
| 51 | \register_setting('iawp_settings', 'iawp_dow', $args); |
| 52 | \add_settings_field('iawp_dow', \esc_html__('First day of week', 'independent-analytics'), [$this, 'starting_dow_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dow']); |
| 53 | $args = ['type' => 'boolean', 'default' => \false, 'sanitize_callback' => 'rest_sanitize_boolean']; |
| 54 | \register_setting('iawp_settings', 'iawp_refresh_salt', $args); |
| 55 | \add_settings_field('iawp_refresh_salt', \esc_html__('Salt refresh rate', 'independent-analytics'), [$this, 'refresh_salt_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'salt']); |
| 56 | } |
| 57 | public function dark_mode_callback() |
| 58 | { |
| 59 | echo \IAWP_SCOPED\iawp_blade()->run('settings.dark-mode', ['dark_mode' => \IAWP_SCOPED\iawp()->get_option('iawp_dark_mode', \false)]); |
| 60 | } |
| 61 | public function track_authenticated_users_callback() |
| 62 | { |
| 63 | echo \IAWP_SCOPED\iawp_blade()->run('settings.track-authenticated-users', ['track_authenticated_users' => \IAWP_SCOPED\iawp()->get_option('iawp_track_authenticated_users', \false)]); |
| 64 | } |
| 65 | public function disable_admin_toolbar_analytics_callback() |
| 66 | { |
| 67 | echo \IAWP_SCOPED\iawp_blade()->run('settings.disable-admin-toolbar-analytics', ['value' => \IAWP_SCOPED\iawp()->get_option('iawp_disable_admin_toolbar_analytics', \false)]); |
| 68 | } |
| 69 | public function disable_widget_callback() |
| 70 | { |
| 71 | echo \IAWP_SCOPED\iawp_blade()->run('settings.disable-widget', ['value' => \IAWP_SCOPED\iawp()->get_option('iawp_disable_widget', \false)]); |
| 72 | } |
| 73 | public function starting_dow_callback() |
| 74 | { |
| 75 | echo \IAWP_SCOPED\iawp_blade()->run('settings.first-day-of-week', ['day_of_week' => \IAWP_SCOPED\iawp()->get_option('iawp_dow', 0), 'days' => [0 => \esc_html__('Sunday', 'independent-analytics'), 1 => \esc_html__('Monday', 'independent-analytics'), 2 => \esc_html__('Tuesday', 'independent-analytics'), 3 => \esc_html__('Wednesday', 'independent-analytics'), 4 => \esc_html__('Thursday', 'independent-analytics'), 5 => \esc_html__('Friday', 'independent-analytics'), 6 => \esc_html__('Saturday', 'independent-analytics')]]); |
| 76 | } |
| 77 | public function refresh_salt_callback() |
| 78 | { |
| 79 | echo \IAWP_SCOPED\iawp_blade()->run('settings.refresh-salt', ['refresh_salt' => \IAWP_SCOPED\iawp()->get_option('iawp_refresh_salt', \false)]); |
| 80 | } |
| 81 | public function register_view_counter_settings() |
| 82 | { |
| 83 | \add_settings_section('iawp-view-counter-settings-section', \esc_html__('Public View Counter', 'independent-analytics'), function () { |
| 84 | }, 'independent-analytics-view-counter-settings'); |
| 85 | $args = ['type' => 'boolean', 'default' => \false, 'sanitize_callback' => 'rest_sanitize_boolean']; |
| 86 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_enable', $args); |
| 87 | \add_settings_field('iawp_view_counter_enable', \esc_html__('Enable the view counter', 'independent-analytics'), [$this, 'view_counter_enable_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'enable']); |
| 88 | $args = ['type' => 'array', 'default' => [], 'sanitize_callback' => [$this, 'sanitize_view_counter_post_types']]; |
| 89 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_post_types', $args); |
| 90 | \add_settings_field('iawp_view_counter_post_types', \esc_html__('Display on these post types', 'independent-analytics'), [$this, 'view_counter_post_types_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'post-types']); |
| 91 | $args = ['type' => 'string', 'default' => 'after', 'sanitize_callback' => [$this, 'sanitize_view_counter_position']]; |
| 92 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_position', $args); |
| 93 | \add_settings_field('iawp_view_counter_position', \esc_html__('Show it in this location', 'independent-analytics'), [$this, 'view_counter_position_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'position']); |
| 94 | $args = ['type' => 'string', 'default' => '', 'sanitize_callback' => [$this, 'sanitize_view_counter_exclude']]; |
| 95 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_exclude', $args); |
| 96 | \add_settings_field('iawp_view_counter_exclude', \esc_html__('Exclude these pages', 'independent-analytics'), [$this, 'view_counter_exclude_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'exclude']); |
| 97 | $default = \function_exists('IAWP_SCOPED\\pll__') ? pll__('Views:', 'independent-analytics') : \__('Views:', 'independent-analytics'); |
| 98 | $args = ['type' => 'string', 'default' => $default, 'sanitize_callback' => 'sanitize_text_field']; |
| 99 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_label', $args); |
| 100 | \add_settings_field('iawp_view_counter_label', \esc_html__('Edit the label', 'independent-analytics'), [$this, 'view_counter_label_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'counter-label']); |
| 101 | $args = ['type' => 'boolean', 'default' => \true, 'sanitize_callback' => 'rest_sanitize_boolean']; |
| 102 | \register_setting('iawp_view_counter_settings', 'iawp_view_counter_icon', $args); |
| 103 | \add_settings_field('iawp_view_counter_icon', \esc_html__('Display the icon', 'independent-analytics'), [$this, 'view_counter_icon_callback'], 'independent-analytics-view-counter-settings', 'iawp-view-counter-settings-section', ['class' => 'icon']); |
| 104 | } |
| 105 | public function view_counter_enable_callback() |
| 106 | { |
| 107 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.enable', ['enable' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_enable', \false)]); |
| 108 | } |
| 109 | public function view_counter_post_types_callback() |
| 110 | { |
| 111 | $site_post_types = \get_post_types(['public' => \true], 'objects'); |
| 112 | $counter = 0; |
| 113 | foreach ($site_post_types as $post_type) { |
| 114 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.post-types', ['counter' => $counter, 'post_type' => $post_type, 'saved' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_post_types', [])]); |
| 115 | $counter++; |
| 116 | } |
| 117 | ?> |
| 118 | <p class="description"><?php |
| 119 | \esc_html_e('Uncheck all boxes to only show view count manually. See shortcode documentation below for details.', 'independent-analytics'); |
| 120 | ?></p> |
| 121 | <?php |
| 122 | } |
| 123 | public function view_counter_position_callback() |
| 124 | { |
| 125 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.position', ['position' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_position', 'after')]); |
| 126 | } |
| 127 | public function view_counter_exclude_callback() |
| 128 | { |
| 129 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.exclude', ['exclude' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_exclude', '')]); |
| 130 | } |
| 131 | public function view_counter_label_callback() |
| 132 | { |
| 133 | $default = \function_exists('IAWP_SCOPED\\pll__') ? pll__('Views:', 'independent-analytics') : \__('Views:', 'independent-analytics'); |
| 134 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.label', ['label' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_label', $default)]); |
| 135 | } |
| 136 | public function view_counter_icon_callback() |
| 137 | { |
| 138 | echo \IAWP_SCOPED\iawp_blade()->run('settings.view-counter.icon', ['icon' => \get_option('iawp_view_counter_icon', \true)]); |
| 139 | } |
| 140 | public function register_blocked_ip_settings() |
| 141 | { |
| 142 | \add_settings_section('iawp-blocked-ips-settings-section', \esc_html__('Block IP Addresses', 'independent-analytics'), function () { |
| 143 | }, 'iawp-blocked-ips-settings'); |
| 144 | $args = ['type' => 'array', 'default' => [], 'sanitize_callback' => [$this, 'sanitize_blocked_ips']]; |
| 145 | \register_setting('iawp_blocked_ip_settings', 'iawp_blocked_ips', $args); |
| 146 | } |
| 147 | public function register_email_report_settings() |
| 148 | { |
| 149 | \add_settings_section('iawp-email-report-settings-section', \esc_html__('Scheduled Email Reports', 'independent-analytics'), function () { |
| 150 | }, 'iawp-email-report-settings'); |
| 151 | $args = ['type' => 'number', 'default' => 9, 'sanitize_callback' => [$this, 'sanitize_email_report_time']]; |
| 152 | \register_setting('iawp_email_report_settings', 'iawp_email_report_time', $args); |
| 153 | $args = ['type' => 'array', 'default' => [], 'sanitize_callback' => [$this, 'sanitize_email_addresses']]; |
| 154 | \register_setting('iawp_email_report_settings', 'iawp_email_report_email_addresses', $args); |
| 155 | $args = ['type' => 'array', 'default' => $this->email_report_colors(), 'sanitize_callback' => [$this, 'sanitize_email_report_colors']]; |
| 156 | \register_setting('iawp_email_report_settings', 'iawp_email_report_colors', $args); |
| 157 | } |
| 158 | public function register_block_by_role_settings() |
| 159 | { |
| 160 | \add_settings_section('iawp-block-by-role-settings-section', \esc_html__('Block by User Role', 'independent-analytics'), function () { |
| 161 | }, 'iawp-block-by-role-settings'); |
| 162 | $args = ['type' => 'array', 'default' => [], 'sanitize_callback' => [$this, 'sanitize_blocked_roles']]; |
| 163 | \register_setting('iawp_block_by_role_settings', 'iawp_blocked_roles', $args); |
| 164 | } |
| 165 | public function sanitize_view_counter_post_types($user_input) |
| 166 | { |
| 167 | $site_post_types = \get_post_types(['public' => \true]); |
| 168 | $to_save = []; |
| 169 | foreach ($user_input as $post_type) { |
| 170 | if (\in_array($post_type, $site_post_types)) { |
| 171 | $to_save[] = $post_type; |
| 172 | } |
| 173 | } |
| 174 | return $to_save; |
| 175 | } |
| 176 | public function sanitize_view_counter_position($user_input) |
| 177 | { |
| 178 | if (\in_array($user_input, ['before', 'after', 'both'])) { |
| 179 | return $user_input; |
| 180 | } else { |
| 181 | return 'after'; |
| 182 | } |
| 183 | } |
| 184 | public function sanitize_view_counter_exclude($user_input) |
| 185 | { |
| 186 | $user_input = \explode(',', $user_input); |
| 187 | $to_save = []; |
| 188 | foreach ($user_input as $id) { |
| 189 | $save = \absint($id); |
| 190 | if ($save != 0) { |
| 191 | $to_save[] = $save; |
| 192 | } |
| 193 | } |
| 194 | $to_save = \implode(',', $to_save); |
| 195 | return $to_save; |
| 196 | } |
| 197 | public function sanitize_blocked_ips($user_input) |
| 198 | { |
| 199 | $to_save = []; |
| 200 | foreach ($user_input as $ip) { |
| 201 | if (!String_Util::str_contains($ip, '*')) { |
| 202 | if (\filter_var($ip, \FILTER_VALIDATE_IP)) { |
| 203 | $to_save[] = $ip; |
| 204 | } |
| 205 | } else { |
| 206 | $subs = \explode('.', $ip); |
| 207 | $built_ip = []; |
| 208 | for ($i = 0; $i < \count($subs); $i++) { |
| 209 | if ($subs[$i] == '*') { |
| 210 | $built_ip[] = $subs[$i]; |
| 211 | } else { |
| 212 | $int = \absint($subs[$i]); |
| 213 | if ($int < 0 || $int > 255) { |
| 214 | break; |
| 215 | } |
| 216 | $built_ip[] = $int; |
| 217 | } |
| 218 | if ($i == \count($subs) - 1) { |
| 219 | $to_save[] = \implode('.', $built_ip); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | return $to_save; |
| 225 | } |
| 226 | public function sanitize_email_addresses($emails) |
| 227 | { |
| 228 | $to_save = []; |
| 229 | foreach ($emails as $email) { |
| 230 | $cleaned = \sanitize_email($email); |
| 231 | if (\is_email($cleaned)) { |
| 232 | $to_save[] = $cleaned; |
| 233 | } |
| 234 | } |
| 235 | return $to_save; |
| 236 | } |
| 237 | public function sanitize_email_report_time($user_time) |
| 238 | { |
| 239 | $accepted_times = []; |
| 240 | for ($i = 0; $i < 24; $i++) { |
| 241 | $accepted_times[] = $i; |
| 242 | } |
| 243 | if (\in_array($user_time, $accepted_times)) { |
| 244 | return $user_time; |
| 245 | } else { |
| 246 | return 9; |
| 247 | } |
| 248 | } |
| 249 | public function sanitize_blocked_roles($blocked_roles) |
| 250 | { |
| 251 | $to_save = []; |
| 252 | $user_roles = \array_keys(\wp_roles()->roles); |
| 253 | foreach ($blocked_roles as $blocked) { |
| 254 | if (\in_array($blocked, $user_roles)) { |
| 255 | $to_save[] = $blocked; |
| 256 | } |
| 257 | } |
| 258 | return $to_save; |
| 259 | } |
| 260 | public function sanitize_email_report_colors($colors) |
| 261 | { |
| 262 | $to_save = []; |
| 263 | // No idea why WP sometimes returns an array, so this is for my sanity |
| 264 | if (\is_string($colors)) { |
| 265 | $colors = \explode(',', $colors); |
| 266 | } |
| 267 | foreach ($colors as $color) { |
| 268 | $to_save[] = \sanitize_hex_color($color); |
| 269 | } |
| 270 | return $to_save; |
| 271 | } |
| 272 | /** |
| 273 | * @return array |
| 274 | */ |
| 275 | private function get_editable_roles() : array |
| 276 | { |
| 277 | $editable_roles = []; |
| 278 | $wp_roles = \wp_roles()->roles; |
| 279 | \array_walk($wp_roles, function ($role, $role_key) use(&$editable_roles) { |
| 280 | if ($role_key === 'administrator') { |
| 281 | return; |
| 282 | } |
| 283 | $read_only_access = $role['capabilities']['iawp_read_only_access'] ?? \false; |
| 284 | $full_access = $role['capabilities']['iawp_full_access'] ?? \false; |
| 285 | $editable_roles[] = ['key' => $role_key, 'name' => $role['name'], 'iawp_no_access' => !$read_only_access && !$full_access, 'iawp_read_only_access' => $read_only_access, 'iawp_full_access' => $full_access]; |
| 286 | }); |
| 287 | return $editable_roles; |
| 288 | } |
| 289 | private function email_report_colors() |
| 290 | { |
| 291 | return ['#5123a0', '#fafafa', '#3a1e6b', '#fafafa', '#5123a0', '#a985e6']; |
| 292 | } |
| 293 | } |
| 294 |