Plugin Directory

Changeset 1300051


Ignore:
Timestamp:
12/03/2015 10:11:41 PM (10 years ago)
Author:
fourlightsweb
Message:

Added "rel" option for all images in a gallery, tested in wp 4.4

Location:
wp-gallery-custom-links/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-gallery-custom-links/trunk/readme.txt

    r1256717 r1300051  
    44Tags: gallery links, gallery link, gallery
    55Requires at least: 3.3.1
    6 Tested up to: 4.3.1
    7 Stable tag: 1.10.5
     6Tested up to: 4.4.0
     7Stable tag: 1.11
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4141* Use `[gallery preserve_click_events="true"]` to keep Lightbox or other onClick events on all custom-linked images in an entire gallery.
    4242* 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.
    4344
    4445= Hooks =
     
    148149== Changelog ==
    149150
     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
    150155= 1.10.5 =
    151156* 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.
     
    264269== Upgrade Notice ==
    265270
     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
    266275= 1.10.5 =
    267276* 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  
    55Text Domain: wp-gallery-custom-links
    66Description: Specify custom links for WordPress gallery images (instead of attachment or file only).
    7 Version: 1.10.5
     7Version: 1.11
    88Author: Four Lights Web Development
    99Author URI: http://www.fourlightsweb.com
     
    224224            $link = '';
    225225            $target = '';
     226            $rel = '';
    226227            $preserve_click = '';
    227228            $remove_link = false;
     
    254255                $target = '_self';
    255256            }
     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            }
    256262            // See if we have additional css classes for this attachment image
    257263            $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_additional_css_classes', true );
     
    275281            }
    276282           
    277             if( $link != '' || $target != '' || $remove_link || $additional_css_classes != '' ) {
     283            if( $link != '' || $target != '' || $rel != '' || $remove_link || $additional_css_classes != '' ) {
    278284                // Replace the attachment href
    279285                $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 );
    281287
    282288                // Replace the file href
    283289                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 );
    285291                // Also, in case of jetpack photon with tiled galleries...
    286292                if( function_exists( 'jetpack_photon_url' ) ) {
     
    298304                            $needle_part_2 = '.wp.com' . $needle_parts[1];
    299305                            $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 );
    301307                        }
    302308                    }
     
    311317                        foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
    312318                            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 );
    314320                        } // End of foreach attachment size
    315321                    } // End if we have attachment sizes
     
    321327    } // End function apply_filter_post_gallery()
    322328   
    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 ) {
    324330        // Build the regex for matching/replacing
    325331        $needle = preg_quote( $default_link );
     
    377383            if( $target != '' && ! $remove_link ) {
    378384                // Replace the link target
    379                 $output = self::add_target( $default_link, $target, $output );
     385                $output = self::add_property( $default_link, 'target', $target, $output );
    380386               
    381387                // Add a class to the link so we can manipulate it with
     
    385391                    $classes_to_add .= 'set-target ';
    386392                }
     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 );
    387399            }
    388400           
     
    419431        $needle = preg_quote( $needle );
    420432        $needle = str_replace( '/', '\/', $needle );
     433
     434        // Clean property values a bit
     435        $class = esc_attr( $class );
    421436       
    422437        // Add a class to the link so we can manipulate it with
     
    436451    } // End function add_class()
    437452   
    438     private static function add_target( $needle, $target, $output ) {
     453    private static function add_property( $needle, $property_name, $property_value, $output ) {
    439454        // Clean up our needle for regexing
    440455        $needle = preg_quote( $needle );
    441456        $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 );
    450469        } else {
    451             // No previous target
    452             $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 );
    453472        } // End if we have a class on the a tag or not
    454473       
    455474        return $output;
    456     } // End function add_target()
     475    } // End function add_property()
    457476   
    458477} // End class WPGalleryCustomLinks
Note: See TracChangeset for help on using the changeset viewer.