Add JS minification and tabbed admin UI

This commit is contained in:
Marcus Quinn
2025-03-13 00:52:17 +00:00
parent 895e50589e
commit f4b6e2d631
3 changed files with 33 additions and 20 deletions

View File

@ -11,4 +11,16 @@
} }
.wpa-superstar-toggle { .wpa-superstar-toggle {
margin: 10px 0; margin: 10px 0;
}
.nav-tab-wrapper {
margin-bottom: 20px;
}
.nav-tab {
background: #f1f1f1;
padding: 8px 16px;
border-radius: 4px 4px 0 0;
}
.nav-tab-active {
background: #fff;
border-bottom: 1px solid #fff;
} }

View File

@ -19,6 +19,7 @@ add_action( 'admin_menu', 'wpa_superstar_admin_menu' );
function wpa_superstar_register_settings() { function wpa_superstar_register_settings() {
register_setting( 'wpa-superstar-settings', 'wpa_superstar_lazy_load' ); register_setting( 'wpa-superstar-settings', 'wpa_superstar_lazy_load' );
register_setting( 'wpa-superstar-settings', 'wpa_superstar_minify_css' ); register_setting( 'wpa-superstar-settings', 'wpa_superstar_minify_css' );
register_setting( 'wpa-superstar-settings', 'wpa_superstar_minify_js' );
} }
add_action( 'admin_init', 'wpa_superstar_register_settings' ); add_action( 'admin_init', 'wpa_superstar_register_settings' );
@ -34,26 +35,13 @@ add_action( 'wp_ajax_wpa_superstar_update_option', 'wpa_superstar_update_option'
// Settings page HTML // Settings page HTML
function wpa_superstar_settings_page() { function wpa_superstar_settings_page() {
$active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general';
?> ?>
<div class="wpa-superstar-wrap"> <div class="wpa-superstar-wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<h2 class="nav-tab-wrapper">
<a href="?page=wpa-superstar&tab=general" class="nav-tab <?php echo $active_tab == 'general' ? 'nav-tab-active' : ''; ?>">General</a>
<a href="?page=wpa-superstar&tab=advanced" class="nav-tab <?php echo $active_tab == 'advanced' ? 'nav-tab-active' : ''; ?>">Advanced</a>
</h2>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php settings_fields( 'wpa-superstar-settings' ); ?> <?php settings_fields( 'wpa-superstar-settings' );
<?php do_settings_sections( 'wpa-superstar-settings' ); ?>
<div class="wpa-superstar-toggle">
<label>
<input type="checkbox" name="wpa_superstar_lazy_load" value="1" <?php checked( get_option( 'wpa_superstar_lazy_load', 1 ) ); ?> />
Enable lazy loading for images
</label>
</div>
<div class="wpa-superstar-toggle">
<label>
<input type="checkbox" name="wpa_superstar_minify_css" value="1" <?php checked( get_option( 'wpa_superstar_minify_css', 0 ) ); ?> />
Enable CSS minification
</label>
</div>
<?php submit_button(); ?>
</form>
</div>
<?php
}

View File

@ -28,4 +28,17 @@ function wpa_superstar_minify_css( $html ) {
); );
return trim( $html ); return trim( $html );
} }
add_filter( 'style_loader_tag', 'wpa_superstar_minify_css' ); add_filter( 'style_loader_tag', 'wpa_superstar_minify_css' );
function wpa_superstar_minify_js( $html ) {
if ( is_admin() || ! get_option( 'wpa_superstar_minify_js', 0 ) ) {
return $html;
}
$html = preg_replace(
array( '/\s+/', '/\/\*.*?\*\//s', '//.*?\n/' ),
array( ' ', '', '' ),
$html
);
return trim( $html );
}
add_filter( 'script_loader_tag', 'wpa_superstar_minify_js' );