Advertisement
artemsemkin

/wp-content/themes/rubenz/inc/functions/the_lazy_image.php

Oct 19th, 2020
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Markup for lazy background/image/video
  5.  */
  6. if ( ! function_exists( 'arts_the_lazy_image' ) ) {
  7.   function arts_the_lazy_image( $args ) {
  8.     $defaults = array(
  9.       'id'   => null,
  10.       'type' => 'background',
  11.       'size' => 'full',
  12.       'class' => array(
  13.         'section' => array(),
  14.         'wrapper' => array(),
  15.         'image' => array()
  16.       ),
  17.       'attribute' => array(
  18.         'image' => array()
  19.       ),
  20.       'parallax' => array(
  21.         'enabled' => false,
  22.         'factor' => 0.1
  23.       ),
  24.     );
  25.  
  26.     $class_section = '';
  27.     $attrs_section = '';
  28.  
  29.     $attrs_wrapper = '';
  30.     $class_wrapper = '';
  31.  
  32.     $class_media = '';
  33.     $attrs_media = '';
  34.  
  35.     $args = wp_parse_args( $args, $defaults );
  36.  
  37.     if ( ! $args['id'] || ! $args['type'] ) {
  38.       return;
  39.     }
  40.  
  41.     // section
  42.     if ( array_key_exists( 'section', $args['class'] ) && is_array( $args['class']['section'] ) && ! empty ( $args['class']['section'] ) ) {
  43.       $class_section = implode(' ', $args['class']['section'] );
  44.     }
  45.  
  46.     // wrapper
  47.     if ( array_key_exists( 'wrapper', $args['class'] ) && is_array( $args['class']['wrapper'] ) && ! empty ( $args['class']['wrapper'] ) ) {
  48.       $class_wrapper = implode(' ', $args['class']['wrapper'] );
  49.     }
  50.  
  51.     // image class
  52.     if ( array_key_exists( 'image', $args['class'] ) && is_array( $args['class']['image'] ) && ! empty ( $args['class']['image'] ) ) {
  53.       $class_media = implode(' ', $args['class']['image'] );
  54.     }
  55.  
  56.     // image attributes
  57.     if ( array_key_exists( 'image', $args['attribute'] ) && is_array( $args['attribute']['image'] ) && ! empty ( $args['attribute']['image'] ) ) {
  58.       $attrs_media = implode(' ', $args['attribute']['image'] );
  59.     }
  60.  
  61.     // parallax
  62.     if ( array_key_exists( 'parallax', $args ) && is_array( $args['parallax'] ) && $args['parallax']['enabled'] ) {
  63.       $attrs_wrapper .= ' data-art-parallax=true';
  64.       $attrs_wrapper .= ' data-art-parallax-factor=' . floatval( $args['parallax']['factor'] );
  65.     }
  66.  
  67.     switch ( $args['type'] ) {
  68.       case 'background':
  69.         $class_media .= ' lazy-bg of-cover';
  70.         break;
  71.       case 'image':
  72.         if ( $args['class']['wrapper'] !== false ) {
  73.           $class_wrapper .= ' lazy';
  74.         }
  75.         break;
  76.       case 'background-video':
  77.         $class_media .= ' of-cover';
  78.         break;
  79.       case 'video':
  80.         //
  81.         break;
  82.     }
  83.  
  84.     if ( $args['type'] === 'background' || $args['type'] === 'image' ) {
  85.       $attrs  = wp_get_attachment_image_src( $args['id'], $args['size'] );
  86.       $alt    = get_post_meta( $args['id'], '_wp_attachment_image_alt', true );
  87.     }
  88.  
  89.     ?>
  90.     <?php if ( ! empty( $class_section ) || ! empty( $attrs_section ) ) : ?>
  91.       <div class="<?php echo esc_attr( $class_section ); ?>" <?php echo esc_attr( $attrs_section ); ?>>
  92.     <?php endif; ?>
  93.       <?php if ( ! empty( $class_wrapper ) || ! empty( $attrs_wrapper ) ) : ?>
  94.         <?php if ( $args['type'] === 'image' ) : ?>
  95.           <div class="<?php echo esc_attr( $class_wrapper ); ?>" <?php echo esc_attr( $attrs_wrapper ); ?> style="padding-bottom: calc( (<?php echo esc_attr( $attrs[2] ); ?> / <?php echo esc_attr( $attrs[1] ); ?>) * 100% ); height: 0;">
  96.         <?php else : ?>
  97.           <div class="<?php echo esc_attr( $class_wrapper ); ?>" <?php echo esc_attr( $attrs_wrapper ); ?>>
  98.         <?php endif; ?>
  99.       <?php endif; ?>
  100.         <?php
  101.           switch ( $args['type'] ) {
  102.             case 'background':
  103.               ?>
  104.               <img class="<?php echo esc_attr( $class_media ); ?>" src="#" data-src="<?php echo esc_attr( $attrs[0] ); ?>" alt="<?php echo esc_attr( $alt ); ?>" <?php echo esc_attr( $attrs_media ); ?> />
  105.               <?php
  106.               break;
  107.             case 'image':
  108.               ?>
  109.               <img class="<?php echo esc_attr( $class_media ); ?>" src="#" data-src="<?php echo esc_attr( $attrs[0] ); ?>" width="<?php echo esc_attr( $attrs[1] ); ?>" height="<?php echo esc_attr( $attrs[2] ); ?>" alt="<?php echo esc_attr( $alt ); ?>"/>
  110.               <?php
  111.               break;
  112.             case 'background-video':
  113.               ?>
  114.               <video class="<?php echo esc_attr( $class_media ); ?>" src="<?php echo esc_url( wp_get_attachment_url( $args['id'] ) ); ?>" playsinline loop muted autoplay></video>
  115.               <?php
  116.               break;
  117.             case 'video':
  118.               ?>
  119.               <video class="<?php echo esc_attr( $class_media ); ?>" src="<?php echo esc_url( wp_get_attachment_url( $args['id'] ) ); ?>" playsinline loop muted autoplay></video>
  120.               <?php
  121.               break;
  122.           }
  123.         ?>
  124.       <?php if ( ! empty( $class_wrapper ) ) : ?>
  125.         </div>
  126.       <?php endif; ?>
  127.     <?php if ( ! empty( $class_section ) || ! empty( $attrs_section ) ) : ?>
  128.       </div>
  129.     <?php endif; ?>
  130.     <?php
  131.   }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement