Guest User

Untitled

a guest
Oct 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. <?php
  2. $data = c27()->merge_options([
  3. 'icon' => '',
  4. 'icon_style' => 1,
  5. 'gallery_items' => [],
  6. 'gallery_item_interface' => 'WP_IMAGE_OBJECT',
  7. 'items_per_row' => 3,
  8. 'items_per_row_mobile' => 2,
  9. 'gallery_type' => 'carousel',
  10. 'wrapper_class' => 'block-element',
  11. 'wrapper_id' => '',
  12. 'ref' => '',
  13. 'is_edit_mode' => false,
  14. ], $data);
  15.  
  16.  
  17. // Since images won't always be available as WP Image Objects, we need to add support for only
  18. // providing the image url's, as is the case with WP Job Manager gallery images. To make multiple methods work,
  19. // in this case by passing image objects and image urls, we need to specify a key with the method used, and then format
  20. // the items to one final format which will then be used in the HTML template.
  21. // The key is 'gallery_item_interface' and currently it accepts the values 'WP_IMAGE_OBJECT', and 'CASE27_JOB_MANAGER_ARRAY'.
  22. $gallery_items = [];
  23.  
  24. if ($data['gallery_item_interface'] == 'WP_IMAGE_OBJECT') {
  25. foreach ($data['gallery_items'] as $item) {
  26. $image_quality = ($data['gallery_type'] == 'carousel-with-preview') ? 'large' : 'medium';
  27.  
  28. $image = wp_get_attachment_image_src($item['item']['id'], $image_quality);
  29. $large_image = wp_get_attachment_image_src($item['item']['id'], 'full');
  30. $gallery_items[] = [
  31. 'image' => [
  32. 'url' => $image ? $image[0] : false,
  33. ],
  34. 'large_image' => [
  35. 'url' => $large_image ? $large_image[0] : false,
  36. ],
  37. ];
  38. }
  39. }
  40.  
  41. if ($data['gallery_item_interface'] == 'CASE27_JOB_MANAGER_ARRAY') {
  42. foreach ($data['gallery_items'] as $item) {
  43. $image_quality = ($data['gallery_type'] == 'carousel-with-preview') ? 'large' : 'medium';
  44.  
  45. $image = job_manager_get_resized_image($item, $image_quality);
  46.  
  47. if (!$image) continue;
  48.  
  49. $large_image = job_manager_get_resized_image($item, 'full');
  50.  
  51. $gallery_items[] = [
  52. 'image' => [
  53. 'url' => $image,
  54. ],
  55. 'large_image' => [
  56. 'url' => $large_image,
  57. ],
  58. ];
  59. }
  60. }
  61.  
  62. if ( count( $gallery_items ) === 2 ) {
  63. $data['items_per_row'] = 2;
  64. }
  65. ?>
  66.  
  67. <div class="<?php echo esc_attr( $data['wrapper_class'] ) ?>" <?php echo $data['wrapper_id'] ? sprintf( 'id="%s"', $data['wrapper_id'] ) : '' ?>>
  68. <?php if (!$data['gallery_type'] || $data['gallery_type'] == 'carousel'): ?>
  69. <?php
  70. if ( count( $gallery_items ) === 1 ) {
  71. $data['items_per_row'] = 1;
  72. $data['items_per_row_mobile'] = 1;
  73. $gallery_items[0]['image']['url'] = $gallery_items[0]['large_image']['url'];
  74. }
  75. ?>
  76.  
  77. <div class="element gallery-carousel-block <?php echo 'carousel-items-' . count( $gallery_items ) ?>">
  78. <div class="pf-head">
  79. <div class="title-style-1 title-style-<?php echo esc_attr( $data['icon_style'] ) ?>">
  80. <?php if ($data['icon_style'] != 3): ?>
  81. <?php echo c27()->get_icon_markup($data['icon']) ?>
  82. <?php endif ?>
  83. <h5><?php echo esc_html( $data['title'] ) ?></h5>
  84. </div>
  85.  
  86. <?php if ( count( $gallery_items ) > 2 ): ?>
  87. <div class="gallery-nav">
  88. <ul>
  89. <li>
  90. <a href="#" class="gallery-prev-btn">
  91. <i class="material-icons">keyboard_arrow_left</i>
  92. </a>
  93. </li>
  94. <li>
  95. <a href="#" class="gallery-next-btn">
  96. <i class="material-icons">keyboard_arrow_right</i>
  97. </a>
  98. </li>
  99. </ul>
  100. </div>
  101. <?php endif ?>
  102.  
  103. </div>
  104.  
  105. <div class="pf-body">
  106. <div class="gallery-carousel owl-carousel" data-items="<?php echo esc_attr( $data['items_per_row'] ) ?>" data-items-mobile="<?php echo esc_attr( $data['items_per_row_mobile'] ) ?>">
  107.  
  108. <?php foreach ((array) $gallery_items as $item): ?>
  109. <?php if ($item['image']['url']): ?>
  110. <a class="item"
  111. href="<?php echo esc_url( $item['large_image'] ? $item['large_image']['url'] : $item['image']['url'] ) ?>"
  112. style="background-image: url('<?php echo esc_url( $item['image']['url'] ) ?>')">
  113. </a>
  114. <?php endif ?>
  115. <?php endforeach ?>
  116.  
  117. </div>
  118. </div>
  119. </div>
  120. <?php endif ?>
  121.  
  122. <?php if ($data['gallery_type'] == 'carousel-with-preview'): ?>
  123. <div class="element slider-padding gallery-block">
  124. <div class="pf-body">
  125. <div class="gallerySlider car-slider">
  126. <div class="owl-carousel galleryPreview">
  127. <?php foreach ((array) $gallery_items as $item): ?>
  128. <?php if ($item['image']['url']):
  129.  
  130. $image_title = get_the_title();
  131.  
  132. if ( isset( $item['large_image']['url'] ) ) {
  133. $image_id = get_attachment_id( $item['large_image']['url'] );
  134. $image_title = get_the_title( $image_id );
  135. }
  136. ?>
  137. <a class="item" href="<?php echo esc_url( $item['large_image'] ? $item['large_image']['url'] : $item['image']['url'] ) ?>">
  138. <img src="<?php echo esc_url( $item['image']['url'] ) ?>" alt="<?php echo esc_attr( $image_title ); ?>">
  139. </a>
  140. <?php endif ?>
  141. <?php endforeach ?>
  142. </div>
  143. <div class="gallery-thumb owl-carousel" data-items="<?php echo esc_attr( $data['items_per_row'] ) ?>" data-items-mobile="<?php echo esc_attr( $data['items_per_row_mobile'] ) ?>">
  144. <?php $i = 0; ?>
  145. <?php foreach ((array) $gallery_items as $item): ?>
  146. <?php if ($item['image']['url']): ?>
  147. <a class="item slide-thumb"
  148. data-slide-no="<?php echo esc_attr( $i ) ?>"
  149. href="<?php echo esc_url( $item['image']['url'] ) ?>"
  150. style="background-image: url('<?php echo esc_url( $item['image']['url'] ) ?>')">
  151. </a>
  152. <?php $i++; endif; ?>
  153. <?php endforeach ?>
  154. </div>
  155. <div class="gallery-nav">
  156. <ul>
  157. <li>
  158. <a href="#" class="gallery-prev-btn">
  159. <i class="material-icons">keyboard_arrow_left</i>
  160. </a>
  161. </li>
  162. <li>
  163. <a href="#" class="gallery-next-btn">
  164. <i class="material-icons">keyboard_arrow_right</i>
  165. </a>
  166. </li>
  167. </ul>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. <?php endif ?>
  173. </div>
  174.  
  175. <?php if ($data['is_edit_mode']): ?>
  176. <script type="text/javascript">case27_ready_script(jQuery);</script>
  177. <?php endif ?>
  178.  
  179. <?php
  180.  
  181. function get_attachment_id( $url ) {
  182. $attachment_id = 0;
  183. $dir = wp_upload_dir();
  184. if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) {
  185. $file = basename( $url );
  186. $query_args = array(
  187. 'post_type' => 'attachment',
  188. 'post_status' => 'inherit',
  189. 'fields' => 'ids',
  190. 'meta_query' => array(
  191. array(
  192. 'value' => $file,
  193. 'compare' => 'LIKE',
  194. 'key' => '_wp_attachment_metadata',
  195. ),
  196. )
  197. );
  198. $query = new WP_Query( $query_args );
  199.  
  200. if ( $query->have_posts() ) {
  201. foreach ( $query->posts as $post_id ) {
  202. $meta = wp_get_attachment_metadata( $post_id );
  203. $original_file = basename( $meta['file'] );
  204. $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
  205. if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
  206. $attachment_id = $post_id;
  207. break;
  208. }
  209. }
  210.  
  211. wp_reset_query();
  212. }
  213. }
  214. return $attachment_id;
  215. }
Add Comment
Please, Sign In to add comment