Admin.php
1 year ago
admin-page.php
1 year ago
screen-debug.php
1 year ago
screen-stats.php
1 year ago
screen-tools.php
1 year ago
admin-page.php
141 lines
| 1 | <?php |
| 2 | if ( basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__) ) { |
| 3 | exit('Please do not load this page directly'); |
| 4 | } |
| 5 | |
| 6 | $wpp_tabs = [ |
| 7 | 'stats' => __('Stats', 'wordpress-popular-posts'), |
| 8 | 'tools' => __('Tools', 'wordpress-popular-posts'), |
| 9 | 'params' => __('Parameters', 'wordpress-popular-posts'), |
| 10 | 'debug' => 'Debug' |
| 11 | ]; |
| 12 | |
| 13 | // Set active tab |
| 14 | if ( isset($_GET['tab'] ) && isset($wpp_tabs[$_GET['tab']] ) ) { |
| 15 | $current = $_GET['tab']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $current will be equal to one of the registered tabs |
| 16 | } else { |
| 17 | $current = 'stats'; |
| 18 | } |
| 19 | |
| 20 | // Update options on form submission |
| 21 | if ( isset($_POST['section']) ) { |
| 22 | |
| 23 | if ( 'stats' == $_POST['section'] ) { |
| 24 | $current = 'stats'; |
| 25 | |
| 26 | if ( isset($_POST['wpp-update-stats-options-token']) && wp_verify_nonce($_POST['wpp-update-stats-options-token'], 'wpp-update-stats-options') ) { |
| 27 | $this->config['stats']['limit'] = ( \WordPressPopularPosts\Helper::is_number($_POST['stats_limit']) && $_POST['stats_limit'] > 0 ) ? (int) $_POST['stats_limit'] : 10; |
| 28 | $this->config['stats']['post_type'] = empty($_POST['stats_type']) ? 'post' : sanitize_text_field($_POST['stats_type']); |
| 29 | $this->config['stats']['freshness'] = isset($_POST['stats_freshness']); |
| 30 | |
| 31 | update_option('wpp_settings_config', $this->config); |
| 32 | echo '<div class="notice notice-success is-dismissible"><p><strong>' . esc_html(__('Settings saved.', 'wordpress-popular-posts')) . '</strong></p></div>'; |
| 33 | } |
| 34 | } |
| 35 | elseif ( 'misc' == $_POST['section'] ) { |
| 36 | $current = 'tools'; |
| 37 | |
| 38 | if ( isset($_POST['wpp-update-misc-options-token'] ) && wp_verify_nonce($_POST['wpp-update-misc-options-token'], 'wpp-update-misc-options') ) { |
| 39 | $this->config['tools']['link']['target'] = sanitize_text_field($_POST['link_target']); |
| 40 | $this->config['tools']['css'] = (bool) $_POST['css']; |
| 41 | $this->config['tools']['experimental'] = isset($_POST['experimental_features']); |
| 42 | |
| 43 | update_option('wpp_settings_config', $this->config); |
| 44 | echo '<div class="notice notice-success is-dismissible"><p><strong>' . esc_html(__('Settings saved.', 'wordpress-popular-posts')) . '</strong></p></div>'; |
| 45 | } |
| 46 | } |
| 47 | elseif ( 'thumb' == $_POST['section'] ) { |
| 48 | $current = 'tools'; |
| 49 | |
| 50 | if ( isset($_POST['wpp-update-thumbnail-options-token']) && wp_verify_nonce($_POST['wpp-update-thumbnail-options-token'], 'wpp-update-thumbnail-options') ) { |
| 51 | if ( |
| 52 | $_POST['thumb_source'] == 'custom_field' |
| 53 | && ( ! isset($_POST['thumb_field']) || empty($_POST['thumb_field']) ) |
| 54 | ) { |
| 55 | echo '<div class="notice notice-error"><p>' . esc_html(__('Please provide the name of your custom field.', 'wordpress-popular-posts')) . '</p></div>'; |
| 56 | } |
| 57 | else { |
| 58 | // thumbnail settings changed, flush transients |
| 59 | if ( $this->config['tools']['cache']['active'] ) { |
| 60 | $this->flush_transients(); |
| 61 | } |
| 62 | |
| 63 | $thumbnail_format = sanitize_text_field($_POST['thumb_format']); |
| 64 | |
| 65 | if ( $thumbnail_format !== $this->config['tools']['thumbnail']['format'] ) { |
| 66 | $this->delete_thumbnails(); |
| 67 | } |
| 68 | |
| 69 | $this->config['tools']['thumbnail']['source'] = sanitize_text_field($_POST['thumb_source']); |
| 70 | $this->config['tools']['thumbnail']['format'] = $thumbnail_format; |
| 71 | $this->config['tools']['thumbnail']['field'] = ( ! empty($_POST['thumb_field']) ) ? sanitize_text_field($_POST['thumb_field']) : 'wpp_thumbnail'; |
| 72 | $this->config['tools']['thumbnail']['default'] = ( ! empty($_POST['upload_thumb_src']) ) ? $_POST['upload_thumb_src'] : $this->config['tools']['thumbnail']['default']; |
| 73 | $this->config['tools']['thumbnail']['resize'] = (bool) $_POST['thumb_field_resize']; |
| 74 | $this->config['tools']['thumbnail']['lazyload'] = (bool) $_POST['thumb_lazy_load']; |
| 75 | |
| 76 | update_option('wpp_settings_config', $this->config ); |
| 77 | echo '<div class="notice notice-success is-dismissible"><p><strong>' . esc_html(__('Settings saved.', 'wordpress-popular-posts')) . '</strong></p></div>'; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | elseif ( 'data' == $_POST['section'] && current_user_can('manage_options') ) { |
| 82 | $current = 'tools'; |
| 83 | |
| 84 | if ( isset($_POST['wpp-update-data-options-token'] ) && wp_verify_nonce($_POST['wpp-update-data-options-token'], 'wpp-update-data-options') ) { |
| 85 | $this->config['tools']['log']['level'] = (int) $_POST['log_option']; |
| 86 | $this->config['tools']['log']['limit'] = (int) $_POST['log_limit']; |
| 87 | $this->config['tools']['log']['expires_after'] = ( \WordPressPopularPosts\Helper::is_number($_POST['log_expire_time']) && $_POST['log_expire_time'] > 0 ) ? (int) $_POST['log_expire_time'] : 180; |
| 88 | $this->config['tools']['ajax'] = (bool) $_POST['ajax']; |
| 89 | |
| 90 | // if any of the caching settings was updated, destroy all transients created by the plugin |
| 91 | if ( |
| 92 | $this->config['tools']['cache']['active'] != $_POST['cache'] |
| 93 | || $this->config['tools']['cache']['interval']['time'] != $_POST['cache_interval_time'] |
| 94 | || $this->config['tools']['cache']['interval']['value'] != $_POST['cache_interval_value'] |
| 95 | ) { |
| 96 | $this->flush_transients(); |
| 97 | } |
| 98 | |
| 99 | $this->config['tools']['cache']['active'] = (bool) $_POST['cache']; |
| 100 | $this->config['tools']['cache']['interval']['time'] = $_POST['cache_interval_time']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 101 | $this->config['tools']['cache']['interval']['value'] = ( isset($_POST['cache_interval_value']) && \WordPressPopularPosts\Helper::is_number($_POST['cache_interval_value']) && $_POST['cache_interval_value'] > 0 ) ? (int) $_POST['cache_interval_value'] : 1; |
| 102 | |
| 103 | $this->config['tools']['sampling']['active'] = (bool) $_POST['sampling']; |
| 104 | $this->config['tools']['sampling']['rate'] = ( isset($_POST['sample_rate']) && \WordPressPopularPosts\Helper::is_number($_POST['sample_rate']) && $_POST['sample_rate'] > 0 ) |
| 105 | ? (int) $_POST['sample_rate'] |
| 106 | : 100; |
| 107 | |
| 108 | update_option('wpp_settings_config', $this->config); |
| 109 | echo '<div class="notice notice-success is-dismissible"><p><strong>' . esc_html(__('Settings saved.', 'wordpress-popular-posts')) . '</strong></p></div>'; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | ?> |
| 115 | |
| 116 | <?php if ( current_user_can('edit_others_posts') ) : ?> |
| 117 | <nav id="wpp-menu"> |
| 118 | <ul> |
| 119 | <li <?php echo ('stats' == $current ) ? 'class="current"' : ''; ?>><a href="<?php echo esc_url(admin_url('options-general.php?page=wordpress-popular-posts&tab=stats')); ?>" title="<?php esc_attr_e('Stats', 'wordpress-popular-posts'); ?>"><span><?php esc_html_e('Stats', 'wordpress-popular-posts'); ?></span></a></li> |
| 120 | <li <?php echo ('tools' == $current ) ? 'class="current"' : ''; ?>><a href="<?php echo esc_url(admin_url('options-general.php?page=wordpress-popular-posts&tab=tools')); ?>" title="<?php esc_attr_e('Tools', 'wordpress-popular-posts'); ?>"><span><?php esc_html_e('Tools', 'wordpress-popular-posts'); ?></span></a></li> |
| 121 | <li <?php echo ('debug' == $current ) ? 'class="current"' : ''; ?>><a href="<?php echo esc_url(admin_url('options-general.php?page=wordpress-popular-posts&tab=debug')); ?>" title="Debug"><span>Debug</span></a></li> |
| 122 | </ul> |
| 123 | </nav> |
| 124 | <?php endif; ?> |
| 125 | |
| 126 | <div class="wpp-wrapper wpp-section-<?php echo esc_attr($current); ?>"> |
| 127 | <div class="wpp-header"> |
| 128 | <h2>WordPress Popular Posts</h2> |
| 129 | <h3><?php echo esc_html($wpp_tabs[$current]); ?></h3> |
| 130 | </div> |
| 131 | |
| 132 | <?php |
| 133 | // Stats |
| 134 | require_once 'screen-stats.php'; |
| 135 | // Tools |
| 136 | require_once 'screen-tools.php'; |
| 137 | // Debug |
| 138 | require_once 'screen-debug.php'; |
| 139 | ?> |
| 140 | </div> |
| 141 |