Advertisement
jjswebs

elastiGallery Class

Apr 19th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.18 KB | None | 0 0
  1. <?
  2.  
  3. class elastiGallery{
  4.    
  5.     public $prefix = "elG_";
  6.    
  7.     public $hook;
  8.    
  9.     public $options;
  10.    
  11.     function elastiGallery($ops){
  12.         $this->__construct($ops);
  13.     }
  14.    
  15.     function __construct($ops){
  16.         $this->options = $ops;
  17.        
  18.         add_action('admin_init', array(&$this, 'register_admin_deps') );
  19.         add_action('admin_menu', array(&$this, 'load_admin_menu') );
  20.         add_action('admin_enqueue_scripts', array(&$this, 'load_admin_deps') );
  21.        
  22.         // Styling and such
  23.         add_action('init', array( &$this, 'register_frontend_deps') );
  24.         add_action('wp_enqueue_scripts', array(&$this, 'load_frontend_deps') );
  25.        
  26.        
  27.         add_action('elastigallery', array(&$this, 'slide_action'), 10, 2 );
  28.         add_filter('post_gallery', array(&$this, 'gallery_filter'), 9, 2 );
  29.        
  30.     }
  31.    
  32.     public function register_frontend_deps(){
  33.        
  34.         wp_register_style('elastislide', plugins_url('css/elastislide.css', dirname(__FILE__) ), '', '1.0', 'all');
  35.         wp_register_style('elastiGallery', plugins_url('css/elastigallery.css', dirname(__FILE__) ), array('elastislide'), '1.0', 'all');
  36.        
  37.        
  38.         wp_register_script('modernizr', plugins_url('js/modernizr.custom.17475.js', dirname(__FILE__) ), array('jquery'), '17475');
  39.         wp_register_script('jquerypp', plugins_url('js/jquerypp.custom.js', dirname(__FILE__) ), array('modernizr'), '1.0');
  40.         wp_register_script('elastislide_js', plugins_url('js/jquery.elastislide.js', dirname(__FILE__) ), array('jquerypp'), '1.0');
  41.         wp_register_script('elastiGallery', plugins_url('js/jquery.elastiGallery.js', dirname(__FILE__) ), array('elastislide_js'), '1.0');
  42.         // Setup dependancy so just enqueue elastiGallery script and.
  43.     }
  44.    
  45.     public function load_frontend_deps(){
  46.        
  47.         wp_enqueue_style('elastiGallery');
  48.         wp_enqueue_script('elastiGallery');
  49.        
  50.     }
  51.    
  52.     public function register_admin_deps(){     
  53.         foreach($this->options as $k => $v) register_setting($this->prefix.'options', $this->prefix.$k);
  54.        
  55.         wp_register_style('spectrum', plugins_url('css/spectrum.css', dirname(__FILE__) ), '', '1.0.9');
  56.         wp_register_script('spectrum', plugins_url('js/spectrum.js', dirname(__FILE__) ), array('jquery'), '1.0.9' );
  57.        
  58.         wp_register_style( $this->prefix.'admin_css', plugins_url('css/admin.css', dirname(__FILE__) ), '', '1.0');
  59.         wp_register_script( $this->prefix.'admin_js', plugins_url('js/admin.js', dirname(__FILE__) ) , '', '1.0');
  60.        
  61.     }
  62.    
  63.     public function load_admin_deps($hook = FALSE){
  64.         if($hook == $this->hook && $hook != false){
  65.             wp_enqueue_media();
  66.            
  67.             wp_enqueue_style('spectrum');
  68.             wp_enqueue_script('spectrum');
  69.            
  70.             wp_enqueue_script($this->prefix.'admin_js');
  71.             wp_enqueue_style($this->prefix.'admin_css');
  72.         }
  73.     }
  74.    
  75.     public function load_admin_menu(){
  76.         $this->hook = add_options_page('EG Options Page', 'EG Options', 'manage_options', $this->prefix.'options', array(&$this, 'render_options_page') );
  77.     }
  78.  
  79.     public function render_options_page(){
  80.        
  81.         ?>
  82.             <div class="wrap">
  83.                 <div id="icon-options-general" class="icon32"><br /></div>
  84.                 <h2>elastiGallery Options Page</h2>
  85.                 <form method="post" action="options.php">
  86.                 <? settings_fields($this->prefix.'options'); ?>
  87.                 <table class="form-table">
  88.                     <tbody>
  89.                         <?
  90.                         foreach ($this->options as $k => $v){
  91.                             ?>
  92.                             <tr valign="top">
  93.                                 <th scope="row"><label for="<? echo $this->prefix.$k; ?>"><? echo $v['name']; ?></label></th>
  94.                                 <td><? echo $this->render_option_field($k, $v); ?>
  95.                                     <? if( isset( $v['description'] ) ): ?>
  96.                                         <p class="description"><? echo $v['description']; ?></p>
  97.                                     <? endif; ?>
  98.                                 </td>
  99.                             </tr>
  100.                             <?
  101.                         }
  102.                         ?>
  103.                     </tbody>
  104.                 </table>
  105.                 <p class="submit">
  106.                     <input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes">
  107.                 </p>
  108.                 </form>
  109.             </div>
  110.         <?
  111.     }
  112.    
  113.     public function render_color_select($key, $data){
  114.        
  115.         $opData = get_option($this->prefix.$key, $data);
  116.        
  117.         $output = '<!-- Color Selects -->';
  118.         foreach($opData as $k => $v){
  119.             $output .= '<input type="text" id="'.$key.'_'.$k.'" name="'.$this->prefix.$key.'['.$k.']" value="'.$v.'" class="color_select">';
  120.         }
  121.        
  122.         return $output;
  123.        
  124.     }
  125.    
  126.     public function render_option_field($key, $data){
  127.         switch($data['type']){
  128.             case 'text':
  129.                 $output = '<input type="text" name="'.$this->prefix.$key.'" id="'.$this->prefix.$key.'" value="'.get_option($this->prefix.$key, $data['default']).'" class="regular-text" />';
  130.                 break;
  131.             case 'password':
  132.                 $output = '<input type="password" name="'.$this->prefix.$key.'" id="'.$this->prefix.$key.'" value="'.get_option($this->prefix.$key).'" class="regular-text" />';
  133.                 break;
  134.             case 'number':
  135.                 $output = '<input type="number" name="'.$this->prefix.$key.'" id="'.$this->prefix.$key.'" value="'.get_option($this->prefix.$key, $data['default']).'" />';
  136.                 break;
  137.             case 'data_array':
  138.                 $output = $this->buildDataArrayFields($key, $data['fields']);
  139.                 break;
  140.             case 'select':
  141.                 $output = $this->buildSelectOptions($key, $data['fields'], $data['default']);
  142.                 break;
  143.             case 'color':
  144.                 $output = $this->render_color_select($key, $data['fields']);
  145.                 break;
  146.             case 'media':
  147.                 $output = $this->buildMediaOption($key);
  148.                 break;
  149.             default:
  150.             break;
  151.         }
  152.         return $output;
  153.     }
  154.    
  155.     public function buildMediaOption($key){
  156.        
  157.         $opData = get_option($this->prefix.$key);
  158.        
  159.         $output = '<div class="uploader">';
  160.         $output .= '<input type="text" name="'.$this->prefix.$key.'" id="'.$this->prefix.$key.'" class="regular-text" value="'.$opData.'" />';
  161.         $output .= '<input type="button" id="'.$this->prefix.$key.'_upload" value="Upload" class="button upload_image_button" data-id="'.$this->prefix.$key.'" />';
  162.         $output .= '</div>';
  163.        
  164.         return $output;
  165.     }
  166.    
  167.     public function buildSelectOptions($key, $data, $def = false){
  168.        
  169.         $opData = get_option($this->prefix.$key, $def);
  170.        
  171.         $output = '<select name="'.$this->prefix.$key.'" id="'.$this->prefix.$key.'">';
  172.         foreach($data as $k => $v){
  173.             $output .= '<option value="'.$k.'" '.selected($opData, $k, false).'>'.$v.'</option>';
  174.         }
  175.         $output .= '</select>';
  176.        
  177.         return $output;
  178.        
  179.     }
  180.    
  181.     public function buildDataArrayFields($key, $fields){
  182.         $opData = get_option($this->prefix.$key);
  183.         ?>
  184.             <a href="javascript:;" class="addrow">[+] Add Row</a>
  185.             <table class="dataArrayFields" id="<? echo $key; ?>">
  186.             <? $rowBase = 1; ?>
  187.             <? if(!empty($opData) && is_array($opData)) :?>
  188.                 <? foreach ($opData as $row): ?>
  189.                     <tr id="data_row_<? echo $rowBase; ?>" class="data_row">
  190.                     <? foreach ($fields as $colName): ?>
  191.                         <td class="data_col <? echo $colName; ?>"><input type="text" name="<? echo $this->prefix.$key ?>[<? echo $rowBase; ?>][<? echo $colName; ?>]" value="<? echo $row[$colName]; ?>"/></td>
  192.                     <? endforeach; ?>
  193.                         <td><a href="javascript:;" id="<? echo $rowBase; ?>" class="removerow">[X]</a></td>
  194.                     </tr>                    
  195.                     <? $rowBase++; ?>
  196.                 <? endforeach; ?>
  197.             <? else: ?>
  198.                 <tr id="data_row_<? echo $rowBase; ?>" class="data_row">
  199.                 <? foreach ($fields as $colName): ?>
  200.                     <td class="data_col <? echo $colName; ?>"><input type="text" name="<? echo $this->prefix.$key ?>[<? echo $rowBase; ?>][<? echo $colName; ?>]" /></td>
  201.                 <? endforeach; ?>
  202.                     <td><a href="javascript:;" id="<? echo $rowBase; ?>" class="removerow">[X]</a></td>
  203.                 </tr>
  204.             <? endif; ?>
  205.             </table>
  206.         <?
  207.     }
  208.    
  209.     public function uninstall(){
  210.         if( !defined( 'WP_UNINSTALL_PLUGIN' ) ) return false;
  211.         // Remove options
  212.         foreach ($this->options as $k => $v) delete_option($this->prefix.$k);
  213.     }
  214.    
  215.     public function gallery_filter($output = false, $attr){
  216.         global $post;
  217.        
  218.         $featured_image_size=empty($featured_image_size) ? "single" : $featured_image_size;
  219.         $featured_image_size_responsive= empty($featured_image_size_responsive) ? "single-medium" : $featured_image_size_responsive;
  220.         $attrData = explode(',', $attr['ids']);
  221.         do_action('elastigallery', $attrData, $attr['orderby']);
  222.         $output = '<div class="article-image">';
  223.         if ( has_post_thumbnail()) {
  224.             $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
  225.             $output .= '<a class="darken" href="' . $large_image_url[0] . '">';
  226.             $output .= get_the_post_thumbnail($post->ID, $featured_image_size);
  227.             $output .= '</a>';
  228.         }
  229.         $output .= '</div>';
  230.         $output .= '<!-- responsive purposes - hidden for standard desktop view-->';
  231.         $output .= '<div class="article-image responsive">';
  232.         if ( has_post_thumbnail($post->ID)) $output .= get_the_post_thumbnail($post->ID, $featured_image_size_responsive);
  233.         $output .= '</div>';
  234.         $output .= '<!-- end responsive purposes -->';
  235.        
  236.         return $output;
  237.     }
  238.    
  239.     public function slide_action($attIDs = false, $order = 'menu_order'){
  240.         global $post;
  241.        
  242.         $numberThumbs = get_option($this->prefix.'number_thumbs');
  243.         $padMethod = get_option($this->prefix.'a_thumb_method');
  244.         $padNumber = get_option($this->prefix.'add_thumb');
  245.         if(is_attachment()){
  246.             // Attachments will not have $attIDs or $order
  247.             $pData = $post->post_parent;
  248.             $curImg = get_the_ID();
  249.  
  250.             $theData = get_post($pData);
  251.             $scRegEx = get_shortcode_regex();
  252.             preg_match( '/'.$scRegEx.'/', $theData->post_content, $matches);
  253.             if($matches[2] == 'gallery'){
  254.                 $attStr = str_replace(" ", "&", trim($matches[3]));
  255.                 $attStr = str_replace('"','',$attStr);
  256.             }
  257.             $attributes = wp_parse_args($attStr, array());
  258.             $attIDs = explode( ',', $attributes['ids'] );
  259.            
  260.         }
  261.        
  262.         // Using get_posts instead of WP_Query because for some reason WP_Query will return $query->found_posts = 0 if post_type == attachment and/or post_parent == $n
  263.         $gpargs = array('numberposts' => -1, 'nopaging' => true, 'post_type' => 'attachment', 'supress_filters' => true);
  264.         if(!empty($order) && $order == 'rand'){
  265.             $gpargs['orderby'] = 'rand';
  266.         }else{
  267.             $gpargs['orderby'] = 'menu_order';
  268.         }
  269.        
  270.         if(!empty($attIDs)){
  271.             $gpargs['post__in'] = $attIDs;
  272.                
  273.             $atc = get_posts($gpargs);
  274.         }
  275.         ?><ul id="carousel" class="elastislide-list"><?
  276.        
  277.         if($numberThumbs == 'desc'){
  278.             $n = intval($attachments->found_posts) + intval($padNumber);
  279.         }else{
  280.             $n = '1';
  281.         }
  282.         if(!empty($atc)) :
  283.             foreach($atc as $image):
  284.                 $current = ($curImg && $curImg == $image->ID) ? 'current' : '';
  285.                
  286.                 ?><li data-id="<? echo $image->ID; ?>" class="image attachment <? echo $current; ?>"><?
  287.                      echo wp_get_attachment_link($image->ID, 'widget-thumbnail', true);
  288.                      echo '<div class="number">'.$n.'</div>';
  289.                 ?></li><?
  290.                
  291.                 if($numberThumbs == 'desc'){
  292.                     $n--;
  293.                 }else{
  294.                     $n++;
  295.                 }
  296.             endforeach;
  297.         endif;
  298.        
  299.         // Now for the padding.
  300.         $padConfig = array(
  301.             'posts_per_page'    =>  $padNumber,
  302.         );
  303.        
  304.         if($padMethod == 'cat'){
  305.             // Regardless, let's do categories for now.
  306.             $catList = wp_get_post_categories($pData);
  307.             $catString = '';
  308.             foreach($catList as $k => $v)
  309.                 $catString .= $v.',';
  310.                
  311.             $catString = substr($catString,0,-1);
  312.             $padConfig['cat'] = $catString;
  313.         }
  314.        
  315.         $padPosts = new WP_Query($padConfig);
  316.         if($padPosts->have_posts()):
  317.             while($padPosts->have_posts()):
  318.                 $padPosts->the_post();
  319.                 ?><li data-id="<? the_ID(); ?>" class="image post"><a href="<? the_permalink(); ?>" title="<? the_title(); ?>" ><?
  320.                     /* Get the permalink of the attachment image and put it here followed by the image itself.
  321.                         ie. <a href="#" title=""><img src="" alt=""></a>
  322.                     */
  323.                     echo get_the_post_thumbnail($post->ID, 'widget-thumbnail');
  324.                    
  325.                 ?></a><?                
  326.                      echo '<div class="number">'.$n.'</div>';
  327.                 ?></li><?
  328.                 if($numberThumbs == 'desc'){
  329.                     $n--;
  330.                 }else{
  331.                     $n++;
  332.                 }
  333.             endwhile;
  334.             wp_reset_query();
  335.             wp_reset_postdata();
  336.         endif;
  337.        
  338.        
  339.         ?></ul><?
  340.        
  341.        
  342.     }
  343.    
  344. }
  345.  
  346. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement