Advertisement
Guest User

WordPress 'Slideshow Gallery' plugin - slideshow-gallery.php

a guest
May 18th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.48 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Slideshow Gallery
  5. Plugin URI: http://wpgallery.tribulant.net
  6. Author: Tribulant Software
  7. Author URI: http://tribulant.com
  8. Description: Feature content in a JavaScript powered slideshow gallery showcase on your WordPress website. The slideshow is flexible and all aspects can easily be configured. Embedding or hardcoding the slideshow gallery is a breeze. To embed into a post/page, simply insert <code>[slideshow]</code> into its content with an optional <code>post_id</code> parameter. To hardcode into any PHP file of your WordPress theme, simply use <code>&lt;?php if (function_exists('slideshow')) { slideshow($output = true, $post_id = false, $gallery_id = false, $params = array()); } ?&gt;</code>.
  9. Version: 1.2.3.2
  10.  
  11. This version copied to pastebin has been amended to support an extra shortcode parameter of 'imgids' - this enables the user to specify an order of the images in the slideshow. See: http://tribulant.com/app/webroot/forums/discussion/314/image-ordering-when-retrieving-from-a-post
  12. */
  13.  
  14. define('DS', DIRECTORY_SEPARATOR);
  15. require_once(dirname(__FILE__) . DS . 'slideshow-gallery-plugin.php');
  16.  
  17. if (!class_exists('Gallery')) {
  18.     class Gallery extends GalleryPlugin {
  19.        
  20.         function Gallery() {
  21.             $url = explode("&", $_SERVER['REQUEST_URI']);
  22.             $this -> url = $url[0];
  23.             $this -> referer = (empty($_SERVER['HTTP_REFERER'])) ? $this -> url : $_SERVER['HTTP_REFERER'];
  24.             $this -> register_plugin('slideshow-gallery', __FILE__);
  25.             $this -> initialize_options();
  26.            
  27.             //WordPress action hooks
  28.             $this -> add_action('wp_head');
  29.             $this -> add_action('admin_menu');
  30.             $this -> add_action('admin_head');
  31.             $this -> add_action('admin_notices');
  32.             $this -> add_action('wp_print_styles', 'print_styles');
  33.             $this -> add_action('admin_print_styles', 'print_styles');
  34.             $this -> add_action('wp_print_scripts', 'print_scripts');
  35.             $this -> add_action('admin_print_scripts', 'print_scripts');
  36.             $this -> add_action('init', 'init_textdomain', 10, 1);
  37.            
  38.             //WordPress filter hooks
  39.             $this -> add_filter('mce_buttons');
  40.             $this -> add_filter('mce_external_plugins');
  41.            
  42.             add_shortcode('slideshow', array($this, 'embed'));
  43.         }
  44.        
  45.         function init_textdomain() {       
  46.             if (function_exists('load_plugin_textdomain')) {
  47.                 load_plugin_textdomain($this -> plugin_name, $this -> plugin_name . DS . 'languages', dirname(plugin_basename(__FILE__)) . DS . 'languages');
  48.             }  
  49.         }
  50.        
  51.         function wp_head() {
  52.            
  53.         }
  54.        
  55.         function admin_menu() {
  56.             $this -> check_roles();
  57.             add_menu_page(__('Slideshow', $this -> plugin_name), __('Slideshow', $this -> plugin_name), 'gallery_slides', $this -> sections -> slides, array($this, 'admin_slides'), $this -> url() . '/images/icon.png');
  58.             $this -> menus['slideshow-slides'] = add_submenu_page($this -> sections -> slides, __('Manage Slides', $this -> plugin_name), __('Manage Slides', $this -> plugin_name), 'gallery_slides', $this -> sections -> slides, array($this, 'admin_slides'));
  59.             $this -> menus['slideshow-galleries'] = add_submenu_page($this -> sections -> slides, __('Manage Galleries', $this -> plugin_name), __('Manage Galleries', $this -> plugin_name), 'gallery_galleries', $this -> sections -> galleries, array($this, 'admin_galleries'));
  60.             $this -> menus['slideshow-settings'] = add_submenu_page($this -> sections -> slides, __('Configuration', $this -> plugin_name), __('Configuration', $this -> plugin_name), 'gallery_settings', $this -> sections -> settings, array($this, 'admin_settings'));
  61.            
  62.             add_action('admin_head-' . $this -> menus['slideshow-settings'], array($this, 'admin_head_gallery_settings'));
  63.         }
  64.        
  65.         function admin_head() {
  66.             $this -> render('head', false, true, 'admin');
  67.         }
  68.        
  69.         function admin_head_gallery_settings() {       
  70.             add_meta_box('submitdiv', __('Save Settings', $this -> plugin_name), array($this -> Metabox, "settings_submit"), $this -> menus['slideshow-settings'], 'side', 'core');
  71.             add_meta_box('aboutdiv', __('About This Plugin', $this -> plugin_name), array($this -> Metabox, "settings_about"), $this -> menus['slideshow-settings'], 'side', 'core');
  72.             add_meta_box('wprelateddiv', __('WordPress Related', $this -> plugin_name), array($this -> Metabox, "settings_wprelated"), $this -> menus['slideshow-settings'], 'normal', 'core');
  73.             add_meta_box('generaldiv', __('General Settings', $this -> plugin_name), array($this -> Metabox, "settings_general"), $this -> menus['slideshow-settings'], 'normal', 'core');
  74.             add_meta_box('linksimagesdiv', __('Links &amp; Images Overlay', $this -> plugin_name), array($this -> Metabox, "settings_linksimages"), $this -> menus['slideshow-settings'], 'normal', 'core');
  75.             add_meta_box('stylesdiv', __('Appearance &amp; Styles', $this -> plugin_name), array($this -> Metabox, "settings_styles"), $this -> menus['slideshow-settings'], 'normal', 'core');
  76.            
  77.             do_action('do_meta_boxes', $this -> menus['slideshow-settings'], 'normal');
  78.             do_action('do_meta_boxes', $this -> menus['slideshow-settings'], 'side');
  79.         }
  80.        
  81.         function admin_notices() {
  82.             $this -> check_uploaddir();
  83.        
  84.             if (!empty($_GET[$this -> pre . 'message'])) {     
  85.                 $msg_type = (!empty($_GET[$this -> pre . 'updated'])) ? 'msg' : 'err';
  86.                 call_user_method('render_' . $msg_type, $this, $_GET[$this -> pre . 'message']);
  87.             }
  88.         }
  89.        
  90.         function mce_buttons($buttons) {
  91.             array_push($buttons, "separator", "gallery");
  92.             return $buttons;
  93.         }
  94.        
  95.         function mce_external_plugins($plugins) {
  96.             $plugins['gallery'] = $this -> url() . '/js/tinymce/editor_plugin.js';
  97.             return $plugins;
  98.         }
  99.        
  100.         function slideshow($output = true, $post_id = null, $exclude = null) {     
  101.             $params['post_id'] = $post_id;
  102.             $params['exclude'] = $exclude;
  103.        
  104.             $content = $this -> embed($params, false);
  105.            
  106.             if ($output == true) {
  107.                 echo $content;
  108.             } else {
  109.                 return $content;
  110.             }
  111.         }
  112.        
  113.         function embed($atts = array(), $content = null) {
  114.             //global variables
  115.             global $wpdb;
  116.             $styles = $this -> get_option('styles');
  117.        
  118.             // default shortcode parameters
  119.             $defaults = array(
  120.                 'source'                =>  "slides",
  121.                 'gallery_id'            =>  false,
  122.                 'orderby'               =>  array('order', "ASC"),
  123.                 'resizeimages'          =>  (($styles['resizeimages'] == "Y") ? "true" : "false"),
  124.                 'imagesoverlay'         =>  (($this -> get_option('imagesthickbox') == "Y") ? "true" : "false"),
  125.                 'width'                 =>  ($styles['width']),
  126.                 'height'                =>  ($styles['height']),
  127.                 'auto'                  =>  (($this -> get_option('autoslide') == "Y") ? "true" : "false"),
  128.                 'autospeed'             =>  ($this -> get_option('autospeed')),
  129.                 'fadespeed'             =>  ($this -> get_option('fadespeed')),
  130.                 'shownav'               =>  (($this -> get_option('shownav') == "Y") ? "true" : "false"),
  131.                 'navopacity'            =>  ($this -> get_option('navopacity')),
  132.                 'navhoveropacity'       =>  ($this -> get_option('navhover')),
  133.                 'showinfo'              =>  (($this -> get_option('information') == "Y") ? "true" : "false"),
  134.                 'infospeed'             =>  ($this -> get_option('infospeed')),
  135.                 'showthumbs'            =>  (($this -> get_option('thumbnails') == "Y") ? "true" : "false"),
  136.                 'thumbsposition'        =>  ($this -> get_option('thumbposition')),
  137.                 'thumbsborder'          =>  ($this -> get_option('thumbactive')),
  138.                 'thumbsspeed'           =>  ($this -> get_option('thumbscrollspeed')),
  139.                 'thumbsspacing'         =>  ($this -> get_option('thumbspacing')),
  140.                 'post_id'               =>  null,
  141.                 'exclude'               =>  null,
  142.                 'custom'                =>  null,
  143.                 'imgids'                =>  null,
  144.             );
  145.                    
  146.             $s = shortcode_atts($defaults, $atts);
  147.             extract($s);
  148.            
  149.             // if this is an RSS/Atom feed, it should not continue...
  150.             if (is_feed()) { return false; }
  151.            
  152.             if (!empty($gallery_id)) {
  153.                 if (!is_array($orderby) || $orderby == "random") {
  154.                     $orderbystring = "ORDER BY RAND()";
  155.                 } else {
  156.                     list($orderf, $orderd) = $orderby;
  157.                    
  158.                     if ($orderf == "order") {
  159.                         $orderbystring = "ORDER BY " . $this -> GallerySlides -> table . ".order " . $orderd . "";
  160.                     } else {
  161.                         $orderbystring = "ORDER BY " . $this -> Slide -> table . "." . $orderf . " " . $orderd . "";
  162.                     }
  163.                 }
  164.            
  165.                 $slidesquery = "SELECT * FROM " . $this -> Slide -> table . " LEFT JOIN " . $this -> GallerySlides -> table .
  166.                 " ON " . $this -> Slide -> table . ".id = " . $this -> GallerySlides -> table . ".slide_id WHERE " .
  167.                 $this -> GallerySlides -> table . ".gallery_id = '" . $gallery_id . "' " . $orderbystring;
  168.                
  169.                 if ($slides = $wpdb -> get_results($slidesquery)) {
  170.                     foreach ($slides as $skey => $slide) {
  171.                         $slides[$skey] -> image_path = 'wp-content/uploads/slideshow-gallery/' . $slide -> image;
  172.                     }
  173.                
  174.                     if ($orderby == "random") { shuffle($slides); }
  175.                     $content = $this -> render('gallery', array('slides' => $slides, 'options' => $s, 'frompost' => false), false, 'default'); 
  176.                 }
  177.             } elseif (!empty($custom) || empty($post_id)) {
  178.                 $slides = $this -> Slide -> find_all(null, null, $orderby);
  179.                
  180.                 if (!empty($exclude)) {
  181.                     $exclude = array_map('trim', explode(',', $exclude));
  182.                    
  183.                     foreach ($slides as $slide_key => $slide) {
  184.                         if (in_array($slide -> id, $exclude)) {
  185.                             unset($slides[$slide_key]);
  186.                         }
  187.                     }
  188.                 }
  189.                
  190.                 if ($orderby == "random") { shuffle($slides); }
  191.                 $content = $this -> render('gallery', array('slides' => $slides, 'options' => $s, 'frompost' => false), false, 'default');
  192.             } else {
  193.                 global $post;
  194.                 $pid = (empty($post_id)) ? $post -> ID : $post_id;
  195.            
  196.                 if (!empty($pid) && $post = get_post($pid)) {
  197.                    
  198.                     // if img ids have been passed in the shortcode, get the posts in order to preserve the order
  199.                     if ($imgids) {
  200.                         $attachments = get_posts("include=" . $imgids . "&post_type=attachment&post_mime_type=image&orderby=post__in");
  201.                     } else {
  202.                         // else get the attachments of the post parent
  203.                         $attachments = get_children("post_parent=" . $post -> ID . "&post_type=attachment&post_mime_type=image&orderby=" . ((!empty($orderby) && $orderby == "random") ? "rand" : "menu_order ASC, ID ASC"));
  204.                     }
  205.                    
  206.                     // if we have attachments, check for excludes and randomness
  207.                     if ($attachments) {
  208.                    
  209.                         if (!empty($exclude)) {
  210.                             $exclude = array_map('trim', explode(',', $exclude));
  211.                            
  212.                             $a = 0;
  213.                             foreach ($attachments as $id => $attachment) {
  214.                                
  215.                                 $a++;
  216.                                 if (in_array($a, $exclude)) {
  217.                                     unset($attachments[$id]);
  218.                                 }
  219.                             }
  220.                         }
  221.                    
  222.                         if ($orderby == "random") { shuffle($attachments); }
  223.                         $content = $this -> render('gallery', array('slides' => $attachments, 'options' => $s, 'frompost' => true), false, 'default');
  224.                     }
  225.                 }
  226.             }
  227.            
  228.             return $content;
  229.         }
  230.        
  231.         function admin_slides() {
  232.             global $wpdb;
  233.            
  234.             switch ($_GET['method']) {
  235.                 case 'delete'           :
  236.                     if (!empty($_GET['id'])) {
  237.                         if ($this -> Slide -> delete($_GET['id'])) {
  238.                             $msg_type = 'message';
  239.                             $message = __('Slide has been removed', $this -> plugin_name);
  240.                         } else {
  241.                             $msg_type = 'error';
  242.                             $message = __('Slide cannot be removed', $this -> plugin_name);
  243.                         }
  244.                     } else {
  245.                         $msg_type = 'error';
  246.                         $message = __('No slide was specified', $this -> plugin_name);
  247.                     }
  248.                    
  249.                     $this -> redirect($this -> referer, $msg_type, $message);
  250.                     break;
  251.                 case 'save'             :
  252.                     if (!empty($_POST)) {
  253.                         if ($this -> Slide -> save($_POST, true)) {
  254.                             $message = __('Slide has been saved', $this -> plugin_name);
  255.                             $this -> redirect($this -> url, "message", $message);
  256.                         } else {
  257.                             $this -> render('slides' . DS . 'save', false, true, 'admin');
  258.                         }
  259.                     } else {
  260.                         $this -> Db -> model = $this -> Slide -> model;
  261.                         $this -> Slide -> find(array('id' => $_GET['id']));
  262.                         $this -> render('slides' . DS . 'save', false, true, 'admin');
  263.                     }
  264.                     break;
  265.                 case 'mass'             :
  266.                     if (!empty($_POST['action'])) {
  267.                         if (!empty($_POST['Slide']['checklist'])) {                    
  268.                             switch ($_POST['action']) {
  269.                                 case 'delete'               :                          
  270.                                     foreach ($_POST['Slide']['checklist'] as $slide_id) {
  271.                                         $this -> Slide -> delete($slide_id);
  272.                                     }
  273.                                    
  274.                                     $message = __('Selected slides have been removed', $this -> plugin_name);
  275.                                     $this -> redirect($this -> url, 'message', $message);
  276.                                     break;
  277.                             }
  278.                         } else {
  279.                             $message = __('No slides were selected', $this -> plugin_name);
  280.                             $this -> redirect($this -> url, "error", $message);
  281.                         }
  282.                     } else {
  283.                         $message = __('No action was specified', $this -> plugin_name);
  284.                         $this -> redirect($this -> url, "error", $message);
  285.                     }
  286.                     break;
  287.                 case 'order'            :
  288.                     if (!empty($_GET['gallery_id'])) {
  289.                         $gallery = $this -> Gallery -> find(array('id' => $_GET['gallery_id']));
  290.                        
  291.                         $slides = array();
  292.                         $gsquery = "SELECT gs.slide_id FROM `" . $this -> GallerySlides -> table . "` gs WHERE `gallery_id` = '" . $gallery -> id . "' ORDER BY gs.order ASC";
  293.                         if ($gs = $wpdb -> get_results($gsquery)) {
  294.                             foreach ($gs as $galleryslide) {
  295.                                 $slides[] = $this -> Slide -> find(array('id' => $galleryslide -> slide_id));
  296.                             }
  297.                         }
  298.                        
  299.                         $this -> render('slides' . DS . 'order', array('gallery' => $gallery, 'slides' => $slides), true, 'admin');
  300.                     } else {
  301.                         $slides = $this -> Slide -> find_all(null, null, array('order', "ASC"));
  302.                         $this -> render('slides' . DS . 'order', array('slides' => $slides), true, 'admin');
  303.                     }
  304.                     break;
  305.                 default                 :
  306.                     $data = $this -> paginate('Slide');            
  307.                     $this -> render('slides' . DS . 'index', array('slides' => $data[$this -> Slide -> model], 'paginate' => $data['Paginate']), true, 'admin');
  308.                     break;
  309.             }
  310.         }
  311.        
  312.         function admin_galleries() {
  313.            
  314.             switch ($_GET['method']) {
  315.                 case 'save'                     :
  316.                     if (!empty($_POST)) {
  317.                         if ($this -> Gallery -> save($_POST, true)) {
  318.                             $message = __('Gallery has been saved', $this -> plugin_name);
  319.                             $this -> redirect($this -> url, "message", $message);
  320.                         } else {
  321.                             $this -> render('galleries' . DS . 'save', false, true, 'admin');
  322.                         }
  323.                     } else {
  324.                         $this -> Db -> model = $this -> Gallery -> model;
  325.                         $this -> Gallery -> find(array('id' => $_GET['id']));
  326.                         $this -> render('galleries' . DS . 'save', false, true, 'admin');
  327.                     }
  328.                     break;
  329.                 case 'view'                     :
  330.                     $this -> Db -> model = $this -> Gallery -> model;
  331.                     $gallery = $this -> Gallery -> find(array('id' => $_GET['id']));
  332.                     $data = $this -> paginate('GallerySlides', "*", false, array('gallery_id' => $gallery -> id));
  333.                    
  334.                     $data['Slide'] = array();
  335.                     if (!empty($data[$this -> GallerySlides -> model])) {
  336.                         foreach ($data[$this -> GallerySlides -> model] as $galleryslide) {
  337.                             $this -> Db -> model = $this -> Slide -> model;
  338.                             $data['Slide'][] = $this -> Slide -> find(array('id' => $galleryslide -> slide_id));
  339.                         }
  340.                     }
  341.                    
  342.                     $this -> render('galleries' . DS . 'view', array('gallery' => $gallery, 'slides' => $data[$this -> Slide -> model], 'paginate' => $data['Paginate']), true, 'admin');
  343.                     break;
  344.                 case 'hardcode'         :
  345.                     $this -> Db -> model = $this -> Gallery -> model;
  346.                     $gallery = $this -> Gallery -> find(array('id' => $_GET['id']));                   
  347.                     $this -> render('galleries' . DS . 'hardcode', array('gallery' => $gallery), true, 'admin');
  348.                     break;
  349.                 case 'delete'           :
  350.                     if (!empty($_GET['id'])) {
  351.                         if ($this -> Gallery -> delete($_GET['id'])) {
  352.                             $msg_type = 'message';
  353.                             $message = __('Gallery has been removed', $this -> plugin_name);
  354.                         } else {
  355.                             $msg_type = 'error';
  356.                             $message = __('Gallery cannot be removed', $this -> plugin_name);  
  357.                         }
  358.                     } else {
  359.                         $msg_type = 'error';
  360.                         $message = __('No gallery was specified', $this -> plugin_name);
  361.                     }
  362.                    
  363.                     $this -> redirect($this -> referer, $msg_type, $message);
  364.                     break;
  365.                 case 'mass'             :
  366.                     if (!empty($_POST['action'])) {
  367.                         if (!empty($_POST['Gallery']['checklist'])) {                      
  368.                             switch ($_POST['action']) {
  369.                                 case 'delete'               :                          
  370.                                     foreach ($_POST['Gallery']['checklist'] as $gallery_id) {
  371.                                         $this -> Gallery -> delete($gallery_id);
  372.                                     }
  373.                                    
  374.                                     $message = __('Selected galleries have been removed', $this -> plugin_name);
  375.                                     $this -> redirect($this -> url, 'message', $message);
  376.                                     break;
  377.                             }
  378.                         } else {
  379.                             $message = __('No slides were selected', $this -> plugin_name);
  380.                             $this -> redirect($this -> url, "error", $message);
  381.                         }
  382.                     } else {
  383.                         $message = __('No action was specified', $this -> plugin_name);
  384.                         $this -> redirect($this -> url, "error", $message);
  385.                     }
  386.                     break;
  387.                 default                         :
  388.                     $data = $this -> paginate('Gallery');
  389.                     $this -> render('galleries' . DS . 'index', array('galleries' => $data[$this -> Gallery -> model], 'paginate' => $data['Paginate']), true, 'admin');
  390.                     break;
  391.             }
  392.         }
  393.        
  394.         function admin_settings() {
  395.             switch ($_GET['method']) {
  396.                 case 'reset'            :
  397.                     global $wpdb;
  398.                     $query = "DELETE FROM `" . $wpdb -> prefix . "options` WHERE `option_name` LIKE '" . $this -> pre . "%';";
  399.                    
  400.                     if ($wpdb -> query($query)) {
  401.                         $message = __('All configuration settings have been reset to their defaults', $this -> plugin_name);
  402.                         $msg_type = 'message';
  403.                         $this -> render_msg($message); 
  404.                     } else {
  405.                         $message = __('Configuration settings could not be reset', $this -> plugin_name);
  406.                         $msg_type = 'error';
  407.                         $this -> render_err($message);
  408.                     }
  409.                    
  410.                     $this -> redirect($this -> url, $msg_type, $message);
  411.                     break;
  412.                 default                 :
  413.                     if (!empty($_POST)) {
  414.                         foreach ($_POST as $pkey => $pval) {                   
  415.                             switch ($pkey) {
  416.                                 case 'permissions'          :
  417.                                     global $wp_roles;
  418.                                     $role_names = $wp_roles -> get_names();
  419.                                
  420.                                     if (!empty($_POST['permissions'])) {
  421.                                         $permissions = $_POST['permissions'];
  422.                                        
  423.                                         foreach ($this -> sections as $section_key => $section_menu) {
  424.                                             foreach ($role_names as $role_key => $role_name) {
  425.                                                 $wp_roles -> remove_cap($role_key, 'gallery_' . $section_key);
  426.                                             }
  427.                                            
  428.                                             if (!empty($permissions[$section_key])) {
  429.                                                 foreach ($permissions[$section_key] as $role) {
  430.                                                     $wp_roles -> add_cap($role, 'gallery_' . $section_key);
  431.                                                 }
  432.                                             } else {
  433.                                                 /* No roles were selected for this capability, at least add 'administrator' */
  434.                                                 $wp_roles -> add_cap('administrator', 'gallery_' . $section_key);
  435.                                                 $permissions[$section_key][] = 'administrator';
  436.                                             }
  437.                                         }
  438.                                     }
  439.                                    
  440.                                     $this -> update_option('permissions', $permissions);
  441.                                     break;
  442.                                 default                     :
  443.                                     $this -> update_option($pkey, $pval);
  444.                                     break;
  445.                             }
  446.                         }
  447.                        
  448.                         $message = __('Configuration has been saved', $this -> plugin_name);
  449.                         $this -> render_msg($message);
  450.                     }  
  451.                     break;
  452.             }
  453.                    
  454.             $this -> render('settings', false, true, 'admin');
  455.         }
  456.     }
  457. }
  458.  
  459. //initialize a Gallery object
  460. $Gallery = new Gallery();
  461. register_activation_hook(plugin_basename(__FILE__), array($Gallery, 'initialize_options'));
  462.  
  463. if (!function_exists('slideshow')) {
  464.     function slideshow($output = true, $gallery_id = null, $post_id = null, $params = array()) {
  465.         $params['gallery_id'] = $gallery_id;
  466.         $params['post_id'] = $post_id;
  467.    
  468.         $Gallery = new Gallery();
  469.         $content = $Gallery -> embed($params, false);
  470.        
  471.         if ($output == true) {
  472.             echo $content;
  473.         } else {
  474.             return $content;
  475.         }
  476.     }
  477. }
  478.  
  479. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement