diff --git a/admin.css b/admin.css index 5199a48..3c63c93 100644 --- a/admin.css +++ b/admin.css @@ -14,3 +14,16 @@ .fixed .column-expires { width: 12%; } + +.nav-tab-content { + display: none; +} +.form-table th small { + display: block; + clear: both; + color: #999; +} +.form-table td label { + display: block; + clear:both; +} \ No newline at end of file diff --git a/admin.js b/admin.js index e69de29..2b6d574 100644 --- a/admin.js +++ b/admin.js @@ -0,0 +1,20 @@ +jQuery(function($){ + + // Admin page tabs + var $tabs = $('.nav-tab-wrapper'), + $panels = $('.nav-tab-content'), + currentHash = window.location.hash; + + $tabs.on('click', 'a', function(e){ + var hash = $(this).attr('href').replace('#tab-', '#tab-content-'); // prevents page scrolling if hash is present + $panels.hide().filter(hash).show(); + $tabs.find('a').removeClass('nav-tab-active').filter($(this)).addClass('nav-tab-active'); + }); + $tabs.find( currentHash ? 'a[href="'+currentHash+'"]' : ':first').trigger('click'); + + // Fix input-inside-label glitch + $panels.on('click', 'input[type=text]', function(e){ + e.preventDefault(); + }) + +}); \ No newline at end of file diff --git a/dependency-minification.php b/dependency-minification.php index 474d1a2..e8bc78e 100644 --- a/dependency-minification.php +++ b/dependency-minification.php @@ -33,13 +33,14 @@ class Dependency_Minification { protected static $minified_count = 0; static $admin_page_hook; - const DEFAULT_ENDPOINT = '_minify'; - const CRON_MINIFY_ACTION = 'minify_dependencies'; - const CACHE_KEY_PREFIX = 'depmin_cache_'; - const FILENAME_PATTERN = '([^/]+?)\.([0-9a-f]+)(?:\.([0-9a-f]+))?\.(css|js)'; - const AJAX_ACTION = 'dependency_minification'; - const ADMIN_PAGE_SLUG = 'dependency-minification'; - const ADMIN_PARENT_PAGE = 'tools.php'; + const DEFAULT_ENDPOINT = '_minify'; + const CRON_MINIFY_ACTION = 'minify_dependencies'; + const CACHE_KEY_PREFIX = 'depmin_cache_'; + const FILENAME_PATTERN = '([^/]+?)\.([0-9a-f]+)(?:\.([0-9a-f]+))?\.(css|js)'; + const AJAX_ACTION = 'dependency_minification'; + const AJAX_OPTIONS_ACTION = 'dependency_minification_options'; + const ADMIN_PAGE_SLUG = 'dependency-minification'; + const ADMIN_PARENT_PAGE = 'tools.php'; static $query_vars = array( 'depmin_handles', @@ -49,8 +50,7 @@ class Dependency_Minification { ); static function setup() { - self::$options = apply_filters( 'dependency_minification_options', array_merge( - array( + $defaults = array( 'endpoint' => self::DEFAULT_ENDPOINT, 'default_exclude_remote_dependencies' => true, 'cache_control_max_age_cache' => 2629743, // 1 month in seconds @@ -59,16 +59,38 @@ static function setup() { 'admin_page_capability' => 'edit_theme_options', 'show_error_messages' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), 'disable_if_wp_debug' => true, - ), - self::$options - ) ); + 'exclude_dependencies' => array(), + 'disabled_on_conditions' => array( + 'all' => false, + 'loggedin' => false, + 'admin' => false, + 'queryvar' => false, + ), + ); + $options = get_option( 'dependency_minification_options', array() ); + self::$options = apply_filters( + 'dependency_minification_options', array_merge( + $defaults, + $options + ) + ); $is_frontend = ! ( is_admin() || in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ); - if ( $is_frontend ) { + $disabled = ( + ( isset( self::$options['disabled_on_conditions']['all'] ) && ! empty( self::$options['disabled_on_conditions']['all'] ) ) + || ( isset( self::$options['disabled_on_conditions']['loggedin'] ) && ! empty( self::$options['disabled_on_conditions']['loggedin'] ) && is_user_logged_in() ) + || ( ! empty( self::$options['disabled_on_conditions']['admin'] ) && is_user_logged_in() && current_user_can( 'manage_options' ) ) + || ( ! empty( self::$options['disabled_on_conditions']['queryvar']['enabled'] ) + && ! empty( self::$options['disabled_on_conditions']['queryvar']['enabled'] ) + && ! empty( $_GET[ self::$options['disabled_on_conditions']['queryvar']['value'] ] ) + ) + ); + + if ( $is_frontend && ! $disabled ) { add_filter( 'print_scripts_array', array( __CLASS__, 'filter_print_scripts_array' ) ); add_filter( 'print_styles_array', array( __CLASS__, 'filter_print_styles_array' ) ); } @@ -78,6 +100,7 @@ static function setup() { add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); add_action( 'wp_ajax_' . self::AJAX_ACTION, array( __CLASS__, 'admin_ajax_handler' ) ); + add_action( 'wp_ajax_' . self::AJAX_OPTIONS_ACTION, array( __CLASS__, 'admin_ajax_options_handler' ) ); add_filter( 'plugin_action_links', array( __CLASS__, 'admin_plugin_action_links' ), 10, 2 ); } @@ -197,7 +220,7 @@ static function admin_notices() { __( 'Permalinks Settings', 'depmin' ) ) ) - ); + ); // xss ok ?>
- + @@ -269,6 +296,33 @@ static function admin_ajax_handler() { exit; } + /** + * @action wp_ajax_dependency_minification_options + */ + static function admin_ajax_options_handler() { + if ( ! current_user_can( self::$options['admin_page_capability'] ) ) { + wp_die( __( 'You are not allowed to do that.', 'depmin' ) ); + } + if ( ! wp_verify_nonce( $_POST['_wpnonce'], self::AJAX_OPTIONS_ACTION ) ) { + wp_die( __( 'Nonce check failed. Try reloading the previous page.', 'depmin' ) ); + } + $updated_count = 0; + if ( ! empty( $_POST['options'] ) ) { + $options = get_option( 'dependency_minification_options' ); + $options['exclude_dependencies'] = array_filter( preg_split( "#[\n\r]+#", $_POST['options']['exclude_dependencies'] ) ); + $options['disabled_on_conditions'] = $_POST['options']['disabled_on_conditions']; + $options['default_exclude_remote_dependencies'] = isset( $_POST['options']['default_exclude_remote_dependencies'] ); + update_option( 'dependency_minification_options', $options ); + } + + $redirect_url = add_query_arg( 'page', self::ADMIN_PAGE_SLUG, admin_url( self::ADMIN_PARENT_PAGE ) ); + $redirect_url = add_query_arg( 'updated', 1, $redirect_url ); + $redirect_url .= '#tab-settings'; + wp_redirect( $redirect_url ); + + die(); + } + /** * @filter plugin_action_links */ @@ -288,10 +342,13 @@ static function admin_page() { $nonce = wp_create_nonce( self::AJAX_ACTION ); ?>