Gallery Custom Links - Version 1.0.8

Version Description

  • Update: Back to OB, maybe there should be an option for that.
  • Update: Get all the images of the page/post content instead of within specific containers previously.
Download this release

Release Info

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

Code changes from version 1.0.7 to 1.0.8

Files changed (6) hide show
  1. composer.json +1 -1
  2. composer.lock +1 -1
  3. gallery_custom_links.php +4 -5
  4. mgcl_admin.php +48 -0
  5. mgcl_core.php +30 -57
  6. readme.txt +6 -1
composer.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
  "require": {
3
- "imangazaliev/didom": "^1.13"
4
  }
5
  }
1
  {
2
  "require": {
3
+ "imangazaliev/didom": "^1.14"
4
  }
5
  }
composer.lock CHANGED
@@ -4,7 +4,7 @@
4
  "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5
  "This file is @generated automatically"
6
  ],
7
- "content-hash": "f9f9f5cd3cfa4a3b2acc09d04bf4ec3f",
8
  "packages": [
9
  {
10
  "name": "imangazaliev/didom",
4
  "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5
  "This file is @generated automatically"
6
  ],
7
+ "content-hash": "dd2d693feb08c59f7273fa9e59333cd3",
8
  "packages": [
9
  {
10
  "name": "imangazaliev/didom",
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.7
7
  Author: Jordy Meow
8
  Author URI: https://meowapps.com
9
  Text Domain: gallery-custom-links
@@ -23,11 +23,10 @@ if ( class_exists( 'Meow_Gallery_Custom_Links' ) ) {
23
  }
24
 
25
  global $mgcl_version;
26
- $mgcl_version = '1.0.7';
27
 
28
- // Admin
29
- // include "mgcl_admin.php";
30
- // $mgcl_admin = new Meow_Gallery_Custom_Links_Admin( 'mgcl', __FILE__, 'gallery-custom-links' );
31
 
32
  // Core
33
  include "mgcl_core.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.8
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.8';
27
 
28
+ include "mgcl_admin.php";
29
+ $mgcl_admin = new Meow_Gallery_Custom_Links_Admin( 'mgcl', __FILE__, 'gallery-custom-links' );
 
30
 
31
  // Core
32
  include "mgcl_core.php";
mgcl_admin.php ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Meow_Gallery_Custom_Links_Admin { // extends MeowApps_Admin
4
+
5
+ public $core;
6
+
7
+ public function __construct( $prefix, $mainfile, $domain ) {
8
+ //parent::__construct( $prefix, $mainfile, $domain );
9
+ if ( is_admin() ) {
10
+ add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
11
+ add_filter( 'attachment_fields_to_save', array( $this, 'apply_filter_attachment_fields_to_save' ), 10 , 2 );
12
+ }
13
+ }
14
+
15
+ function attachment_fields_to_edit( $fields, $post ) {
16
+ $fields['gallery_link_url'] = array(
17
+ 'label' => __( 'Link URL', 'gallery-custom-links' ),
18
+ 'input' => 'text',
19
+ 'value' => get_post_meta( $post->ID, '_gallery_link_url', true )
20
+ );
21
+ $target_value = get_post_meta( $post->ID, '_gallery_link_target', true );
22
+ $fields['gallery_link_target'] = array(
23
+ 'label' => __( 'Link Target', 'gallery-custom-links' ),
24
+ 'input' => 'html',
25
+ 'html' => '
26
+ <select name="attachments[' . $post->ID . '][gallery_link_target]" id="attachments[' . $post->ID . '][gallery_link_target]">
27
+ <option value="_self"' . ( $target_value == '_self' ? ' selected="selected"' : '' ) . '>' .
28
+ __( 'Same page', 'gallery-custom-links' ) .
29
+ '</option>
30
+ <option value="_blank"' . ( $target_value == '_blank' ? ' selected="selected"' : '' ) . '>' .
31
+ __( 'New page', 'gallery-custom-links' ) .
32
+ '</option>
33
+ </select>'
34
+ );
35
+ return $fields;
36
+ }
37
+
38
+ function apply_filter_attachment_fields_to_save( $post, $attachment ) {
39
+ if ( isset( $attachment['gallery_link_url'] ) )
40
+ update_post_meta( $post['ID'], '_gallery_link_url', $attachment['gallery_link_url'] );
41
+ if ( isset( $attachment['gallery_link_target'] ) )
42
+ update_post_meta( $post['ID'], '_gallery_link_target', $attachment['gallery_link_target'] );
43
+ return $post;
44
+ }
45
+
46
+ }
47
+
48
+ ?>
mgcl_core.php CHANGED
@@ -8,66 +8,34 @@ use DiDom\Element;
8
  class Meow_Gallery_Custom_Links
9
  {
10
  public $isEnabled = true;
11
- public $isOb = false;
12
 
13
  public function __construct() {
14
 
15
- if ( is_admin() ) {
16
- add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
17
- add_filter( 'attachment_fields_to_save', array( $this, 'apply_filter_attachment_fields_to_save' ), 10 , 2 );
18
- }
19
- else {
20
- // Extra support
21
- add_action( 'init', array( $this, 'init' ) );
22
-
23
- $this->isOb = false;
24
 
25
- // Processing
26
  add_action( 'init', array( $this, 'start' ) );
27
- if ( $this->isOb )
28
- add_action( 'shutdown', array( $this, 'shutdown' ) );
29
- else
30
- add_filter( 'the_content', array( $this, 'linkify' ), 20 );
 
31
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
32
  }
33
  }
34
 
35
- function attachment_fields_to_edit( $fields, $post ) {
36
- $fields['gallery_link_url'] = array(
37
- 'label' => __( 'Link URL', 'gallery-custom-links' ),
38
- 'input' => 'text',
39
- 'value' => get_post_meta( $post->ID, '_gallery_link_url', true )
40
- );
41
- $target_value = get_post_meta( $post->ID, '_gallery_link_target', true );
42
- $fields['gallery_link_target'] = array(
43
- 'label' => __( 'Link Target', 'gallery-custom-links' ),
44
- 'input' => 'html',
45
- 'html' => '
46
- <select name="attachments[' . $post->ID . '][gallery_link_target]" id="attachments[' . $post->ID . '][gallery_link_target]">
47
- <option value="_self"' . ( $target_value == '_self' ? ' selected="selected"' : '' ) . '>' .
48
- __( 'Same page', 'gallery-custom-links' ) .
49
- '</option>
50
- <option value="_blank"' . ( $target_value == '_blank' ? ' selected="selected"' : '' ) . '>' .
51
- __( 'New page', 'gallery-custom-links' ) .
52
- '</option>
53
- </select>'
54
- );
55
- return $fields;
56
- }
57
-
58
- function apply_filter_attachment_fields_to_save( $post, $attachment ) {
59
- if ( isset( $attachment['gallery_link_url'] ) )
60
- update_post_meta( $post['ID'], '_gallery_link_url', $attachment['gallery_link_url'] );
61
- if ( isset( $attachment['gallery_link_target'] ) )
62
- update_post_meta( $post['ID'], '_gallery_link_target', $attachment['gallery_link_target'] );
63
- return $post;
64
  }
65
 
66
  function start() {
67
  $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
68
- if ( !$this->isEnabled )
69
- return;
70
- if ( $this->isOb )
71
  ob_start( array( $this, "linkify" ) );
72
  }
73
 
@@ -119,7 +87,7 @@ class Meow_Gallery_Custom_Links
119
  $potentialLinkNode = $parent;
120
  $maxDepth = 10;
121
  do {
122
- if ( $potentialLinkNode->tag === 'a' ) {
123
  $potentialLinkNode->attr( 'href', $url );
124
  $class = $potentialLinkNode->attr( 'class' );
125
  $class = empty( $class ) ? 'custom-link no-lightbox' : ( $class . ' custom-link no-lightbox' );
@@ -154,24 +122,29 @@ class Meow_Gallery_Custom_Links
154
  return false;
155
  }
156
 
157
- function init() {
158
- include "mgcl_extra.php";
159
- new Meow_Gallery_Custom_Links_Extra();
160
- }
161
-
162
  function linkify( $buffer ) {
 
163
  if ( !$this->isEnabled || !isset( $buffer ) || trim( $buffer ) === '' )
164
  return $buffer;
165
  $html = new Document();
166
  $html->preserveWhiteSpace();
167
- $html->loadHtml( $buffer, 0 );
 
 
 
 
 
 
168
  $hasChanges = false;
169
- $classes = apply_filters( 'gallery_custom_links_classes', array( '.gallery', '.wp-block-gallery' ) );
 
170
  foreach ( $classes as $class ) {
171
- foreach ( $html->find( $class . ' img' ) as $element )
172
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
 
173
  }
174
- return $hasChanges ? $html->html() : $buffer;
 
175
  }
176
 
177
  function shutdown() {
8
  class Meow_Gallery_Custom_Links
9
  {
10
  public $isEnabled = true;
11
+ public $isOb = true;
12
 
13
  public function __construct() {
14
 
15
+ if ( is_admin() )
16
+ return;
 
 
 
 
 
 
 
17
 
18
+ if ( $this->isOb ) {
19
  add_action( 'init', array( $this, 'start' ) );
20
+ add_action( 'shutdown', array( $this, 'shutdown' ) );
21
+ add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
22
+ }
23
+ else {
24
+ add_filter( 'the_content', array( $this, 'linkify' ), 10 );
25
  add_action( 'wp_footer', array( $this, 'unlink_lightboxes_script' ) ) ;
26
  }
27
  }
28
 
29
+ function init() {
30
+ //add_action( 'init', array( $this, 'init' ) );
31
+ // We don't need this now, we go through all the images.
32
+ include "mgcl_extra.php";
33
+ new Meow_Gallery_Custom_Links_Extra();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }
35
 
36
  function start() {
37
  $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
38
+ if ( $this->isEnabled && $this->isOb )
 
 
39
  ob_start( array( $this, "linkify" ) );
40
  }
41
 
87
  $potentialLinkNode = $parent;
88
  $maxDepth = 10;
89
  do {
90
+ if ( property_exists( $potentialLinkNode, 'tag' ) && $potentialLinkNode->tag === 'a' ) {
91
  $potentialLinkNode->attr( 'href', $url );
92
  $class = $potentialLinkNode->attr( 'class' );
93
  $class = empty( $class ) ? 'custom-link no-lightbox' : ( $class . ' custom-link no-lightbox' );
122
  return false;
123
  }
124
 
 
 
 
 
 
125
  function linkify( $buffer ) {
126
+ $this->isEnabled = apply_filters( 'gallery_custom_links_enabled', true );
127
  if ( !$this->isEnabled || !isset( $buffer ) || trim( $buffer ) === '' )
128
  return $buffer;
129
  $html = new Document();
130
  $html->preserveWhiteSpace();
131
+ if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) ) {
132
+ $html->loadHtml( $buffer, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
133
+ }
134
+ else {
135
+ $html->loadHtml( $buffer, 0 );
136
+ }
137
+
138
  $hasChanges = false;
139
+ $classes = array( '' ); // Get all images
140
+ //$classes = apply_filters( 'gallery_custom_links_classes', array( '.entry-content', '.gallery', '.wp-block-gallery' ) );
141
  foreach ( $classes as $class ) {
142
+ foreach ( $html->find( $class . ' img' ) as $element ) {
143
  $hasChanges = $this->linkify_element( $element ) || $hasChanges;
144
+ }
145
  }
146
+ $finalHtml = $html->html();
147
+ return $hasChanges ? $finalHtml : $buffer;
148
  }
149
 
150
  function shutdown() {
readme.txt CHANGED
@@ -3,7 +3,8 @@ 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.7
 
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
 
@@ -40,6 +41,10 @@ Replace all the files. Nothing else to do.
40
 
41
  == Changelog ==
42
 
 
 
 
 
43
  = 1.0.7 =
44
  * Update: Not using OB anymore; going through the content filter (this behavior can be changed internally), better and faster this way.
45
  * Fix: Avoid issues with static variables which are not registered on older PHP versions.
3
  Tags: custom, links, gallery, gutenberg
4
  Requires at least: 4.9
5
  Tested up to: 5.0
6
+ Requires PHP: 7.0
7
+ Stable tag: 1.0.8
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.0.8 =
45
+ * Update: Back to OB, maybe there should be an option for that.
46
+ * Update: Get all the images of the page/post content instead of within specific containers previously.
47
+
48
  = 1.0.7 =
49
  * Update: Not using OB anymore; going through the content filter (this behavior can be changed internally), better and faster this way.
50
  * Fix: Avoid issues with static variables which are not registered on older PHP versions.