Plugin Directory

Changeset 1920720


Ignore:
Timestamp:
08/07/2018 02:53:00 AM (8 years ago)
Author:
doddo
Message:

Add exclude_regex attr

Location:
gallery-from-regex-matches/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gallery-from-regex-matches/trunk/gallery-from-regex-matches.php

    r1802609 r1920720  
    22Plugin Name: Gallery From Regex Matches
    33Plugin URI: https://wordpress.org/plugins/gallery-from-regex-matches/
    4 Version: 0.6.0
     4Version: 0.6.1
    55License: GPL2
    66Description: Maintain a gallery by including all published images in your library which match a regex (in name, title or description).
     
    1212defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1313
    14 add_shortcode( 'gallery_from_regex_matches', 'gallery_from_regex_matches_shortcode_callback');
     14add_shortcode( 'gallery_from_regex_matches', 'Gallery_From_Regex_Matches::shortcode_callback');
    1515
    1616
    1717/**
    18  * Callback from the gallery_from_regex_matches shortcode. calls gallery_shortcode end echos the output to create a gallery.
    1918 *
    20  * @since 0.6.0 Added the $limit argument.
    21  * @since 0.5.1 Added the $encoding argument.
    22  * @since 0.0.1
     19 * @author petter
    2320 *
    24  * @param array $attr {
    25  *     Attributes of the gallery shortcode.
    26  *
    27  *     @type string       $regex       Regular expression to match against all published attachents, matches gets added to gallery.
    28  *     @type string       $exclude_ids A comma-separated list of IDs of attachments filter out if matched by the regex.
    29  *     @type string       $encoding    If the regex field is encoded (to escape special characters), this value specifies appropriate encoding.
    30  *     @type integer      $limit       Limit the amount of pictures to include
    31  *     @see gallery_shortcode for the rest of the valid $attr values,
    32  * }
    33  **/
    34 function gallery_from_regex_matches_shortcode_callback($attr) {
    35     global $wpdb;
    36     $ids = Array();
    37     $excludeIdsTerm = '';
    38     $limit = empty($attr['limit']) ? 10000 : $attr['limit']; 
    39     // check if there are ids to exclude
    40 
    41     // if type is missing, then set it to "rectangular"
    42     if (empty($attr['type']))
    43         $attr['type'] = 'rectangular';
    44 
    45     if (!empty($attr['regex'])){
    46         $regex = $attr['regex'];
    47 
    48         // Handle the cases in which the regex is encoded.
    49         if (!empty($attr['encoding'])){
    50             if ($attr['encoding'] === 'html'){
    51                 $regex = html_entity_decode($regex, ENT_QUOTES, 'UTF-8');
    52             } elseif ($attr['encoding'] === 'base64') {
    53                 $regex = base64_decode($regex);
     21 */
     22class Gallery_From_Regex_Matches {
     23    /**
     24     * Decode a string if $encoding is set to valid type.
     25     *
     26     * @param string $encoding  defines how the string should be decoded. Valid types are 'html' or 'base64'
     27     * @param string $string_to_decode  the string to decode
     28     * 
     29     */
     30    private static function decode_string($encoding, $string_to_decode){
     31        switch($encoding){
     32            case "html":
     33                return html_entity_decode($string_to_decode, ENT_QUOTES, 'UTF-8');
     34            case "base64":
     35                return base64_decode($string_to_decode);
     36            default:
     37                return $string_to_decode;
     38        }
     39    }
     40   
     41    /**
     42     * Callback from the gallery_from_regex_matches shortcode. calls gallery_shortcode end echos the output to create a gallery.
     43     *
     44     * @since 0.6.1 Added the $exclude_regex argument.
     45     * @since 0.6.0 Added the $limit argument.
     46     * @since 0.5.1 Added the $encoding argument.
     47     * @since 0.0.1
     48     *
     49     * @param array $attr {
     50     *     Attributes of the gallery shortcode.
     51     *
     52     *     @type string       $regex         Regular expression to match against all published attachents, matches gets added to gallery.
     53     *     @type string       $exclude_ids   A comma-separated list of IDs of attachments filter out if matched by the regex.
     54     *     @type string       $exclude_regex Regular expression to exclude from matches.
     55     *     @type string       $encoding      If the regex or exclude_regex fields are encoded, this value specifies appropriate encoding.
     56     *     @type integer      $limit         Limit the amount of pictures to include
     57     *     @see gallery_shortcode for the rest of the valid $attr values,
     58     * }
     59     **/
     60    public static function shortcode_callback($attr) {
     61        global $wpdb;
     62        $ids = Array();
     63        $exclude_ids_term = '';
     64        $encoding = empty($attr['encoding']) ? null : $attr['encoding'];
     65       
     66        $exclude_regex = empty($attr['exclude_regex'])
     67            ? ''
     68            : Gallery_From_Regex_Matches::decode_string($encoding, $attr['exclude_regex']);
     69        $regex = empty($attr['regex'])
     70            ? ''
     71            : Gallery_From_Regex_Matches::decode_string($encoding, $attr['regex']);
     72       
     73        $limit = empty($attr['limit']) ? 10000 : $attr['limit'];
     74   
     75        if (empty($attr['type']))
     76            $attr['type'] = 'rectangular';
     77   
     78        if ($regex != ''){
     79                   
     80            // previous versions of plugin used pcre to search the ids,
     81            // and those are enclosed like this '/<regex>/<flags>',
     82            // these needs to be stripped for backwards-compatiblity.
     83            // Technically other flags could've been used also...
     84            $regex = preg_replace('|^/(.*)/[imsxe]*$|', "$1", $regex);
     85           
     86            if (!empty($attr['exclude_ids'])){
     87                $valid_exclude_ids = array_filter( explode(',', $attr['exclude_ids']), 'is_numeric');
     88                if (!empty($valid_exclude_ids))
     89                    $exclude_ids_term = 'AND c.ID not in (' . join(",", $valid_exclude_ids ) . ')';
    5490            }
     91           
     92            $query = $wpdb->prepare("
     93                SELECT c.ID FROM $wpdb->posts c
     94                LEFT JOIN $wpdb->posts p
     95                ON
     96                    (c.post_parent = p.ID)
     97                WHERE c.post_type = 'attachment'
     98                AND p.post_status = 'publish'
     99                $exclude_ids_term
     100                AND c.post_mime_type IN
     101                    ('image/jpeg', 'image/gif', 'image/png',
     102                    'image/bmp', 'image/tiff', 'image/x-icon')
     103                AND CONCAT(c.post_excerpt, c.post_title, c.post_content) REGEXP  %s
     104                AND (
     105                        %s = ''
     106                        OR CONCAT(c.post_excerpt, c.post_title, c.post_content) NOT REGEXP  %s
     107                    )
     108                ORDER BY c.post_date DESC
     109                LIMIT %d
     110            ", array($regex, $exclude_regex, $exclude_regex, $limit));
     111               
     112            $ids = $wpdb->get_results( $query , ARRAY_N );
    55113        }
    56        
    57         // previous versions of plugin used pcre to search the ids,
    58         // and those are enclosed like this '/<regex>/<flags>',
    59         // these needs to be stripped for backwards-compatiblity.
    60         // Technically other flags could've been used also...
    61         $regex = preg_replace('|^/(.*)/[imsxe]*$|', "$1", $regex);
     114   
     115        if (!empty($ids))
     116            $attr['ids'] = array_column($ids, 0);
     117   
     118        // unset this so that $attr looks like something the media_shotcode might expect.
     119        unset($attr['regex']);
     120        unset($attr['exclude_ids']);
     121        unset($attr['exclude_regex']);
     122        unset($attr['encoding']);
     123        unset($attr['limit']);
    62124           
    63         if (!empty($attr['ids'])){
    64             // handle that later
    65         }
    66                
    67         if (!empty($attr['exclude_ids'])){
    68             $validExcludeIds = array_filter( explode(',', $attr['exclude_ids']), 'is_numeric');
    69             if (!empty($validExcludeIds))
    70                 $excludeIdsTerm = 'AND c.ID not in (' . join(",", $validExcludeIds ) . ')';
    71         }
    72 
    73         $query = $wpdb->prepare("
    74             SELECT c.ID from $wpdb->posts c left join $wpdb->posts p
    75             ON
    76                 (c.post_parent = p.ID)
    77             WHERE c.post_type = 'attachment'
    78             AND p.post_status = 'publish'
    79             $excludeIdsTerm
    80             AND c.post_mime_type IN
    81                 ('image/jpeg', 'image/gif', 'image/png',
    82                 'image/bmp', 'image/tiff', 'image/x-icon')
    83             AND CONCAT(c.post_excerpt, c.post_title, c.post_content) REGEXP  %s
    84 
    85             ORDER BY c.post_date DESC
    86             LIMIT %d
    87            
    88         ", array($regex, $limit));
    89        
    90         $ids = $wpdb->get_results( $query , ARRAY_N );
    91        
    92        
     125        echo (gallery_shortcode($attr));
    93126    }
    94 
    95     if (!empty($ids))
    96         $attr['ids'] = array_column($ids, 0);
    97 
    98     // unset this so that $attr looks like something the media_shotcode might expect.
    99     unset($attr['regex']);
    100     unset($attr['exclude_ids']);
    101     unset($attr['encoding']);
    102     unset($attr['limit']);
    103        
    104     echo (gallery_shortcode($attr));
    105127}
    106128?>
  • gallery-from-regex-matches/trunk/readme.txt

    r1802604 r1920720  
    44Requires at least: 4.9.1
    55Requires PHP: 7.0
    6 Tested up to: 4.9.1
     6Tested up to: 4.9.8
    77Stable tag: 0.6.0
    88License: GPLv2 or later
     
    1313== Description ==
    1414
    15 A really simple plugin which adds a shortcode: `[gallery_from_regex_matches]` in which a regex should be specified. It will then look for matches in the descriptions and titles of all published images in your library, and create a gallery from the matches it finds.
     15A plugin which adds a shortcode: `[gallery_from_regex_matches]` in which a regex attribute should be specified. It will then look for matches in the descriptions and titles of all published images in your library, and create a gallery from the matches it finds.
    1616
    1717* Use for example like this `[gallery_from_regex_matches regex="lillill" exclude_ids="764"]`, and it will generate a gallery of all your pictures of "Lillill", except the one which has ID 764.
    1818* Sometimes it might be advicable to limit the amount of matches. that can be achieved by adding `limit` to the shortcode. `[gallery_from_regex_matches regex="lillill" exclude_ids="764" limit="10"]`.
     19* Sometimes you want to make a gallery of "cats" but not "catsup", but you don't want to add [word boundary](https://dev.mysql.com/doc/refman/5.7/en/regexp.html) `[[:>:]]`, in this case you can `exclude_regex=catsup`. Then it could look something along the line of `[gallery_from_regex_matches regex="cats" exclude_regex="catsup"]`.
    1920* The same params (except for `ids`) as for the `[media]` shortcode can be used (as this plugin calls that shortcodes callback function (`gallery_shortcode()`) to generate the gallery), so if you want to specify a particular type (style) of gallery from for example your cat pictures , this might do the trick: `[gallery_from_regex_matches regex="(cat|kitten)" type="square"]`.
    2021* The regular expressions follows the [MySQL Regexp syntax](https://dev.mysql.com/doc/refman/5.7/en/regexp.html)
     
    4849```
    4950
     51To encode a string to base64, it can be done from the terminal like this:
     52```
     53% echo -n "catsup" | base64
     54Y2F0c3Vw
     55% echo -n Y2F0c3Vw | base64 --decode
     56catsup%
     57```
    5058
    5159== Installation ==
     
    5563
    5664== Changelog ==
     65
     66= 0.6.1 =
     67* Added `exclude_regex` shortcode parameter.
    5768
    5869= 0.6.0 =
Note: See TracChangeset for help on using the changeset viewer.