Theme Options -> Builder -> Advanced)
* 3) створити крон джоб
* # dev
* 0 2 * * * /home/fasoonde/public_html/wp-content/themes/fasoon/bin/optimize_images.sh
*
* # live
* 0 1 * * * /home/fasoonde/public_html/wp-content/themes/fasoon/bin/optimize_images.sh
*/
if ( is_admin() ) {
return;
}
// include libs
require_once 'lib/minify/vendor/autoload.php';
use MatthiasMullie\Minify;
require_once 'lib/Qs/Packer/Html.php';
// scripts optimization
if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) {
define( 'CONCATENATE_SCRIPTS', false );
}
if ( ! defined( 'COMPRESS_SCRIPTS' ) ) {
define( 'COMPRESS_SCRIPTS', false );
}
define( 'FASOON_THEME_PACKER_DIR', WP_CONTENT_DIR . '/__fasoon' );
define( 'FASOON_THEME_PACKER_DIR_URL', content_url( '/__fasoon' ) );
/**
* @param string $handle
* @param string $type script|stylesheet
*
* @return array|mixed
*/
function fasoon_theme_register_handle( $handle, $type = 'script' ) {
$_file = FASOON_THEME_PACKER_DIR . '/packer.config.php';
if ( ! is_dir( FASOON_THEME_PACKER_DIR ) ) {
if ( ! mkdir( FASOON_THEME_PACKER_DIR ) ) {
return false;
}
}
// init config
if ( ! is_file( $_file ) ) {
$_config = [
'stylesheet' => [],
'script' => [],
];
$content = "all_deps($scripts->queue);
$indexes = [];
foreach ( $scripts->to_do as $handle ) {
$index = fasoon_theme_register_handle( $handle, 'script' );
if ( false === $index ) {
return; // something went wrong!!!
}
$indexes[$handle] = $index;
}
asort( $indexes, SORT_NUMERIC );
foreach (array_keys( $indexes ) as $handle ) {
// exclude scripts with extra 'conditional', 'before' and 'after'
/** @var _WP_Dependency $handle_obj */
$handle_obj = $scripts->registered[$handle];
if ( ! empty( $handle_obj->extra['conditional'] )
|| ! empty( $handle_obj->extra['before'] )
|| ! empty( $handle_obj->extra['after'] )
) {
unset( $indexes[$handle] );
}
// remove external scripts
$url_info = parse_url( $handle_obj->src );
if ( ! empty( $url_info['host'] ) && $url_info['host'] !== $_SERVER['HTTP_HOST'] ) {
unset( $indexes[$handle] );
}
}
$packed_file_name = join( '-', $indexes ) . '.js';
$packed_file_path = FASOON_THEME_PACKER_DIR . '/' . $packed_file_name;
$packed_file_url = FASOON_THEME_PACKER_DIR_URL . '/' . $packed_file_name;
if ( ! is_file( $packed_file_path ) ) {
if ( ! $packed = fopen( $packed_file_path, 'w+' ) ) {
return;
}
$www_path = rtrim( get_home_path(), '/' );
foreach (array_keys( $indexes ) as $handle ) {
/** @var _WP_Dependency $handle_obj */
$handle_obj = $scripts->registered[$handle];
if ( $handle_obj->src ) {
if ( 0 === strpos( $handle_obj->src, '/wp-includes' ) ) {
$content = file_get_contents( $www_path . $handle_obj->src );
} else {
$src = $handle_obj->src;
$site_url = site_url();
$site_url = substr( $site_url, strpos( $site_url, '://' ) + 3 );
if ( 0 === strpos( $src, 'http' ) ) {
$src = substr( $src, strpos( $src, '://' ) + 3 );
$src = str_replace( $site_url, '', $src );
} elseif ( 0 === strpos( $src, '//' ) ) {
$src = substr( $src, 2 );
$src = str_replace( $site_url, '', $src );
}
if ( false === strpos( $src, '.min.js' ) ) {
$minifier = new Minify\JS( $www_path . $src );
$content = $minifier->minify();
} else {
$content = file_get_contents( $www_path . $src );
}
}
$content .= $delim;
fwrite( $packed, $content );
}
}
fclose( $packed );
}
// collect extra['data'] and remove merged scripts
$data = '';
/** @var WP_Theme $theme */
$theme = wp_get_theme();
if ( 'divi' == strtolower( $theme->get( 'Template' ) ) ) {
$data .= 'var et_pb_box_shadow_elements = [];' . $delim; // костиль, якась сволота дописує до інлайн скрипта цю стрічку, не знайшов хто це робить
}
foreach (array_keys( $indexes ) as $handle ) {
/** @var _WP_Dependency $handle_obj */
$handle_obj = $scripts->registered[ $handle ];
if ( ! empty( $handle_obj->extra['data'] ) ) {
$data .= $handle_obj->extra['data'] . $delim;
}
$wp_scripts->dequeue( $handle );
}
// enqueue merged script
wp_enqueue_script( 'fasoon_merged_scripts', $packed_file_url, [], false, false );
$wp_scripts->registered['fasoon_merged_scripts']->extra['data'] = $data;
}
add_filter( 'script_loader_tag', 'fasoon_theme_script_loader_tag' );
function fasoon_theme_script_loader_tag( $tag ) {
$tag = str_replace( '
SCRIPT;
}
// styles optimization
if ( ! defined( 'COMPRESS_CSS' ) ) {
define( 'COMPRESS_CSS', false );
}
/**
* Method replace related paths for resources inside css
* @param string $srcPath Relative path of source file (e.g.: css/modules)
* @param string $dstPath Relative path of destination file (e.g.: css)
* @param string $content Source file content
* @return string
* @throws Exception
*/
function fasoon_theme_prepareCssResourcePaths($srcPath, $dstPath, $content)
{
$srcPath = trim($srcPath, DIRECTORY_SEPARATOR);
$dstPath = trim($dstPath, DIRECTORY_SEPARATOR);
if ($srcPath == $dstPath || parse_url($srcPath, PHP_URL_HOST) || parse_url($dstPath, PHP_URL_HOST)) {
return $content;
}
$newRelativePath = fasoon_theme_getRelativePath($dstPath, $srcPath);
$content = preg_replace_callback(
'#url\(\s*(([\'"])?([^\s\)]+)?\1?)\s*\)#',
function ($match) use ($newRelativePath) {
$quote = $match[2];
$path = $match[1];
if (0 === strpos($path, 'data:')) {
return $match[0];
}
if ($quote) {
$path = trim($path, $quote);
}
if (false !== strpos($path, '://') || '/' === $path[0]) {
return $match[0];
}
while (0 === strpos($path, '../') && $newRelativePath && $newRelativePath != '.') {
$newRelativePath = dirname($newRelativePath);
$path = substr($path, 3);
}
if ($newRelativePath && $newRelativePath != '.') {
$path = $newRelativePath . '/' . $path;
}
return "url({$quote}{$path}{$quote})";
},
$content
);
return $content;
}
function fasoon_theme_getRelativePath($srcPath, $dstPath)
{
$srcPath = $srcPath ? explode(DIRECTORY_SEPARATOR, trim($srcPath, DIRECTORY_SEPARATOR)) : [];
$dstPath = $dstPath ? explode(DIRECTORY_SEPARATOR, trim($dstPath, DIRECTORY_SEPARATOR)) : [];
while ($srcPath && $dstPath && $srcPath[0] == $dstPath[0]) {
array_shift($srcPath);
array_shift($dstPath);
}
$relativePath = trim(str_repeat('/..', count($srcPath)), DIRECTORY_SEPARATOR);
if ($dstPath) {
$relativePath .= ($relativePath ? '/' : '') . implode('/', $dstPath);
}
return $relativePath;
}
add_action( 'wp_print_styles', 'fasoon_theme_wp_print_styles', PHP_INT_MAX );
function fasoon_theme_wp_print_styles() {
$delim = "\n\n";
$wp_styles = wp_styles();
$styles = clone $wp_styles;
$styles->all_deps($styles->queue);
$indexes = [];
foreach ( $styles->to_do as $handle ) {
$index = fasoon_theme_register_handle( $handle, 'stylesheet' );
if ( false === $index ) {
return; // something went wrong!!!
}
$indexes[$handle] = $index;
}
asort( $indexes, SORT_NUMERIC );
$after = [];
foreach (array_keys( $indexes ) as $handle ) {
// exclude stylesheets with extra 'rtl', 'before' and 'after'
/** @var _WP_Dependency $handle_obj */
$handle_obj = $styles->registered[$handle];
if ( ! empty( $handle_obj->extra['rtl'] ) || ! empty( $handle_obj->extra['before'] ) ) {
unset( $indexes[$handle] );
}
if ( ! empty( $handle_obj->extra['after'] ) ) {
$after = array_merge($after, (array) $handle_obj->extra['after']);
}
// remove external stylesheets
$url_info = parse_url( $handle_obj->src );
if ( ! empty( $url_info['host'] ) && $url_info['host'] !== $_SERVER['HTTP_HOST'] ) {
unset( $indexes[$handle] );
}
}
$packed_file_name = join( '-', $indexes ) . '.css';
$packed_file_path = FASOON_THEME_PACKER_DIR . '/' . $packed_file_name;
$packed_file_url = FASOON_THEME_PACKER_DIR_URL . '/' . $packed_file_name;
if ( ! is_file( $packed_file_path ) ) {
if ( ! $packed = fopen( $packed_file_path, 'w+' ) ) {
return;
}
$www_path = rtrim( get_home_path(), '/' );
foreach (array_keys( $indexes ) as $handle ) {
/** @var _WP_Dependency $handle_obj */
$handle_obj = $styles->registered[$handle];
if ( $handle_obj->src ) {
if ( 0 === strpos( $handle_obj->src, '/wp-includes' ) ) {
$content = file_get_contents( $www_path . $handle_obj->src );
} else {
$src = $handle_obj->src;
$site_url = site_url();
$site_url = substr( $site_url, strpos( $site_url, '://' ) + 3 );
if ( 0 === strpos( $src, 'http' ) ) {
$src = substr( $src, strpos( $src, '://' ) + 3 );
$src = str_replace( $site_url, '', $src );
} elseif ( 0 === strpos( $src, '//' ) ) {
$src = substr( $src, 2 );
$src = str_replace( $site_url, '', $src );
}
if ( false === strpos( $src, '.min.css' ) ) {
$minifier = new Minify\CSS( $www_path . $src );
$content = $minifier->minify();
} else {
$content = file_get_contents( $www_path . $src );
}
$content = fasoon_theme_prepareCssResourcePaths(
dirname($src),
str_replace( $www_path, '', FASOON_THEME_PACKER_DIR ),
$content
);
}
$content .= $delim;
fwrite( $packed, $content );
$wp_styles->dequeue( $handle );
}
}
fclose( $packed );
}
foreach (array_keys( $indexes ) as $handle ) {
$wp_styles->dequeue( $handle );
}
// enqueue merged script
wp_enqueue_style( 'fasoon_merged_style', $packed_file_url );
$wp_styles->registered['fasoon_merged_style']->extra['after'] = $after;
}
if ( ! function_exists( 'et_divi_fonts_url' ) ) {
function et_divi_fonts_url() {
return ''; // disable load google fonts from CDN
}
}
// html compressor
function fasoon_theme_remove_comments( $html ) {
$scripts_pattern = '/\