-
Notifications
You must be signed in to change notification settings - Fork 11
Consolidate init and skip optimizing in AMP mode #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ | |
| exit; | ||
| } | ||
|
|
||
| require_once __DIR__ . '/settings.php'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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 ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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 ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
|
@@ -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() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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_loadedin the main module andinitin 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.