PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/theme/library/helpers/class-image.php

https://gitlab.com/pacificsky/adult-education-pathways-dev
PHP | 351 lines | 143 code | 59 blank | 149 comment | 15 complexity | 4e33a30cd4f3633a5e5fd488adf21a44 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Responsive Images
  4. * Implementation
  5. *
  6. * @author Max G J Panas <http://maxpanas.com>
  7. */
  8. /**
  9. * Class MOZ_Image
  10. *
  11. */
  12. class MOZ_Image {
  13. /**
  14. * Determine whether an
  15. * image should be
  16. * lazy-loaded
  17. *
  18. * default: false
  19. *
  20. * @param array $flags
  21. *
  22. * @return bool
  23. */
  24. protected static function is_lazy( $flags ) {
  25. return isset( $flags['lazy'] ) && ! ! $flags['lazy'];
  26. }
  27. /**
  28. * Determine whether a
  29. * lazy image should have
  30. * the lazy class from the
  31. * start
  32. *
  33. * default: true
  34. *
  35. * @param array $flags
  36. *
  37. * @return bool
  38. */
  39. protected static function should_add_lazy_class( $flags ) {
  40. return isset( $flags['lazy_class'] )
  41. ? ! ! $flags['lazy_class']
  42. : true;
  43. }
  44. /**
  45. * Maybe "lazify" the attributes
  46. * of an image depending on
  47. * whether it should
  48. * be lazy-loaded
  49. *
  50. * @param array $flags
  51. * @param array $attrs
  52. * @param bool|null $add_class
  53. * @param bool|null $add_src
  54. *
  55. * @return array
  56. */
  57. public static function maybe_lazify( $flags, $attrs, $add_class = true, $add_src = true ) {
  58. if ( ! self::is_lazy( $flags ) ) {
  59. return $attrs;
  60. }
  61. $lazifiables = array( 'src', 'srcset', 'sizes' );
  62. foreach ( $lazifiables as $lazifiable ) {
  63. if ( isset( $attrs[ $lazifiable ] ) ) {
  64. $attrs["data-$lazifiable"] = $attrs[ $lazifiable ];
  65. unset( $attrs[ $lazifiable ] );
  66. }
  67. }
  68. if ( $add_class && self::should_add_lazy_class( $flags ) ) {
  69. $attrs['class'] = isset( $attrs['class'] )
  70. ? "{$attrs['class']} lazyload"
  71. : 'lazyload';
  72. }
  73. if ( $add_src ) {
  74. $attrs['src'] = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
  75. }
  76. return $attrs;
  77. }
  78. /**
  79. * Get an image's alt
  80. * attribute content
  81. *
  82. * @param $image
  83. *
  84. * @return string
  85. */
  86. public static function get_img_alt( $image ) {
  87. return trim( strip_tags( get_post_meta( $image, '_wp_attachment_image_alt', true ) ) );
  88. }
  89. /**
  90. * Print an image's alt
  91. * attribute content
  92. *
  93. * @param $image
  94. */
  95. public static function img_alt( $image ) {
  96. echo self::get_img_alt( $image );
  97. }
  98. /**
  99. * Get an image's src
  100. * attribute for the
  101. * specified size
  102. *
  103. * @param $image
  104. * @param string $size
  105. *
  106. * @return bool
  107. */
  108. public static function get_src( $image, $size = 'full' ) {
  109. if ( $src = wp_get_attachment_image_src( $image, $size, false ) ) {
  110. return $src[0];
  111. }
  112. return false;
  113. }
  114. /**
  115. * Print an image's src
  116. * attribute for the
  117. * specified size
  118. *
  119. * @param $image
  120. * @param string $size
  121. */
  122. public static function src( $image, $size = 'full' ) {
  123. echo self::get_src( $image, $size );
  124. }
  125. /**
  126. * Get the markup for a
  127. * responsive background
  128. * image
  129. *
  130. * @param int $image
  131. * @param string $base_size
  132. * @param array $sizes
  133. * @param array|null $attrs
  134. * @param array|null $flags
  135. *
  136. * @return bool|string
  137. */
  138. public static function get_background( $image, $base_size, $sizes, $attrs = array(), $flags = array() ) {
  139. if ( ! wp_attachment_is_image( $image ) ) {
  140. return false;
  141. }
  142. $bgel_attrs = array_merge( array(
  143. 'class' => '',
  144. 'role' => 'img',
  145. 'alt' => self::get_img_alt( $image )
  146. ), (array) $attrs );
  147. $alt = $bgel_attrs['alt'];
  148. unset( $bgel_attrs['alt'] );
  149. $content = MOZ_Html::get_element( 'span', array( 'class' => 'visuallyhidden' ), $alt );
  150. $bgel_attrs['class'] .= ' moz-background-picture';
  151. if ( self::is_lazy( $flags ) ) {
  152. // lazy loaded background image
  153. if ( self::should_add_lazy_class( $flags ) ) {
  154. $bgel_attrs['class'] .= ' lazyload';
  155. }
  156. $bgset = array( self::get_src( $image, $base_size ) );
  157. foreach ( $sizes as $size => $query ) {
  158. $src = self::get_src( $image, $size );
  159. $bgset[] = "$src [$query]";
  160. }
  161. $bgel_attrs['data-sizes'] = 'auto';
  162. $bgel_attrs['data-bgset'] = implode( ' | ', array_reverse( $bgset ) );
  163. } else {
  164. // not lazy-loaded background image
  165. $unique = uniqid( 'moz-background-picture--' );
  166. $bgel_attrs['class'] .= " $unique";
  167. ob_start(); ?>
  168. <style>
  169. .<?php echo $unique; ?> {
  170. background-image: url('<?php self::src( $image, $base_size ); ?>');
  171. }
  172. <?php foreach ( $sizes as $size => $query ) : ?>
  173. @media all and <?php echo esc_html( $query ); ?> {
  174. .<?php echo $unique; ?> {
  175. background-image: url('<?php self::src( $image, $size ); ?>');
  176. }
  177. }
  178. <?php endforeach; ?>
  179. </style>
  180. <?php
  181. $content .= ob_get_clean();
  182. }
  183. return MOZ_Html::get_element( 'span', $bgel_attrs, $content );
  184. }
  185. /**
  186. * Print the markup for a
  187. * responsive background
  188. * image
  189. *
  190. * @param int $image
  191. * @param string $base_size
  192. * @param array $sizes
  193. * @param array|null $attrs
  194. * @param array|null $flags
  195. */
  196. public static function background( $image, $base_size, $sizes, $attrs = array(), $flags = array() ) {
  197. echo self::get_background( $image, $base_size, $sizes, $attrs, $flags );
  198. }
  199. /**
  200. * Get the markup for a
  201. * picture element
  202. *
  203. * @param int $image
  204. * @param string $base_size
  205. * @param array $sizes
  206. * @param array|null $attrs
  207. * @param array|null $flags
  208. *
  209. * @return bool|string
  210. */
  211. public static function get_picture( $image, $base_size, $sizes, $attrs = array(), $flags = array() ) {
  212. if ( ! wp_attachment_is_image( $image ) ) {
  213. return false;
  214. }
  215. $content = array();
  216. // required for IE9 support...
  217. $content[] = '<!--[if IE 9]><video style="display: none;"><![endif]-->';
  218. foreach ( array_reverse( $sizes ) as $size => $query ) {
  219. $source_attrs = self::maybe_lazify( $flags, array(
  220. 'srcset' => esc_attr( self::get_src( $image, $size ) ),
  221. 'media' => esc_attr( $query ),
  222. 'type' => esc_attr( get_post_mime_type( $image ) )
  223. ), false, false );
  224. $content[] = MOZ_Html::get_sc_element( 'source', $source_attrs );
  225. }
  226. $content[] = '<!--[if IE 9]></video><![endif]-->';
  227. $img_attrs = self::maybe_lazify( $flags, array_merge( array(
  228. 'srcset' => esc_attr( self::get_src( $image, $base_size ) ),
  229. 'alt' => self::get_img_alt( $image )
  230. ), (array) $attrs ) );
  231. $content[] = MOZ_Html::get_sc_element( 'img', $img_attrs );
  232. return MOZ_Html::get_element( 'picture', null, implode( '', $content ) );
  233. }
  234. /**
  235. * Print the markup for a
  236. * picture element
  237. *
  238. * @param int $image
  239. * @param string $base_size
  240. * @param array $sizes
  241. * @param array|null $attrs
  242. * @param array|null $flags
  243. */
  244. public static function picture( $image, $base_size, $sizes, $attrs = array(), $flags = array() ) {
  245. echo self::get_picture( $image, $base_size, $sizes, $attrs, $flags );
  246. }
  247. /**
  248. * Get the markup for an image
  249. * using srcset and sizes
  250. *
  251. * @param int $image
  252. * @param array $sources
  253. * @param array $sizes
  254. * @param array|null $attrs
  255. * @param array|null $flags
  256. *
  257. * @returns string
  258. */
  259. public static function get_image( $image, $sources, $sizes, $attrs = array(), $flags = array() ) {
  260. if ( ! wp_attachment_is_image( $image ) ) {
  261. return false;
  262. }
  263. $srcset = array();
  264. foreach ( $sources as $size ) {
  265. if ( $src = wp_get_attachment_image_src( $image, $size, false ) ) {
  266. $srcset[] = "{$src[0]} {$src[1]}w {$src[2]}h";
  267. }
  268. }
  269. if ( empty( $srcset ) ) {
  270. return false;
  271. }
  272. $img_attrs = self::maybe_lazify( $flags, array_merge( array(
  273. 'srcset' => implode( ', ', $srcset ),
  274. 'sizes' => implode( ', ', $sizes ),
  275. 'alt' => self::get_img_alt( $image )
  276. ), (array) $attrs ) );
  277. return MOZ_Html::get_sc_element( 'img', $img_attrs );
  278. }
  279. /**
  280. * Print the markup for an image
  281. * using srcset and sizes
  282. *
  283. * @param int $image
  284. * @param array $sources
  285. * @param array $sizes
  286. * @param array|null $attrs
  287. * @param array|null $flags
  288. */
  289. public static function image( $image, $sources, $sizes, $attrs = array(), $flags = array() ) {
  290. echo self::get_image( $image, $sources, $sizes, $attrs, $flags );
  291. }
  292. }