Plugin Directory

Changeset 149570


Ignore:
Timestamp:
08/27/2009 09:46:20 AM (16 years ago)
Author:
yakuphan
Message:

Version 2.0

Location:
advanced-random-posts/trunk
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • advanced-random-posts/trunk/adv-random-posts.php

    r73867 r149570  
    33Plugin Name: Advanced Random Posts
    44Plugin URI: http://www.yakupgovler.com/?p=416
    5 Description: Display random posts from selected categories or current category or all posts.
    6 Version: 1.1
     5Description: Display random posts from selected categories or current category or all posts with thumbnail images (optional).
     6Version: 2.0
    77Author: Yakup GÖVLER
    88Author URI: http://www.yakupgovler.com
    99*/
    1010
    11 function yg_randomposts_init() {
    12     if (!function_exists('register_sidebar_widget')) {
    13         return;
    14     }
    15 
    16     function yg_randomposts_widget($args) {
    17 
    18         extract($args);
    19         $options = get_option('yg_randomposts');
    20         $title = htmlspecialchars($options['title']);
    21         $limit = intval($options['entries-number']);
    22         $cats = htmlspecialchars($options['categories']);
    23         $currentcat = intval($options['currentcat']);
    24         echo $before_widget.$before_title.$title.$after_title;
    25         echo "\n<ul>\n";
    26          yg_randomposts("limit=$limit&cats=$cats&currentcat=$currentcat");
    27         echo "</ul>\n";
    28         echo $after_widget;
    29     }
    30 
    31     function yg_randomposts_options() {
    32         $options = get_option('yg_randomposts');
    33         if (!is_array($options)) {
    34             $options = array('title' => 'Random Posts', 'entries-number' => '10', 'currentcat' => 0, 'categories' => '');
    35             update_option('yg_randomposts', $options);
    36         }
    37         if ($_POST['yg-randomposts-submit']) {
    38         $options['entries-number'] = intval($_POST['yg-randomposts-entries-number']);
    39         if (($options['entries-number'] < 1) || ($options['entries-number'] > 20)) $options['entries-number'] = 10;
    40           $options['title'] = strip_tags(stripslashes($_POST['yg-randomposts-title']));
    41           $options['currentcat'] = ($_POST['yg-randomposts-currentcat']) ? 1 : 0;
    42           $cats = str_replace(" ", "", strip_tags(stripslashes($_POST['yg-randomposts-categories'])));
    43           if (!intval($cats)) $cats='';
    44           $options['categories'] = $cats;
    45          
    46           update_option('yg_randomposts', $options);
    47         }
    48 ?>
    49         <p>
    50          <label for="yg-randomposts-title">
    51           <?php _e( 'Title:' ); ?>
    52           <input class="widefat" id="yg-randomposts-title" name="yg-randomposts-title" type="text" value="<?php echo htmlspecialchars($options['title']); ?>" />
    53          </label>
    54         </p>
    55         <p>
    56          <label for="yg-randomposts-entries-number"><?php _e('Number of posts to show:'); ?>
    57          <input style="width: 15%; text-align:center; padding: 3px;" id="yg-randomposts-entries-number" name="yg-randomposts-entries-number" type="text" value="<?php echo intval($options['entries-number']); ?>" />
    58         <br /><small><?php _e('(at most 20)'); ?></small>
    59          </label>
    60         </p>
    61         <p>
    62          <label for="yg-randomposts-currentcat"><input class="checkbox" type="checkbox"  id="yg-randomposts-currentcat" name="yg-randomposts-currentcat" <?php echo ($options['currentcat'] == 1) ? 'checked="checked"' : '';?> /> <?php _e('Get posts from current category'); ?></label>
    63         </p>
    64         <p>
    65          <label for="yg-randomposts-categories"><?php _e('Categories to get posts:'); ?>
    66          <input class="widefat" id="yg-randomposts-categories" name="yg-randomposts-categories" type="text" value="<?php echo htmlspecialchars($options['categories']); ?>" />
    67         <br /><small><?php _e('(Category ids separated with a comma)'); ?></small>
    68          </label>
    69         </p>
    70         <input type="hidden" id="yg-randomposts-submit" name="yg-randomposts-submit" value="1" />
    71 
    72 <?php
    73     }
    74     // Register Widget
    75     register_sidebar_widget('Advanced Random Posts', 'yg_randomposts_widget');
    76     register_widget_control('Advanced Random Posts', 'yg_randomposts_options');
     11class yg_adv_random_posts extends WP_Widget {
     12    function yg_adv_random_posts() {
     13     //Load Language
     14     load_plugin_textdomain( 'adv-rnd-posts', false, dirname(plugin_basename(__FILE__)) .  '/lang' );
     15     $widget_ops = array('description' => __('Shows Random Posts. You can customize it easily.', 'adv-rnd-posts') );
     16     //Create widget
     17     $this->WP_Widget('advancedrandomposts', __('Advanced Random Posts', 'adv-rnd-posts'), $widget_ops);
     18    }
     19
     20  function widget($args, $instance) {
     21            extract($args, EXTR_SKIP);
     22            echo $before_widget;
     23            $title = empty($instance['title']) ? __('Random Posts', 'adv-rnd-posts') : apply_filters('widget_title', $instance['title']);
     24            $parameters = array(
     25              'title' => $title,
     26                'limit' => (int) $instance['show-num'],
     27                'excerpt' => (int) $instance['excerpt-length'],
     28                'actcat' => (bool) $instance['actcat'],
     29                'cats' => esc_attr($instance['cats']),
     30                'cusfield' => esc_attr($instance['cus-field']),
     31                'w' => (int) $instance['width'],
     32                'h' => (int) $instance['height'],
     33                'firstimage' => (bool) $instance['firstimage'],
     34                'atimage' =>(bool) $instance['atimage'],
     35                'defimage' => esc_url($instance['defimage'])
     36            );
     37
     38            if ( !empty( $title ) ) {
     39            echo $before_title . $title . $after_title;
     40            };
     41        //print random posts
     42                yg_randomposts($parameters);
     43            echo $after_widget;
     44  } //end of widget
     45   
     46    //Update widget options
     47  function update($new_instance, $old_instance) {
     48
     49        $instance = $old_instance;
     50        //get old variables
     51        $instance['title'] = esc_attr($new_instance['title']);
     52        $instance['show-num'] = (int) abs($new_instance['show-num']);
     53        if ($instance['show-num'] > 20) $instance['show-num'] = 20;
     54        $instance['excerpt-length'] = (int) abs($new_instance['excerpt-length']);
     55        $instance['cats'] = esc_attr($new_instance['cats']);
     56        $instance['actcat'] = $new_instance['actcat'] ? 1 : 0;
     57      $instance['cus-field'] = esc_attr($new_instance['cus-field']);
     58        $instance['width'] = esc_attr($new_instance['width']);
     59        $instance['height'] = esc_attr($new_instance['height']);
     60        $instance['firstimage'] = $new_instance['first-image'] ? 1 : 0;
     61        $instance['atimage'] = $new_instance['atimage'] ? 1 : 0;
     62        $instance['defimage'] = esc_url($new_instance['def-image']);
     63        return $instance;
     64  } //end of update
     65   
     66    //Widget options form
     67  function form($instance) {
     68        $instance = wp_parse_args( (array) $instance, array( 'title' => __('Advanced Random Posts','adv-rnd-posts'), 'show-num' => 10, 'excerpt-length' => 0, 'actcat' => 0, 'cats' => '', 'cus-field' => '', 'width' => '', 'height' => '', 'firstimage' => 0, 'atimage' => 0,'defimage'=>'' ) );
     69       
     70        $title = esc_attr($instance['title']);
     71        $show_num = (int) $instance['show-num'];
     72        $excerpt_length = (int) $instance['excerpt-length'];
     73        $cats = esc_attr($instance['cats']);
     74        $actcat = (bool) $instance['actcat'];
     75        $cus_field = esc_attr($instance['cus-field']);
     76        $width = esc_attr($instance['width']);
     77        $height = esc_attr($instance['height']);
     78        $firstimage = (bool) $instance['firstimage'];
     79        $atimage = (bool) $instance['atimage'];
     80        $defimage = esc_url($instance['defimage']);
     81
     82        ?>
     83        <p>
     84           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:');?>
     85            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
     86           </label>
     87        </p>
     88        <p>
     89           <label for="<?php echo $this->get_field_id('show-num'); ?>"><?php _e('Number of posts to show:');?>
     90          <input id="<?php echo $this->get_field_id('show-num'); ?>" name="<?php echo $this->get_field_name('show-num'); ?>" type="text" value="<?php echo $show_num; ?>" size ="3" /><br />
     91            <small><?php _e('(at most 20)','adv-rnd-posts'); ?></small>
     92          </label>
     93      </p>
     94        <p>
     95          <label for="<?php echo $this->get_field_id('excerpt-length'); ?>"><?php _e('Excerpt length (letters):', 'adv-rnd-posts');?>
     96          <input id="<?php echo $this->get_field_id('excerpt-length'); ?>" name="<?php echo $this->get_field_name('excerpt-length'); ?>" type="text" value="<?php echo $excerpt_length; ?>" size ="3" /><br />
     97            <small>(<?php _e('0 - Don\'t show excerpt', 'adv-rnd-posts');?>)</small>
     98          </label>
     99      </p>
     100        <p>
     101          <label for="<?php echo $this->get_field_id('cus-field'); ?>"><?php _e('Thumbnail Custom Field Name:', 'adv-rnd-posts');?>
     102          <input id="<?php echo $this->get_field_id('cus-field'); ?>" name="<?php echo $this->get_field_name('cus-field'); ?>" type="text" value="<?php echo $cus_field; ?>" size ="20" />
     103          </label><br />
     104          <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'adv-rnd-posts');?> <input id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="text" value="<?php echo $width; ?>" size ="3" /></label>px<br />
     105            <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'adv-rnd-posts');?> <input id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" type="text" value="<?php echo $height; ?>" size ="3" /></label>px
     106      </p>
     107        <p>
     108            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('first-image'); ?>" name="<?php echo $this->get_field_name('first-image'); ?>"<?php checked( $firstimage ); ?> />
     109            <label for="<?php echo $this->get_field_id('first-image'); ?>"><?php _e('Get first image of post', 'adv-rnd-posts');?></label>
     110        </p>
     111        <p>
     112            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('atimage'); ?>" name="<?php echo $this->get_field_name('atimage'); ?>"<?php checked( $atimage ); ?> />
     113            <label for="<?php echo $this->get_field_id('atimage'); ?>"><?php _e('Get first attached image of post', 'adv-rnd-posts');?></label>
     114        </p>
     115      <p>
     116          <label for="<?php echo $this->get_field_id('def-image'); ?>"><?php _e('Default image:', 'adv-rnd-posts');?>
     117          <input class="widefat" id="<?php echo $this->get_field_id('def-image'); ?>" name="<?php echo $this->get_field_name('def-image'); ?>" type="text" value="<?php echo $defimage; ?>" /><br />
     118            <small>(<?php _e('if there is no thumbnail, use this', 'adv-rnd-posts');?></small>
     119          </label>
     120      </p> 
     121      <p>
     122          <label for="<?php echo $this->get_field_id('cats'); ?>"><?php _e('Categories:', 'adv-rnd-posts');?>
     123          <input class="widefat" id="<?php echo $this->get_field_id('cats'); ?>" name="<?php echo $this->get_field_name('cats'); ?>" type="text" value="<?php echo $cats; ?>" /><br />
     124            <small>(<?php _e('Category IDs, separated by commas.', 'adv-rnd-posts');?>)</small>
     125          </label>
     126      </p>
     127        <p>
     128            <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('actcat'); ?>" name="<?php echo $this->get_field_name('actcat'); ?>"<?php checked( $actcat ); ?> />
     129            <label for="<?php echo $this->get_field_id('actcat'); ?>"> <?php _e('Get posts from current category', 'adv-rnd-posts');?></label>
     130        </p>
     131   <?php
     132  } //end of form
    77133}
    78134
    79 function yg_randomposts($args = '') {
    80     global $wpdb;
    81     $defaults = array(
    82         'limit' => 10, 'cats' => '', 'currentcat' => 0
    83     );
     135add_action( 'widgets_init', create_function('', 'return register_widget("yg_adv_random_posts");') );
     136//Register Widget
     137
     138 // Show random posts function
     139 function yg_randomposts($args = '') {
     140  global $wpdb;
     141    $defaults = array('limit' => 10, 'excerpt' => 0, 'actcat' => 0, 'cats'=>'', 'cusfield' =>'', 'w' => 48, 'h' => 48, 'firstimage' => 0, 'atimage' => 0, 'defimage' => '');
    84142    $args = wp_parse_args( $args, $defaults );
    85143    extract($args);
    86144   
    87     $limit = intval($limit);
    88     $cats = str_replace(" ", "", $cats);
     145    $limit = (int) abs($limit);
     146    $firstimage = (bool) $firstimage;
     147    $atimage = (bool) $atimage;
     148    $defimage = esc_url($defimage);
     149    $w = (int) $w;
     150    $h = (int) $h;
     151   
     152    $excerptlength = (int) abs($excerpt);
     153    $excerpt = '';
     154    $cats = str_replace(" ", "", esc_attr($cats));
    89155    if (($limit < 1 ) || ($limit > 20)) $limit = 10;
    90     if (($currentcat) && (is_category())) {
     156   
     157    if (($actcat) && (is_category())) {
    91158     $cats = get_query_var('cat');
    92159    }
    93     if (($currentcat) && (is_single())) {
     160    if (($actcat) && (is_single())) {
    94161     $cats = '';
    95      foreach (get_the_category() as $categories) {
    96        $cats .= $categories->cat_ID.' ';
    97        
     162     foreach (get_the_category() as $cats) {
     163       $cats .= $cats->cat_ID.' ';
    98164     }
    99165     $cats = str_replace(" ", ",", trim($cats));
    100166    }
     167   
    101168    if (!intval($cats)) $cats='';
    102     if ($cats == '') $sql = "SELECT id, post_title, post_name FROM $wpdb->posts WHERE ((post_status='publish') AND (post_type = 'post') AND ($wpdb->posts.post_password = '')) ORDER BY RAND() LIMIT $limit";
    103     else $sql="SELECT $wpdb->posts.id, $wpdb->posts.post_title FROM $wpdb->posts, $wpdb->term_relationships WHERE $wpdb->posts.post_type = 'post'
    104 AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = ''
    105 and $wpdb->posts.id = $wpdb->term_relationships.object_id and $wpdb->term_relationships.term_taxonomy_id in ($cats)
    106 GROUP BY $wpdb->posts.id ORDER BY rand(), $wpdb->posts.post_date desc LIMIT $limit";
    107     $randomposts = $wpdb->get_results($sql);
     169    $query = "cat=$cats&showposts=$limit&orderby=rand";
     170    $rnd_posts = get_posts($query); //get posts by random
    108171    $postlist = '';
    109     foreach ($randomposts as $post) {
    110       $post_title = htmlspecialchars(stripslashes($post->post_title));
    111       $postlist .= "<li><a href=\"" . get_permalink($post->id) . "\" title=\"". $post_title ."\">" . $post_title ."</a></li>\n";
    112 
     172    foreach ($rnd_posts as $post) {
     173          $post_title = htmlspecialchars(stripslashes($post->post_title));
     174            if ($excerptlength) {
     175                $excerpt = $post->post_excerpt;
     176                if ( '' == $excerpt ) {
     177                    $text = $post->post_content;
     178                    $text = strip_shortcodes( $text );
     179                    $text = str_replace(']]>', ']]&gt;', $text);
     180                    $text = strip_tags($text);
     181                    $excerpt_length = 100;
     182                    $words = explode(' ', $text, $excerpt_length + 1);
     183                    if (count($words) > $excerpt_length) {
     184                        array_pop($words);
     185                        $text = implode(' ', $words);
     186                    }
     187                    $excerpt = $text;
     188                }
     189               
     190              if(strlen($excerpt) > $excerptlength) {
     191                 $excerpt = mb_substr($excerpt, 0, $excerptlength) . '...';
     192                }
     193                $excerpt = ': ' . $excerpt;
     194            }
     195            $image = '';
     196            $img = '';
     197            if ($cusfield) {
     198             $cusfield = esc_attr($cusfield);
     199             $img = get_post_meta($post->ID, $cusfield, true);
     200            }
     201
     202             if (!$img && $firstimage) {
     203               $match_count = preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $match_array, PREG_PATTERN_ORDER);     
     204               $img = $match_array[1][0];
     205             }
     206           if (!$img && $atimage)
     207               $img = yg_get_firstimage($post->ID);
     208                 
     209             if (!$img && $defimage)
     210               $img = $defimage;
     211                 
     212             if ($img)
     213              $image = '<img src="' . $img . '" title="' . $post_title . '" class="random-posts-thumb" width="' . $w . '" height="' . $h . '" />';
     214
     215               
     216      $postlist .= "<li><a href=\"" . get_permalink($post->ID) . "\" title=\"". $post_title ."\" alt=\"". $post_title ."\">$image" . $post_title ."</a>$excerpt</li>\n";
    113217    }
     218        echo '<ul class="advanced-random-posts">';     
    114219    echo $postlist;
    115 
     220        echo '</ul>';
    116221 }
    117222
    118 add_action('plugins_loaded', 'yg_randomposts_init');
     223    function yg_get_firstimage($post_id='', $size='thumbnail') {
     224     $id = (int) $post_id;
     225     $args = array(
     226      'post_type' => 'attachment',
     227      'post_mime_type' => 'image',
     228      'numberposts' => 1,
     229      'order' => 'ASC',
     230      'orderby' => 'menu_order ID',
     231      'post_status' => null,
     232      'post_parent' => $id
     233     );
     234     $attachments = get_posts($args);
     235     if ($attachments) {
     236       $img = wp_get_attachment_image_src($attachments[0]->ID, $size);
     237       return $img[0];
     238     }else{
     239       return '';
     240     }
     241    }
     242 
    119243?>
  • advanced-random-posts/trunk/readme.txt

    r71632 r149570  
    33Tags: Random, Posts, Random Posts, Category
    44Donate link: http://www.yakupgovler.com/?p=416
    5 Requires at least: 2.3
    6 Tested up to: 2.6.3
    7 Stable tag: 1.1
     5Requires at least: 2.8
     6Tested up to: 2.8.4
     7Stable tag: 2.0
    88
    9 Display random posts from selected categories or current category or all posts.
     9Display random posts from selected categories or current category or all posts with thumbnail images (optional).
    1010
    1111== Description ==
    12 Please, UPDATE v1.0 to v1.1 immediately. It had a bug but i fixed it.
    13 Advanced Random Posts Widget displays your posts by selecting randomly. It gets posts from selected categories or current category or all posts. When your visitors are at home, it gets posts from all posts or selected category. If you set 'Get posts from current category', when visitors see single post, widget lists posts in the same category of single post or when visitors click a category link, it gets posts from current category.
     12Advanced Random Posts Widget displays your posts by selecting randomly with thumbnail images (optional). It gets posts from selected categories or current category or all posts. When your visitors are at home, it gets posts from all posts or selected category. If you set 'Get posts from current category', when visitors see single post, widget lists posts in the same category of single post or when visitors click a category link, it gets posts from current category.
     13
     14Notice: Version 2.0 requires at least 2.8.
    1415
    1516== Installation ==
    1617
    1718= Installation =
    18 1. Make sure you are running WordPress version 2.3 or better. It won't work with older versions.
     191. Make sure you are running WordPress version 2.8 or better. It won't work with older versions. If you have older versions, please download v1.1
    19202. Download the zip file and extract the contents.
    20 3. Upload the 'advanced-random-posts' folder or 'adv-random-posts.php' to your plugins directory (wp-content/plugins/).
     213. Upload the 'advanced-random-posts' folder (wp-content/plugins/).
    21224. Activate the plugin through the 'plugins' page in WP.
    22235. See 'Design'->'Widgets' to place it on your sidebar. Set the settings.
     
    2829
    2930= I want to display only the posts in two categories. =
    30 You have to write their category's ids -seperated with a comma- to 'Categories to get posts' textbox.
     31You have to write their category's ids -seperated with a comma- to 'Categories' textbox.
    3132
    3233= I don't use Widgets. How can use this widget? =
     
    4849How many posts to display
    4950
     51= Excerpt length (letters) =
     52You know that
     53
     54= Thumbnail Custom Field Name =
     55If you want to display the thumbnail of your posts via a custom field, write its name.
     56
     57= Height - Width =
     58Images size.
     59
     60= Get first image of post =
     61If you don't want to use custom field, plugin will get first image from your post content.
     62
     63= Get first attached image of post =
     64Plugin gets first attached image of post.
     65
     66= Default image =
     67If post has no image, plugin display this image. Ex: http://www.yakupgovler.com/default-image.png
     68
     69
     70Notice: If you use three options, plugin uses custom field image firstly. If the post has no custom field, it gets first image from content. At last it gets first attached image. I suggest not to use "Get first image of post" for performance. It queries much more.
     71
     72= Categories =
     73Plugin gets posts in these categories. (Category IDs, separated by commas.)
     74
     75
    5076= Get posts from current category: =
    51 Posts will be get from current category (single post's categories or current category).
     77Posts will be get from current category (single post's category or current category).
    5278
    53 = Categories to get posts: =
    54 Which categories posts are in.
    5579
    56 == Version history ==
    57 = Version 1.1 =
    58 *Bug fixed, showing the same post id.
     80== Changelog ==
     81
     82= 2.0 =
     83* Adding thumbnail image support
     84* Rewriten by using WordPress 2.8 Widget Class API
     85
     86= 1.1 =
     87* Bug fixed, showing the same post id.
     88
    5989= Version 1.0 =
    6090* Initial release version.
Note: See TracChangeset for help on using the changeset viewer.