| 1 |
<?php |
| 2 |
|
| 3 |
namespace OptimoleWP\BgOptimizer; |
| 4 |
|
| 5 |
use OptimoleWP\PageProfiler\Profile; |
| 6 |
use OptimoleWP\Preload\Links; |
| 7 |
use Optml_Lazyload_Replacer; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Lazyload |
| 11 |
* |
| 12 |
* @package OptimoleWP\BgOptimizer |
| 13 |
*/ |
| 14 |
class Lazyload { |
| 15 |
const MARKER = '/* OPTML_VIEWPORT_BG_SELECTORS */'; |
| 16 |
/** |
| 17 |
* Get the current personalized CSS for lazy loading. |
| 18 |
* |
| 19 |
* @return string The personalized CSS. |
| 20 |
*/ |
| 21 |
public static function get_current_personalized_css() { |
| 22 |
return self::get_personalized_css( Profile::get_current_profile_data() ); |
| 23 |
} |
| 24 |
/** |
| 25 |
* Get personalized CSS based on profile data. |
| 26 |
* |
| 27 |
* @param array $data Profile data. |
| 28 |
* |
| 29 |
* @return string The personalized CSS. |
| 30 |
*/ |
| 31 |
public static function get_personalized_css( $data ) { |
| 32 |
$lazyload_selectors = array_values( Optml_Lazyload_Replacer::get_background_lazyload_selectors() ); |
| 33 |
$lazyload_selectors = array_fill_keys( $lazyload_selectors, true ); |
| 34 |
$css_selectors = []; |
| 35 |
$preload_urls = []; |
| 36 |
foreach ( Profile::get_active_devices() as $device ) { |
| 37 |
$personalized_selectors = $data[ $device ]['bg'] ?? []; |
| 38 |
$lcp_data = $data[ $device ]['lcp'] ?? []; |
| 39 |
if ( OPTML_DEBUG ) { |
| 40 |
do_action( 'optml_log', 'personalized_selectors: ' . $device . ' ' . print_r( $personalized_selectors, true ) ); |
| 41 |
do_action( 'optml_log', 'LCP data: ' . $device . ' ' . print_r( $lcp_data, true ) ); |
| 42 |
} |
| 43 |
$css_selectors[ $device ] = []; |
| 44 |
foreach ( $personalized_selectors as $selector => $above_fold_selectors ) { |
| 45 |
if ( ! isset( $lazyload_selectors[ $selector ] ) ) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
if ( empty( $above_fold_selectors ) ) { |
| 49 |
$css_selectors[ $device ][] = 'html ' . strip_tags( $selector ) . ':not(.optml-bg-lazyloaded)'; |
| 50 |
} else { |
| 51 |
foreach ( $above_fold_selectors as $above_fold_selector => $bg_urls ) { |
| 52 |
$css_selectors[ $device ][] = 'html ' . strip_tags( $selector ) . ':not(' . strip_tags( $above_fold_selector ) . '):not(.optml-bg-lazyloaded)'; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$preload_urls[ $device ] = []; |
| 58 |
$css_selectors[ $device ] = array_unique( $css_selectors[ $device ] ); |
| 59 |
if ( isset( $lcp_data['type'] ) && $lcp_data['type'] === 'bg' ) { |
| 60 |
if ( ! empty( $lcp_data['bgSelector'] ) ) { |
| 61 |
$css_selectors[ $device ] = array_map( |
| 62 |
function ( $selector ) use ( $lcp_data ) { |
| 63 |
return $selector . ':not(' . strip_tags( $lcp_data['bgSelector'] ) . ')'; |
| 64 |
}, |
| 65 |
$css_selectors[ $device ] |
| 66 |
); |
| 67 |
} |
| 68 |
if ( ! empty( $lcp_data['bgUrls'] ) ) { |
| 69 |
$preload_urls[ $device ] = array_merge( $preload_urls[ $device ], $lcp_data['bgUrls'] ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
if ( OPTML_DEBUG ) { |
| 74 |
do_action( 'optml_log', 'BGCSS selectors: ' . print_r( $css_selectors, true ) ); |
| 75 |
do_action( 'optml_log', 'BGPreload URLs: ' . print_r( $preload_urls, true ) ); |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( array_intersect( $preload_urls[ Profile::DEVICE_TYPE_MOBILE ], $preload_urls[ Profile::DEVICE_TYPE_DESKTOP ] ) as $url ) { |
| 79 |
Links::add_link( [ 'url' => $url, 'priority' => 'high' ] ); |
| 80 |
} |
| 81 |
|
| 82 |
$hide_rule = ' { background-image: none !important; }'; |
| 83 |
$mobile_selectors = implode( ',', $css_selectors[ Profile::DEVICE_TYPE_MOBILE ] ); |
| 84 |
$desktop_selectors = implode( ',', $css_selectors[ Profile::DEVICE_TYPE_DESKTOP ] ); |
| 85 |
|
| 86 |
if ( $mobile_selectors === $desktop_selectors ) { |
| 87 |
return empty( $mobile_selectors ) ? '' : $mobile_selectors . $hide_rule; |
| 88 |
} |
| 89 |
// if any of those are empty, return the other one |
| 90 |
if ( empty( $mobile_selectors ) ) { |
| 91 |
return $desktop_selectors . $hide_rule; |
| 92 |
} |
| 93 |
if ( empty( $desktop_selectors ) ) { |
| 94 |
return $mobile_selectors . $hide_rule; |
| 95 |
} |
| 96 |
|
| 97 |
// generate media query for desktop and mobile |
| 98 |
$media_query = '@media (max-width: 600px) { ' . $mobile_selectors . $hide_rule . ' } @media (min-width: 600px) { ' . $desktop_selectors . $hide_rule . ' }'; |
| 99 |
return $media_query; |
| 100 |
} |
| 101 |
} |
| 102 |
|