Changeset 1920720
- Timestamp:
- 08/07/2018 02:53:00 AM (8 years ago)
- Location:
- gallery-from-regex-matches/trunk
- Files:
-
- 2 edited
-
gallery-from-regex-matches.php (modified) (2 diffs)
-
readme.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gallery-from-regex-matches/trunk/gallery-from-regex-matches.php
r1802609 r1920720 2 2 Plugin Name: Gallery From Regex Matches 3 3 Plugin URI: https://wordpress.org/plugins/gallery-from-regex-matches/ 4 Version: 0.6. 04 Version: 0.6.1 5 5 License: GPL2 6 6 Description: Maintain a gallery by including all published images in your library which match a regex (in name, title or description). … … 12 12 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 13 13 14 add_shortcode( 'gallery_from_regex_matches', ' gallery_from_regex_matches_shortcode_callback');14 add_shortcode( 'gallery_from_regex_matches', 'Gallery_From_Regex_Matches::shortcode_callback'); 15 15 16 16 17 17 /** 18 * Callback from the gallery_from_regex_matches shortcode. calls gallery_shortcode end echos the output to create a gallery.19 18 * 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 23 20 * 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 */ 22 class 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 ) . ')'; 54 90 } 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 ); 55 113 } 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']); 62 124 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)); 93 126 } 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));105 127 } 106 128 ?> -
gallery-from-regex-matches/trunk/readme.txt
r1802604 r1920720 4 4 Requires at least: 4.9.1 5 5 Requires PHP: 7.0 6 Tested up to: 4.9. 16 Tested up to: 4.9.8 7 7 Stable tag: 0.6.0 8 8 License: GPLv2 or later … … 13 13 == Description == 14 14 15 A really simple plugin which adds a shortcode: `[gallery_from_regex_matches]` in which a regexshould 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.15 A 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. 16 16 17 17 * 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. 18 18 * 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"]`. 19 20 * 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"]`. 20 21 * The regular expressions follows the [MySQL Regexp syntax](https://dev.mysql.com/doc/refman/5.7/en/regexp.html) … … 48 49 ``` 49 50 51 To encode a string to base64, it can be done from the terminal like this: 52 ``` 53 % echo -n "catsup" | base64 54 Y2F0c3Vw 55 % echo -n Y2F0c3Vw | base64 --decode 56 catsup% 57 ``` 50 58 51 59 == Installation == … … 55 63 56 64 == Changelog == 65 66 = 0.6.1 = 67 * Added `exclude_regex` shortcode parameter. 57 68 58 69 = 0.6.0 =
Note: See TracChangeset
for help on using the changeset viewer.