PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions.php

https://gitlab.com/aristath/shoestrap-slider
PHP | 285 lines | 204 code | 62 blank | 19 comment | 67 complexity | f877c5addc18a41d068d0c64b4255c5e MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. /*
  4. * Replace gallery_shortcode()
  5. */
  6. function shoestrap_slider_gallery( $attr ) {
  7. global $ss_layout, $ss_settings, $ss_framework;
  8. $post = get_post();
  9. $shoestrap_slider_height = $ss_settings['shoestrap_slider_height'];
  10. if ( ! isset( $shoestrap_slider_height ) || empty( $shoestrap_slider_height ) ) {
  11. $shoestrap_slider_height = 450;
  12. }
  13. static $instance = 0;
  14. $instance++;
  15. if ( ! empty( $attr['ids'] ) ) {
  16. $attr['orderby'] = ( empty( $attr['orderby'] ) ) ? 'post__in' : '';
  17. $attr['include'] = $attr['ids'];
  18. }
  19. $output = apply_filters( 'post_gallery', '', $attr );
  20. if ( $output != '' ) {
  21. return $output;
  22. }
  23. if ( isset( $attr['orderby'] ) ) {
  24. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  25. if ( ! $attr['orderby'] ) {
  26. unset( $attr['orderby'] );
  27. }
  28. }
  29. extract( shortcode_atts( array(
  30. 'order' => 'ASC',
  31. 'orderby' => 'menu_order ID',
  32. 'id' => $post->ID,
  33. 'itemtag' => '',
  34. 'icontag' => '',
  35. 'captiontag' => '',
  36. 'columns' => 4,
  37. 'size' => 'thumbnail',
  38. 'include' => '',
  39. 'exclude' => '',
  40. 'link' => 'file',
  41. 'type' => 'default',
  42. 'height' => $shoestrap_slider_height,
  43. ), $attr ) );
  44. // If type is set to default, return the default Shoetrap gallery
  45. if ( $type == 'default' ) {
  46. return shoestrap_gallery( $attr );
  47. } else {
  48. // if type is not default, continue processing
  49. $id = intval( $id );
  50. $columns = ( 12 % $columns == 0 ) ? $columns: 4;
  51. if ( $order === 'RAND' ) {
  52. $orderby = 'none';
  53. }
  54. if ( ! empty( $include ) ) {
  55. $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  56. $attachments = array();
  57. foreach ( $_attachments as $key => $val ) {
  58. $attachments[$val->ID] = $_attachments[$key];
  59. }
  60. } elseif ( ! empty( $exclude ) ) {
  61. $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  62. } else {
  63. $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  64. }
  65. if ( empty( $attachments ) ) {
  66. return '';
  67. }
  68. if ( is_feed() ) {
  69. $output = "\n";
  70. foreach ( $attachments as $att_id => $attachment ) {
  71. $output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
  72. }
  73. return $output;
  74. }
  75. $unique = ( get_query_var( 'page' ) ) ? $instance . '-p' . get_query_var( 'page' ) : $instance;
  76. $output = shoestrap_slider_helper( 'wrapper_start', 'gallery-' . $post->ID . '-' . $unique, '', $type );
  77. $i = 0; foreach ( $attachments as $id => $attachment ) { $i++; }
  78. $output .= shoestrap_slider_helper( 'before_inner_start', 'gallery-' . $post->ID . '-' . $unique, $i, $type );
  79. $output .= shoestrap_slider_helper( 'inner_start', 'slides', '', $type );
  80. $width = $ss_layout->content_width_px();
  81. $i = 0;
  82. foreach ( $attachments as $id => $attachment ) {
  83. $imageurl = wp_get_attachment_url( $id );
  84. $image_args = array( "url" => $imageurl, "width" => $width, "height" => $height );
  85. $image = Shoestrap_Image::image_resize( $image_args );
  86. $image_url = $image['url'];
  87. $output .= shoestrap_slider_helper( 'slide_element_start', $imageurl, $i, $type ) . '<img src="' . $image_url . '" />';
  88. if ( trim( $attachment->post_excerpt ) ) {
  89. $output .= shoestrap_slider_helper( 'caption_start', '', '', $type );
  90. $output .= wptexturize( $attachment->post_excerpt );
  91. $output .= shoestrap_slider_helper( 'caption_end', '', '', $type );
  92. }
  93. $output .= shoestrap_slider_helper( 'slide_element_end', '', '', $type );
  94. $i++;
  95. }
  96. $output .= shoestrap_slider_helper( 'inner_end', '', '', $type );
  97. $output .= shoestrap_slider_helper( 'before_wrapper_end', 'gallery-' . $post->ID . '-' . $unique, '', $type );
  98. $output .= shoestrap_slider_helper( 'wrapper_end', '', '', $type );
  99. if ( $type == 'flexslider_thumbs' ) {
  100. $output .= '<div id="carousel" class="flexslider"><ul class="slides">';
  101. $i = 0;
  102. foreach ( $attachments as $id => $attachment ) {
  103. $imageurl = wp_get_attachment_url( $id );
  104. $image_args = array( "url" => $imageurl, "width" => $width, "height" => $height );
  105. $image = Shoestrap_Image::image_resize( $image_args );
  106. $image_url = $image['url'];
  107. $output .= shoestrap_slider_helper( 'slide_element_start', $imageurl, $i, $type ) . '<img src="' . $image_url . '" />';
  108. $output .= shoestrap_slider_helper( 'slide_element_end', '', '', $type );
  109. $i++;
  110. }
  111. $output .= '</ul></div>';
  112. }
  113. $output .= shoestrap_slider_gallery_script( '.gallery-' . $post->ID . '-' . $unique, $type );
  114. return $output;
  115. }
  116. }
  117. /*
  118. * Replace default gallery with our custom shortcode
  119. */
  120. function shoestrap_slider_gallery_setup_after_theme() {
  121. remove_shortcode( 'gallery' );
  122. add_shortcode( 'gallery', 'shoestrap_slider_gallery' );
  123. }
  124. add_action( 'after_setup_theme', 'shoestrap_slider_gallery_setup_after_theme' );
  125. /*
  126. * The script required for the sliders.
  127. */
  128. function shoestrap_slider_gallery_script( $element = '', $type = 'default' ) {
  129. global $ss_layout, $ss_framework;
  130. if ( SS_FRAMEWORK != 'bootstrap' && $type == 'bootstrap' ) {
  131. $type = 'flexslider';
  132. }
  133. if ( $type != 'default' ) {
  134. // The Bootstrap Carousel script
  135. if ( $type == 'bootstrap' ) {
  136. $script = '$j("' . $element . '").carousel();';
  137. } elseif ( $type == 'flexslider' || $type == 'flexslider_thumbs' ) { // If flexslider is selected, process the below
  138. // The basic script
  139. $script = '$j("' . $element . '").flexslider({ animation: "slide" });';
  140. // The script that adds thumbs if selected.
  141. if ( $type == 'flexslider_thumbs' ) {
  142. $script = '
  143. $j("#carousel").flexslider({
  144. animation: "slide",
  145. controlNav: false,
  146. animationLoop: false,
  147. slideshow: false,
  148. itemWidth: ' . ( $ss_layout->content_width_px() / 4 ) . ',
  149. itemMargin: 0,
  150. asNavFor: "' . $element . '"
  151. });
  152. $j("' . $element . '").flexslider({
  153. animation: "slide",
  154. controlNav: false,
  155. animationLoop: false,
  156. slideshow: false,
  157. sync: "#carousel"
  158. });';
  159. }
  160. }
  161. return '<script>var $j = jQuery.noConflict(); $j(window).load(function() {' . $script . '});</script>';
  162. }
  163. }
  164. /*
  165. * Slider Helper function
  166. */
  167. function shoestrap_slider_helper( $element, $class, $count = 0, $type = 'default' ) {
  168. if ( $type != 'default' ) {
  169. if ( SS_FRAMEWORK != 'bootstrap' && $type == 'bootstrap' ) {
  170. $type = 'flexslider';
  171. }
  172. $content = '';
  173. // Elements for flexslider
  174. if ( $type == 'flexslider' || $type == 'flexslider_thumbs' ) {
  175. if ( $element == 'wrapper_start' ) {
  176. $content = '<div class="flexslider ' . $class . '">';
  177. } elseif ( $element == 'wrapper_end' ) {
  178. $content = '</div>';
  179. } elseif ( $element == 'inner_start' ) {
  180. $content = '<ul class="slides">';
  181. } elseif ( $element == 'inner_end' ) {
  182. $content = '</ul>';
  183. } elseif ( $element == 'slide_element_start' ) {
  184. $content = '<li data-thumb="' . $class . '">';
  185. } elseif ( $element == 'slide_element_end' ) {
  186. $content = '</li>';
  187. } elseif ( $element == 'caption_start' ) {
  188. $content = '<p class="flex-caption caption hidden">';
  189. } elseif ( $element == 'caption_end' ) {
  190. $content = '</p>';
  191. }
  192. // Elements for Bootstrap Carousel
  193. } elseif ( $type == 'bootstrap' ) {
  194. if ( $element == 'wrapper_start' ) {
  195. $content = '<div id="' . $class . '" class="carousel slide ' . $class . '">';
  196. } elseif ( $element == 'wrapper_end' ) {
  197. $content = '</div>';
  198. } elseif ( $element == 'inner_start' ) {
  199. $content = '<div class="carousel-inner ' . $class . '">';
  200. } elseif ( $element == 'inner_end' ) {
  201. $content = '</div>';
  202. } elseif ( $element == 'slide_element_start' ) {
  203. $content = ( $count == 0 ) ? '<div class="item active">' : '<div class="item ' . $count . '">';
  204. } elseif ( $element == 'slide_element_end' ) {
  205. $content = '</div>';
  206. } elseif ( $element == 'before_wrapper_end' ) {
  207. $content = '<a class="left carousel-control" href="#' . $class . '" data-slide="prev"><span class="el-icon-prev"></span></a><a class="right carousel-control" href="#' . $class . '" data-slide="next"><span class="el-icon-next"></span></a>';
  208. } elseif ( $element == 'caption_start' ) {
  209. $content = '<div class="carousel-caption">';
  210. } elseif ( $element == 'caption_end' ) {
  211. $content = '</div>';
  212. } elseif ( $element == 'before_inner_start' ) {
  213. $content = '<ol class="carousel-indicators">';
  214. for ( $i=0; $i<$count ; $i++ ) {
  215. $content .= ( $i == 0 ) ? '<li data-target="#' . $class . '" data-slide-to="' . $i . '" class="active"></li>' : '<li data-target="#' . $class . '" data-slide-to="' . $i . '"></li>';
  216. }
  217. $content .= '</ol>';
  218. }
  219. }
  220. return $content;
  221. }
  222. }