Plugin Directory

Changeset 1939482


Ignore:
Timestamp:
09/11/2018 06:52:18 PM (8 years ago)
Author:
Binternet
Message:

added ‘random’ option

Location:
categories-multiple-images/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • categories-multiple-images/trunk/categories-multiple-images.php

    r1789918 r1939482  
    44 * Plugin Name: Categories Multiple Images
    55 * Description: Categories Multiple Images Plugin allows you to add multiple images to categories or any other custom taxonomy.
    6  * Version: 1.0.3
     6 * Version: 1.1
    77 * Author: Lior Broshi, Binternet
    88 * Author URI: http://www.binternet.co.il
     
    1111 */
    1212 
    13  /*  Copyright 2015  Lior Broshi (binternet)  (email : lior@binternet.co.il)
     13 /*  Copyright 2018  Lior Broshi (binternet)  (email : lior@binternet.co.il)
    1414
    1515    This program is free software; you can redistribute it and/or modify
     
    179179    }
    180180
    181        
    182181    /**
    183182     * Categories_Multiple_Images::options_page()
     
    313312
    314313    }
    315    
     314
     315    /*
     316    |--------------------------------------------------------------------------
     317    | Statics
     318    |--------------------------------------------------------------------------
     319    */
     320
    316321    /**
    317322     * Categories_Multiple_Images::get_image()
    318323     * Returns the image(s)
    319324     * @param mixed $term_id                Term ID
    320      * @param mixed $image_number           Specific Image Number
     325     * @param mixed $image_number           Specific Image Number -OR- 'random' which returns a random image
    321326     * @param string $size                  Image Size [thumb, full etc...]
    322      * @param bool $return_placeholder      Use image placeholder incase no image was found
     327     * @param bool $return_placeholder      Use image placeholder in case no image was found
    323328     * @return
    324329     */
     
    330335       
    331336        if ( empty( $term_id ) ) {
    332            
     337            # No specific term ID was provided, try fetching from WP_Query
    333338            if ( is_category() ) {
    334339                $term_id = get_query_var('cat');
     
    343348                return FALSE;
    344349            }
    345            
    346350        }
    347        
     351
     352        # Placeholder image
    348353        $placeholder_image = self::get_placeholder_image();
    349354       
    350355        # Load the image(s)
    351356        if ( empty( $image_number ) ) {
    352            
    353             # Get them all
    354             $total_images = self::validate( get_option( 'cmi_total_images' ), 'total_images' );
    355             $images = array();
    356            
    357             for ( $i = 1, $n = $total_images; $i <= $n; $i++ ) {
    358                
    359                 # Get that image
    360                 $image = get_option( "cmi_taxonomy_image{$i}_{$term_id}" );
    361                
    362                 if ( empty( $image ) ) {
    363                    
    364                     # Wops, no such image, placeholder?
    365                     $image = ( TRUE == $return_placeholder ) ? $placeholder_image : $image;
    366                    
    367                 }
    368                 elseif ( ! empty( $size ) ) {
    369                    
    370                     # Get specific size
    371                     $thumb_id = self::get_attachment_id_from_url( $image );
    372 
    373                     if ( ! empty( $thumb_id ) ) {
    374                         # Gotcha
    375                         $attachment = wp_get_attachment_image_src( $thumb_id, $size );
    376                         $image = $attachment[0];
    377                     }
    378                    
    379                 }
    380                
    381                 # Add to result array
    382                 $images[] = $image;
    383             }
    384            
    385             return $images;
     357            # No specific image was given, return them all
     358            return self::get_all_images( $term_id, $return_placeholder );
     359        }
     360        elseif ( strtolower( $image_number ) == 'rand' OR strtolower( $image_number ) == 'random' ) {
     361            # A Random image was requested
     362            $all_images = self::get_all_images( $term_id, $return_placeholder );
     363            return $all_images[ array_rand( $all_images ) ];
    386364        }
    387365        else {
    388            
    389             # Get specific image
    390            
     366            # Get a specific image
    391367            $image = get_option( "cmi_taxonomy_image{$image_number}_{$term_id}" );
    392368           
    393369            if ( empty( $image ) ) {
    394                
    395370                # Wops, no such image, placeholder?
    396371                $image = ( TRUE == $return_placeholder ) ? $placeholder_image : $image;
    397                
    398372            }
    399373            elseif ( ! empty( $size ) ) {
    400                
    401374                # Get specific size
    402375                $thumb_id = self::get_attachment_id_from_url( $image );
    403 
    404376                if ( ! empty( $thumb_id ) ) {
    405377                    # Gotcha
     
    407379                    $image = $attachment[0];
    408380                }
    409                
    410381            }
    411382           
    412383            return $image;
    413384        }
    414        
    415385    }
    416386   
     
    423393    /**
    424394     * Categories_Multiple_Images::validate()
    425      *
     395     * Validation of misc stuff such as array, 'total_images' value
    426396     * @param mixed $value
    427397     * @param mixed $what
     
    431401       
    432402        if ( empty( $what ) ) {
    433             return;
     403            return NULL;
    434404        }
    435405       
     
    459429       
    460430    }
     431
     432    /**
     433     * Returns all images for a given term ID
     434     * @param integer $term_id
     435     * @return array
     436     */
     437    static function get_all_images( $term_id, $return_placeholder = TRUE ) {
     438
     439        $placeholder_image = self::get_placeholder_image();
     440        $total_images = self::validate( get_option( 'cmi_total_images' ), 'total_images' );
     441        $images = array();
     442
     443        for ( $i = 1, $n = $total_images; $i <= $n; $i++ ) {
     444
     445            # Get that image
     446            $image = get_option( "cmi_taxonomy_image{$i}_{$term_id}" );
     447
     448            if ( empty( $image ) ) {
     449                # Wops, no such image, placeholder?
     450                if ( TRUE == $return_placeholder ) {
     451                    $image = $placeholder_image;
     452                }
     453                else {
     454                    # Skip this image since its empty and no placeholder should be used
     455                    continue;
     456                }
     457            }
     458            elseif ( ! empty( $size ) ) {
     459
     460                # Get specific size
     461                $thumb_id = self::get_attachment_id_from_url( $image );
     462
     463                if ( ! empty( $thumb_id ) ) {
     464                    # Gotcha
     465                    $attachment = wp_get_attachment_image_src( $thumb_id, $size );
     466                    $image = $attachment[0];
     467                }
     468
     469            }
     470
     471            # Add to result array
     472            $images[] = $image;
     473        }
     474
     475        # Return all images
     476        return $images;
     477    }
    461478   
    462479}
  • categories-multiple-images/trunk/readme.txt

    r1789921 r1939482  
    33332. Activate the plugin through the 'Plugins' menu in WordPress
    34343. Place `<?php Categories_Multiple_Images::get_image( term_id, image_number, image_size, use_placeholder ); ?> ` in your templates
     35   Values are:
     36   - term_id - Category ID (or any other term_id)
     37   - image_number - Desired image number (starting from 1), you can also use 'random' and a random image from that category will be returned
     38   - image_size - desired image size, defaults to 'full'
     39   - use_placeholder - should a placeholder should be returned if the image does not exists
    3540
    3641== Changelog ==
     42
     43= 1.1 =
     44* Added a new option to pass 'random' as an image number and get a random image for a given category ID
     45* Some documentation and refactoring
    3746
    3847= 1.0.3 =
Note: See TracChangeset for help on using the changeset viewer.