| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Statify: Statify_Settings class |
|---|
| 4 | * |
|---|
| 5 | * This file contains the plugin's settings capabilities. |
|---|
| 6 | * |
|---|
| 7 | * @package Statify |
|---|
| 8 | * @since 1.7 |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | // Quit if accessed directly.. |
|---|
| 12 | defined( 'ABSPATH' ) || exit; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Class Statify_Settings |
|---|
| 16 | * |
|---|
| 17 | * @since 1.7 |
|---|
| 18 | */ |
|---|
| 19 | class Statify_Settings { |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Registers all options using the WP Settings API. |
|---|
| 23 | * |
|---|
| 24 | * @return void |
|---|
| 25 | */ |
|---|
| 26 | public static function register_settings() { |
|---|
| 27 | register_setting( 'statify', 'statify', array( __CLASS__, 'sanitize_options' ) ); |
|---|
| 28 | |
|---|
| 29 | // Global settings. |
|---|
| 30 | add_settings_section( |
|---|
| 31 | 'statify-global', |
|---|
| 32 | __( 'Global settings', 'statify' ), |
|---|
| 33 | null, |
|---|
| 34 | 'statify' |
|---|
| 35 | ); |
|---|
| 36 | add_settings_field( |
|---|
| 37 | 'statify-days', |
|---|
| 38 | __( 'Period of data saving', 'statify' ), |
|---|
| 39 | array( __CLASS__, 'options_days' ), |
|---|
| 40 | 'statify', |
|---|
| 41 | 'statify-global', |
|---|
| 42 | array( 'label_for' => 'statify-days' ) |
|---|
| 43 | ); |
|---|
| 44 | add_settings_field( |
|---|
| 45 | 'statify-snippet', |
|---|
| 46 | __( 'Tracking method', 'statify' ), |
|---|
| 47 | array( __CLASS__, 'options_snippet' ), |
|---|
| 48 | 'statify', |
|---|
| 49 | 'statify-global', |
|---|
| 50 | array( 'label_for' => 'statify-snippet' ) |
|---|
| 51 | ); |
|---|
| 52 | |
|---|
| 53 | // Dashboard widget settings. |
|---|
| 54 | add_settings_section( |
|---|
| 55 | 'statify-dashboard', |
|---|
| 56 | __( 'Dashboard Widget', 'statify' ), |
|---|
| 57 | array( __CLASS__, 'header_dashboard' ), |
|---|
| 58 | 'statify' |
|---|
| 59 | ); |
|---|
| 60 | add_settings_field( |
|---|
| 61 | 'statify-days_show', |
|---|
| 62 | __( 'Period of data display in Dashboard', 'statify' ), |
|---|
| 63 | array( __CLASS__, 'options_days_show' ), |
|---|
| 64 | 'statify', |
|---|
| 65 | 'statify-dashboard', |
|---|
| 66 | array( 'label_for' => 'statify-days-show' ) |
|---|
| 67 | ); |
|---|
| 68 | add_settings_field( |
|---|
| 69 | 'statify-limit', |
|---|
| 70 | __( 'Number of entries in top lists', 'statify' ), |
|---|
| 71 | array( __CLASS__, 'options_limit' ), |
|---|
| 72 | 'statify', |
|---|
| 73 | 'statify-dashboard', |
|---|
| 74 | array( 'label_for' => 'statify-limit' ) |
|---|
| 75 | ); |
|---|
| 76 | add_settings_field( |
|---|
| 77 | 'statify-today', |
|---|
| 78 | __( 'Top lists only for today', 'statify' ), |
|---|
| 79 | array( __CLASS__, 'options_today' ), |
|---|
| 80 | 'statify', |
|---|
| 81 | 'statify-dashboard', |
|---|
| 82 | array( 'label_for' => 'statify-today' ) |
|---|
| 83 | ); |
|---|
| 84 | add_settings_field( |
|---|
| 85 | 'statify-show-totals', |
|---|
| 86 | __( 'Show totals', 'statify' ), |
|---|
| 87 | array( __CLASS__, 'options_show_totals' ), |
|---|
| 88 | 'statify', |
|---|
| 89 | 'statify-dashboard', |
|---|
| 90 | array( 'label_for' => 'statify-show-totals' ) |
|---|
| 91 | ); |
|---|
| 92 | |
|---|
| 93 | // Exclusion settings. |
|---|
| 94 | add_settings_section( |
|---|
| 95 | 'statify-skip', |
|---|
| 96 | __( 'Skip tracking for ...', 'statify' ), |
|---|
| 97 | array( __CLASS__, 'header_skip' ), |
|---|
| 98 | 'statify' |
|---|
| 99 | ); |
|---|
| 100 | add_settings_field( |
|---|
| 101 | 'statify-skip-referrer', |
|---|
| 102 | __( 'Disallowed referrers', 'statify' ), |
|---|
| 103 | array( __CLASS__, 'options_skip_blacklist' ), |
|---|
| 104 | 'statify', |
|---|
| 105 | 'statify-skip', |
|---|
| 106 | array( 'label_for' => 'statify-skip-referrer' ) |
|---|
| 107 | ); |
|---|
| 108 | add_settings_field( |
|---|
| 109 | 'statify-skip-logged_in', |
|---|
| 110 | __( 'Logged in users', 'statify' ), |
|---|
| 111 | array( __CLASS__, 'options_skip_logged_in' ), |
|---|
| 112 | 'statify', |
|---|
| 113 | 'statify-skip', |
|---|
| 114 | array( 'label_for' => 'statify-skip-logged_in' ) |
|---|
| 115 | ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Option for data collection period. |
|---|
| 120 | * |
|---|
| 121 | * @return void |
|---|
| 122 | */ |
|---|
| 123 | public static function options_days() { |
|---|
| 124 | ?> |
|---|
| 125 | <input id="statify-days" name="statify[days]" type="number" min="1" value="<?php echo esc_attr( Statify::$_options['days'] ); ?>"> |
|---|
| 126 | <?php esc_html_e( 'days', 'statify' ); ?> |
|---|
| 127 | (<?php esc_html_e( 'Default', 'statify' ); ?>: 14) |
|---|
| 128 | <?php |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Option for tracking via JS. |
|---|
| 133 | * |
|---|
| 134 | * @return void |
|---|
| 135 | */ |
|---|
| 136 | public static function options_snippet() { |
|---|
| 137 | ?> |
|---|
| 138 | <p> |
|---|
| 139 | <?php self::show_snippet_option( Statify::TRACKING_METHOD_DEFAULT, __( 'Default tracking', 'statify' ) ); ?> |
|---|
| 140 | <br> |
|---|
| 141 | <?php self::show_snippet_option( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK, __( 'JavaScript based tracking with nonce check', 'statify' ) ); ?> |
|---|
| 142 | <br> |
|---|
| 143 | <?php self::show_snippet_option( Statify::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK, __( 'JavaScript based tracking without nonce check', 'statify' ) ); ?> |
|---|
| 144 | </p> |
|---|
| 145 | <p class="description"> |
|---|
| 146 | <?php esc_html_e( 'JavaScript based tracking is strongly recommended if caching or AMP is in use.', 'statify' ); ?> |
|---|
| 147 | <?php esc_html_e( 'Disable the nonce check if the caching time is longer than the nonce time or you miss views due to 403 Forbidden errors.', 'statify' ); ?> |
|---|
| 148 | </p> |
|---|
| 149 | <?php |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Outputs the input radio for an option. |
|---|
| 154 | * |
|---|
| 155 | * @param int $value the value for the input radio. |
|---|
| 156 | * @param string $label the label. |
|---|
| 157 | */ |
|---|
| 158 | private static function show_snippet_option( $value, $label ) { |
|---|
| 159 | ?> |
|---|
| 160 | <label> |
|---|
| 161 | <input name="statify[snippet]" type="radio" value="<?php echo esc_html( $value ); ?>" <?php checked( Statify::$_options['snippet'], $value ); ?>> |
|---|
| 162 | <?php echo esc_html( $label ); ?> |
|---|
| 163 | </label> |
|---|
| 164 | <?php |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Section header for "Dashboard Widget" section. |
|---|
| 169 | * |
|---|
| 170 | * @return void |
|---|
| 171 | */ |
|---|
| 172 | public static function header_dashboard() { |
|---|
| 173 | ?> |
|---|
| 174 | <p> |
|---|
| 175 | <?php esc_html_e( 'The following options affect the admin dashboard widget.', 'statify' ); ?> |
|---|
| 176 | </p> |
|---|
| 177 | <?php |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Option for data display period. |
|---|
| 182 | * |
|---|
| 183 | * @return void |
|---|
| 184 | */ |
|---|
| 185 | public static function options_days_show() { |
|---|
| 186 | ?> |
|---|
| 187 | <input id="statify-days-show" name="statify[days_show]" type="number" min="1" value="<?php echo esc_attr( Statify::$_options['days_show'] ); ?>"> |
|---|
| 188 | <?php esc_html_e( 'days', 'statify' ); ?> |
|---|
| 189 | (<?php esc_html_e( 'Default', 'statify' ); ?>: 14) |
|---|
| 190 | <?php |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Option for number of entries in top lists. |
|---|
| 195 | * |
|---|
| 196 | * @return void |
|---|
| 197 | */ |
|---|
| 198 | public static function options_limit() { |
|---|
| 199 | ?> |
|---|
| 200 | <input id="statify-limit" name="statify[limit]" type="number" min="1" max="100" value="<?php echo esc_attr( Statify::$_options['limit'] ); ?>"> |
|---|
| 201 | (<?php esc_html_e( 'Default', 'statify' ); ?>: 3) |
|---|
| 202 | <?php |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Option for number of entries in top lists. |
|---|
| 207 | * |
|---|
| 208 | * @return void |
|---|
| 209 | */ |
|---|
| 210 | public static function options_today() { |
|---|
| 211 | ?> |
|---|
| 212 | <input id="statify-today" type="checkbox" name="statify[today]" value="1" <?php checked( Statify::$_options['today'], 1 ); ?>> |
|---|
| 213 | (<?php esc_html_e( 'Default', 'statify' ); ?>: <?php esc_html_e( 'No', 'statify' ); ?>) |
|---|
| 214 | <?php |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * Option for showing visit totals. |
|---|
| 219 | * |
|---|
| 220 | * @return void |
|---|
| 221 | */ |
|---|
| 222 | public static function options_show_totals() { |
|---|
| 223 | ?> |
|---|
| 224 | <input id="statify-show-totals" type="checkbox" name="statify[show_totals]" value="1" <?php checked( Statify::$_options['show_totals'], 1 ); ?>> |
|---|
| 225 | (<?php esc_html_e( 'Default', 'statify' ); ?>: <?php esc_html_e( 'No', 'statify' ); ?>) |
|---|
| 226 | <?php |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | /** |
|---|
| 230 | * Section header for "Skip tracking for..." section. |
|---|
| 231 | * |
|---|
| 232 | * @return void |
|---|
| 233 | */ |
|---|
| 234 | public static function header_skip() { |
|---|
| 235 | ?> |
|---|
| 236 | <p> |
|---|
| 237 | <?php echo wp_kses( __( 'The following options define cases in which a view will <strong>not</strong> be tracked.', 'statify' ), array( 'strong' => array() ) ); ?> |
|---|
| 238 | </p> |
|---|
| 239 | <?php |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | /** |
|---|
| 243 | * Option to skip tracking for disallowed referrers. |
|---|
| 244 | * |
|---|
| 245 | * @return void |
|---|
| 246 | */ |
|---|
| 247 | public static function options_skip_blacklist() { |
|---|
| 248 | ?> |
|---|
| 249 | <input id="statify-skip-referrer" type="checkbox" name="statify[blacklist]" value="1"<?php checked( Statify::$_options['blacklist'] ); ?>> |
|---|
| 250 | (<?php esc_html_e( 'Default', 'statify' ); ?>: <?php esc_html_e( 'No', 'statify' ); ?>) |
|---|
| 251 | <p class="description"><?php esc_html_e( 'Enabling this option excludes any views with referrers listed in the list of disallowed comment keys.', 'statify' ); ?></p> |
|---|
| 252 | <?php |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | /** |
|---|
| 256 | * Option to skip tracking for logged in uses. |
|---|
| 257 | * |
|---|
| 258 | * @return void |
|---|
| 259 | */ |
|---|
| 260 | public static function options_skip_logged_in() { |
|---|
| 261 | ?> |
|---|
| 262 | <input id="statify-skip-logged_in" type="checkbox" name="statify[skip][logged_in]" value="1"<?php checked( Statify::$_options['skip']['logged_in'] ); ?>> |
|---|
| 263 | (<?php esc_html_e( 'Default', 'statify' ); ?>: <?php esc_html_e( 'Yes', 'statify' ); ?>) |
|---|
| 264 | <p class="description"><?php esc_html_e( 'Enabling this option excludes any views of logged-in users from tracking.', 'statify' ); ?></p> |
|---|
| 265 | <?php |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * Action to be triggered after Statify options have been saved. |
|---|
| 270 | * Delete transient data to refresh the dashboard widget and flushes Cachify cache, if the plugin is available and |
|---|
| 271 | * JS settings have changed. |
|---|
| 272 | * |
|---|
| 273 | * @since 1.7.1 |
|---|
| 274 | * |
|---|
| 275 | * @param array $old_value The old options value. |
|---|
| 276 | * @param array $value The updated options value. |
|---|
| 277 | * |
|---|
| 278 | * @return void |
|---|
| 279 | */ |
|---|
| 280 | public static function action_update_options( $old_value, $value ) { |
|---|
| 281 | // Delete transient. |
|---|
| 282 | delete_transient( 'statify_data' ); |
|---|
| 283 | |
|---|
| 284 | // Clear Cachify cache, if JS settings have changed. |
|---|
| 285 | if ( $old_value['snippet'] !== $value['snippet'] && has_action( 'cachify_flush_cache' ) ) { |
|---|
| 286 | do_action( 'cachify_flush_cache' ); |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * Validate and sanitize submitted options. |
|---|
| 292 | * |
|---|
| 293 | * @param array $options Original options. |
|---|
| 294 | * |
|---|
| 295 | * @return array Validated and sanitized options. |
|---|
| 296 | */ |
|---|
| 297 | public static function sanitize_options( $options ) { |
|---|
| 298 | |
|---|
| 299 | // Sanitize numeric values. |
|---|
| 300 | $res = array(); |
|---|
| 301 | foreach ( array( 'days', 'days_show', 'limit' ) as $o ) { |
|---|
| 302 | $res[ $o ] = Statify::$_options[ $o ]; |
|---|
| 303 | if ( isset( $options[ $o ] ) && (int) $options[ $o ] > 0 ) { |
|---|
| 304 | $res[ $o ] = (int) $options[ $o ]; |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | if ( $res['limit'] > 100 ) { |
|---|
| 308 | $res['limit'] = 100; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | if ( isset( $options['snippet'] ) ) { |
|---|
| 312 | $method = (int) $options['snippet']; |
|---|
| 313 | if ( in_array( |
|---|
| 314 | $method, |
|---|
| 315 | array( |
|---|
| 316 | Statify::TRACKING_METHOD_DEFAULT, |
|---|
| 317 | Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK, |
|---|
| 318 | Statify::TRACKING_METHOD_JAVASCRIPT_WITHOUT_NONCE_CHECK, |
|---|
| 319 | ), |
|---|
| 320 | true |
|---|
| 321 | ) ) { |
|---|
| 322 | $res['snippet'] = $method; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | // Get checkbox values. |
|---|
| 327 | foreach ( array( 'today', 'blacklist', 'show_totals' ) as $o ) { |
|---|
| 328 | $res[ $o ] = isset( $options[ $o ] ) && 1 === (int) $options[ $o ] ? 1 : 0; |
|---|
| 329 | } |
|---|
| 330 | $res['skip']['logged_in'] = isset( $options['skip']['logged_in'] ) && 1 === (int) $options['skip']['logged_in'] ? 1 : 0; |
|---|
| 331 | |
|---|
| 332 | return $res; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * Creates a menu entry in the settings menu. |
|---|
| 337 | * |
|---|
| 338 | * @return void |
|---|
| 339 | */ |
|---|
| 340 | public static function add_admin_menu() { |
|---|
| 341 | add_options_page( |
|---|
| 342 | __( 'Statify', 'statify' ), |
|---|
| 343 | __( 'Statify', 'statify' ), |
|---|
| 344 | 'manage_options', |
|---|
| 345 | 'statify-settings', |
|---|
| 346 | array( __CLASS__, 'create_settings_page' ) |
|---|
| 347 | ); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | /** |
|---|
| 351 | * Creates the settings pages. |
|---|
| 352 | * |
|---|
| 353 | * @return void |
|---|
| 354 | */ |
|---|
| 355 | public static function create_settings_page() { |
|---|
| 356 | ?> |
|---|
| 357 | |
|---|
| 358 | <div class="wrap"> |
|---|
| 359 | <h1><?php esc_html_e( 'Statify Settings', 'statify' ); ?></h1> |
|---|
| 360 | |
|---|
| 361 | <form id="statify-settings" method="post" action="options.php"> |
|---|
| 362 | <?php |
|---|
| 363 | settings_fields( 'statify' ); |
|---|
| 364 | do_settings_sections( 'statify' ); |
|---|
| 365 | submit_button(); |
|---|
| 366 | ?> |
|---|
| 367 | <p class="alignright"> |
|---|
| 368 | <a href="<?php echo esc_url( __( 'https://wordpress.org/plugins/statify/', 'statify' ) ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Documentation', 'statify' ); ?></a> |
|---|
| 369 | • <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=TD4AMD2D8EMZW" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Donate', 'statify' ); ?></a> |
|---|
| 370 | • <a href="<?php echo esc_url( __( 'https://wordpress.org/support/plugin/statify', 'statify' ) ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Support', 'statify' ); ?></a> |
|---|
| 371 | </p> |
|---|
| 372 | |
|---|
| 373 | </form> |
|---|
| 374 | </div> |
|---|
| 375 | |
|---|
| 376 | <?php |
|---|
| 377 | } |
|---|
| 378 | } |
|---|