Gallery Custom Links - Version 1.0.4

Version Description

  • Fix: Support images embedded in a few layer of tags before the link tag.
  • 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.
  • Add: Compatibility with extra galleries is made through a filter (which anybody can use) and the file mgcl_extra.php.
  • Info: If you like the plugin, your reviews are welcome here :) Thank you!
Download this release

Release Info

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

Code changes from version 1.0.2 to 1.0.4

Files changed (4) hide show
  1. gallery_custom_links.php +3 -2
  2. mgcl_core.php +40 -24
  3. mgcl_extra.php +23 -0
  4. readme.txt +7 -1
gallery_custom_links.php CHANGED
@@ -3,8 +3,9 @@
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.2
7
  Author: Jordy Meow
 
8
  Text Domain: gallery-custom-links
9
  Domain Path: /languages
10
 
@@ -22,7 +23,7 @@ if ( class_exists( 'Meow_Gallery_Custom_Links' ) ) {
22
  }
23
 
24
  global $mgcl_version;
25
- $mgcl_version = '1.0.2';
26
 
27
  // Admin
28
  // 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.4
7
  Author: Jordy Meow
8
+ Author URI: https://meowapps.com
9
  Text Domain: gallery-custom-links
10
  Domain Path: /languages
11
 
23
  }
24
 
25
  global $mgcl_version;
26
+ $mgcl_version = '1.0.4';
27
 
28
  // Admin
29
  // include "mgcl_admin.php";
mgcl_core.php CHANGED
@@ -8,6 +8,7 @@ use DiDom\Element;
8
  class Meow_Gallery_Custom_Links
9
  {
10
  public function __construct() {
 
11
  add_action( 'wp_head', array( $this, 'wp_head' ) );
12
  add_action( 'wp_footer', array( $this, 'wp_footer' ) );
13
  add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
@@ -92,41 +93,56 @@ class Meow_Gallery_Custom_Links
92
  if ( empty( $target ) )
93
  $target = '_self';
94
  $parent = $element->parent();
95
- if ( $parent->tag === 'a' ) {
96
- $parent->attr( 'href', $url );
97
- $parent->attr( 'target', $target );
98
- return true;
99
- }
100
- else {
101
- if ( $parent->tag === 'figure' )
102
- $parent = $parent->parent();
103
- $a = new Element('a');
104
- $a->attr( 'href', $url );
105
- $a->attr( 'onclick', 'event.stopPropagation()' );
106
- $a->attr( 'target', $target );
107
- $a->appendChild( $parent->children() );
108
- foreach( $parent->children() as $img ) {
109
- $img->remove();
110
  }
111
- $parent->appendChild( $a );
112
- return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  }
 
 
114
  }
115
  }
116
  return false;
117
  }
118
 
 
 
 
 
 
119
  function linkify( $buffer ) {
120
  if ( !isset( $buffer ) || trim( $buffer ) === '' )
121
  return $buffer;
122
- $html = new Document( $buffer );
 
 
123
  $hasChanges = false;
124
- foreach ( $html->find('.gallery img') as $element )
125
- $hasChanges = $this->linkify_element( $element ) || $hasChanges;
126
- foreach ( $html->find('.wp-block-gallery img') as $element )
127
- $hasChanges = $this->linkify_element( $element ) || $hasChanges;
128
- if ( class_exists( 'Meow_Gallery_Core' ) ) {
129
- foreach ( $html->find('.mgl-gallery img') as $element )
130
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
131
  }
132
  return $hasChanges ? $html : $buffer;
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 );
93
  if ( empty( $target ) )
94
  $target = '_self';
95
  $parent = $element->parent();
96
+
97
+ // Let's look for the closest link tag enclosing the image
98
+ $potentialLinkNode = $parent;
99
+ $maxDepth = 10;
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
+
112
+ // There is no link tag, so we add one and move the image under it
113
+ if ( $parent->tag === 'figure' )
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() );
121
+ foreach( $parent->children() as $img ) {
122
+ $img->remove();
123
  }
124
+ $parent->appendChild( $a );
125
+ return true;
126
  }
127
  }
128
  return false;
129
  }
130
 
131
+ function init() {
132
+ include "mgcl_extra.php";
133
+ new Meow_Gallery_Custom_Links_Extra();
134
+ }
135
+
136
  function linkify( $buffer ) {
137
  if ( !isset( $buffer ) || trim( $buffer ) === '' )
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;
mgcl_extra.php ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Meow_Gallery_Custom_Links_Extra
4
+ {
5
+ public function __construct() {
6
+ if ( class_exists( 'Meow_Gallery_Core' ) )
7
+ add_filter( 'gallery_custom_links_classes', array( $this, 'meow_gallery' ), 10, 1 );
8
+ if ( function_exists( 'kadence_gallery' ) )
9
+ add_filter( 'gallery_custom_links_classes', array( $this, 'kadence_gallery' ), 10, 1 );
10
+ }
11
+
12
+ function meow_gallery( $classes ) {
13
+ array_push( $classes, '.mgl-gallery' );
14
+ return $classes;
15
+ }
16
+
17
+ function kadence_gallery( $classes ) {
18
+ array_push( $classes, '.kad-wp-gallery' );
19
+ return $classes;
20
+ }
21
+ }
22
+
23
+ ?>
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.2
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,12 @@ Replace all the files. Nothing else to do.
36
 
37
  == Changelog ==
38
 
 
 
 
 
 
 
39
  = 1.0.2 =
40
  * Fix: Now works with thumbnails in src.
41
  * Update: Optimization (does not regenerate pages which aren't impacted by changes).
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
 
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.
42
+ * Add: Compatibility with extra galleries is made through a filter (which anybody can use) and the file mgcl_extra.php.
43
+ * Info: If you like the plugin, your reviews are welcome [here](https://wordpress.org/support/plugin/gallery-custom-links/reviews/?rate=5#new-post. ) :) Thank you!
44
+
45
  = 1.0.2 =
46
  * Fix: Now works with thumbnails in src.
47
  * Update: Optimization (does not regenerate pages which aren't impacted by changes).