ajax
3 years ago
migrations
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
dashboard_options.php
3 years ago
dashboard_widget.php
3 years ago
db.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
independent_analytics.php
3 years ago
known_referrers.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
312 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 | } |
| 13 | |
| 14 | /** |
| 15 | * @return array |
| 16 | */ |
| 17 | private function get_editable_roles(): array |
| 18 | { |
| 19 | $editable_roles = []; |
| 20 | $wp_roles = wp_roles()->roles; |
| 21 | |
| 22 | array_walk($wp_roles, function ($role, $role_key) use (&$editable_roles) { |
| 23 | if ($role_key === 'administrator') { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | $read_only_access = $role['capabilities']['iawp_read_only_access'] ?? false; |
| 28 | $full_access = $role['capabilities']['iawp_full_access'] ?? false; |
| 29 | |
| 30 | $editable_roles[] = [ |
| 31 | 'key' => $role_key, |
| 32 | 'name' => $role['name'], |
| 33 | 'iawp_no_access' => !$read_only_access && !$full_access, |
| 34 | 'iawp_read_only_access' => $read_only_access, |
| 35 | 'iawp_full_access' => $full_access, |
| 36 | ]; |
| 37 | }); |
| 38 | |
| 39 | return $editable_roles; |
| 40 | } |
| 41 | |
| 42 | public function render_settings() |
| 43 | { |
| 44 | echo IAWP()->templates()->render('settings/index'); |
| 45 | echo IAWP()->templates()->render('settings/block_ips', [ |
| 46 | 'ips' => IAWP()->get_option('iawp_blocked_ips', []), |
| 47 | ]); |
| 48 | echo IAWP()->templates()->render('settings/capabilities', [ |
| 49 | 'editable_roles' => $this->get_editable_roles(), |
| 50 | 'capabilities' => Capability_Manager::all_capabilities(), |
| 51 | ]); |
| 52 | echo IAWP()->templates()->render('settings/view_counter'); |
| 53 | echo IAWP()->templates()->render('settings/export'); |
| 54 | echo IAWP()->templates()->render('settings/delete', [ |
| 55 | 'site_name' => get_bloginfo('name'), |
| 56 | 'site_url' => site_url(), |
| 57 | ]); |
| 58 | } |
| 59 | |
| 60 | public function register_settings() |
| 61 | { |
| 62 | add_settings_section('iawp-settings-section', esc_html__('Basic Settings', 'iawp'), '', 'independent-analytics-settings'); |
| 63 | |
| 64 | $args = [ |
| 65 | 'type' => 'boolean', |
| 66 | 'default' => false, |
| 67 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 68 | ]; |
| 69 | register_setting('iawp_settings', 'iawp_dark_mode', $args); |
| 70 | add_settings_field('iawp_dark_mode', esc_html__('Dark mode', 'iawp'), [$this, 'dark_mode_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dark-mode']); |
| 71 | |
| 72 | register_setting('iawp_settings', 'iawp_track_authenticated_users', $args); |
| 73 | 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']); |
| 74 | |
| 75 | $args = [ |
| 76 | 'type' => 'integer', |
| 77 | 'default' => 1, |
| 78 | 'sanitize_callback' => 'absint', |
| 79 | ]; |
| 80 | register_setting('iawp_settings', 'iawp_dow', $args); |
| 81 | add_settings_field('iawp_dow', esc_html__('First day of week', 'iawp'), [$this, 'starting_dow_callback'], 'independent-analytics-settings', 'iawp-settings-section', ['class' => 'dow']); |
| 82 | } |
| 83 | |
| 84 | public function dark_mode_callback() |
| 85 | { |
| 86 | echo IAWP()->templates()->render('settings/dark_mode', [ |
| 87 | 'dark_mode' => IAWP()->get_option('iawp_dark_mode', false), |
| 88 | ]); |
| 89 | } |
| 90 | |
| 91 | public function track_authenticated_users_callback() |
| 92 | { |
| 93 | echo IAWP()->templates()->render('settings/track_authenticated_users', [ |
| 94 | 'track_authenticated_users' => IAWP()->get_option('iawp_track_authenticated_users', false), |
| 95 | ]); |
| 96 | } |
| 97 | |
| 98 | public function starting_dow_callback() |
| 99 | { |
| 100 | echo IAWP()->templates()->render('settings/first_day_of_week', [ |
| 101 | 'day_of_week' => IAWP()->get_option('iawp_dow', 0), |
| 102 | 'days' => [ |
| 103 | 0 => esc_html__('Sunday', 'iawp'), |
| 104 | 1 => esc_html__('Monday', 'iawp'), |
| 105 | 2 => esc_html__('Tuesday', 'iawp'), |
| 106 | 3 => esc_html__('Wednesday', 'iawp'), |
| 107 | 4 => esc_html__('Thursday', 'iawp'), |
| 108 | 5 => esc_html__('Friday', 'iawp'), |
| 109 | 6 => esc_html__('Saturday', 'iawp'), |
| 110 | ], |
| 111 | ]); |
| 112 | } |
| 113 | |
| 114 | public function register_view_counter_settings() |
| 115 | { |
| 116 | add_settings_section('iawp-view-counter-settings-section', esc_html__('Public View Counter', 'iawp'), '', 'independent-analytics-view-counter-settings'); |
| 117 | |
| 118 | $args = [ |
| 119 | 'type' => 'boolean', |
| 120 | 'default' => false, |
| 121 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 122 | ]; |
| 123 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_enable', $args); |
| 124 | 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']); |
| 125 | |
| 126 | $args = [ |
| 127 | 'type' => 'array', |
| 128 | 'default' => [], |
| 129 | 'sanitize_callback' => [$this, 'sanitize_view_counter_post_types'], |
| 130 | ]; |
| 131 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_post_types', $args); |
| 132 | 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']); |
| 133 | |
| 134 | $args = [ |
| 135 | 'type' => 'string', |
| 136 | 'default' => 'after', |
| 137 | 'sanitize_callback' => [$this, 'sanitize_view_counter_position'], |
| 138 | ]; |
| 139 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_position', $args); |
| 140 | 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']); |
| 141 | |
| 142 | $args = [ |
| 143 | 'type' => 'string', |
| 144 | 'default' => '', |
| 145 | 'sanitize_callback' => [$this, 'sanitize_view_counter_exclude'], |
| 146 | ]; |
| 147 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_exclude', $args); |
| 148 | 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']); |
| 149 | |
| 150 | $args = [ |
| 151 | 'type' => 'string', |
| 152 | 'default' => '', |
| 153 | 'sanitize_callback' => 'sanitize_text_field', |
| 154 | ]; |
| 155 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_label', $args); |
| 156 | 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']); |
| 157 | |
| 158 | $args = [ |
| 159 | 'type' => 'boolean', |
| 160 | 'default' => true, |
| 161 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 162 | ]; |
| 163 | register_setting('iawp_view_counter_settings', 'iawp_view_counter_icon', $args); |
| 164 | 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']); |
| 165 | } |
| 166 | |
| 167 | public function view_counter_enable_callback() |
| 168 | { |
| 169 | echo IAWP()->templates()->render('settings/view_counter/enable', [ |
| 170 | 'enable' => IAWP()->get_option('iawp_view_counter_enable', false), |
| 171 | ]); |
| 172 | } |
| 173 | |
| 174 | public function view_counter_post_types_callback() |
| 175 | { |
| 176 | $site_post_types = get_post_types(['public' => true], 'objects'); |
| 177 | $counter = 0; |
| 178 | foreach ($site_post_types as $post_type) { |
| 179 | echo IAWP()->templates()->render('settings/view_counter/post_types', [ |
| 180 | 'counter' => $counter, |
| 181 | 'post_type' => $post_type, |
| 182 | 'saved' => IAWP()->get_option('iawp_view_counter_post_types', []), |
| 183 | ]); |
| 184 | $counter++; |
| 185 | } ?> |
| 186 | <p class="description"><?php esc_html_e('Uncheck all boxes to only show view count manually. See shortcode documentation below for details.', 'iawp'); ?></p> |
| 187 | <?php |
| 188 | } |
| 189 | |
| 190 | public function view_counter_position_callback() |
| 191 | { |
| 192 | echo IAWP()->templates()->render('settings/view_counter/position', [ |
| 193 | 'position' => IAWP()->get_option('iawp_view_counter_position', 'after'), |
| 194 | ]); |
| 195 | } |
| 196 | |
| 197 | public function view_counter_exclude_callback() |
| 198 | { |
| 199 | echo IAWP()->templates()->render('settings/view_counter/exclude', [ |
| 200 | 'exclude' => IAWP()->get_option('iawp_view_counter_exclude', ''), |
| 201 | ]); |
| 202 | } |
| 203 | |
| 204 | public function view_counter_label_callback() |
| 205 | { |
| 206 | echo IAWP()->templates()->render('settings/view_counter/label', [ |
| 207 | 'label' => IAWP()->get_option('iawp_view_counter_label', esc_html__('Views:', 'iawp')), |
| 208 | ]); |
| 209 | } |
| 210 | |
| 211 | public function view_counter_icon_callback() |
| 212 | { |
| 213 | echo IAWP()->templates()->render('settings/view_counter/icon', [ |
| 214 | 'icon' => get_option('iawp_view_counter_icon', true), |
| 215 | ]); |
| 216 | } |
| 217 | |
| 218 | public function register_blocked_ip_settings() |
| 219 | { |
| 220 | add_settings_section('iawp-blocked-ips-settings-section', esc_html__('Block IP Addresses', 'iawp'), '', 'iawp-blocked-ips-settings'); |
| 221 | |
| 222 | $args = [ |
| 223 | 'type' => 'array', |
| 224 | 'default' => [], |
| 225 | 'sanitize_callback' => [$this, 'sanitize_blocked_ips'], |
| 226 | ]; |
| 227 | register_setting('iawp_blocked_ip_settings', 'iawp_blocked_ips', $args); |
| 228 | } |
| 229 | |
| 230 | public function register_user_permission_settings() |
| 231 | { |
| 232 | add_settings_section('iawp-user-permission-settings-section', esc_html__('User Permissions', 'iawp'), '', 'iawp-user-permission-settings'); |
| 233 | |
| 234 | $args = [ |
| 235 | 'type' => 'bool', |
| 236 | 'default' => false, |
| 237 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 238 | ]; |
| 239 | register_setting('iawp_user_permission_settings', 'iawp_white_label', $args); |
| 240 | } |
| 241 | |
| 242 | public function sanitize_view_counter_post_types($user_input) |
| 243 | { |
| 244 | $site_post_types = get_post_types(['public' => true]); |
| 245 | $to_save = []; |
| 246 | foreach ($user_input as $post_type) { |
| 247 | if (in_array($post_type, $site_post_types)) { |
| 248 | $to_save[] = $post_type; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return $to_save; |
| 253 | } |
| 254 | |
| 255 | public function sanitize_view_counter_position($user_input) |
| 256 | { |
| 257 | if (in_array($user_input, ['before', 'after', 'both'])) { |
| 258 | return $user_input; |
| 259 | } else { |
| 260 | return 'after'; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | public function sanitize_view_counter_exclude($user_input) |
| 265 | { |
| 266 | $user_input = explode(',', $user_input); |
| 267 | $to_save = []; |
| 268 | |
| 269 | foreach ($user_input as $id) { |
| 270 | $save = absint($id); |
| 271 | if ($save != 0) { |
| 272 | $to_save[] = $save; |
| 273 | } |
| 274 | } |
| 275 | $to_save = implode(',', $to_save); |
| 276 | |
| 277 | return $to_save; |
| 278 | } |
| 279 | |
| 280 | public function sanitize_blocked_ips($user_input) |
| 281 | { |
| 282 | $to_save = []; |
| 283 | foreach ($user_input as $ip) { |
| 284 | if (!StringUtil::str_contains($ip, '*')) { |
| 285 | if (filter_var($ip, FILTER_VALIDATE_IP)) { |
| 286 | $to_save[] = $ip; |
| 287 | } |
| 288 | } else { |
| 289 | $subs = explode('.', $ip); |
| 290 | $built_ip = []; |
| 291 | |
| 292 | for ($i = 0; $i < count($subs); $i++) { |
| 293 | if ($subs[$i] == '*') { |
| 294 | $built_ip[] = $subs[$i]; |
| 295 | } else { |
| 296 | $int = absint($subs[$i]); |
| 297 | if ($int < 0 || $int > 255) { |
| 298 | break; |
| 299 | } |
| 300 | $built_ip[] = $int; |
| 301 | } |
| 302 | if ($i == count($subs) - 1) { |
| 303 | $to_save[] = implode('.', $built_ip); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return $to_save; |
| 310 | } |
| 311 | } |
| 312 |