Skip to content
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

Fixes #1707 Autoptimize & Avada lazyload compatibility #1739

Merged
merged 5 commits into from May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions inc/3rd-party/plugins/autoptimize.php
Expand Up @@ -11,6 +11,25 @@
endif;

if ( class_exists( 'autoptimizeConfig' ) ) :
/**
* Deactivate WP Rocket lazyload if Autoptimize lazyload is enabled
*
* @since 3.3.4
* @author Remy Perona
*
* @param string $old_value Previous autoptimize option value.
* @param string $value New autoptimize option value.
* @return void
*/
function rocket_maybe_deactivate_lazyload( $old_value, $value ) {
if ( empty( $old_value['autoptimize_imgopt_checkbox_field_3'] ) && ! empty( $value['autoptimize_imgopt_checkbox_field_3'] ) ) {
update_rocket_option( 'lazyload', 0 );
update_rocket_option( 'lazyload_iframes', 0 );
update_rocket_option( 'lazyload_youtube', 0 );
}
}
add_action( 'update_option_autoptimize_imgopt_settings', 'rocket_maybe_deactivate_lazyload', 10, 2 );

/**
* Deactivate WP Rocket HTML Minification if Autoptimize HTML minification is enabled
*
Expand Down Expand Up @@ -103,9 +122,35 @@ function rocket_activate_autoptimize() {
if ( 'on' === get_option( 'autoptimize_css_defer' ) ) {
update_rocket_option( 'async_css', 0 );
}

$lazyload = get_option( 'autoptimize_imgopt_settings' );

if ( ! empty( $lazyload['autoptimize_imgopt_checkbox_field_3'] ) ) {
update_rocket_option( 'lazyload', 0 );
update_rocket_option( 'lazyload_iframes', 0 );
update_rocket_option( 'lazyload_youtube', 0 );
}
}
add_action( 'activate_autoptimize/autoptimize.php', 'rocket_activate_autoptimize', 11 );

/**
* Disable WP Rocket lazyload fields if Autoptimize lazyload is enabled
*
* @since 3.3.4
* @author Remy Perona
*
* @return bool
*/
function rocket_maybe_disable_lazyload() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't functions related to Autoptimize be named accordingly? Like rocket_autoptimize_maybe_disable_lazyload().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the previous functions were already named that way so I kept the same schema

$lazyload = get_option( 'autoptimize_imgopt_settings' );

if ( is_plugin_active( 'autoptimize/autoptimize.php' ) && ! empty( $lazyload['autoptimize_imgopt_checkbox_field_3'] ) ) {
return true;
}

return false;
}

/**
* Disable WP Rocket HTML minification field if Autoptimize HTML minification is enabled
*
Expand Down
54 changes: 48 additions & 6 deletions inc/3rd-party/themes/avada.php
Expand Up @@ -6,7 +6,7 @@
if ( 'Avada' === $current_theme->get( 'Name' ) ) {
// When Avada theme purge its own cache.
add_action( 'avada_clear_dynamic_css_cache', 'rocket_clean_domain' );

/**
* Conflict with Avada theme and WP Rocket CDN
*
Expand All @@ -18,17 +18,22 @@
*/
function rocket_fix_cdn_for_avada_theme( $vars, $handle ) {
if ( 'avada-dynamic' === $handle && get_rocket_option( 'cdn' ) ) {

$src = get_rocket_cdn_url( get_template_directory_uri() . '/assets/less/theme/dynamic.less' );
$src = get_rocket_cdn_url( get_template_directory_uri() . '/assets/less/theme/dynamic.less' );
$vars['template-directory'] = sprintf( '~"%s"', dirname( dirname( dirname( dirname( $src ) ) ) ) );
$vars['lessurl'] = sprintf( '~"%s"', dirname( $src ) );
$vars['lessurl'] = sprintf( '~"%s"', dirname( $src ) );
}
return $vars;
}
add_filter( 'less_vars', 'rocket_fix_cdn_for_avada_theme', 11, 2 );

$avada_options = get_option( 'avada_theme_options' );

/**
* Exclude fusion styles from cache busting to prevent cache dir issues
*
* @author Remy Perona
*
* @param array $excluded_files An array of excluded files.
* @return array
*/
function rocket_exclude_avada_dynamic_css( $excluded_files ) {
$upload_dir = wp_upload_dir();

Expand All @@ -37,4 +42,41 @@ function rocket_exclude_avada_dynamic_css( $excluded_files ) {
return $excluded_files;
}
add_filter( 'rocket_exclude_cache_busting', 'rocket_exclude_avada_dynamic_css' );

/**
* Deactivate WP Rocket lazyload if Avada lazyload is enabled
*
* @since 3.3.4
* @author Remy Perona
*
* @param string $old_value Previous Avada option value.
* @param string $value New Avada option value.
* @return void
*/
function rocket_avada_maybe_deactivate_lazyload( $old_value, $value ) {
if ( empty( $old_value['lazy_load'] ) && ! empty( $value['lazy_load'] ) ) {
update_rocket_option( 'lazyload', 0 );
}
}
add_action( 'update_option_avada_theme_options', 'rocket_avada_maybe_deactivate_lazyload', 10, 2 );
}

/**
* Disable WP Rocket lazyload field if Avada lazyload is enabled
*
* @since 3.3.4
* @author Remy Perona
*
* @return bool
*/
function rocket_avada_maybe_disable_lazyload() {
$avada_options = get_option( 'avada_theme_options' );
$current_theme = wp_get_theme();

if ( 'Avada' === $current_theme->get( 'Name' ) && ! empty( $avada_options['lazy_load'] ) ) {
return true;
}

return false;
}

17 changes: 17 additions & 0 deletions inc/classes/admin/settings/class-page.php
Expand Up @@ -898,6 +898,8 @@ private function media_section() {
'url' => $lazyload_beacon['url'],
],
'page' => 'media',
// translators: %1$s = “WP Rocket”.
'helper' => rocket_maybe_disable_lazyload() ? sprintf( __( 'Lazyload is currently activated in <strong>Autoptimize</strong>. If you want to use %1$s’s lazyload, disable this option in Autoptimize.', 'rocket' ), WP_ROCKET_PLUGIN_NAME ) : '',
],
'emoji_section' => [
'title' => __( 'Emoji 👻', 'rocket' ),
Expand All @@ -923,9 +925,17 @@ private function media_section() {
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'container_class' => [
( rocket_avada_maybe_disable_lazyload() || rocket_maybe_disable_lazyload() ) ? 'wpr-isDisabled' : '',
],
'input_attr' => [
'disabled' => ( rocket_avada_maybe_disable_lazyload() || rocket_maybe_disable_lazyload() ) ? 1 : 0,
],
'description' => rocket_avada_maybe_disable_lazyload() ? _x('Lazyload for images is currently activated in Avada. If you want to use WP Rocket’s LazyLoad, disable this option in Avada.', 'Avada', 'rocket' ) : '',
],
'lazyload_iframes' => [
'container_class' => [
rocket_maybe_disable_lazyload() ? 'wpr-isDisabled' : '',
'wpr-isParent',
],
'type' => 'checkbox',
Expand All @@ -934,9 +944,13 @@ private function media_section() {
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_maybe_disable_lazyload() ? 1 : 0,
],
],
'lazyload_youtube' => [
'container_class' => [
rocket_maybe_disable_lazyload() ? 'wpr-isDisabled' : '',
'wpr-field--children',
],
'type' => 'checkbox',
Expand All @@ -947,6 +961,9 @@ private function media_section() {
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_maybe_disable_lazyload() ? 1 : 0,
],
],
'emoji' => [
'type' => 'checkbox',
Expand Down