Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package WordPress
- * @subpackage Default_Theme
- */
- add_action('init', 'widget_query_widget_register');
- function widget_query_widget_register() {
- $options = get_option('widget_query_widget');
- if(isset($options[0])) unset($options[0]);
- $prefix = 'query-widget'; // $id prefix
- $name = __('Query Widget','hybrid');
- $control_ops = array('width' => 200, 'height' => 200, 'id_base' => $prefix);
- if(!empty($options)){
- foreach(array_keys($options) as $widget_number){
- $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'));
- wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_query_widget', $widget_ops, array( 'number' => $widget_number ));
- wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_query_widget_control', $control_ops, array( 'number' => $widget_number ));
- }
- } else{
- $options = array();
- $widget_number = 1;
- $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'));
- wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_query_widget', $widget_ops, array( 'number' => $widget_number ));
- wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_query_widget_control', $control_ops, array( 'number' => $widget_number ));
- }
- }
- function widget_query_widget($args, $widget_args = 1) {
- extract($args, EXTR_SKIP);
- if(is_numeric($widget_args))
- $widget_args = array('number' => $widget_args);
- $widget_args = wp_parse_args($widget_args, array('number' => -1));
- extract($widget_args, EXTR_SKIP);
- $options = get_option('widget_query_widget');
- if(!isset($options[$number]))
- return;
- $opts = $options[$number];
- if(!empty($opts['title'])) { $title = apply_filters('widget_title', $opts['title']); }
- else { $title = 'Query Widget';}
- $tag = $opts['tag'];
- $exception = (int)$opts['exception'];
- $list = (int)$opts['list'];
- $category = $opts['category_name'];
- if($opts['category_exclude'])
- $category__not_in = explode(',', str_replace(' ', '', $opts['category_exclude']));
- $no_dupes_set = $opts['no_dupes_set'];
- $no_dupes_get = $opts['no_dupes_get'];
- if(!empty($opts['thumb'])) { $thumb = 'Yes'; } else { $thumb = $opts['thumb']; }
- // Array for the exceptions query_post
- $query_exception = array(
- 'showposts' => $exception + $list,
- 'category_name' => $category,
- 'category__not_in' => $category__not_in,
- );
- // Array for the lists query_post
- $query_list = array(
- 'showposts' => $list,
- 'category_name' => $category,
- 'offset' => $exception,
- 'category__not_in' => $category__not_in,
- );
- echo $before_widget;
- if($title)
- echo $before_title . $title . $after_title;
- //Keep original Query
- $temp_query = $wp_query;
- //New Query is shining
- query_posts($query_exception);
- (int)$post_nr = 1;
- print_r($do_not_include);
- while (have_posts()) : the_post();
- global $post;
- //Set no dupes?
- if(!empty($no_dupes_set)) :
- echo 'setting no dupes. ';
- $do_not_include[] = $post->ID;
- endif;
- //Get no dupes?
- if(!empty($no_dupes_get)) :
- echo 'getting no dupes. ';
- if(isset($do_not_include)):
- echo 'check for dupes. ';
- if(in_array($post->ID, $do_not_include)) continue;
- endif;
- endif;
- update_post_caches($posts);
- if($post_nr <= $exception) :
- if($thumb == 'Yes') : ?>
- <div class="thumbnail">
- <?php get_the_image(); ?>
- </div>
- <?php endif; ?>
- <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
- <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
- <?php the_excerpt('Read the rest of this entry »');
- else: ?>
- <ul>
- <li>
- <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
- <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
- </li>
- </ul>
- <?php endif;
- (int)$post_nr = (int)$post_nr + 1;
- endwhile;
- //print_r($do_not_include);
- //Restore good'ol Query
- $wp_query = $temp_query;
- echo $after_widget;
- }
- function widget_query_widget_control($args) {
- $prefix = 'query-widget'; // $id prefix
- $options = get_option('widget_query_widget');
- if(empty($options)) $options = array();
- if(isset($options[0])) unset($options[0]);
- // update options array
- if(!empty($_POST[$prefix]) && is_array($_POST)){
- foreach($_POST[$prefix] as $widget_number => $values){
- if(empty($values) && isset($options[$widget_number])) // user clicked cancel
- continue;
- if(!isset($options[$widget_number]) && $args['number'] == -1){
- $args['number'] = $widget_number;
- $options['last_number'] = $widget_number;
- }
- $options[$widget_number] = $values;
- }
- // update number
- if($args['number'] == -1 && !empty($options['last_number'])){
- $args['number'] = $options['last_number'];
- }
- // clear unused options and update options in DB. return actual options array
- $options = bf_smart_multiwidget_update($prefix, $options, $_POST[$prefix], $_POST['sidebar'], 'widget_query_widget');
- }
- // $number - is dynamic number for multi widget, gived by WP
- // by default $number = -1 (if no widgets activated). In this case we should use %i% for inputs
- // to allow WP generate number automatically
- $number = ($args['number'] == -1)? '%i%' : $args['number'];
- // now we can output control
- $opts = @$options[$number];
- $title = strip_tags(stripslashes(@$opts['title']));
- $category_name = strip_tags(stripslashes(@$opts['category_name']));
- $category__not_in = strip_tags(stripslashes(@$opts['category_exclude']));
- $tag = strip_tags(stripslashes(@$opts['tag']));
- $thumb = strip_tags(stripslashes(@$opts['thumb']));
- $class = strip_tags(stripslashes(@$opts['class']));
- $exception = strip_tags(stripslashes($opts['exception']));
- $list = strip_tags(stripslashes($opts['list']));
- $no_dupes_set = strip_tags(stripslashes($opts['no_dupes_set']));
- $no_dupes_get = strip_tags(stripslashes($opts['no_dupes_get']));
- ?>
- <p>
- Title
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][title]" value="<?php echo $title; ?>" /><br />
- </p>
- <p>
- Category
- <?php
- $cats = get_categories(array('type' => 'post'));
- $cats[] = false;
- ?>
- <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][category_name]">
- <?php foreach($cats as $cat) : ?>
- <option <?php if($cat->name == $category_name) echo 'selected="selected"'; ?>><?php echo $cat->name; ?></option>
- <?php endforeach; ?>
- </select>
- </p>
- <p>
- Category not in
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][category_exclude]" value="<?php echo $category__not_in; ?>" /><br />
- </p>
- <p>
- Excetions (int 0>)
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][exception]" value="<?php echo $exception; ?>" /><br />
- </p>
- <p>
- List (int 0>)
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][list]" value="<?php echo $list; ?>" /><br />
- </p>
- <p>
- Thumbnail?
- <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][thumb]">
- <option <?php if($thumb == 'Yes') echo 'selected="selected"'; ?>>Yes</option>
- <option <?php if($thumb == 'No') echo 'selected="selected"'; ?>>No</option>
- </select>
- <p>
- <p>
- Class
- <select name="<?php echo $prefix; ?>[<?php echo $number; ?>][class]">
- <option <?php if('grid_3 alpha' == $class) echo 'selected="selected"'; ?>>grid_3 alpha</option>
- <option <?php if('grid_3' == $class) echo 'selected="selected"'; ?>>grid_3</option>
- <option <?php if('grid_3 omega' == $class) echo 'selected="selected"'; ?>>grid_3 omega</option>
- <option <?php if('grid_6 alpha_omega' == $class) echo 'selected="selected"'; ?>>grid_6 alpha_omega</option>
- </select>
- </p>
- <p>
- No dublicates (set #)
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][no_dupes_set]" value="<?php echo $no_dupes_set; ?>" /><br />
- </p>
- <p>
- No dublicates (get #)
- <input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][no_dupes_get]" value="<?php echo $no_dupes_get; ?>" /><br />
- </p>
- <?php
- }
- if(!function_exists('bf_smart_multiwidget_update')){
- function bf_smart_multiwidget_update($id_prefix, $options, $post, $sidebar, $option_name = ''){
- global $wp_registered_widgets;
- static $updated = false;
- // get active sidebar
- $sidebars_widgets = wp_get_sidebars_widgets();
- if ( isset($sidebars_widgets[$sidebar]) )
- $this_sidebar =& $sidebars_widgets[$sidebar];
- else
- $this_sidebar = array();
- // search unused options
- foreach ( $this_sidebar as $_widget_id ) {
- if(preg_match('/'.$id_prefix.'-([0-9]+)/i', $_widget_id, $match)){
- $widget_number = $match[1];
- // $_POST['widget-id'] contain current widgets set for current sidebar
- // $this_sidebar is not updated yet, so we can determine which was deleted
- if(!in_array($match[0], $_POST['widget-id'])){
- unset($options[$widget_number]);
- }
- }
- }
- // update database
- if(!empty($option_name)){
- update_option($option_name, $options);
- $updated = true;
- }
- // return updated array
- return $options;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement