Plugin Directory

Changeset 1802320


Ignore:
Timestamp:
01/13/2018 12:16:53 PM (8 years ago)
Author:
doddo
Message:

Add possibility of decoding encoded regexes

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

Legend:

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

    r1800844 r1802320  
    22Plugin Name: Gallery From Regex Matches
    33Plugin URI: https://wordpress.org/plugins/gallery-from-regex-matches/
    4 Version: 0.5
     4Version: 0.5.1
    55License: GPL2
    66Description: Maintain a gallery by including all published images in your library which match a regex (in name, title or description).
     
    1717/**
    1818 * Callback from the gallery_from_regex_matches shortcode. calls gallery_shortcode end echos the output to create a gallery.
     19 *
     20 * @since 0.5.1 Added the $encoding argument.
    1921 * @since 0.0.1
    2022 *
     
    2426 *     @type string       $regex       Regular expression to match against all published attachents, matches gets added to gallery.
    2527 *     @type string       $exclude_ids A comma-separated list of IDs of attachments filter out if matched by the regex.
     28 *     @type string       $encoding    If the regex field is encoded (to escape special characters), this value specifies appropriate encoding.
    2629 *     @see gallery_shortcode for the rest of the valid $attr values,
    2730 * }
     
    3942
    4043    if (!empty($attr['regex'])){
     44        $regex = $attr['regex'];
     45
     46        // Handle the cases in which the regex is encoded.
     47        if (!empty($attr['encoding'])){
     48            if ($attr['encoding'] === 'html'){
     49                $regex = html_entity_decode($regex, ENT_QUOTES, 'UTF-8');
     50            } elseif ($attr['encoding'] === 'base64') {
     51                $regex = base64_decode($regex);
     52            }
     53        }
     54
    4155        if (!empty($attr['ids'])){
    4256            // handle that later
     
    5266            $search_fields = Array($image->post_name, $image->post_excerpt, $image->post_title);
    5367           
    54             if (preg_grep($attr['regex'], $search_fields)
    55                     and !in_array($image->ID, $exclude_ids)){
     68            if (preg_grep($regex, $search_fields) and !in_array($image->ID, $exclude_ids)){
    5669                array_push($ids, $image->ID);
    5770            }
     
    6578    unset($attr['regex']);
    6679    unset($attr['exclude_ids']);
     80    unset($attr['encoding']);
    6781       
    6882    echo (gallery_shortcode($attr));
  • gallery-from-regex-matches/trunk/readme.txt

    r1801309 r1802320  
    55Requires PHP: 7.0
    66Tested up to: 4.9.1
    7 Stable tag: 0.5
     7Stable tag: 0.5.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1717* Use for example like this `[gallery_from_regex_matches regex="/lillill/i" exclude_ids="764"]`, and it will generate a gallery of all your pictures of "Lillill", except the one which has ID 764.
    1818* 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)/i" type="square"]`.
     19* The regular expressions follows the [PHP pcre pattern syntax](http://php.net/manual/en/reference.pcre.pattern.syntax.php)
     20* Some characters will per default not work inside of the shortcode, see the "Caveats" section.
    1921* There are no bells and whistles for this plugin but it could potentionally be added in the futre.
    2022
     23== Caveats ==
     24
     25Since the regex used by the plugin is specified as an attribute value inside of a shortcode, and the [Shortcode Api](https://codex.wordpress.org/Shortcode_API) disallows the use of certain characters which could be a part of a valid, useful regex, there are some things to consider.
     26
     27
     28>  Attribute values must never contain the following characters:
     29>  * Square braces: [ ]
     30>  * Quotes: " '
     31>
     32> [ ... ]
     33> The recommended method of escaping special characters in shortcode attributes is HTML encoding. Most importantly, any user input appearing in a shortcode attribute must be escaped or stripped of special characters.
     34 
     35* if in the regex you need to macth a character set `[a-z]`, then (following the shortcode Api documentation guidline), they need to be encoded. There are two encodings which the plugin accepts: "html" and "base64"
     36* Currently there is no way to encode these things with help of the plugin, although it is planned for a future release, so the regexes must be encoded manually for now'
     37For example, for regex `/w[0u][0u]t/i` can be expressed either:
     38   
     391. With HTML encoding: `[gallery_from_regex_matches regex="/w[0u][0u]t/i" encoding="html"]`
     402. With Base64 encoding: `[gallery_from_regex_matches regex="L3dbMHVdWzB1XXQvaQ==" encoding="base64"]`
    2141
    2242== Installation ==
     
    2747== Changelog ==
    2848
     49= 0.5.1 =
     50* Add possibility of decoding encoded regexes, so that special characters which would collide with the Shortcode Api can still be used in them.
     51
    2952= 0.5 =
    3053* Search also title field
Note: See TracChangeset for help on using the changeset viewer.