Guest User

post-gallery-slider/post-gallery-slider.php (active)

a guest
Apr 16th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Post gallery slider
  4. Plugin URI: http://github.com/kasparsj/post-gallery-slider
  5. Description: Post gallery slider, with thumbnails and with nice animation, and auto height.
  6. Author: Kaspars Jaudzems
  7. Author URI: http://kasparsj.wordpress.com
  8. Version: 1.1.1
  9. */
  10.  
  11. // Exit if accessed directly
  12. if( !defined( 'ABSPATH' ) ) exit;
  13.  
  14. define('POST_GALLERY_SLIDER_DIR', dirname(__FILE__));
  15.  
  16. class PostGallerySlider {
  17.  
  18. static protected $instance = 0;
  19.  
  20. protected $options;
  21. protected $sizes = array(
  22. "large" => "large",
  23. "medium" => "medium",
  24. "gallery-image" => "custom",
  25. "full" => "full (not recommended)",
  26. "thumbnail" => "thumbnail (not recommended)");
  27. protected $thumb_sizes = array(
  28. "thumbnail" => "thumbnail",
  29. "medium" => "medium",
  30. "gallery-thumb" => "custom",
  31. "large" => "large (not recommended)",
  32. "full" => "full (not recommended)");
  33.  
  34. public function __construct() {
  35. add_action('init', array($this, 'init'));
  36. add_action('admin_init', array($this, 'admin_init'));
  37. add_action('admin_menu', array($this, 'admin_menu'));
  38. add_filter('post_gallery', array($this, 'post_gallery'), 1001, 2);
  39. }
  40.  
  41. public function init() {
  42. $this->options = get_option('post_gallery_slider');
  43.  
  44. if ((@$this->options['restore'] == 'on') || (!is_array($this->options))) {
  45. $this->options = array(
  46. "size" => "large",
  47. "width" => 650,
  48. "height" => 0,
  49. "show_thumbs" => "before",
  50. "thumb_size" => "thumbnail",
  51. "thumb_width" => 0,
  52. "thumb_height" => 60
  53. );
  54. update_option('post_gallery_slider', $this->options);
  55. }
  56. else {
  57. if ($this->options["size"] == "gallery-image")
  58. add_image_size("gallery-image", $this->options['width'], $this->options['height']);
  59.  
  60. if ($this->options["thumb_size"] == "gallery-thumb")
  61. add_image_size("gallery-thumb", $this->options['thumb_width'], $this->options['thumb_height']);
  62. }
  63. }
  64.  
  65. public function admin_init() {
  66. register_setting('post_gallery_slider', 'post_gallery_slider', array($this, 'validate_options'));
  67. }
  68.  
  69. public function admin_menu() {
  70. add_options_page('Post Gallery Slider', 'Post Gallery Slider', 'manage_options', 'post_gallery_slider', array($this, 'options_page'));
  71. }
  72.  
  73. public function post_gallery($null, $attr = array()) {
  74. global $post;
  75.  
  76. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  77. if ( isset( $attr['orderby'] ) ) {
  78. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  79. if ( !$attr['orderby'] )
  80. unset( $attr['orderby'] );
  81. }
  82.  
  83. extract(shortcode_atts(array(
  84. 'order' => 'ASC',
  85. 'orderby' => 'menu_order ID',
  86. 'id' => $post->ID,
  87. 'columns' => 3,
  88. 'size' => $this->options["size"],
  89. 'thumb_size' => $this->options["thumb_size"],
  90. 'include' => '',
  91. 'exclude' => ''
  92. ), $attr));
  93.  
  94. $id = intval($id);
  95. if ( 'RAND' == $order )
  96. $orderby = 'none';
  97.  
  98. if ( !empty($include) ) {
  99. $include = preg_replace( '/[^0-9,]+/', '', $include );
  100. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  101.  
  102. $attachments = array();
  103. foreach ( $_attachments as $key => $val ) {
  104. $attachments[$val->ID] = $_attachments[$key];
  105. }
  106. } elseif ( !empty($exclude) ) {
  107. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  108. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  109. } else {
  110. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  111. }
  112.  
  113. if ( empty($attachments) )
  114. return '';
  115.  
  116. if ( is_feed() ) {
  117. $output = "\n";
  118. foreach ( $attachments as $att_id => $attachment )
  119. $output .= wp_get_attachment_link($att_id, $this->options["size"], true) . "\n";
  120. return $output;
  121. }
  122.  
  123. wp_register_script( 'jquery.sudoSlider.js', plugins_url('post-gallery-slider') . '/js/jquery.sudoSlider.js', array('jquery'), '3.0.1' );
  124. wp_enqueue_script( 'jquery.sudoSlider.js' );
  125. add_action( 'wp_footer', array($this, 'footer'), 10000 );
  126.  
  127. // NOT USED ATM
  128. //$itemtag = tag_escape($itemtag);
  129. //$captiontag = tag_escape($captiontag);
  130. //$columns = intval($columns);
  131. //$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  132. //$float = is_rtl() ? 'right' : 'left';
  133.  
  134. $first_image = wp_get_attachment_image_src(key($attachments), $size, true);
  135. $instance = ++self::$instance;
  136. return $this->include_template("gallery.php", array(
  137. 'id' => $id,
  138. 'size' => $size,
  139. 'thumb_size' => $thumb_size,
  140. 'attachments' => $attachments,
  141. 'width' => $first_image[1],
  142. 'height' => $first_image[2],
  143. 'instance' => $instance,
  144. 'options' => $this->options,
  145. 'css' => str_replace('#gallery', '#gallery-'.$instance, file_get_contents(POST_GALLERY_SLIDER_DIR."/css/gallery.css"))
  146. ), true);
  147. }
  148.  
  149. public function footer() {
  150. if( wp_script_is( 'jquery', 'done' ) ) {
  151. $this->include_template("footer.php");
  152. }
  153. }
  154.  
  155. public function validate_options($input) {
  156. return $input;
  157. }
  158.  
  159. public function options_page() {
  160. $this->include_template("options-page.php", array(
  161. 'options' => $this->options,
  162. 'sizes' => $this->sizes,
  163. 'thumb_sizes' => $this->thumb_sizes
  164. ));
  165. }
  166.  
  167. protected function include_template($template, $vars = array(), $return = false) {
  168. if ($return) ob_start();
  169. extract($vars);
  170. include("templates/".$template);
  171. if ($return) return ob_get_clean();
  172. }
  173. }
  174.  
  175. new PostGallerySlider;
  176. ?>
Advertisement
Add Comment
Please, Sign In to add comment