Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions concat-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
require_once __DIR__ . '/dependency-path-mapping.php';
require_once __DIR__ . '/utils.php';

if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}

class Page_Optimize_CSS_Concat extends WP_Styles {
private $dependency_path_mapping;
private $old_styles;
Expand Down Expand Up @@ -212,14 +208,3 @@ function __set( $key, $value ) {
}
}

function page_optimize_css_concat_init() {
global $wp_styles;

if ( page_optimize_should_concat_css() ) {
$wp_styles = new Page_Optimize_CSS_Concat( $wp_styles );
$wp_styles->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}
}

add_action( 'init', 'page_optimize_css_concat_init' );
Copy link
Copy Markdown
Member Author

@brandonpayton brandonpayton Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having different initialization timing with plugins_loaded in the main module and init in the concat modules, let's simplify and have everything driven in a single place by a single action. This makes loading logic readable in one place and thus easier to understand.


15 changes: 0 additions & 15 deletions concat-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
require_once __DIR__ . '/dependency-path-mapping.php';
require_once __DIR__ . '/utils.php';

if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}

class Page_Optimize_JS_Concat extends WP_Scripts {
private $dependency_path_mapping;
private $old_scripts;
Expand Down Expand Up @@ -295,14 +291,3 @@ function __set( $key, $value ) {
}
}

function page_optimize_js_concat_init() {
global $wp_scripts;

if ( ! is_admin() && ( page_optimize_should_concat_js() || page_optimize_load_mode_js() ) ) {
$wp_scripts = new Page_Optimize_JS_Concat( $wp_scripts );
$wp_scripts->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}
}

add_action( 'init', 'page_optimize_js_concat_init' );

59 changes: 45 additions & 14 deletions page-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
exit;
}

require_once __DIR__ . '/settings.php';
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The settings don't need to be conditionally loaded. Let's load them unconditionally.


function page_optimize_cache_cleanup( $cache_folder = false, $file_age = DAY_IN_SECONDS ) {
if ( ! is_dir( $cache_folder ) ) {
return;
Expand Down Expand Up @@ -100,7 +102,8 @@ function page_optimize_should_concat_js() {
return $_GET['concat-js'] !== '0';
}

return !! get_option( 'page_optimize-js', page_optimize_js_default() );
$should_concat = !! get_option( 'page_optimize-js', page_optimize_js_default() );
return apply_filters( 'page_optimize_should_concat_js', $should_concat );
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're revisiting conditional loading, we might as well support a first-class should-concat filter for JS.

}

// TODO: Support JS load mode regardless of whether concat is enabled
Expand All @@ -121,7 +124,8 @@ function page_optimize_should_concat_css() {
return $_GET['concat-css'] !== '0';
}

return !! get_option( 'page_optimize-css', page_optimize_css_default() );
$should_concat = !! get_option( 'page_optimize-css', page_optimize_css_default() );
return apply_filters( 'page_optimize_should_concat_css', $should_concat );
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're revisiting conditional loading, we might as well support a first-class should-concat filter for CSS.

}

function page_optimize_js_default() {
Expand Down Expand Up @@ -265,38 +269,65 @@ function page_optimize_schedule_cache_cleanup() {
}

// Cases when we don't want to concat
function page_optimize_bail() {
function page_optimize_should_optimize() {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I wanted to publish a related filter for this function, I was looking for a better, non-slang word for "bail", but none satisfied me. I ended up thinking it was better to remove the negation and make this a positive "should optimize" along with a filter of the same name.

// Bail if we're in customizer
global $wp_customize;
if ( isset( $wp_customize ) ) {
return true;
return false;
}

// Avoid interfering with AMP mode
if ( function_exists( 'amp_is_request' ) && amp_is_request() ) {
return false;
}

// Bail if Divi theme is active, and we're in the Divi Front End Builder
if ( ! empty( $_GET['et_fb'] ) && 'Divi' === wp_get_theme()->get_template() ) {
return true;
return false;
}

// Bail if we're editing pages in Brizy Editor
if ( class_exists( 'Brizy_Editor' ) && method_exists( 'Brizy_Editor', 'prefix' ) && ( isset( $_GET[ Brizy_Editor::prefix( '-edit-iframe' ) ] ) || isset( $_GET[ Brizy_Editor::prefix( '-edit' ) ] ) ) ) {
return true;
return false;
}

return false;
return true;
}

function page_optimize_init() {
if ( page_optimize_bail() ) {
if ( ! apply_filters( 'page_optimize_should_optimize' , page_optimize_should_optimize() ) ) {
return;
}

page_optimize_schedule_cache_cleanup();

require_once __DIR__ . '/settings.php';
require_once __DIR__ . '/concat-css.php';
require_once __DIR__ . '/concat-js.php';
if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}

$concat_js = ! is_admin() && ( page_optimize_should_concat_js() || page_optimize_load_mode_js() );
if ( $concat_js ) {
require_once __DIR__ . '/concat-js.php';
global $wp_scripts;
$wp_scripts = new Page_Optimize_JS_Concat( $wp_scripts );
$wp_scripts->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}

// Disable Jetpack photon-cdn for static JS/CSS
add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' );
$concat_css = page_optimize_should_concat_css();
if ( $concat_css ) {
require_once __DIR__ . '/concat-css.php';
global $wp_styles;
$wp_styles = new Page_Optimize_CSS_Concat( $wp_styles );
$wp_styles->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}

if ( $concat_js || $concat_css ) {
// Disable Jetpack photon-cdn for static JS/CSS if we're doing any concat
add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' );
Copy link
Copy Markdown
Member Author

@brandonpayton brandonpayton Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If page-optimize isn't doing anything, we should probably let Jetpack site accelerator do its thing. 😅

}
}
add_action( 'plugins_loaded', 'page_optimize_init' );
// Use a late action prior to `wp_enqueue_scripts` to give plugins a chance
// to set up and determine things that may affect optimization.
// Example: We cannot ask the AMP plugin whether we are in AMP mode
// until the `parse_query` action has fired.
add_action( 'wp', 'page_optimize_init' );