Changeset 1300051
- Timestamp:
- 12/03/2015 10:11:41 PM (10 years ago)
- Location:
- wp-gallery-custom-links/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (4 diffs)
-
wp-gallery-custom-links.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-gallery-custom-links/trunk/readme.txt
r1256717 r1300051 4 4 Tags: gallery links, gallery link, gallery 5 5 Requires at least: 3.3.1 6 Tested up to: 4. 3.17 Stable tag: 1.1 0.56 Tested up to: 4.4.0 7 Stable tag: 1.11 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 41 41 * Use `[gallery preserve_click_events="true"]` to keep Lightbox or other onClick events on all custom-linked images in an entire gallery. 42 42 * Use `[gallery remove_links="true"]` to remove links on all images in an entire gallery. 43 * Use `[gallery rel="nofollow"]` to set a rel attribute with value "nofollow" on all links in an entire gallery. 43 44 44 45 = Hooks = … … 148 149 == Changelog == 149 150 151 = 1.11 = 152 * By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow) 153 * Tested with WordPress 4.4 154 150 155 = 1.10.5 = 151 156 * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel. … … 264 269 == Upgrade Notice == 265 270 271 = 1.11 = 272 * By popular demand, added the ability to set a "rel" property on all images in a gallery (e.g. nofollow) 273 * Tested with WordPress 4.4 274 266 275 = 1.10.5 = 267 276 * Changed translation text domain from a variable to strings, because apparently a variable doesn't universally work no matter how smart it makes me feel. -
wp-gallery-custom-links/trunk/wp-gallery-custom-links.php
r1256717 r1300051 5 5 Text Domain: wp-gallery-custom-links 6 6 Description: Specify custom links for WordPress gallery images (instead of attachment or file only). 7 Version: 1.1 0.57 Version: 1.11 8 8 Author: Four Lights Web Development 9 9 Author URI: http://www.fourlightsweb.com … … 224 224 $link = ''; 225 225 $target = ''; 226 $rel = ''; 226 227 $preserve_click = ''; 227 228 $remove_link = false; … … 254 255 $target = '_self'; 255 256 } 257 // See if we have a rel for this link 258 if( isset( $attr['rel'] ) ) { 259 // Add a rel property to this image link with the specified value 260 $rel = $attr['rel']; // Note: this is escaped before being dropped in 261 } 256 262 // See if we have additional css classes for this attachment image 257 263 $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_additional_css_classes', true ); … … 275 281 } 276 282 277 if( $link != '' || $target != '' || $re move_link || $additional_css_classes != '' ) {283 if( $link != '' || $target != '' || $rel != '' || $remove_link || $additional_css_classes != '' ) { 278 284 // Replace the attachment href 279 285 $needle = get_attachment_link( $attachment_id ); 280 $output = self::replace_link( $needle, $link, $target, $ preserve_click, $remove_link, $additional_css_classes, $output );286 $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ); 281 287 282 288 // Replace the file href 283 289 list( $needle ) = wp_get_attachment_image_src( $attachment_id, '' ); 284 $output = self::replace_link( $needle, $link, $target, $ preserve_click, $remove_link, $additional_css_classes, $output );290 $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ); 285 291 // Also, in case of jetpack photon with tiled galleries... 286 292 if( function_exists( 'jetpack_photon_url' ) ) { … … 298 304 $needle_part_2 = '.wp.com' . $needle_parts[1]; 299 305 $needle_reassembled = $needle_part_1 . $j . $needle_part_2; 300 $output = self::replace_link( $needle_reassembled, $link, $target, $ preserve_click, $remove_link, $additional_css_classes, $output );306 $output = self::replace_link( $needle_reassembled, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ); 301 307 } 302 308 } … … 311 317 foreach( $attachment_sizes as $attachment_size => $attachment_info ) { 312 318 list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size ); 313 $output = self::replace_link( $needle, $link, $target, $ preserve_click, $remove_link, $additional_css_classes, $output );319 $output = self::replace_link( $needle, $link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ); 314 320 } // End of foreach attachment size 315 321 } // End if we have attachment sizes … … 321 327 } // End function apply_filter_post_gallery() 322 328 323 private static function replace_link( $default_link, $custom_link, $target, $ preserve_click, $remove_link, $additional_css_classes, $output ) {329 private static function replace_link( $default_link, $custom_link, $target, $rel, $preserve_click, $remove_link, $additional_css_classes, $output ) { 324 330 // Build the regex for matching/replacing 325 331 $needle = preg_quote( $default_link ); … … 377 383 if( $target != '' && ! $remove_link ) { 378 384 // Replace the link target 379 $output = self::add_ target( $default_link, $target, $output );385 $output = self::add_property( $default_link, 'target', $target, $output ); 380 386 381 387 // Add a class to the link so we can manipulate it with … … 385 391 $classes_to_add .= 'set-target '; 386 392 } 393 } 394 395 // Custom Rel 396 if( $rel != '' && ! $remove_link ) { 397 // Replace the link rel 398 $output = self::add_property( $default_link, 'rel', $rel, $output ); 387 399 } 388 400 … … 419 431 $needle = preg_quote( $needle ); 420 432 $needle = str_replace( '/', '\/', $needle ); 433 434 // Clean property values a bit 435 $class = esc_attr( $class ); 421 436 422 437 // Add a class to the link so we can manipulate it with … … 436 451 } // End function add_class() 437 452 438 private static function add_ target( $needle, $target, $output ) {453 private static function add_property( $needle, $property_name, $property_value, $output ) { 439 454 // Clean up our needle for regexing 440 455 $needle = preg_quote( $needle ); 441 456 $needle = str_replace( '/', '\/', $needle ); 442 443 // Add a target to the link (or overwrite what's there) 444 if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) { 445 // href comes before target 446 $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*>)/U', '$1'.$target.'$2', $output ); 447 } elseif( preg_match( '/<a[^>]*target\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) { 448 // href comes after target 449 $output = preg_replace( '/(<a[^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/U', '$1'.$target.'$2', $output ); 457 458 // Clean property values a bit 459 $property_name = esc_attr( $property_name ); 460 $property_value = esc_attr( $property_value ); 461 462 // Add a property to the link (or overwrite what's there) 463 if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*'.$property_name.'\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) { 464 // href comes before property 465 $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*'.$property_name.'\s*=\s*["\'])[^"\']*(["\'][^>]*>)/U', '$1'.$property_value.'$2', $output ); 466 } elseif( preg_match( '/<a[^>]*'.$property_name.'\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) { 467 // href comes after property 468 $output = preg_replace( '/(<a[^>]*'.$property_name.'\s*=\s*["\'])[^"\']*(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/U', '$1'.$property_value.'$2', $output ); 450 469 } else { 451 // No previous target452 $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 target="'.$target.'"$2', $output );470 // No previous property 471 $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/U', '$1 '.$property_name.'="'.$property_value.'"$2', $output ); 453 472 } // End if we have a class on the a tag or not 454 473 455 474 return $output; 456 } // End function add_ target()475 } // End function add_property() 457 476 458 477 } // End class WPGalleryCustomLinks
Note: See TracChangeset
for help on using the changeset viewer.