Skip to content

Commit

Permalink
Merge pull request #1739 from wp-media/fix/1707-lazyload-compatibility
Browse files Browse the repository at this point in the history
Fixes #1707 Autoptimize & Avada lazyload compatibility
  • Loading branch information
Screenfeed committed May 28, 2019
2 parents b349339 + 928c72c commit 305b9f3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
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() {
$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 @@ -876,6 +876,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 @@ -901,9 +903,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 @@ -912,9 +922,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 @@ -925,6 +939,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

0 comments on commit 305b9f3

Please sign in to comment.