artemsemkin

Rhye Images Always Full Size

Jan 10th, 2021
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arts_the_lazy_image( $args ) {
  2.   $defaults = array(
  3.     'id'        => null,
  4.     'type'      => 'background',
  5.     'size'      => 'full',
  6.     'caption'   => null,
  7.     'class'     => array(
  8.       'section' => array(),
  9.       'wrapper' => array(),
  10.       'image'   => array(),
  11.       'caption' => array(),
  12.       'overlay' => array(),
  13.     ),
  14.     'parallax'  => array(
  15.       'enabled' => false,
  16.       'factor'  => 0.1,
  17.     ),
  18.     'animation' => false,
  19.     'mask'      => false,
  20.     'overlay'   => false,
  21.   );
  22.  
  23.   $class_section = '';
  24.   $attrs_section = '';
  25.  
  26.   $attrs_wrapper = '';
  27.   $class_wrapper = '';
  28.  
  29.   $class_caption = '';
  30.  
  31.   $class_media = '';
  32.  
  33.   $class_overlay = '';
  34.  
  35.   $placeholder_src = apply_filters( 'arts/lazy/placeholder', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAHCGzyUAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAADa6r/EAAAAC0lEQVQI12NolQQAASYAn89qhTcAAAAASUVORK5CYII=' );
  36.  
  37.   $args = wp_parse_args( $args, $defaults );
  38.  
  39.   if ( ! $args['id'] || ! $args['type'] ) {
  40.     return;
  41.   }
  42.  
  43.   // section
  44.   if ( array_key_exists( 'section', $args['class'] ) && is_array( $args['class']['section'] ) && ! empty( $args['class']['section'] ) ) {
  45.     $class_section = implode( ' ', $args['class']['section'] );
  46.   }
  47.  
  48.   // wrapper
  49.   if ( array_key_exists( 'wrapper', $args['class'] ) && is_array( $args['class']['wrapper'] ) && ! empty( $args['class']['wrapper'] ) ) {
  50.     $class_wrapper = implode( ' ', $args['class']['wrapper'] );
  51.   }
  52.  
  53.   // caption
  54.   if ( array_key_exists( 'caption', $args['class'] ) && is_array( $args['class']['caption'] ) && ! empty( $args['class']['caption'] ) ) {
  55.     $class_caption = implode( ' ', $args['class']['caption'] );
  56.   }
  57.  
  58.   // image
  59.   if ( array_key_exists( 'image', $args['class'] ) && is_array( $args['class']['image'] ) && ! empty( $args['class']['image'] ) ) {
  60.     $class_media = implode( ' ', $args['class']['image'] );
  61.   }
  62.  
  63.   // parallax
  64.   if ( array_key_exists( 'parallax', $args ) && is_array( $args['parallax'] ) && $args['parallax']['enabled'] ) {
  65.     $class_wrapper .= ' overflow';
  66.     $attrs_section .= ' data-arts-parallax=true';
  67.     $attrs_section .= ' data-arts-parallax-factor=' . floatval( $args['parallax']['factor'] );
  68.   }
  69.  
  70.   // overlay
  71.   if ( $args['overlay'] && array_key_exists( 'overlay', $args['class'] ) && is_array( $args['class']['overlay'] ) && ! empty( $args['class']['overlay'] ) ) {
  72.     $class_overlay = implode( ' ', $args['class']['overlay'] );
  73.   }
  74.  
  75.   // mask
  76.   if ( $args['mask'] ) {
  77.     $class_wrapper .= ' mask-reveal';
  78.   }
  79.  
  80.   // animation
  81.   if ( $args['animation'] ) {
  82.     $attrs_section .= ' data-arts-os-animation=true';
  83.   }
  84.  
  85.   switch ( $args['type'] ) {
  86.     case 'background':
  87.       if ( $args['class']['wrapper'] !== false ) {
  88.         $class_wrapper .= ' lazy-bg';
  89.       }
  90.       $class_media .= ' of-cover';
  91.       break;
  92.     case 'image':
  93.       if ( $args['class']['wrapper'] !== false ) {
  94.         $class_wrapper .= ' lazy';
  95.       }
  96.       break;
  97.     case 'texture':
  98.       if ( $args['class']['wrapper'] !== false ) {
  99.         $class_wrapper .= ' lazy';
  100.       }
  101.       break;
  102.     case 'background-video':
  103.       $class_media .= ' of-cover';
  104.       break;
  105.     case 'video':
  106.       break;
  107.   }
  108.  
  109.   if ( $args['type'] === 'background' || $args['type'] === 'image' || $args['type'] === 'texture' ) {
  110.     $attrs  = wp_get_attachment_image_src( $args['id'], $args['size'] );
  111.     $srcset = wp_get_attachment_image_srcset( $args['id'], $args['size'] );
  112.     $sizes  = wp_get_attachment_image_sizes( $args['id'], $args['size'] );
  113.     $alt    = get_post_meta( $args['id'], '_wp_attachment_image_alt', true );
  114.   }
  115.  
  116.   ?>
  117.   <?php if ( ! empty( $class_section ) || ! empty( $attrs_section ) ) : ?>
  118.     <div class="<?php echo esc_attr( trim( $class_section ) ); ?>" <?php echo esc_attr( trim( $attrs_section ) ); ?>>
  119.   <?php endif; ?>
  120.     <?php if ( ! empty( $class_wrapper ) || ! empty( $attrs_wrapper ) ) : ?>
  121.       <?php if ( $args['type'] === 'image' || $args['type'] === 'texture' ) : ?>
  122.         <div class="<?php echo esc_attr( trim( $class_wrapper ) ); ?>" <?php echo esc_attr( trim( $attrs_wrapper ) ); ?> style="padding-bottom: calc( (<?php echo esc_attr( $attrs[2] ); ?> / <?php echo esc_attr( $attrs[1] ); ?>) * 100% ); height: 0;">
  123.       <?php else : ?>
  124.         <div class="<?php echo esc_attr( trim( $class_wrapper ) ); ?>" <?php echo esc_attr( trim( $attrs_wrapper ) ); ?>>
  125.       <?php endif; ?>
  126.       <?php if ( $args['mask'] ) : ?>
  127.         <div class="mask-reveal__layer mask-reveal__layer-1">
  128.           <div class="mask-reveal__layer mask-reveal__layer-2">
  129.       <?php endif; ?>
  130.     <?php endif; ?>
  131.       <?php
  132.       switch ( $args['type'] ) {
  133.         case 'background':
  134.           ?>
  135.             <img class="<?php echo esc_attr( trim( $class_media ) ); ?>" src="<?php echo esc_attr( $placeholder_src ); ?>" data-src="<?php echo esc_attr( $attrs[0] ); ?>"  alt="<?php echo esc_attr( $alt ); ?>" />
  136.             <?php
  137.           break;
  138.         case 'image':
  139.           ?>
  140.             <img class="<?php echo esc_attr( trim( $class_media ) ); ?>" src="<?php echo esc_attr( $placeholder_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 ); ?>"/>
  141.             <?php
  142.           break;
  143.         case 'texture':
  144.           ?>
  145.             <img class="<?php echo esc_attr( trim( $class_media ) ); ?>" src="<?php echo esc_attr( $attrs[0] ); ?>" srcset="<?php echo esc_attr( $srcset ); ?>" sizes="<?php echo esc_attr( $sizes ); ?>" alt="<?php echo esc_attr( $alt ); ?>" />
  146.             <?php
  147.           break;
  148.         case 'background-video':
  149.           ?>
  150.             <video class="<?php echo esc_attr( trim( $class_media ) ); ?>" src="<?php echo esc_url( wp_get_attachment_url( $args['id'] ) ); ?>" playsinline loop muted autoplay></video>
  151.             <?php
  152.           break;
  153.         case 'video':
  154.           ?>
  155.             <video class="<?php echo esc_attr( trim( $class_media ) ); ?>" src="<?php echo esc_url( wp_get_attachment_url( $args['id'] ) ); ?>" playsinline loop muted autoplay></video>
  156.             <?php
  157.           break;
  158.       }
  159.       ?>
  160.     <?php if ( ! empty( $class_wrapper ) ) : ?>
  161.       <?php if ( $args['mask'] ) : ?>
  162.             <?php if ( $args['overlay'] ) : ?>
  163.               <div class="overlay <?php echo esc_attr( trim( $class_overlay ) ); ?>"></div>
  164.             <?php endif; ?>
  165.           </div>
  166.         </div>
  167.       <?php endif; ?>
  168.       </div>
  169.     <?php endif; ?>
  170.     <?php if ( array_key_exists( 'caption', $args ) && ! empty( $args['caption'] ) ) : ?>
  171.       <?php
  172.       if ( $args['caption'] === true ) {
  173.         $caption = wp_get_attachment_caption( $args['id'] );
  174.       } else {
  175.         $caption = $args['caption'];
  176.       }
  177.       ?>
  178.       <div class="<?php echo esc_attr( $class_caption ); ?>"><?php echo esc_html( $caption ); ?></div>
  179.     <?php endif; ?>
  180.     <?php if ( $args['overlay'] && ! $args['mask'] ) : ?>
  181.       <div class="overlay <?php echo esc_attr( trim( $class_overlay ) ); ?>"></div>
  182.     <?php endif; ?>
  183.   <?php if ( ! empty( $class_section ) || ! empty( $attrs_section ) ) : ?>
  184.     </div>
  185.   <?php endif; ?>
  186.   <?php
  187. }
  188.  
RAW Paste Data