WP Gallery Custom Links - Version 1.11

Version Description

  • By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow)
  • Tested with WordPress 4.4
Download this release

Release Info

Developer fourlightsweb
Plugin Icon wp plugin WP Gallery Custom Links
Version 1.11
Comparing to
See all releases

Code changes from version 1.10.5 to 1.11

Files changed (2) hide show
  1. readme.txt +17 -4
  2. wp-gallery-custom-links.php +38 -19
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
- === WP Gallery Custom Links ===
2
- Contributors: fourlightsweb
3
  Donate link: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/#donate
4
  Tags: gallery links, gallery link, gallery
5
  Requires at least: 3.3.1
6
- Tested up to: 4.3.1
7
- Stable tag: 1.10.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -12,6 +12,10 @@ Specify custom links for WordPress gallery images (instead of attachment or file
12
 
13
  == Description ==
14
 
 
 
 
 
15
  = Overview =
16
 
17
  If you've ever had a WordPress gallery of staff, product, or other images and needed
@@ -40,6 +44,7 @@ will be removed to allow them to function as regular links.
40
  * Use `[gallery open_all_in_new_window="true"]` and `[gallery open_all_in_same_window="true"]` to open all images in an entire gallery in a new window/the same window, respectively.
41
  * Use `[gallery preserve_click_events="true"]` to keep Lightbox or other onClick events on all custom-linked images in an entire gallery.
42
  * Use `[gallery remove_links="true"]` to remove links on all images in an entire gallery.
 
43
 
44
  = Hooks =
45
 
@@ -147,6 +152,10 @@ accomplish this, which is outside the scope of this plugin.
147
 
148
  == Changelog ==
149
 
 
 
 
 
150
  = 1.10.5 =
151
  * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel.
152
 
@@ -263,6 +272,10 @@ directly attached to the post.
263
 
264
  == Upgrade Notice ==
265
 
 
 
 
 
266
  = 1.10.5 =
267
  * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel.
268
 
1
+ === WP Gallery Custom Links ===
2
+ Contributors: fourlightsweb, johnogg
3
  Donate link: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/#donate
4
  Tags: gallery links, gallery link, gallery
5
  Requires at least: 3.3.1
6
+ Tested up to: 4.4.0
7
+ Stable tag: 1.11
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
12
 
13
  == Description ==
14
 
15
+ = NOTE: This plugin is no longer supported by the developer =
16
+ Sorry, but I no longer have the time to support this plugin. If anyone is interested in taking it
17
+ over please contact me.
18
+
19
  = Overview =
20
 
21
  If you've ever had a WordPress gallery of staff, product, or other images and needed
44
  * Use `[gallery open_all_in_new_window="true"]` and `[gallery open_all_in_same_window="true"]` to open all images in an entire gallery in a new window/the same window, respectively.
45
  * Use `[gallery preserve_click_events="true"]` to keep Lightbox or other onClick events on all custom-linked images in an entire gallery.
46
  * Use `[gallery remove_links="true"]` to remove links on all images in an entire gallery.
47
+ * Use `[gallery rel="nofollow"]` to set a rel attribute with value "nofollow" on all links in an entire gallery.
48
 
49
  = Hooks =
50
 
152
 
153
  == Changelog ==
154
 
155
+ = 1.11 =
156
+ * By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow)
157
+ * Tested with WordPress 4.4
158
+
159
  = 1.10.5 =
160
  * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel.
161
 
272
 
273
  == Upgrade Notice ==
274
 
275
+ = 1.11 =
276
+ * By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow)
277
+ * Tested with WordPress 4.4
278
+
279
  = 1.10.5 =
280
  * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel.
281
 
wp-gallery-custom-links.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: WP Gallery Custom Links
4
  Plugin URI: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/
5
  Text Domain: wp-gallery-custom-links
6
  Description: Specify custom links for WordPress gallery images (instead of attachment or file only).
7
- Version: 1.10.5
8
  Author: Four Lights Web Development
9
  Author URI: http://www.fourlightsweb.com
10
  License: GPL2
@@ -223,6 +223,7 @@ class WPGalleryCustomLinks {
223
  foreach ( $attachment_ids as $attachment_id ) {
224
  $link = '';
225
  $target = '';
 
226
  $preserve_click = '';
227
  $remove_link = false;
228
  $additional_css_classes = '';
@@ -253,6 +254,11 @@ class WPGalleryCustomLinks {
253
  // This should allow _blank by default lady to set her gallery to _self at once
254
  $target = '_self';
255
  }
 
 
 
 
 
256
  // See if we have additional css classes for this attachment image
257
  $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_additional_css_classes', true );
258
  if( $attachment_meta ) {
@@ -274,14 +280,14 @@ class WPGalleryCustomLinks {
274
  $remove_link = true;
275
  }
276
 
277
- if( $link != '' || $target != '' || $remove_link || $additional_css_classes != '' ) {
278
  // Replace the attachment href
279
  $needle = get_attachment_link( $attachment_id );
280
- $output = self::replace_link( $needle, $link, $target, $preserve_click, $remove_link, $additional_css_classes, $output );
281
 
282
  // Replace the file href
283
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, '' );
284
- $output = self::replace_link( $needle, $link, $target, $preserve_click, $remove_link, $additional_css_classes, $output );
285
  // Also, in case of jetpack photon with tiled galleries...
286
  if( function_exists( 'jetpack_photon_url' ) ) {
287
  // The CDN url currently is generated with "$subdomain = rand( 0, 2 );",
@@ -297,7 +303,7 @@ class WPGalleryCustomLinks {
297
  $needle_part_1 = preg_replace( '/\d+$/U', '', $needle_parts[0] );
298
  $needle_part_2 = '.wp.com' . $needle_parts[1];
299
  $needle_reassembled = $needle_part_1 . $j . $needle_part_2;
300
- $output = self::replace_link( $needle_reassembled, $link, $target, $preserve_click, $remove_link, $additional_css_classes, $output );
301
  }
302
  }
303
  }
@@ -310,7 +316,7 @@ class WPGalleryCustomLinks {
310
  if( is_array( $attachment_sizes ) && count( $attachment_sizes ) > 0 ) {
311
  foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
312
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size );
313
- $output = self::replace_link( $needle, $link, $target, $preserve_click, $remove_link, $additional_css_classes, $output );
314
  } // End of foreach attachment size
315
  } // End if we have attachment sizes
316
  } // End if we have attachment metadata (specifically sizes)
@@ -320,7 +326,7 @@ class WPGalleryCustomLinks {
320
  return $output;
321
  } // End function apply_filter_post_gallery()
322
 
323
- private static function replace_link( $default_link, $custom_link, $target, $preserve_click, $remove_link, $additional_css_classes, $output ) {
324
  // Build the regex for matching/replacing
325
  $needle = preg_quote( $default_link );
326
  $needle = str_replace( '/', '\/', $needle );
@@ -376,7 +382,7 @@ class WPGalleryCustomLinks {
376
  // Custom Target
377
  if( $target != '' && ! $remove_link ) {
378
  // Replace the link target
379
- $output = self::add_target( $default_link, $target, $output );
380
 
381
  // Add a class to the link so we can manipulate it with
382
  // javascript later (only if we're opening it in a new window -
@@ -385,6 +391,12 @@ class WPGalleryCustomLinks {
385
  $classes_to_add .= 'set-target ';
386
  }
387
  }
 
 
 
 
 
 
388
 
389
  // Pre-custom link class
390
  if( $custom_link != '' && ! $remove_link ) { // Same criteria as "custom link" block below
@@ -418,6 +430,9 @@ class WPGalleryCustomLinks {
418
  // Clean up our needle for regexing
419
  $needle = preg_quote( $needle );
420
  $needle = str_replace( '/', '\/', $needle );
 
 
 
421
 
422
  // Add a class to the link so we can manipulate it with
423
  // javascript later
@@ -435,24 +450,28 @@ class WPGalleryCustomLinks {
435
  return $output;
436
  } // End function add_class()
437
 
438
- private static function add_target( $needle, $target, $output ) {
439
  // Clean up our needle for regexing
440
  $needle = preg_quote( $needle );
441
  $needle = str_replace( '/', '\/', $needle );
 
 
 
 
442
 
443
- // Add a target to the link (or overwrite what's there)
444
- if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
445
- // href comes before target
446
- $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*>)/U', '$1'.$target.'$2', $output );
447
- } elseif( preg_match( '/<a[^>]*target\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) {
448
- // href comes after target
449
- $output = preg_replace( '/(<a[^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/U', '$1'.$target.'$2', $output );
450
  } else {
451
- // No previous target
452
- $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 target="'.$target.'"$2', $output );
453
  } // End if we have a class on the a tag or not
454
 
455
  return $output;
456
- } // End function add_target()
457
 
458
  } // End class WPGalleryCustomLinks
4
  Plugin URI: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/
5
  Text Domain: wp-gallery-custom-links
6
  Description: Specify custom links for WordPress gallery images (instead of attachment or file only).
7
+ Version: 1.11
8
  Author: Four Lights Web Development
9
  Author URI: http://www.fourlightsweb.com
10
  License: GPL2
223
  foreach ( $attachment_ids as $attachment_id ) {
224
  $link = '';
225
  $target = '';
226
+ $rel = '';
227
  $preserve_click = '';
228
  $remove_link = false;
229
  $additional_css_classes = '';
254
  // This should allow _blank by default lady to set her gallery to _self at once
255
  $target = '_self';
256
  }
257
+ // See if we have a rel for this link
258
+ if( isset( $attr['rel'] ) ) {
259
+ // Add a rel property to this image link with the specified value
260
+ $rel = $attr['rel']; // Note: this is escaped before being dropped in
261
+ }
262
  // See if we have additional css classes for this attachment image
263
  $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_additional_css_classes', true );
264
  if( $attachment_meta ) {
280
  $remove_link = true;
281
  }
282
 
283
+ if( $link != '' || $target != '' || $rel != '' || $remove_link || $additional_css_classes != '' ) {
284
  // Replace the attachment href
285
  $needle = get_attachment_link( $attachment_id );
286
+ $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output );
287
 
288
  // Replace the file href
289
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, '' );
290
+ $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output );
291
  // Also, in case of jetpack photon with tiled galleries...
292
  if( function_exists( 'jetpack_photon_url' ) ) {
293
  // The CDN url currently is generated with "$subdomain = rand( 0, 2 );",
303
  $needle_part_1 = preg_replace( '/\d+$/U', '', $needle_parts[0] );
304
  $needle_part_2 = '.wp.com' . $needle_parts[1];
305
  $needle_reassembled = $needle_part_1 . $j . $needle_part_2;
306
+ $output = self::replace_link( $needle_reassembled, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output );
307
  }
308
  }
309
  }
316
  if( is_array( $attachment_sizes ) && count( $attachment_sizes ) > 0 ) {
317
  foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
318
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size );
319
+ $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output );
320
  } // End of foreach attachment size
321
  } // End if we have attachment sizes
322
  } // End if we have attachment metadata (specifically sizes)
326
  return $output;
327
  } // End function apply_filter_post_gallery()
328
 
329
+ private static function replace_link( $default_link, $custom_link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ) {
330
  // Build the regex for matching/replacing
331
  $needle = preg_quote( $default_link );
332
  $needle = str_replace( '/', '\/', $needle );
382
  // Custom Target
383
  if( $target != '' && ! $remove_link ) {
384
  // Replace the link target
385
+ $output = self::add_property( $default_link, 'target', $target, $output );
386
 
387
  // Add a class to the link so we can manipulate it with
388
  // javascript later (only if we're opening it in a new window -
391
  $classes_to_add .= 'set-target ';
392
  }
393
  }
394
+
395
+ // Custom Rel
396
+ if( $rel != '' && ! $remove_link ) {
397
+ // Replace the link rel
398
+ $output = self::add_property( $default_link, 'rel', $rel, $output );
399
+ }
400
 
401
  // Pre-custom link class
402
  if( $custom_link != '' && ! $remove_link ) { // Same criteria as "custom link" block below
430
  // Clean up our needle for regexing
431
  $needle = preg_quote( $needle );
432
  $needle = str_replace( '/', '\/', $needle );
433
+
434
+ // Clean property values a bit
435
+ $class = esc_attr( $class );
436
 
437
  // Add a class to the link so we can manipulate it with
438
  // javascript later
450
  return $output;
451
  } // End function add_class()
452
 
453
+ private static function add_property( $needle, $property_name, $property_value, $output ) {
454
  // Clean up our needle for regexing
455
  $needle = preg_quote( $needle );
456
  $needle = str_replace( '/', '\/', $needle );
457
+
458
+ // Clean property values a bit
459
+ $property_name = esc_attr( $property_name );
460
+ $property_value = esc_attr( $property_value );
461
 
462
+ // Add a property to the link (or overwrite what's there)
463
+ if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*'.$property_name.'\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
464
+ // href comes before property
465
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*'.$property_name.'\s*=\s*["\'])[^"\']*(["\'][^>]*>)/U', '$1'.$property_value.'$2', $output );
466
+ } elseif( preg_match( '/<a[^>]*'.$property_name.'\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) {
467
+ // href comes after property
468
+ $output = preg_replace( '/(<a[^>]*'.$property_name.'\s*=\s*["\'])[^"\']*(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/U', '$1'.$property_value.'$2', $output );
469
  } else {
470
+ // No previous property
471
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 '.$property_name.'="'.$property_value.'"$2', $output );
472
  } // End if we have a class on the a tag or not
473
 
474
  return $output;
475
+ } // End function add_property()
476
 
477
  } // End class WPGalleryCustomLinks