Gallery Custom Links - Version 0.0.2

Version Description

  • Update: If the ID of the Media is not found in the HTML, it will resolve it through the DB from the filename.
Download this release

Release Info

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

Code changes from version 0.0.1 to 0.0.2

Files changed (3) hide show
  1. gallery_custom_links.php +1 -1
  2. mgcl_core.php +36 -4
  3. readme.txt +5 -2
gallery_custom_links.php CHANGED
@@ -22,7 +22,7 @@ if ( class_exists( 'Meow_Gallery_Custom_Links' ) ) {
22
  }
23
 
24
  global $mgcl_version;
25
- $mgcl_version = '0.0.1';
26
 
27
  // Admin
28
  // include "mgcl_admin.php";
22
  }
23
 
24
  global $mgcl_version;
25
+ $mgcl_version = '0.0.2';
26
 
27
  // Admin
28
  // include "mgcl_admin.php";
mgcl_core.php CHANGED
@@ -49,12 +49,44 @@ class Meow_Gallery_Custom_Links
49
  ob_start( array( $this, "linkify" ) );
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  function linkify_element( $element ) {
53
  $classes = $element->attr('class');
54
- if ( preg_match( '/wp-image-([0-9]{1,10})/i', $classes, $matches ) ) {
55
- $url = get_post_meta( $matches[1], '_gallery_link_url', true );
 
 
 
 
 
 
 
 
 
 
 
56
  if ( !empty( $url ) ) {
57
- $target = get_post_meta( $matches[1], '_gallery_link_target', true );
58
  if ( empty( $target ) )
59
  $target = '_self';
60
  $parent = $element->parent();
@@ -71,13 +103,13 @@ class Meow_Gallery_Custom_Links
71
  $a->attr( 'target', $target );
72
  $a->appendChild( $parent->children() );
73
  foreach( $parent->children() as $img ) {
74
-
75
  $img->remove();
76
  }
77
  $parent->appendChild( $a );
78
  }
79
  }
80
  }
 
81
  }
82
 
83
  function linkify( $buffer ) {
49
  ob_start( array( $this, "linkify" ) );
50
  }
51
 
52
+ // Clean the path from the domain and common folders
53
+ // Originally written for the WP Retina 2x plugin
54
+ function get_pathinfo_from_image_src( $image_src ) {
55
+ $uploads = wp_upload_dir();
56
+ $uploads_url = trailingslashit( $uploads['baseurl'] );
57
+ if ( strpos( $image_src, $uploads_url ) === 0 )
58
+ return ltrim( substr( $image_src, strlen( $uploads_url ) ), '/');
59
+ else if ( strpos( $image_src, wp_make_link_relative( $uploads_url ) ) === 0 )
60
+ return ltrim( substr( $image_src, strlen( wp_make_link_relative( $uploads_url ) ) ), '/');
61
+ $img_info = parse_url( $image_src );
62
+ return ltrim( $img_info['path'], '/' );
63
+ }
64
+
65
+ function resolve_image_id( $url ) {
66
+ global $wpdb;
67
+ $url = $this->get_pathinfo_from_image_src( $url );
68
+ $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%s'", '%' . $url . '%' );
69
+ $attachment = $wpdb->get_col( $query );
70
+ return empty( $attachment ) ? null : $attachment[0];
71
+ }
72
+
73
  function linkify_element( $element ) {
74
  $classes = $element->attr('class');
75
+ $mediaId = null;
76
+
77
+ // Check if the wp-image-xxx class exists
78
+ if ( preg_match( '/wp-image-([0-9]{1,10})/i', $classes, $matches ) )
79
+ $mediaId = $matches[1];
80
+ // Otherwise, resolve the ID from the URL
81
+ else {
82
+ $url = $element->attr('src');
83
+ $mediaId = $this->resolve_image_id( $url );
84
+ }
85
+
86
+ if ( $mediaId ) {
87
+ $url = get_post_meta( $mediaId, '_gallery_link_url', true );
88
  if ( !empty( $url ) ) {
89
+ $target = get_post_meta( $mediaId, '_gallery_link_target', true );
90
  if ( empty( $target ) )
91
  $target = '_self';
92
  $parent = $element->parent();
103
  $a->attr( 'target', $target );
104
  $a->appendChild( $parent->children() );
105
  foreach( $parent->children() as $img ) {
 
106
  $img->remove();
107
  }
108
  $parent->appendChild( $a );
109
  }
110
  }
111
  }
112
+
113
  }
114
 
115
  function linkify( $buffer ) {
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: 0.0.1
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
 
@@ -17,7 +17,7 @@ Two fields are added to your images, in your Media Library: Link URL and Link Ta
17
 
18
  === Compatibility ===
19
 
20
- It currently works with the native WP Gallery, the Gutenberg Gallery, and the Meow Gallery. It should actually work with any gallery plugin using the 'gallery' class and Responsive Images (src-set). Let me know if you would like more galleries to be supported, it should be easy. It does not work with NextGEN, as it does not work with Responsive Images.
21
 
22
  === Thanks ===
23
 
@@ -36,6 +36,9 @@ Replace all the files. Nothing else to do.
36
 
37
  == Changelog ==
38
 
 
 
 
39
  = 0.0.1 =
40
  * First release.
41
 
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 4.9
5
  Tested up to: 5.0
6
+ Stable tag: 0.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
 
17
 
18
  === Compatibility ===
19
 
20
+ It currently works with the native WP Gallery, the Gutenberg Gallery, and the Meow Gallery. It should actually work with any gallery plugin using the 'gallery' class and Responsive Images (src-set). Let me know if you would like more galleries to be supported, it should be easy.
21
 
22
  === Thanks ===
23
 
36
 
37
  == Changelog ==
38
 
39
+ = 0.0.2 =
40
+ * Update: If the ID of the Media is not found in the HTML, it will resolve it through the DB from the filename.
41
+
42
  = 0.0.1 =
43
  * First release.
44