Gallery Custom Links - Version 1.0.6

Version Description

  • Fix: Now works with the most tenacious lightboxes.
  • Update: The way the HTML was modified to make sure it is compliant.
Download this release

Release Info

Developer TigrouMeow
Plugin Icon 128x128 Gallery Custom Links
Version 1.0.6
Comparing to
See all releases

Code changes from version 1.0.4 to 1.0.6

Files changed (3) hide show
  1. gallery_custom_links.php +2 -2
  2. mgcl_core.php +87 -12
  3. readme.txt +8 -1
gallery_custom_links.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Gallery Custom Links
4
  Plugin URI: https://meowapps.com
5
  Description: Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
6
- Version: 1.0.4
7
  Author: Jordy Meow
8
  Author URI: https://meowapps.com
9
  Text Domain: gallery-custom-links
@@ -23,7 +23,7 @@ if ( class_exists( 'Meow_Gallery_Custom_Links' ) ) {
23
  }
24
 
25
  global $mgcl_version;
26
- $mgcl_version = '1.0.4';
27
 
28
  // Admin
29
  // include "mgcl_admin.php";
3
  Plugin Name: Gallery Custom Links
4
  Plugin URI: https://meowapps.com
5
  Description: Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
6
+ Version: 1.0.6
7
  Author: Jordy Meow
8
  Author URI: https://meowapps.com
9
  Text Domain: gallery-custom-links
23
  }
24
 
25
  global $mgcl_version;
26
+ $mgcl_version = '1.0.6';
27
 
28
  // Admin
29
  // include "mgcl_admin.php";
mgcl_core.php CHANGED
@@ -7,12 +7,23 @@ use DiDom\Element;
7
 
8
  class Meow_Gallery_Custom_Links
9
  {
 
 
10
  public function __construct() {
11
- add_action( 'init', array( $this, 'init' ) );
12
- add_action( 'wp_head', array( $this, 'wp_head' ) );
13
- add_action( 'wp_footer', array( $this, 'wp_footer' ) );
14
- add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
15
- add_filter( 'attachment_fields_to_save', array( $this, 'apply_filter_attachment_fields_to_save' ), 10 , 2 );
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
  function attachment_fields_to_edit( $fields, $post ) {
@@ -46,7 +57,10 @@ class Meow_Gallery_Custom_Links
46
  return $post;
47
  }
48
 
49
- function wp_head() {
 
 
 
50
  ob_start( array( $this, "linkify" ) );
51
  }
52
 
@@ -100,12 +114,17 @@ class Meow_Gallery_Custom_Links
100
  do {
101
  if ( $potentialLinkNode->tag === 'a' ) {
102
  $potentialLinkNode->attr( 'href', $url );
103
- $potentialLinkNode->attr( 'class', 'custom-link' );
 
 
104
  $potentialLinkNode->attr( 'onclick', 'event.stopPropagation()' );
105
  $potentialLinkNode->attr( 'target', $target );
106
  return true;
107
  }
108
- $potentialLinkNode = $potentialLinkNode->parent();
 
 
 
109
  }
110
  while ( $potentialLinkNode && $maxDepth-- >= 0 );
111
 
@@ -114,7 +133,7 @@ class Meow_Gallery_Custom_Links
114
  $parent = $parent->parent();
115
  $a = new Element('a');
116
  $a->attr( 'href', $url );
117
- $a->attr( 'class', 'custom-link' );
118
  $a->attr( 'onclick', 'event.stopPropagation()' );
119
  $a->attr( 'target', $target );
120
  $a->appendChild( $parent->children() );
@@ -138,19 +157,75 @@ class Meow_Gallery_Custom_Links
138
  return $buffer;
139
  $html = new Document();
140
  $html->preserveWhiteSpace();
141
- $html->loadHtml( $buffer, 0 );
142
  $hasChanges = false;
143
  $classes = apply_filters( 'gallery_custom_links_classes', array( '.gallery', '.wp-block-gallery' ) );
144
  foreach ( $classes as $class ) {
145
  foreach ( $html->find( $class . ' img' ) as $element )
146
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
147
  }
148
- return $hasChanges ? $html : $buffer;
149
  }
150
 
151
- function wp_footer() {
 
 
152
  @ob_end_flush();
153
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
 
156
  ?>
7
 
8
  class Meow_Gallery_Custom_Links
9
  {
10
+ public $isEnabled = true;
11
+
12
  public function __construct() {
13
+
14
+ if ( is_admin() ) {
15
+ add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
16
+ add_filter( 'attachment_fields_to_save', array( $this, 'apply_filter_attachment_fields_to_save' ), 10 , 2 );
17
+ }
18
+ else {
19
+ // Extra support
20
+ add_action( 'init', array( $this, 'init' ) );
21
+
22
+ // Processing
23
+ add_action( 'init', array( $this, 'start' ) );
24
+ add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
25
+ add_action( 'shutdown', array( $this, 'shutdown' ) );
26
+ }
27
  }
28
 
29
  function attachment_fields_to_edit( $fields, $post ) {
57
  return $post;
58
  }
59
 
60
+ function start() {
61
+ $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
62
+ if ( !$this->isEnabled )
63
+ return;
64
  ob_start( array( $this, "linkify" ) );
65
  }
66
 
114
  do {
115
  if ( $potentialLinkNode->tag === 'a' ) {
116
  $potentialLinkNode->attr( 'href', $url );
117
+ $class = $potentialLinkNode->attr( 'class' );
118
+ $class = empty( $class ) ? 'custom-link no-lightbox' : ( $class . ' custom-link no-lightbox' );
119
+ $potentialLinkNode->attr( 'class', $class );
120
  $potentialLinkNode->attr( 'onclick', 'event.stopPropagation()' );
121
  $potentialLinkNode->attr( 'target', $target );
122
  return true;
123
  }
124
+ if ( method_exists( $potentialLinkNode, 'parent' ) )
125
+ $potentialLinkNode = $potentialLinkNode->parent();
126
+ else
127
+ break;
128
  }
129
  while ( $potentialLinkNode && $maxDepth-- >= 0 );
130
 
133
  $parent = $parent->parent();
134
  $a = new Element('a');
135
  $a->attr( 'href', $url );
136
+ $a->attr( 'class', 'custom-link no-lightbox' );
137
  $a->attr( 'onclick', 'event.stopPropagation()' );
138
  $a->attr( 'target', $target );
139
  $a->appendChild( $parent->children() );
157
  return $buffer;
158
  $html = new Document();
159
  $html->preserveWhiteSpace();
160
+ $html->loadHtml( $buffer, LIBXML_HTML_NOIMPLIED );
161
  $hasChanges = false;
162
  $classes = apply_filters( 'gallery_custom_links_classes', array( '.gallery', '.wp-block-gallery' ) );
163
  foreach ( $classes as $class ) {
164
  foreach ( $html->find( $class . ' img' ) as $element )
165
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
166
  }
167
+ return $hasChanges ? $html->html() : $buffer;
168
  }
169
 
170
+ function shutdown() {
171
+ if ( !$this->isEnabled )
172
+ return;
173
  @ob_end_flush();
174
  }
175
+
176
+ function unlink_lightboxes_script() {
177
+ ?>
178
+ <script>
179
+ // Used by Gallery Custom Links to handle tenacious Lightboxes
180
+ jQuery(document).ready(function () {
181
+
182
+ function mgclInit() {
183
+ if (jQuery.fn.off) {
184
+ jQuery('.no-lightbox, .no-lightbox img').off('click'); // jQuery 1.7+
185
+ }
186
+ else {
187
+ jQuery('.no-lightbox, .no-lightbox img').unbind('click'); // < jQuery 1.7
188
+ }
189
+ jQuery('a.no-lightbox').click(mgclOnClick);
190
+
191
+ if (jQuery.fn.off) {
192
+ jQuery('a.set-target').off('click'); // jQuery 1.7+
193
+ }
194
+ else {
195
+ jQuery('a.set-target').unbind('click'); // < jQuery 1.7
196
+ }
197
+ jQuery('a.set-target').click(mgclOnClick);
198
+ }
199
+
200
+ function mgclOnClick() {
201
+ if (!this.target || this.target == '' || this.target == '_self')
202
+ window.location = this.href;
203
+ else
204
+ window.open(this.href,this.target);
205
+ return false;
206
+ }
207
+
208
+ // From WP Gallery Custom Links
209
+ // Reduce the number of conflicting lightboxes
210
+ function mgclAddLoadEvent(func) {
211
+ var oldOnload = window.onload;
212
+ if (typeof window.onload != 'function') {
213
+ window.onload = func;
214
+ } else {
215
+ window.onload = function() {
216
+ oldOnload();
217
+ func();
218
+ }
219
+ }
220
+ }
221
+
222
+ mgclAddLoadEvent(mgclInit);
223
+ mgclInit();
224
+
225
+ });
226
+ </script>
227
+ <?php
228
+ }
229
  }
230
 
231
  ?>
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: TigrouMeow
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 4.9
5
  Tested up to: 5.0
6
- Stable tag: 1.0.4
7
 
8
  Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
9
 
@@ -36,6 +36,13 @@ Replace all the files. Nothing else to do.
36
 
37
  == Changelog ==
38
 
 
 
 
 
 
 
 
39
  = 1.0.4 =
40
  * Fix: Support images embedded in a few layer of tags before the link tag.
41
  * Add: Added a class on the a-tag, for the ones who would like to add some styling to linked images. The Meow Lightbox is already handling this, by avoiding showing a zoom cursor when hovering images.
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 4.9
5
  Tested up to: 5.0
6
+ Stable tag: 1.0.6
7
 
8
  Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
9
 
36
 
37
  == Changelog ==
38
 
39
+ = 1.0.6 =
40
+ * Fix: Now works with the most tenacious lightboxes.
41
+ * Update: The way the HTML was modified to make sure it is compliant.
42
+
43
+ = 1.0.5 =
44
+ * Add: Filter to let the user enables/disables the plugin depending on conditions. Check the official page to know more about this: [Gallery Custom Links](https://meowapps.com/plugin/gallery-custom-links/).
45
+
46
  = 1.0.4 =
47
  * Fix: Support images embedded in a few layer of tags before the link tag.
48
  * Add: Added a class on the a-tag, for the ones who would like to add some styling to linked images. The Meow Lightbox is already handling this, by avoiding showing a zoom cursor when hovering images.