Gallery Custom Links - Version 1.1.0

Version Description

  • Update: Using HtmlDomParser fully (which should avoid broken HTML). It is possible to override the plugin hidden options to switch to different mode, but I am trying to find a mode that works for 99% of the users first.
  • Info: I need your support. Don't hesitate to try different versions of the plugin if it was working fine for you in the past (old versions). You can also let me a good review to motivate me, that helps a lot.
Download this release

Release Info

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

Code changes from version 1.0.9 to 1.1.0

Files changed (3) hide show
  1. gallery_custom_links.php +2 -2
  2. mgcl_core.php +68 -24
  3. readme.txt +6 -4
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.9
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.9';
27
 
28
  include "mgcl_admin.php";
29
  $mgcl_admin = new Meow_Gallery_Custom_Links_Admin( 'mgcl', __FILE__, 'gallery-custom-links' );
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.1.0
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.1.0';
27
 
28
  include "mgcl_admin.php";
29
  $mgcl_admin = new Meow_Gallery_Custom_Links_Admin( 'mgcl', __FILE__, 'gallery-custom-links' );
mgcl_core.php CHANGED
@@ -10,9 +10,10 @@ class Meow_Gallery_Custom_Links
10
  {
11
  public $isEnabled = true;
12
  // use OB on the whole page, or only go through the the_content ($renderingMode will be ignored)
13
- public $isObMode = true;
14
  // 'HtmlDomParser' (less prone to break badly formatted HTML) or 'DiDom' (faster)
15
  public $parsingEngine = 'HtmlDomParser';
 
16
 
17
  public function __construct() {
18
 
@@ -26,7 +27,7 @@ class Meow_Gallery_Custom_Links
26
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
27
  }
28
  else {
29
- add_filter( 'the_content', array( $this, 'linkify' ), 10 );
30
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
31
  }
32
  }
@@ -86,18 +87,25 @@ class Meow_Gallery_Custom_Links
86
  }
87
 
88
  function linkify_element( $element ) {
89
- $classes = $element->attr('class');
 
 
90
  $mediaId = null;
91
 
92
  // Check if the wp-image-xxx class exists
 
93
  if ( preg_match( '/wp-image-([0-9]{1,10})/i', $classes, $matches ) )
94
  $mediaId = $matches[1];
95
  // Otherwise, resolve the ID from the URL
96
  else {
97
- $url = $element->attr('src');
98
  $mediaId = $this->resolve_image_id( $url );
99
  }
100
 
 
 
 
 
101
  if ( $mediaId ) {
102
  $url = get_post_meta( $mediaId, '_gallery_link_url', true );
103
  if ( !empty( $url ) ) {
@@ -105,21 +113,52 @@ class Meow_Gallery_Custom_Links
105
  if ( empty( $target ) )
106
  $target = '_self';
107
  $parent = $element->parent();
108
- if ( $this->parsingEngine === 'HtmlDomParser' ) {
109
- $src = $element->src;
110
- $mglSrc = $element->{'mgl-src'};
111
- if ( $parent->{'tag'} === 'figure' )
112
- $parent = $parent->parent();
113
- $a = new Element('a');
114
- $a->{'href'} = $url;
115
- $a->{'class'} = 'custom-link no-lightbox';
116
- $a->{'onclick'} = 'event.stopPropagation()';
117
- $a->{'target'} = $target;
118
- $a->appendChild( $parent->children() );
119
- foreach( $parent->children() as $img ) {
120
- $img->remove();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  }
122
- $parent->appendChild( $a );
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
  else {
125
  if ( $parent->tag === 'figure' )
@@ -145,13 +184,18 @@ class Meow_Gallery_Custom_Links
145
  $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
146
  if ( !$this->isEnabled || !isset( $buffer ) || trim( $buffer ) === '' )
147
  return $buffer;
148
- $html = new Document();
149
- $html->preserveWhiteSpace();
150
- if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) ) {
151
- $html->loadHtml( $buffer, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
152
  }
153
  else {
154
- $html->loadHtml( $buffer, 0 );
 
 
 
 
 
155
  }
156
 
157
  $hasChanges = false;
@@ -162,7 +206,7 @@ class Meow_Gallery_Custom_Links
162
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
163
  }
164
  }
165
- $finalHtml = $html->html();
166
  return $hasChanges ? $finalHtml : $buffer;
167
  }
168
 
10
  {
11
  public $isEnabled = true;
12
  // use OB on the whole page, or only go through the the_content ($renderingMode will be ignored)
13
+ public $isObMode = false;
14
  // 'HtmlDomParser' (less prone to break badly formatted HTML) or 'DiDom' (faster)
15
  public $parsingEngine = 'HtmlDomParser';
16
+ public $enableLogs = false;
17
 
18
  public function __construct() {
19
 
27
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
28
  }
29
  else {
30
+ add_filter( 'the_content', array( $this, 'linkify' ), 100 );
31
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
32
  }
33
  }
87
  }
88
 
89
  function linkify_element( $element ) {
90
+
91
+ $classes = $this->parsingEngine === 'HtmlDomParser' ? $element->class : $element->attr('class');
92
+
93
  $mediaId = null;
94
 
95
  // Check if the wp-image-xxx class exists
96
+ $url = null;
97
  if ( preg_match( '/wp-image-([0-9]{1,10})/i', $classes, $matches ) )
98
  $mediaId = $matches[1];
99
  // Otherwise, resolve the ID from the URL
100
  else {
101
+ $url = $this->parsingEngine === 'HtmlDomParser' ? $element->src : $element->attr('src');
102
  $mediaId = $this->resolve_image_id( $url );
103
  }
104
 
105
+ if ( $this->enableLogs ) {
106
+ error_log( 'Linker: Found img tag with classes: ' . $classes );
107
+ }
108
+
109
  if ( $mediaId ) {
110
  $url = get_post_meta( $mediaId, '_gallery_link_url', true );
111
  if ( !empty( $url ) ) {
113
  if ( empty( $target ) )
114
  $target = '_self';
115
  $parent = $element->parent();
116
+
117
+ if ( $this->enableLogs ) {
118
+ error_log( 'Linker: Found Media ' . $mediaId . ' (URL: ' . $url . ')' );
119
+ }
120
+
121
+ // Let's look for the closest link tag enclosing the image
122
+ $potentialLinkNode = $parent;
123
+ $maxDepth = 5;
124
+ do {
125
+ if ( !empty( $potentialLinkNode ) && $potentialLinkNode->tag === 'a' ) {
126
+
127
+ if ( $this->enableLogs ) {
128
+ error_log( 'Linker: The current link (' . $potentialLinkNode->{'href'} . ') will be replaced.' );
129
+ }
130
+
131
+ if ( $this->parsingEngine === 'HtmlDomParser' ) {
132
+ $potentialLinkNode->{'href'} = $url;
133
+ $class = $potentialLinkNode->{'class'};
134
+ $class = empty( $class ) ? 'custom-link no-lightbox' : ( $class . ' custom-link no-lightbox' );
135
+ $potentialLinkNode->{'class'} = $class;
136
+ $potentialLinkNode->{'onclick'} = 'event.stopPropagation()';
137
+ $potentialLinkNode->{'target'} = $target;
138
+ }
139
+ else {
140
+ $potentialLinkNode->attr( 'href', $url );
141
+ $class = $potentialLinkNode->attr( 'class' );
142
+ $class = empty( $class ) ? 'custom-link no-lightbox' : ( $class . ' custom-link no-lightbox' );
143
+ $potentialLinkNode->attr( 'class', $class );
144
+ $potentialLinkNode->attr( 'onclick', 'event.stopPropagation()' );
145
+ $potentialLinkNode->attr( 'target', $target );
146
+ }
147
+ return true;
148
  }
149
+ if ( method_exists( $potentialLinkNode, 'parent' ) )
150
+ $potentialLinkNode = $potentialLinkNode->parent();
151
+ else
152
+ break;
153
+ }
154
+ while ( $potentialLinkNode && $maxDepth-- >= 0 );
155
+
156
+ // There is no link tag, so we add one and move the image under it
157
+ if ( $this->enableLogs ) {
158
+ error_log( 'Linker: Will embed the IMG tag.' );
159
+ }
160
+ if ( $this->parsingEngine === 'HtmlDomParser' ) {
161
+ $element->outertext = '<a href="' . $url . '" class="custom-link no-lightbox" onclick="event.stopPropagation()" target="' . $target . '">' . $element . '</a>';
162
  }
163
  else {
164
  if ( $parent->tag === 'figure' )
184
  $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
185
  if ( !$this->isEnabled || !isset( $buffer ) || trim( $buffer ) === '' )
186
  return $buffer;
187
+
188
+ if ( $this->parsingEngine === 'HtmlDomParser' ) {
189
+ $html = new HtmlDomParser();
190
+ $html = $html->str_get_html( $buffer, true, true, DEFAULT_TARGET_CHARSET, false );
191
  }
192
  else {
193
+ $html = new Document();
194
+ $html->preserveWhiteSpace();
195
+ if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) )
196
+ $html->loadHtml( $buffer, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
197
+ else
198
+ $html->loadHtml( $buffer, 0 );
199
  }
200
 
201
  $hasChanges = false;
206
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
207
  }
208
  }
209
+ $finalHtml = $this->parsingEngine === 'HtmlDomParser' ? $html : $html->html();
210
  return $hasChanges ? $finalHtml : $buffer;
211
  }
212
 
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: TigrouMeow
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 5.0
5
- Tested up to: 5.2
6
  Requires PHP: 7.0
7
- Stable tag: 1.0.9
8
 
9
  Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
10
 
@@ -41,10 +41,12 @@ Replace all the files. Nothing else to do.
41
 
42
  == Changelog ==
43
 
 
 
 
 
44
  = 1.0.9 =
45
- * Info: This plugin is way more work than expected, please motivate me by giving it a good review and also my trying my other plugins (https://meowapps.com). Thanks a lot for that :) I will do my best to make this plugin perfect.
46
  * Update: Avoid interfering at all with all Ajax/Rest requests.
47
- * Update: Use HtmlDomParser instead of DiDom (this parser is less sensitive to badly formatted HTML). I am also thinking of doing it all with regexp, but let's see that later.
48
  * Info: Sorry for the last bunch of updates, some way of modifying HTML works for some, and not for others, and I am still trying to find a solution which works for everyone.
49
 
50
  = 1.0.8 =
2
  Contributors: TigrouMeow
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 5.0
5
+ Tested up to: 5.1
6
  Requires PHP: 7.0
7
+ Stable tag: 1.1.0
8
 
9
  Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery and others.
10
 
41
 
42
  == Changelog ==
43
 
44
+ = 1.1.0 =
45
+ * Update: Using HtmlDomParser fully (which should avoid broken HTML). It is possible to override the plugin hidden options to switch to different mode, but I am trying to find a mode that works for 99% of the users first.
46
+ * Info: I need your support. Don't hesitate to try different versions of the plugin if it was working fine for you in the past ([old versions](https://wordpress.org/plugins/gallery-custom-links/advanced/)). You can also let me a good review to motivate me, that helps a lot.
47
+
48
  = 1.0.9 =
 
49
  * Update: Avoid interfering at all with all Ajax/Rest requests.
 
50
  * Info: Sorry for the last bunch of updates, some way of modifying HTML works for some, and not for others, and I am still trying to find a solution which works for everyone.
51
 
52
  = 1.0.8 =