Skip to content

Commit

Permalink
Merge pull request #2652 from kprajapatii/master
Browse files Browse the repository at this point in the history
new core function to minimize JS
  • Loading branch information
kprajapatii authored Aug 7, 2024
2 parents 76bfd7f + 621f930 commit 60ca3a6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions includes/formatting-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,4 +737,43 @@ function geodir_sanitize_html_class( $string ) {
$string = trim( implode( ' ', $string ) );

return $string;
}

/**
* JavaScript Minifier.
*
* @since 2.3.71
*
* @param string $script Input JavaScript.
* @return string Minified JavaScript.
*/
function geodir_minify_js( $script ) {
if ( trim( $script ) === "" ) {
return $script;
}

$script = preg_replace(
array(
// Remove comment(s)
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
// Remove white-space(s) outside the string and regex
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
// Remove the last semicolon
'#;+\}#',
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
// --ibid. From `foo['bar']` to `foo.bar`
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
),
array(
'$1',
'$1$2',
'}',
'$1$3',
'$1.$3'
),
$script
);

return $script;
}

0 comments on commit 60ca3a6

Please sign in to comment.