Plugin Directory

Changeset 1117650


Ignore:
Timestamp:
03/21/2015 09:05:39 PM (11 years ago)
Author:
achmafooma
Message:

Bug 399: Code Cleanup and Intersanifcation

Location:
gallery-shortcode-style-to-head/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • gallery-shortcode-style-to-head/trunk

    • Property svn:ignore set to
      .buildpath
      .project
      .settings
  • gallery-shortcode-style-to-head/trunk/gallery-shortcode-style-to-head.php

    r678538 r1117650  
    22/*
    33Plugin Name: Gallery Shortcode Style to Head
    4 Plugin URI: http://www.scottbradford.us/software/gallery-shortcode-style-to-head/
    5 Description: Moves the gallery shortcode styles to the head so it doesn't break XHTML validation; allows disabling or modifying the default gallery styles. 
    6 Author: Scott Bradford
    7 Author URI: http://www.scottbradford.us/
    8 Version: 2.3
     4Plugin URI: http://www.intersanity.com/software/
     5Description: Moves the gallery shortcode styles to the head so it doesn't break XHTML validation; allows disabling or modifying the default gallery styles.
     6Author: Intersanity Enterprises
     7Author URI: http://www.intersanity.com/
     8Version: 2.3.1
    99
    1010    Copyright (c) 2008 Matt Martz (http://sivel.net) (original author)
    11     Copyright (c) 2009-2012 Scott Bradford (http://www.scottbradford.us) (current maintainer)
    12        
     11    Copyright (c) 2009-2015 Intersanity Enterprises (Scott Bradford) (http://www.intersanity.com/software/) (current maintainer)
     12
    1313    Gallery Shortcode Style to Head is released under the GNU General Public License (GPL)
    1414    http://www.gnu.org/licenses/gpl-2.0.txt
    1515*/
    1616
    17 add_action('init','gssth_init');
    18 
    19 function gssth_init() {
     17add_action('init', 'gssth_init');
     18
     19function gssth_init () {
    2020    // define the default styles
    2121    global $defStyle;
     
    3131
    3232// initialize the settings variables
    33 add_action('admin_init', 'add_gssth_setting' );
    34 function add_gssth_setting() {
    35     register_setting( 'media', 'gssth_disable_gallery_style' );
    36     register_setting( 'media', 'gssth_override_gallery_style' );
    37     add_settings_section('gssth','Gallery CSS Styles','display_gssth_description','media');
    38     add_settings_field('gssth_disable_gallery_style','Disable gallery CSS in \'head\'','build_disable_gallery_styles','media','gssth');
    39     add_settings_field('gssth_override_gallery_style','Modify gallery CSS style','build_override_gallery_styles','media','gssth');
     33add_action('admin_init', 'add_gssth_setting');
     34function add_gssth_setting () {
     35    register_setting('media', 'gssth_disable_gallery_style');
     36    register_setting('media', 'gssth_override_gallery_style');
     37    add_settings_section('gssth', 'Gallery CSS Styles', 'display_gssth_description', 'media');
     38    add_settings_field('gssth_disable_gallery_style', 'Disable gallery CSS in \'head\'', 'build_disable_gallery_styles', 'media', 'gssth');
     39    add_settings_field('gssth_override_gallery_style', 'Modify gallery CSS style', 'build_override_gallery_styles', 'media', 'gssth');
    4040}
    4141
    4242// display the GSSTH description
    43 function display_gssth_description() {
     43function display_gssth_description () {
    4444    echo "<p>Override or disable the default WordPress gallery styles. To reset to default styles, un-check the disable option, clear out the style code (so it is completely empty), and save the changes.</p>";
    4545}
    4646
    4747// handle the disable/enable check box
    48 function build_disable_gallery_styles() {
     48function build_disable_gallery_styles () {
    4949    $checked = "";
    50     if (get_option('gssth_disable_gallery_style'))
     50    if (get_option('gssth_disable_gallery_style')) {
    5151        $checked=" checked='checked'";
     52    }
    5253    echo "<fieldset><legend class=\"screen-reader-text\"><span>Disable the default gallery CSS styles</span></legend>
    5354<label for=\"disable_gallery_styles\"><input name=\"gssth_disable_gallery_style\" type=\"checkbox\" id=\"gssth_disable_gallery_style\" value=\"1\"" . $checked . " /> Disable the default gallery CSS styles (so you can handle it in your template stylesheets)</label>
     
    5657
    5758// handle the style override field
    58 function build_override_gallery_styles() {
     59function build_override_gallery_styles () {
    5960    global $defStyle;
    60     if (get_option('gssth_override_gallery_style'))
     61    if (get_option('gssth_override_gallery_style')) {
    6162        $content = get_option('gssth_override_gallery_style');
    62     else
     63    } else {
    6364        $content = $defStyle;
     65    }
    6466    echo "<textarea style='font-size: 90%; width:95%;' name='gssth_override_gallery_style' id='gssth_override_gallery_style' rows='15' >" . $content . "</textarea>";
    6567}
    6668
    6769
    68 // This function is largely taken from media.php with manual patches based off of 
     70// This function is largely taken from media.php with manual patches based off of
    6971// http://trac.wordpress.org/attachment/ticket/6380/6380-style.diff
    7072
    71 function gallery_shortcode_style_out ( $attr ) {
     73function gallery_shortcode_style_out ($attr) {
    7274    global $post, $wp_locale;
    7375
    7476    static $instance = 0;
    7577    $instance++;
    76    
    77     if ( ! empty( $attr['ids'] ) ) {
     78
     79    if (!empty( $attr['ids'])) {
    7880        // 'ids' is explicitly ordered, unless you specify otherwise.
    79         if ( empty( $attr['orderby'] ) )
     81        if (empty($attr['orderby'])) {
    8082            $attr['orderby'] = 'post__in';
     83        }
    8184        $attr['include'] = $attr['ids'];
    8285    }
     
    8487    // Allow plugins/themes to override the default gallery template.
    8588    $output = apply_filters('post_gallery', '', $attr);
    86     if ( $output != '' )
     89    if ($output != '') {
    8790        return $output;
     91    }
    8892
    8993    // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
    90     if ( isset( $attr['orderby'] ) ) {
    91         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
    92         if ( !$attr['orderby'] )
     94    if (isset($attr['orderby'])) {
     95        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
     96        if (!$attr['orderby']) {
    9397            unset( $attr['orderby'] );
     98        }
    9499    }
    95100
     
    108113
    109114    $id = intval($id);
    110     if ( 'RAND' == $order )
     115    if ('RAND' == $order) {
    111116        $orderby = 'none';
    112 
    113     if ( !empty($include) ) {
    114         $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
     117    }
     118    if (!empty($include)) {
     119        $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
    115120
    116121        $attachments = array();
    117         foreach ( $_attachments as $key => $val ) {
     122        foreach ($_attachments as $key => $val) {
    118123            $attachments[$val->ID] = $_attachments[$key];
    119124        }
    120     } elseif ( !empty($exclude) ) {
    121         $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
     125    } elseif (!empty($exclude)) {
     126        $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
    122127    } else {
    123         $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    124     }
    125 
    126     if ( empty($attachments) )
     128        $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
     129    }
     130
     131    if (empty($attachments)) {
    127132        return '';
    128 
    129     if ( is_feed() ) {
     133    }
     134
     135    if (is_feed()) {
    130136        $output = "\n";
    131         foreach ( $attachments as $att_id => $attachment )
     137        foreach ($attachments as $att_id => $attachment) {
    132138            $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
     139        }
    133140        return $output;
    134141    }
     
    140147
    141148    $selector = "gallery-{$instance}";
    142    
    143     $size_class = sanitize_html_class( $size, '' );
     149
     150    $size_class = sanitize_html_class($size, '');
    144151    $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
    145     $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
     152    $output = apply_filters('gallery_style', $gallery_style . "\n\t\t" . $gallery_div);
    146153
    147154    $i = 0;
    148     foreach ( $attachments as $id => $attachment ) {
     155    foreach ($attachments as $id => $attachment) {
    149156        $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
    150157
     
    154161                $link
    155162            </{$icontag}>";
    156         if ( $captiontag && trim($attachment->post_excerpt) ) {
     163        if ($captiontag && trim($attachment->post_excerpt)) {
    157164            $output .= "
    158165                <{$captiontag} class='wp-caption-text gallery-caption'>
     
    161168        }
    162169        $output .= "</{$itemtag}>";
    163         if ( $columns > 0 && ++$i % $columns == 0 )
     170        if ($columns > 0 && ++$i % $columns == 0) {
    164171            $output .= '<div style="clear: both"></div>';
     172        }
    165173    }
    166174
     
    174182// Default gallery style taken from media.php with .gallery-item width removed
    175183// .gallery-item width applied inline in gallery_shortcode_style_out().
    176 function gallery_style () { 
     184function gallery_style () {
    177185    global $defStyle;
    178186    $output = "
     
    180188<style type='text/css'>
    181189";
    182     if (get_option('gssth_override_gallery_style')) { // if the style is saved, export the saved style
    183         $output .= get_option('gssth_override_gallery_style');     
    184     } else { // if we don't have any styles set right now
     190    if (get_option('gssth_override_gallery_style')) {
     191        // if the style is saved, export the saved style
     192        $output .= get_option('gssth_override_gallery_style');
     193    } else {
     194        // if we don't have any styles set right now
    185195        $output .= $defStyle;
    186196    }
     
    189199</style>";
    190200    echo $output;
    191 } 
     201}
    192202
    193203// Look ahead to check if any posts contain the [gallery] shortcode
    194204// if true then add default gallery style to head
    195 function gallery_scan () {
    196     global $posts;
    197  
    198     if ( !is_array ( $posts ) )
    199         return;
    200  
    201     foreach ( $posts as $post ) {
    202            if ( false !== strpos ( $post->post_content, '[gallery' ) ) {
    203                    add_action ( 'wp_head', 'gallery_style' );
    204                    break;
    205            }
    206     }
    207 }
     205function gallery_scan () {
     206    global $posts;
     207
     208    if (!is_array($posts)) {
     209        return;
     210    }
     211
     212    foreach ($posts as $post) {
     213        if (false !== strpos($post->post_content, '[gallery')) {
     214            add_action('wp_head', 'gallery_style');
     215            break;
     216        }
     217    }
     218}
    208219
    209220// Tell WordPress what to do
    210 remove_shortcode ( 'gallery_shortcode' );           // Remove included WordPress [gallery] shortcode function
    211 add_shortcode ( 'gallery' , 'gallery_shortcode_style_out' );    // Add new [gallery] shortcode function
    212 if (!get_option('gssth_disable_gallery_style')) {  // don't do the look-ahead if styles are disabled
    213     add_action ( 'template_redirect' , 'gallery_scan' );        // Add look ahead for [gallery] shortcode
    214 }
    215 ?>
     221remove_shortcode('gallery_shortcode'); // Remove included WordPress [gallery] shortcode function
     222add_shortcode('gallery', 'gallery_shortcode_style_out'); // Add new [gallery] shortcode function
     223if (!get_option('gssth_disable_gallery_style')) {
     224    // don't do the look-ahead if styles are disabled
     225    add_action('template_redirect', 'gallery_scan'); // Add look ahead for [gallery] shortcode
     226}
  • gallery-shortcode-style-to-head/trunk/readme.txt

    r678539 r1117650  
    33Tags: gallery, shortcode, style, css, xhtml, head, validation
    44Requires at least: 3.0
    5 Tested up to: 3.5
     5Tested up to: 4.1
    66Stable tag: 2.3
    77
    88Moves the gallery shortcode styles to the head so it doesn't break XHTML
    9 validation; allows disabling or modifying the default gallery styles. 
     9validation; allows disabling or modifying the default gallery styles.
    1010
    1111== Description ==
     
    7171== Changelog ==
    7272
     73= 2.3.1 (2015-02-xx): =
     74* Plugin is now translateable.
     75* Code cleanup and support for latest WordPress.
     76
    7377= 2.3 (2013-03-08): =
    7478* Corrected 'undefined varibale' notice and RTL language detection.
Note: See TracChangeset for help on using the changeset viewer.