Advertisement
Guest User

dimaggio

a guest
Feb 25th, 2009
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.55 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package WordPress
  4.  * @subpackage Default_Theme
  5.  */
  6.  
  7. add_action('init', 'widget_query_widget_register');
  8. function widget_query_widget_register() {
  9.    
  10.     $options = get_option('widget_query_widget');
  11.     if(isset($options[0])) unset($options[0]);
  12.    
  13.     $prefix = 'query-widget'; // $id prefix
  14.     $name = __('Query Widget','hybrid');
  15.     $control_ops = array('width' => 200, 'height' => 200, 'id_base' => $prefix);
  16.  
  17.     if(!empty($options)){
  18.         foreach(array_keys($options) as $widget_number){
  19.             $widget_ops = array('classname' => 'widget_query_widget' . ' ' . $options[$widget_number]['class'], 'description' => __('This is an example of a widget which you can add many times'));
  20.             wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_query_widget', $widget_ops, array( 'number' => $widget_number ));
  21.             wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_query_widget_control', $control_ops, array( 'number' => $widget_number ));
  22.         }
  23.     } else{
  24.         $options = array();
  25.         $widget_number = 1;
  26.         $widget_ops = array('classname' => 'widget_query_widget' . ' ' . $options[$widget_number]['class'], 'description' => __('This is an example of a widget which you can add many times'));
  27.         wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_query_widget', $widget_ops, array( 'number' => $widget_number ));
  28.         wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_query_widget_control', $control_ops, array( 'number' => $widget_number ));
  29.     }
  30.        
  31. }
  32.  
  33. function widget_query_widget($args, $widget_args = 1) {
  34.     extract($args, EXTR_SKIP);
  35.  
  36.     if(is_numeric($widget_args))
  37.         $widget_args = array('number' => $widget_args);
  38.  
  39.     $widget_args = wp_parse_args($widget_args, array('number' => -1));
  40.  
  41.     extract($widget_args, EXTR_SKIP);
  42.  
  43.     $options = get_option('widget_query_widget');
  44.  
  45.     if(!isset($options[$number]))
  46.         return;
  47.    
  48.     $opts = $options[$number];
  49.  
  50.     if(!empty($opts['title'])) { $title = apply_filters('widget_title', $opts['title']); }
  51.         else { $title = 'Query Widget';}
  52.     $tag = $opts['tag'];
  53.     $exception = (int)$opts['exception'];
  54.     $list = (int)$opts['list'];
  55.     $category = $opts['category_name'];
  56.     if($opts['category_exclude'])
  57.         $category__not_in = explode(',', str_replace(' ', '', $opts['category_exclude']));
  58.     $no_dupes_set = $opts['no_dupes_set'];
  59.     $no_dupes_get = $opts['no_dupes_get'];
  60.    
  61.     if(!empty($opts['thumb'])) { $thumb = 'Yes'; } else { $thumb = $opts['thumb']; }
  62.        
  63.     // Array for the exceptions query_post
  64.     $query_exception = array(
  65.         'showposts' => $exception + $list,
  66.         'category_name' => $category,
  67.         'category__not_in' => $category__not_in,
  68.     );
  69.    
  70.     // Array for the lists query_post
  71.     $query_list = array(
  72.         'showposts' => $list,
  73.         'category_name' => $category,
  74.         'offset' => $exception,
  75.         'category__not_in' => $category__not_in,
  76.     );
  77.        
  78.    
  79.     echo $before_widget;
  80.     if($title)
  81.         echo $before_title . $title . $after_title;
  82.    
  83.     //Keep original Query
  84.     $temp_query = $wp_query;
  85.     //New Query is shining
  86.     query_posts($query_exception);
  87.     (int)$post_nr = 1;
  88.    
  89.     print_r($do_not_include);
  90.    
  91.     while (have_posts()) : the_post();
  92.     global $post;
  93.    
  94.     //Set no dupes?
  95.     if(!empty($no_dupes_set)) :
  96.         echo 'setting no dupes. ';
  97.         $do_not_include[] = $post->ID;
  98.     endif;
  99.    
  100.     //Get no dupes?
  101.     if(!empty($no_dupes_get)) :
  102.         echo 'getting no dupes. ';
  103.         if(isset($do_not_include)):
  104.             echo 'check for dupes. ';
  105.             if(in_array($post->ID, $do_not_include)) continue;
  106.         endif;
  107.     endif;
  108.    
  109.     update_post_caches($posts);
  110.    
  111.         if($post_nr <= $exception) :
  112.             if($thumb == 'Yes') : ?>
  113.                 <div class="thumbnail">
  114.                     <?php get_the_image(); ?>
  115.                 </div>
  116.             <?php endif; ?>
  117.                 <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  118.                 <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  119.                 <?php the_excerpt('Read the rest of this entry &raquo;');
  120.         else: ?>
  121.             <ul>
  122.                 <li>
  123.                     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  124.                     <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  125.                 </li>
  126.             </ul>
  127.         <?php endif;
  128.     (int)$post_nr = (int)$post_nr + 1;
  129.     endwhile;
  130.    
  131.     //print_r($do_not_include);
  132.    
  133.     //Restore good'ol Query
  134.     $wp_query = $temp_query;
  135.    
  136.     echo $after_widget;
  137. }
  138.  
  139. function widget_query_widget_control($args) {
  140.  
  141.     $prefix = 'query-widget'; // $id prefix
  142.  
  143.     $options = get_option('widget_query_widget');
  144.     if(empty($options)) $options = array();
  145.     if(isset($options[0])) unset($options[0]);
  146.  
  147.     // update options array
  148.     if(!empty($_POST[$prefix]) && is_array($_POST)){
  149.         foreach($_POST[$prefix] as $widget_number => $values){
  150.             if(empty($values) && isset($options[$widget_number])) // user clicked cancel
  151.                 continue;
  152.  
  153.             if(!isset($options[$widget_number]) && $args['number'] == -1){
  154.                 $args['number'] = $widget_number;
  155.                 $options['last_number'] = $widget_number;
  156.             }
  157.             $options[$widget_number] = $values;
  158.         }
  159.  
  160.         // update number
  161.         if($args['number'] == -1 && !empty($options['last_number'])){
  162.             $args['number'] = $options['last_number'];
  163.         }
  164.  
  165.         // clear unused options and update options in DB. return actual options array
  166.         $options = bf_smart_multiwidget_update($prefix, $options, $_POST[$prefix], $_POST['sidebar'], 'widget_query_widget');
  167.     }
  168.  
  169.     // $number - is dynamic number for multi widget, gived by WP
  170.     // by default $number = -1 (if no widgets activated). In this case we should use %i% for inputs
  171.     //   to allow WP generate number automatically
  172.     $number = ($args['number'] == -1)? '%i%' : $args['number'];
  173.  
  174.     // now we can output control
  175.     $opts = @$options[$number];
  176.  
  177.     $title = strip_tags(stripslashes(@$opts['title']));
  178.     $category_name = strip_tags(stripslashes(@$opts['category_name']));
  179.     $category__not_in = strip_tags(stripslashes(@$opts['category_exclude']));
  180.     $tag = strip_tags(stripslashes(@$opts['tag']));
  181.     $thumb = strip_tags(stripslashes(@$opts['thumb']));
  182.     $class = strip_tags(stripslashes(@$opts['class']));
  183.     $exception = strip_tags(stripslashes($opts['exception']));
  184.     $list = strip_tags(stripslashes($opts['list']));
  185.     $no_dupes_set = strip_tags(stripslashes($opts['no_dupes_set']));
  186.     $no_dupes_get = strip_tags(stripslashes($opts['no_dupes_get']));
  187.  
  188.     ?>
  189.     <p>
  190.         Title
  191.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][title]" value="<?php echo $title; ?>" /><br />
  192.     </p>
  193.     <p>
  194.         Category
  195.         <?php
  196.             $cats = get_categories(array('type' => 'post'));
  197.             $cats[] = false;
  198.         ?>
  199.         <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][category_name]">
  200.             <?php foreach($cats as $cat) : ?>
  201.                 <option <?php if($cat->name == $category_name) echo 'selected="selected"'; ?>><?php echo $cat->name; ?></option>
  202.             <?php endforeach; ?>
  203.         </select>
  204.     </p>
  205.     <p>
  206.         Category not in
  207.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][category_exclude]" value="<?php echo $category__not_in; ?>" /><br />
  208.     </p>
  209.     <p>
  210.         Excetions (int 0>)
  211.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][exception]" value="<?php echo $exception; ?>" /><br />
  212.     </p>
  213.     <p>
  214.         List (int 0>)
  215.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][list]" value="<?php echo $list; ?>" /><br />
  216.     </p>
  217.     <p>
  218.         Thumbnail?
  219.         <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][thumb]">
  220.             <option <?php if($thumb == 'Yes') echo 'selected="selected"'; ?>>Yes</option>
  221.             <option <?php if($thumb == 'No') echo 'selected="selected"'; ?>>No</option>
  222.         </select>
  223.     <p>
  224.     <p>
  225.         Class
  226.         <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][class]">
  227.             <option <?php if('grid_3 alpha' == $class) echo 'selected="selected"'; ?>>grid_3 alpha</option>
  228.             <option <?php if('grid_3' == $class) echo 'selected="selected"'; ?>>grid_3</option>
  229.             <option <?php if('grid_3 omega' == $class) echo 'selected="selected"'; ?>>grid_3 omega</option>
  230.             <option <?php if('grid_6 alpha_omega' == $class) echo 'selected="selected"'; ?>>grid_6 alpha_omega</option>
  231.         </select>
  232.     </p>
  233.     <p>
  234.         No dublicates (set #)
  235.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][no_dupes_set]" value="<?php echo $no_dupes_set; ?>" /><br />
  236.     </p>
  237.     <p>
  238.         No dublicates (get #)
  239.         <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][no_dupes_get]" value="<?php echo $no_dupes_get; ?>" /><br />
  240.     </p>
  241.     <?php
  242. }
  243.  
  244. if(!function_exists('bf_smart_multiwidget_update')){
  245.     function bf_smart_multiwidget_update($id_prefix, $options, $post, $sidebar, $option_name = ''){
  246.         global $wp_registered_widgets;
  247.         static $updated = false;
  248.  
  249.         // get active sidebar
  250.         $sidebars_widgets = wp_get_sidebars_widgets();
  251.         if ( isset($sidebars_widgets[$sidebar]) )
  252.             $this_sidebar =& $sidebars_widgets[$sidebar];
  253.         else
  254.             $this_sidebar = array();
  255.  
  256.         // search unused options
  257.         foreach ( $this_sidebar as $_widget_id ) {
  258.             if(preg_match('/'.$id_prefix.'-([0-9]+)/i', $_widget_id, $match)){
  259.                 $widget_number = $match[1];
  260.  
  261.                 // $_POST['widget-id'] contain current widgets set for current sidebar
  262.                 // $this_sidebar is not updated yet, so we can determine which was deleted
  263.                 if(!in_array($match[0], $_POST['widget-id'])){
  264.                     unset($options[$widget_number]);
  265.                 }
  266.             }
  267.         }
  268.  
  269.         // update database
  270.         if(!empty($option_name)){
  271.             update_option($option_name, $options);
  272.             $updated = true;
  273.         }
  274.  
  275.         // return updated array
  276.         return $options;
  277.     }
  278. }
  279. ?>
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement