WP Gallery Custom Links - Version 1.12

Version Description

  • Fixed issue with links not showing when using WP_Tiles in later versions of Wordpress.
  • Tested with WordPress 4.7.2
Download this release

Release Info

Developer johnogg
Plugin Icon wp plugin WP Gallery Custom Links
Version 1.12
Comparing to
See all releases

Code changes from version 1.11 to 1.12

Files changed (2) hide show
  1. readme.txt +25 -26
  2. wp-gallery-custom-links.php +54 -55
readme.txt CHANGED
@@ -1,10 +1,9 @@
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,15 +11,11 @@ Specify custom links for WordPress gallery images (instead of attachment or file
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
22
  to link them to other pages but couldn't, this plugin is for you!
23
-
24
  This plugin adds a "Gallery Link URL" field when editing images. If the image
25
  is included in a gallery, the "Gallery Link URL" value will be used as the link on
26
  the image instead of the raw file or the attachment post. There are also several
@@ -64,8 +59,8 @@ function my_gallery_link_url_filter( $link, $attachment_id, $post_id ) { return
64
 
65
  = #1) Will this plugin work with my theme's galleries? =
66
 
67
- Possibly. WP Gallery Custom Links plugin was designed for use with
68
- 1) WordPress's [gallery] shortcode and 2) images uploaded through the
69
  WordPress media manager. Some themes use these features, and others
70
  have their own proprietary way of saving gallery images and drawing out the gallery.
71
  Provided your theme meets the criteria above, the plugin should work with it. You
@@ -74,14 +69,14 @@ might also want to see #6 below.
74
  = #2) Will this plugin work with NextGen galleries? =
75
 
76
  No, this plugin is not compatible with NextGen galleries. WP Gallery Custom Links was
77
- designed for use with 1) WordPress's [gallery] shortcode and 2) images uploaded through the
78
  WordPress media manager. NextGen galleries uses its own [nggallery] etc. shortcodes
79
  that function outside of the WordPress [gallery] shortcode.
80
 
81
  = #3) When I enable the plugin, the styling on my gallery changes. Why? =
82
 
83
  The way the plugin works requires the gallery generation code to be run twice. This
84
- may result in it being labeled as "#gallery-2" instead of "#gallery-1."
85
  Check your HTML and CSS for these changes and adjust accordingly.
86
 
87
  = #4) I'd like to use the custom link in my own gallery code or in a different custom layout. How can I get the custom link? =
@@ -100,7 +95,7 @@ see #5 in the old version's readme file here: http://plugins.svn.wordpress.org/w
100
 
101
  = #6) When I enable the plugin, nothing in my gallery changes, even though I have custom links set. Why? =
102
 
103
- Thing to try #0.5: make sure your gallery is using the [gallery] shortcode (i.e. you've created the gallery by clicking
104
  the "Add Media" button and then "Create Gallery" on the side of the media pop-up window). If you don't see a [gallery] shortcode
105
  in your content and/or you've created the gallery by using a custom form or shortcode generated by your theme (e.g. "I selected
106
  'Gallery' as a page template option in a dropdown" or "I'm seeing [somethemename_gallery] in my content"),
@@ -123,14 +118,14 @@ the function (assumes the attributes variable passed to the shortcode function i
123
  `$output = apply_filters('post_gallery', '', $attr);
124
  if ( $output != '' )
125
  return $output;`
126
-
127
  You may want to see http://wordpress.org/support/topic/wont-work-syntax-error for an example of adding this code.
128
-
129
  This thing to try is a bit on the programmy side, so if you're having trouble, my suggestion would be to contact your theme author
130
  and ask that they support the "post_gallery" filter in their gallery shortcode function. This would not only fix it for your theme most thoroughly,
131
  but would also fix it for any future users also using that same theme. Otherwise, any WordPress developer should be able to help you
132
  with the code changes to customize your theme to support the post_gallery filter like WordPress core (not something I consider in the realm of free support, sorry).
133
-
134
  = #7) The custom links are working fine, but I need help changing the formatting/styling on my gallery, such as spacing between images, aligning images, or changing image size. =
135
 
136
  This plugin just changes links, plus a bit of auxiliary functionality to help with changing the links. It
@@ -152,6 +147,10 @@ accomplish this, which is outside the scope of this plugin.
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
@@ -207,7 +206,7 @@ forcing the same window, instead of doing whatever the theme does by default.
207
  * Fixed an issue where items with the same custom link were not having lightbox
208
  removed properly
209
  * Added support for the "ids" attribute added in WP 3.5
210
- * Updated help text for the Gallery Link URL field
211
 
212
  = 1.6.0 =
213
  * By popular demand, added the ability to remove links from individual images
@@ -227,9 +226,9 @@ events ("remove" by default).
227
  set all custom-linked images in a gallery to "preserve" its OnClick events.
228
 
229
  = 1.3.0 =
230
- * Added support for the "ignore_gallery_link_urls" gallery shortcode attribute to
231
  ignore custom links on a gallery and use the normal file/attachment setting.
232
- * Added support for IDs in the "include" gallery shortcode attribute that aren't
233
  directly attached to the post.
234
 
235
  = 1.2.2 =
@@ -327,11 +326,11 @@ forcing the same window, instead of doing whatever the theme does by default.
327
  * Fixed an issue where multiple items with the same custom links were not having lightbox
328
  removed properly
329
  * Added support for the "ids" attribute added in WP 3.5
330
- * Updated help text for the Gallery Link URL field
331
 
332
  = 1.6.0 =
333
  * By popular demand, added the ability to remove links from individual images
334
- or an entire gallery.
335
 
336
  = 1.5.1 =
337
  * Fixed a possible error with an undefined "preserve_click" variable.
@@ -347,9 +346,9 @@ events ("remove" by default).
347
  set all custom-linked images in a gallery to "preserve" its OnClick events.
348
 
349
  = 1.3.0 =
350
- * Added support for the "ignore_gallery_link_urls" gallery shortcode attribute to
351
  ignore custom links on a gallery and use the normal file/attachment setting.
352
- * Added support for IDs in the "include" gallery shortcode attribute that aren't
353
  directly attached to the post.
354
 
355
  = 1.2.2 =
@@ -388,4 +387,4 @@ directly attached to the post.
388
  * Changed priority on post_gallery filter from 10 to 999 to help ensure it runs after anything else
389
 
390
  = 1.0.0 =
391
- * Initial release
1
+ === WP Gallery Custom Links ===
2
+ Contributors: johnogg
 
3
  Tags: gallery links, gallery link, gallery
4
  Requires at least: 3.3.1
5
+ Tested up to: 4.7.2
6
+ Stable tag: 1.12
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
11
 
12
  == Description ==
13
 
 
 
 
 
14
  = Overview =
15
 
16
  If you've ever had a WordPress gallery of staff, product, or other images and needed
17
  to link them to other pages but couldn't, this plugin is for you!
18
+
19
  This plugin adds a "Gallery Link URL" field when editing images. If the image
20
  is included in a gallery, the "Gallery Link URL" value will be used as the link on
21
  the image instead of the raw file or the attachment post. There are also several
59
 
60
  = #1) Will this plugin work with my theme's galleries? =
61
 
62
+ Possibly. WP Gallery Custom Links plugin was designed for use with
63
+ 1) WordPress's [gallery] shortcode and 2) images uploaded through the
64
  WordPress media manager. Some themes use these features, and others
65
  have their own proprietary way of saving gallery images and drawing out the gallery.
66
  Provided your theme meets the criteria above, the plugin should work with it. You
69
  = #2) Will this plugin work with NextGen galleries? =
70
 
71
  No, this plugin is not compatible with NextGen galleries. WP Gallery Custom Links was
72
+ designed for use with 1) WordPress's [gallery] shortcode and 2) images uploaded through the
73
  WordPress media manager. NextGen galleries uses its own [nggallery] etc. shortcodes
74
  that function outside of the WordPress [gallery] shortcode.
75
 
76
  = #3) When I enable the plugin, the styling on my gallery changes. Why? =
77
 
78
  The way the plugin works requires the gallery generation code to be run twice. This
79
+ may result in it being labeled as "#gallery-2" instead of "#gallery-1."
80
  Check your HTML and CSS for these changes and adjust accordingly.
81
 
82
  = #4) I'd like to use the custom link in my own gallery code or in a different custom layout. How can I get the custom link? =
95
 
96
  = #6) When I enable the plugin, nothing in my gallery changes, even though I have custom links set. Why? =
97
 
98
+ Thing to try #0.5: make sure your gallery is using the [gallery] shortcode (i.e. you've created the gallery by clicking
99
  the "Add Media" button and then "Create Gallery" on the side of the media pop-up window). If you don't see a [gallery] shortcode
100
  in your content and/or you've created the gallery by using a custom form or shortcode generated by your theme (e.g. "I selected
101
  'Gallery' as a page template option in a dropdown" or "I'm seeing [somethemename_gallery] in my content"),
118
  `$output = apply_filters('post_gallery', '', $attr);
119
  if ( $output != '' )
120
  return $output;`
121
+
122
  You may want to see http://wordpress.org/support/topic/wont-work-syntax-error for an example of adding this code.
123
+
124
  This thing to try is a bit on the programmy side, so if you're having trouble, my suggestion would be to contact your theme author
125
  and ask that they support the "post_gallery" filter in their gallery shortcode function. This would not only fix it for your theme most thoroughly,
126
  but would also fix it for any future users also using that same theme. Otherwise, any WordPress developer should be able to help you
127
  with the code changes to customize your theme to support the post_gallery filter like WordPress core (not something I consider in the realm of free support, sorry).
128
+
129
  = #7) The custom links are working fine, but I need help changing the formatting/styling on my gallery, such as spacing between images, aligning images, or changing image size. =
130
 
131
  This plugin just changes links, plus a bit of auxiliary functionality to help with changing the links. It
147
 
148
  == Changelog ==
149
 
150
+ = 1.12 =
151
+ * Fixed issue with links not showing when using WP_Tiles in later versions of Wordpress.
152
+ * Tested with WordPress 4.7.2
153
+
154
  = 1.11 =
155
  * By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow)
156
  * Tested with WordPress 4.4
206
  * Fixed an issue where items with the same custom link were not having lightbox
207
  removed properly
208
  * Added support for the "ids" attribute added in WP 3.5
209
+ * Updated help text for the Gallery Link URL field
210
 
211
  = 1.6.0 =
212
  * By popular demand, added the ability to remove links from individual images
226
  set all custom-linked images in a gallery to "preserve" its OnClick events.
227
 
228
  = 1.3.0 =
229
+ * Added support for the "ignore_gallery_link_urls" gallery shortcode attribute to
230
  ignore custom links on a gallery and use the normal file/attachment setting.
231
+ * Added support for IDs in the "include" gallery shortcode attribute that aren't
232
  directly attached to the post.
233
 
234
  = 1.2.2 =
326
  * Fixed an issue where multiple items with the same custom links were not having lightbox
327
  removed properly
328
  * Added support for the "ids" attribute added in WP 3.5
329
+ * Updated help text for the Gallery Link URL field
330
 
331
  = 1.6.0 =
332
  * By popular demand, added the ability to remove links from individual images
333
+ or an entire gallery.
334
 
335
  = 1.5.1 =
336
  * Fixed a possible error with an undefined "preserve_click" variable.
346
  set all custom-linked images in a gallery to "preserve" its OnClick events.
347
 
348
  = 1.3.0 =
349
+ * Added support for the "ignore_gallery_link_urls" gallery shortcode attribute to
350
  ignore custom links on a gallery and use the normal file/attachment setting.
351
+ * Added support for IDs in the "include" gallery shortcode attribute that aren't
352
  directly attached to the post.
353
 
354
  = 1.2.2 =
387
  * Changed priority on post_gallery filter from 10 to 999 to help ensure it runs after anything else
388
 
389
  = 1.0.0 =
390
+ * Initial release
wp-gallery-custom-links.php CHANGED
@@ -1,18 +1,17 @@
1
  <?php
2
  /*
3
  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.11
8
- Author: Four Lights Web Development
9
  Author URI: http://www.fourlightsweb.com
10
  License: GPL2
11
 
12
- Copyright 2014 Four Lights Web Development, LLC. (email : development@fourlightsweb.com)
13
 
14
  This program is free software; you can redistribute it and/or modify
15
- it under the terms of the GNU General Public License, version 2, as
16
  published by the Free Software Foundation.
17
 
18
  This program is distributed in the hope that it will be useful,
@@ -40,24 +39,24 @@ class WPGalleryCustomLinks {
40
  // $GLOBALS['shortcode_tags']['gallery'] ->
41
  // apply_filter('post_gallery') *
42
  // apply_filter('post_gallery') (second call, simply returns output passed in)
43
- // return same content passed in to this second recursive call
44
  // return "filter" $output with replaced links to original $GLOBALS['shortcode_tags']['gallery'] call
45
  private static $first_call = true;
46
  private static $class_name = 'WPGalleryCustomLinks';
47
-
48
  public static function init() {
49
  // Add the filter for editing the custom url field
50
  add_filter( 'attachment_fields_to_edit', array( self::$class_name, 'apply_filter_attachment_fields_to_edit' ), null, 2 );
51
-
52
  // Add the filter for saving the custom url field
53
  add_filter( 'attachment_fields_to_save', array( self::$class_name, 'apply_filter_attachment_fields_to_save' ), null , 2 );
54
-
55
  // Add the filter for when the post_gallery is written out
56
- add_filter( 'post_gallery', array( self::$class_name, 'apply_filter_post_gallery' ), 999, 2 );
57
-
58
  // Require the javascript to disable lightbox
59
  add_action( 'wp_enqueue_scripts', array( self::$class_name, 'do_action_wp_enqueue_scripts' ) );
60
-
61
  // Load translations
62
  load_plugin_textdomain( 'wp-gallery-custom-links', false, basename( dirname( __FILE__ ) ) . '/languages' );
63
  } // End function init()
@@ -66,7 +65,7 @@ class WPGalleryCustomLinks {
66
  // The codex says this is needed to load plugin language files
67
  load_plugin_textdomain( 'wp-gallery-custom-links', false, basename( dirname( __FILE__ ) ) . '/languages' );
68
  }
69
-
70
  public static function do_action_wp_enqueue_scripts() {
71
  wp_enqueue_script(
72
  'wp-gallery-custom-links-js',
@@ -76,10 +75,10 @@ class WPGalleryCustomLinks {
76
  true
77
  );
78
  }
79
-
80
  public static function apply_filter_attachment_fields_to_edit( $form_fields, $post ) {
81
  $help_css = 'z-index:999;display:none;position:absolute;margin-top:-100px;background-color:#ffffe0;text-align:left;border:1px solid #dfdfdf;padding:10px;width:75%;font-weight:normal;border-radius:3px;';
82
-
83
  // Gallery Link URL field
84
  $form_fields['gallery_link_url'] = array(
85
  'label' => __( 'Gallery Link URL', 'wp-gallery-custom-links' ) .
@@ -137,7 +136,7 @@ class WPGalleryCustomLinks {
137
  );
138
  return $form_fields;
139
  } // End function apply_filter_attachment_fields_to_edit()
140
-
141
  public static function apply_filter_attachment_fields_to_save( $post, $attachment ) {
142
  // Save our custom meta fields
143
  if( isset( $attachment['gallery_link_url'] ) ) {
@@ -153,15 +152,15 @@ class WPGalleryCustomLinks {
153
  update_post_meta( $post['ID'], '_gallery_link_additional_css_classes', $attachment['gallery_link_additional_css_classes'] );
154
  }
155
  return $post;
156
- } // End function apply_filter_attachment_fields_to_save()
157
-
158
  public static function apply_filter_post_gallery( $output, $attr ) {
159
  global $post;
160
-
161
  // Determine what our postID for attachments is - either
162
  // from our shortcode attr or from $post->ID. If we don't
163
  // have one from either of those places...something weird
164
- // is going on, so just bail.
165
  if( isset( $attr['id'] ) ) {
166
  $post_id = intval( $attr['id'] );
167
  } else if( $post ) {
@@ -169,7 +168,7 @@ class WPGalleryCustomLinks {
169
  } else {
170
  return ' ';
171
  }
172
-
173
  if( isset( $attr['ignore_gallery_link_urls'] ) && strtolower( trim( $attr['ignore_gallery_link_urls'] ) ) === 'true' ) {
174
  // If the user has passed in a parameter to ignore the custom link
175
  // URLs for this gallery, just skip over this whole plugin and
@@ -195,11 +194,11 @@ class WPGalleryCustomLinks {
195
  if ( isset( $GLOBALS['shortcode_tags'] ) && isset( $GLOBALS['shortcode_tags']['gallery'] ) ) {
196
  $gallery_shortcode_function = $GLOBALS['shortcode_tags']['gallery'];
197
  }
198
-
199
- // Run whatever gallery shortcode function has been set up,
200
  // default, theme-specified or whatever
201
- $output = call_user_func( $gallery_shortcode_function, $attr );
202
-
203
  $attachment_ids = array();
204
  if( isset( $attr['ids'] ) ) {
205
  // WP 3.5+:
@@ -216,10 +215,10 @@ class WPGalleryCustomLinks {
216
  if( isset( $attr['include'] ) ) {
217
  $attachment_ids = array_merge( $attachment_ids, explode( ',', $attr['include'] ) );
218
  }
219
-
220
  // Make sure we don't replace the same one multiple times
221
  $attachment_ids = array_unique( $attachment_ids );
222
-
223
  foreach ( $attachment_ids as $attachment_id ) {
224
  $link = '';
225
  $target = '';
@@ -227,13 +226,13 @@ class WPGalleryCustomLinks {
227
  $preserve_click = '';
228
  $remove_link = false;
229
  $additional_css_classes = '';
230
- $attachment_id = intval( $attachment_id );
231
-
232
  // See if we have a custom url for this attachment image
233
  $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_url', true );
234
  if( $attachment_meta ) {
235
  $link = $attachment_meta;
236
-
237
  // Apply filters to the custom link (e.g. the case of prefixing internal
238
  // links with /en/ or /fr/ etc. for languages on the fly)
239
  $link = apply_filters( 'wpgcl_filter_raw_gallery_link_url', $link, $attachment_id, $post_id );
@@ -274,12 +273,12 @@ class WPGalleryCustomLinks {
274
  // Override the individual setting if the gallery shortcode says to preserve on all
275
  $preserve_click = 'preserve';
276
  }
277
-
278
  // See if we need to remove the link for this image or not
279
  if( strtolower( trim( $link ) ) === '[none]' || ( isset( $attr['remove_links'] ) && strtolower( trim( $attr['remove_links'] ) ) === 'true' ) ) {
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 );
@@ -325,7 +324,7 @@ class WPGalleryCustomLinks {
325
 
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 );
@@ -337,12 +336,12 @@ class WPGalleryCustomLinks {
337
  } else {
338
  $classes_to_add = '';
339
  }
340
-
341
  // Remove Link
342
  if( $remove_link ) {
343
  // Break the output up into two parts: everything before this href
344
- // and everything after. Note: sometimes the image url will
345
- // appear multiple times in the source (e.g. "data-orig-file" in
346
  // jetpack), so use the regex needle to find the href first, then break
347
  // up the link.
348
  $output_parts = preg_replace( $needle, '^^^HREF^^^', $output );
@@ -350,14 +349,14 @@ class WPGalleryCustomLinks {
350
  if( count( $output_parts ) == 2 ) {
351
  $output_part_1 = $output_parts[0];
352
  $output_part_2 = $output_parts[1];
353
-
354
  // Take out the <a> from the end of part 1, from its
355
  // opening angle bracket
356
  $pos = strrpos( $output_part_1, '<' );
357
  if( $pos !== false ) {
358
  $output_part_1 = substr( $output_part_1, 0, $pos );
359
  }
360
-
361
  // And then take out everything up through the first > that comes after that
362
  // (which would be the closing angle bracket of <a>)
363
  $pos = strpos( $output_part_2, '>' );
@@ -366,24 +365,24 @@ class WPGalleryCustomLinks {
366
  // Add in a span where the link used to be, just in case
367
  $output_part_2 = '<span class="no-link'. ( $preserve_click != 'preserve' ? ' no-lightbox' : '' ) . '">' . $output_part_2;
368
  }
369
-
370
  // And then take out the first </a> that comes after that
371
  $pos = strpos( $output_part_2, '</a>' );
372
  if( $pos !== false ) {
373
  // Also close out the span where the link used to be
374
  $output_part_2 = substr( $output_part_2, 0, $pos ) . '</span>' . substr( $output_part_2, $pos+4 );
375
  }
376
-
377
  // And then stitch them back together again, without the link parts
378
  $output = $output_part_1 . $output_part_2;
379
  }
380
  }
381
-
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 -
389
  // don't want to auto-unbind lightbox if it's the same window)
@@ -397,7 +396,7 @@ class WPGalleryCustomLinks {
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
403
  // Add a class to the link so we can manipulate it with
@@ -409,23 +408,23 @@ class WPGalleryCustomLinks {
409
  $classes_to_add .= 'no-lightbox ';
410
  }
411
  } // End if we have a custom link to swap in
412
-
413
  // Add any classes, if needed (saves on some regexes to do it all at once)
414
  if( $classes_to_add != '' ) {
415
  $output = self::add_class( $default_link, trim( $classes_to_add ), $output );
416
  }
417
-
418
  // Custom Link
419
- if( $custom_link != '' && ! $remove_link ) {
420
  // If we found the href to swap out, perform the href replacement,
421
  // and add some javascript to prevent lightboxes from kicking in
422
  $output = preg_replace( $needle, 'href="' . $custom_link . '"', $output );
423
  } // End if we have a custom link to swap in
424
  } // End if we found the attachment to replace in the output
425
-
426
  return $output;
427
  } // End function replace_link()
428
-
429
  private static function add_class( $needle, $class, $output ) {
430
  // Clean up our needle for regexing
431
  $needle = preg_quote( $needle );
@@ -433,7 +432,7 @@ class WPGalleryCustomLinks {
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
439
  if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*class\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
@@ -446,10 +445,10 @@ class WPGalleryCustomLinks {
446
  // No previous class
447
  $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 class="'.$class.'"$2', $output );
448
  } // End if we have a class on the a tag or not
449
-
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 );
@@ -458,7 +457,7 @@ class WPGalleryCustomLinks {
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
@@ -470,8 +469,8 @@ class WPGalleryCustomLinks {
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
1
  <?php
2
  /*
3
  Plugin Name: WP Gallery Custom Links
 
4
  Text Domain: wp-gallery-custom-links
5
  Description: Specify custom links for WordPress gallery images (instead of attachment or file only).
6
+ Version: 1.12
7
+ Author: johnogg
8
  Author URI: http://www.fourlightsweb.com
9
  License: GPL2
10
 
11
+ Copyright 2017 John Griffiths
12
 
13
  This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License, version 2, as
15
  published by the Free Software Foundation.
16
 
17
  This program is distributed in the hope that it will be useful,
39
  // $GLOBALS['shortcode_tags']['gallery'] ->
40
  // apply_filter('post_gallery') *
41
  // apply_filter('post_gallery') (second call, simply returns output passed in)
42
+ // return same content passed in to this second recursive call
43
  // return "filter" $output with replaced links to original $GLOBALS['shortcode_tags']['gallery'] call
44
  private static $first_call = true;
45
  private static $class_name = 'WPGalleryCustomLinks';
46
+
47
  public static function init() {
48
  // Add the filter for editing the custom url field
49
  add_filter( 'attachment_fields_to_edit', array( self::$class_name, 'apply_filter_attachment_fields_to_edit' ), null, 2 );
50
+
51
  // Add the filter for saving the custom url field
52
  add_filter( 'attachment_fields_to_save', array( self::$class_name, 'apply_filter_attachment_fields_to_save' ), null , 2 );
53
+
54
  // Add the filter for when the post_gallery is written out
55
+ add_filter( 'post_gallery', array( self::$class_name, 'apply_filter_post_gallery' ), 9999, 2 );
56
+
57
  // Require the javascript to disable lightbox
58
  add_action( 'wp_enqueue_scripts', array( self::$class_name, 'do_action_wp_enqueue_scripts' ) );
59
+
60
  // Load translations
61
  load_plugin_textdomain( 'wp-gallery-custom-links', false, basename( dirname( __FILE__ ) ) . '/languages' );
62
  } // End function init()
65
  // The codex says this is needed to load plugin language files
66
  load_plugin_textdomain( 'wp-gallery-custom-links', false, basename( dirname( __FILE__ ) ) . '/languages' );
67
  }
68
+
69
  public static function do_action_wp_enqueue_scripts() {
70
  wp_enqueue_script(
71
  'wp-gallery-custom-links-js',
75
  true
76
  );
77
  }
78
+
79
  public static function apply_filter_attachment_fields_to_edit( $form_fields, $post ) {
80
  $help_css = 'z-index:999;display:none;position:absolute;margin-top:-100px;background-color:#ffffe0;text-align:left;border:1px solid #dfdfdf;padding:10px;width:75%;font-weight:normal;border-radius:3px;';
81
+
82
  // Gallery Link URL field
83
  $form_fields['gallery_link_url'] = array(
84
  'label' => __( 'Gallery Link URL', 'wp-gallery-custom-links' ) .
136
  );
137
  return $form_fields;
138
  } // End function apply_filter_attachment_fields_to_edit()
139
+
140
  public static function apply_filter_attachment_fields_to_save( $post, $attachment ) {
141
  // Save our custom meta fields
142
  if( isset( $attachment['gallery_link_url'] ) ) {
152
  update_post_meta( $post['ID'], '_gallery_link_additional_css_classes', $attachment['gallery_link_additional_css_classes'] );
153
  }
154
  return $post;
155
+ } // End function apply_filter_attachment_fields_to_save()
156
+
157
  public static function apply_filter_post_gallery( $output, $attr ) {
158
  global $post;
159
+
160
  // Determine what our postID for attachments is - either
161
  // from our shortcode attr or from $post->ID. If we don't
162
  // have one from either of those places...something weird
163
+ // is going on, so just bail.
164
  if( isset( $attr['id'] ) ) {
165
  $post_id = intval( $attr['id'] );
166
  } else if( $post ) {
168
  } else {
169
  return ' ';
170
  }
171
+
172
  if( isset( $attr['ignore_gallery_link_urls'] ) && strtolower( trim( $attr['ignore_gallery_link_urls'] ) ) === 'true' ) {
173
  // If the user has passed in a parameter to ignore the custom link
174
  // URLs for this gallery, just skip over this whole plugin and
194
  if ( isset( $GLOBALS['shortcode_tags'] ) && isset( $GLOBALS['shortcode_tags']['gallery'] ) ) {
195
  $gallery_shortcode_function = $GLOBALS['shortcode_tags']['gallery'];
196
  }
197
+
198
+ // Run whatever gallery shortcode function has been set up,
199
  // default, theme-specified or whatever
200
+ $output = call_user_func( $gallery_shortcode_function, $attr );
201
+
202
  $attachment_ids = array();
203
  if( isset( $attr['ids'] ) ) {
204
  // WP 3.5+:
215
  if( isset( $attr['include'] ) ) {
216
  $attachment_ids = array_merge( $attachment_ids, explode( ',', $attr['include'] ) );
217
  }
218
+
219
  // Make sure we don't replace the same one multiple times
220
  $attachment_ids = array_unique( $attachment_ids );
221
+
222
  foreach ( $attachment_ids as $attachment_id ) {
223
  $link = '';
224
  $target = '';
226
  $preserve_click = '';
227
  $remove_link = false;
228
  $additional_css_classes = '';
229
+ $attachment_id = intval( $attachment_id );
230
+
231
  // See if we have a custom url for this attachment image
232
  $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_url', true );
233
  if( $attachment_meta ) {
234
  $link = $attachment_meta;
235
+
236
  // Apply filters to the custom link (e.g. the case of prefixing internal
237
  // links with /en/ or /fr/ etc. for languages on the fly)
238
  $link = apply_filters( 'wpgcl_filter_raw_gallery_link_url', $link, $attachment_id, $post_id );
273
  // Override the individual setting if the gallery shortcode says to preserve on all
274
  $preserve_click = 'preserve';
275
  }
276
+
277
  // See if we need to remove the link for this image or not
278
  if( strtolower( trim( $link ) ) === '[none]' || ( isset( $attr['remove_links'] ) && strtolower( trim( $attr['remove_links'] ) ) === 'true' ) ) {
279
  $remove_link = true;
280
  }
281
+
282
  if( $link != '' || $target != '' || $rel != '' || $remove_link || $additional_css_classes != '' ) {
283
  // Replace the attachment href
284
  $needle = get_attachment_link( $attachment_id );
324
 
325
  return $output;
326
  } // End function apply_filter_post_gallery()
327
+
328
  private static function replace_link( $default_link, $custom_link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ) {
329
  // Build the regex for matching/replacing
330
  $needle = preg_quote( $default_link );
336
  } else {
337
  $classes_to_add = '';
338
  }
339
+
340
  // Remove Link
341
  if( $remove_link ) {
342
  // Break the output up into two parts: everything before this href
343
+ // and everything after. Note: sometimes the image url will
344
+ // appear multiple times in the source (e.g. "data-orig-file" in
345
  // jetpack), so use the regex needle to find the href first, then break
346
  // up the link.
347
  $output_parts = preg_replace( $needle, '^^^HREF^^^', $output );
349
  if( count( $output_parts ) == 2 ) {
350
  $output_part_1 = $output_parts[0];
351
  $output_part_2 = $output_parts[1];
352
+
353
  // Take out the <a> from the end of part 1, from its
354
  // opening angle bracket
355
  $pos = strrpos( $output_part_1, '<' );
356
  if( $pos !== false ) {
357
  $output_part_1 = substr( $output_part_1, 0, $pos );
358
  }
359
+
360
  // And then take out everything up through the first > that comes after that
361
  // (which would be the closing angle bracket of <a>)
362
  $pos = strpos( $output_part_2, '>' );
365
  // Add in a span where the link used to be, just in case
366
  $output_part_2 = '<span class="no-link'. ( $preserve_click != 'preserve' ? ' no-lightbox' : '' ) . '">' . $output_part_2;
367
  }
368
+
369
  // And then take out the first </a> that comes after that
370
  $pos = strpos( $output_part_2, '</a>' );
371
  if( $pos !== false ) {
372
  // Also close out the span where the link used to be
373
  $output_part_2 = substr( $output_part_2, 0, $pos ) . '</span>' . substr( $output_part_2, $pos+4 );
374
  }
375
+
376
  // And then stitch them back together again, without the link parts
377
  $output = $output_part_1 . $output_part_2;
378
  }
379
  }
380
+
381
  // Custom Target
382
  if( $target != '' && ! $remove_link ) {
383
  // Replace the link target
384
  $output = self::add_property( $default_link, 'target', $target, $output );
385
+
386
  // Add a class to the link so we can manipulate it with
387
  // javascript later (only if we're opening it in a new window -
388
  // don't want to auto-unbind lightbox if it's the same window)
396
  // Replace the link rel
397
  $output = self::add_property( $default_link, 'rel', $rel, $output );
398
  }
399
+
400
  // Pre-custom link class
401
  if( $custom_link != '' && ! $remove_link ) { // Same criteria as "custom link" block below
402
  // Add a class to the link so we can manipulate it with
408
  $classes_to_add .= 'no-lightbox ';
409
  }
410
  } // End if we have a custom link to swap in
411
+
412
  // Add any classes, if needed (saves on some regexes to do it all at once)
413
  if( $classes_to_add != '' ) {
414
  $output = self::add_class( $default_link, trim( $classes_to_add ), $output );
415
  }
416
+
417
  // Custom Link
418
+ if( $custom_link != '' && ! $remove_link ) {
419
  // If we found the href to swap out, perform the href replacement,
420
  // and add some javascript to prevent lightboxes from kicking in
421
  $output = preg_replace( $needle, 'href="' . $custom_link . '"', $output );
422
  } // End if we have a custom link to swap in
423
  } // End if we found the attachment to replace in the output
424
+
425
  return $output;
426
  } // End function replace_link()
427
+
428
  private static function add_class( $needle, $class, $output ) {
429
  // Clean up our needle for regexing
430
  $needle = preg_quote( $needle );
432
 
433
  // Clean property values a bit
434
  $class = esc_attr( $class );
435
+
436
  // Add a class to the link so we can manipulate it with
437
  // javascript later
438
  if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*class\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
445
  // No previous class
446
  $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 class="'.$class.'"$2', $output );
447
  } // End if we have a class on the a tag or not
448
+
449
  return $output;
450
  } // End function add_class()
451
+
452
  private static function add_property( $needle, $property_name, $property_value, $output ) {
453
  // Clean up our needle for regexing
454
  $needle = preg_quote( $needle );
457
  // Clean property values a bit
458
  $property_name = esc_attr( $property_name );
459
  $property_value = esc_attr( $property_value );
460
+
461
  // Add a property to the link (or overwrite what's there)
462
  if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*'.$property_name.'\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
463
  // href comes before property
469
  // No previous property
470
  $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 '.$property_name.'="'.$property_value.'"$2', $output );
471
  } // End if we have a class on the a tag or not
472
+
473
  return $output;
474
  } // End function add_property()
475
+
476
+ } // End class WPGalleryCustomLinks