| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: The Combiner |
|---|
| 4 | Plugin URI: http://dd32.id.au/wordpress-plugins/?plugin=combine-css-js |
|---|
| 5 | Description: Combine CSS & JS into one large monolithic file |
|---|
| 6 | Author: DD32 |
|---|
| 7 | Version: 1.0 |
|---|
| 8 | Author URI: http://dd32.id.au/ |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | add_action('admin_init', 'combine_init'); |
|---|
| 12 | function combine_init() { |
|---|
| 13 | //Handler |
|---|
| 14 | add_action('admin_post_css', 'combine_css'); |
|---|
| 15 | add_action('admin_post_js', 'combine_js'); |
|---|
| 16 | |
|---|
| 17 | //Styles |
|---|
| 18 | //remove_action('wp_head', 'wp_print_styles', 9); //non-admin |
|---|
| 19 | remove_action('admin_print_styles', 'wp_print_styles', 20); |
|---|
| 20 | //add_action('wp_head', 'combine_css_link');//non-admin |
|---|
| 21 | add_action('admin_print_styles', 'combine_css_link', 20); |
|---|
| 22 | |
|---|
| 23 | //Scripts |
|---|
| 24 | //remove_action('wp_head', 'wp_print_scripts', 9); //non-admin |
|---|
| 25 | remove_action('admin_print_scripts', 'wp_print_scripts', 20); |
|---|
| 26 | //add_action('wp_head', 'combine_js_link'); //non-admin |
|---|
| 27 | add_action('admin_print_scripts', 'combine_js_link', 20); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | function combine_css() { |
|---|
| 31 | global $wp_styles; |
|---|
| 32 | if ( !is_a($wp_styles, 'WP_Styles') ) |
|---|
| 33 | $wp_styles = new WP_Styles(); |
|---|
| 34 | |
|---|
| 35 | header('content-type: text/css; charset: UTF-8'); |
|---|
| 36 | |
|---|
| 37 | $content = ''; |
|---|
| 38 | foreach ( (array)$wp_styles->registered as $style ) { |
|---|
| 39 | if ( file_exists(ABSPATH . $style->src) ) |
|---|
| 40 | $content .= file_get_contents(ABSPATH . $style->src); |
|---|
| 41 | $content .= "\n\n"; |
|---|
| 42 | } |
|---|
| 43 | echo $content; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | function combine_css_link() { |
|---|
| 47 | echo '<link rel="stylesheet" type="text/css" media="all" href="' . admin_url('admin-post.php?action=css' . (is_admin() ? '&admin=true' : '') ) . '" />' . "\n"; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | function combine_js() { |
|---|
| 51 | global $wp_scripts; |
|---|
| 52 | if ( !is_a($wp_scripts, 'WP_Scripts') ) |
|---|
| 53 | $wp_scripts = new WP_Scripts(); |
|---|
| 54 | |
|---|
| 55 | header("content-type: text/javascript; charset: UTF-8"); |
|---|
| 56 | |
|---|
| 57 | $content = ''; |
|---|
| 58 | foreach ( (array)$wp_scripts->registered as $script ) { |
|---|
| 59 | if ( file_exists(ABSPATH . $script->src) ) |
|---|
| 60 | $content .= file_get_contents(ABSPATH . $script->src) . "\n\n"; |
|---|
| 61 | if ( isset($script->extra['l10n']) ) { |
|---|
| 62 | $object_name = $script->extra['l10n'][0]; |
|---|
| 63 | |
|---|
| 64 | $content .= "\t$object_name = {\n"; |
|---|
| 65 | $eol = ''; |
|---|
| 66 | foreach ( $script->extra['l10n'][1] as $var => $val ) { |
|---|
| 67 | $content .= "$eol\t\t$var: \"" . js_escape( $val ) . '"'; |
|---|
| 68 | $eol = ",\n"; |
|---|
| 69 | } |
|---|
| 70 | $content .= "\n\t}\n\n"; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | echo $content; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | function combine_js_link() { |
|---|
| 77 | echo '<script type="text/javascript" src="' . admin_url('admin-post.php?action=js' . (is_admin() ? '&admin=true' : '') ) . '"></script>' . "\n"; |
|---|
| 78 | } |
|---|
| 79 | ?> |
|---|