Skip to content

Instantly share code, notes, and snippets.

@aadilprabhakar
Last active February 5, 2018 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadilprabhakar/aef3fff9f8dbe897ca449479bb9cd2e3 to your computer and use it in GitHub Desktop.
Save aadilprabhakar/aef3fff9f8dbe897ca449479bb9cd2e3 to your computer and use it in GitHub Desktop.
Combine+Minify styles of parent theme and add as inline-style in wordpress
function parent_style_cache() {
global $wp_styles, $wpdb;
$updateFlag = 0;
$returnCss = null;
/**
* Check if our table exists else create
*/
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$table_name = $wpdb->prefix . "theme_cache";
$sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time varchar(25) NOT NULL,
name varchar(25) NOT NULL,
text LONGTEXT NOT NULL,
PRIMARY KEY (id)
);";
dbDelta($sql);
/**
* Insert empty row
*/
$exists = $wpdb->get_row( "SELECT * FROM `$table_name` WHERE name = 'parent_styles' LIMIT 1;" );
if(!$exists) {
$wpdb->query("INSERT INTO `$table_name`(`time`,`text`,`name`) VALUES('','','parent_styles');");
}
/**
* Check for latest styles
*/
$now = date('YmdHis');
$result = $wpdb->get_row("SELECT `time`,`text` FROM `$table_name` WHERE `name` = 'parent_styles' LIMIT 1;");
if ($result) {
$diff = $now - ($result->time);
if (($now - $result->time) > 86400) {
$updateFlag = 1;
} else {
$returnCss = stripslashes($result->text);
}
}
#DEBUG#$updateFlag = 1;
/**
* If updateFlag == 1, update the styles in database
*/
$wp_styles->all_deps($wp_styles->queue);
$handles = $wp_styles->to_do;
$deregHandle = [];
$css_code = '';
foreach ($handles as $handle) {
$src = strtok($wp_styles->registered[$handle]->src, '?');
if (strpos($src, 'http') !== false) {
$site_url = site_url();
if (strpos($src, $site_url) !== false) {
$css_file_path = str_replace($site_url, '', $src);
} else {
$css_file_path = $src;
}
$css_file_path = ltrim($css_file_path, '/');
} else {
$css_file_path = ltrim($src, '/');
}
if( $handle != 'child-style' && strpos($css_file_path, 'fonts.googleapis.com') === false ){
print "<!-- HANDLE $handle $src -->";
$deregHandle[] = $handle;
if ($updateFlag === 1) {
$css_code .= file_get_contents($css_file_path);
}
}
if ($css_code != "" && $updateFlag == 1) {
// Remove comments
$css_code = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css_code);
// Remove space after colons
$css_code = str_replace(': ', ':', $css_code);
// Remove whitespace
$css_code = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css_code);
//
}
}
/**
* Lets update the database cache
*/
if ($updateFlag === 1):
$returnCss = $css_code;
$css_code = addslashes($css_code);
if ($wpdb->update($table_name, ['text' => $css_code, 'time' => date('YmdHis')], ['name' => 'parent_styles'])) {
// echo '<!-- STYLE CACHE UPDATED -->';
}
endif;
foreach ($deregHandle as $handle) {
print "<!-- DEREGISTERING $handle -->";
wp_deregister_style($handle);
}
// print '<!-- RETURN CSS is ' . $returnCss .' -->'."\r\n<br/>";
wp_add_inline_style('child-style', $returnCss);
// echo '/* I CAN INSERT HERE, IF YOU INSTRUCT ME TO */';
// # -- FUTURE UPGRADE
// wp_enqueue_style('merged-style', get_stylesheet_directory_uri() . '/css/merged-style.css');
}
add_action('wp_print_styles', 'parent_style_cache');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment