-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic-additional-css.php
113 lines (100 loc) · 2.79 KB
/
dynamic-additional-css.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Plugin Name: Dynamic Additional CSS
* Plugin URI: https://github.com/seothemes/dynamic-additional-css
* Description: Writes Customizer Additional CSS to dynamic stylesheet.
* Version: 1.0.0
* Author: Lee Anthony
* Author URI: https://seothemes.com/
* Copyright: SEO Themes 2019
* Text Domain: dynamic-additional-css
* Domain Path: /languages
* License: GPL-3.0-or-later
*/
namespace SeoThemes\DynamicAdditionalCss;
// Bail if no Additional CSS.
if ( ! wp_get_custom_css() ) {
return;
}
add_action('after_setup_theme', __NAMESPACE__ . '\remove_default_css');
/**
* Remove default inline styles.
*
* Hooked to `after_setup_theme` to make Customizer check work.
*
* @since 1.0.0
*
* @return void
*/
function remove_default_css() {
if ( ! is_customize_preview() ) {
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_dynamic_css' );
/**
* Enqueues dynamic CSS on frontend or adds inline styles if in Customizer.
*
* @since 1.0.0
*
* @return void
*/
function enqueue_dynamic_css() {
$handle = 'dynamic-additional';
if ( ! is_customize_preview() ) {
wp_enqueue_style(
$handle,
\admin_url( 'admin-ajax.php' ) . '?action=load_css_ajax&wpnonce=' . wp_create_nonce( 'dynamic-css-nonce' ),
[],
'1.0.0',
'all'
);
}
}
add_action( 'wp_ajax_load_css_ajax', __NAMESPACE__ . '\load_css_ajax' );
add_action( 'wp_ajax_nopriv_load_css_ajax', __NAMESPACE__ . '\load_css_ajax' );
/**
* Load the dynamic CSS with ajax (if nonce is ok).
*
* @since 1.0.0
*
* @return void
*/
function load_css_ajax() {
$nonce = $_REQUEST['wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'dynamic-css-nonce' ) ) {
die( 1 );
} else {
header( 'Content-type: text/css; charset: UTF-8' );
echo generate_css();
}
exit;
}
/**
* Generates the CSS output.
*
* Includes quick and dirty way to mostly minify CSS with PHP.
*
* @since 1.0.0
* @author Gary Jones
*
* @param string $css CSS to minify.
*
* @return string Minified CSS
*/
function generate_css() {
// Get additional CSS.
$css = strip_tags( wp_get_custom_css() );
// Minify a bit.
$css = preg_replace( '/\s+/', ' ', $css );
$css = preg_replace( '/(\s+)(\/\*(.*?)\*\/)(\s+)/', '$2', $css );
$css = preg_replace( '~/\*(?![\!|\*])(.*?)\*/~', '', $css );
$css = preg_replace( '/;(?=\s*})/', '', $css );
$css = preg_replace( '/(,|:|;|\{|}|\*\/|>) /', '$1', $css );
$css = preg_replace( '/ (,|;|\{|}|\(|\)|>)/', '$1', $css );
$css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css );
$css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css );
$css = preg_replace( '/0 0 0 0/', '0', $css );
$css = preg_replace( '/#([a-f0-9])\\1([a-f0-9])\\2([a-f0-9])\\3/i', '#\1\2\3', $css );
return trim( $css );
}