Plugin Directory

Changeset 705217


Ignore:
Timestamp:
04/29/2013 05:20:06 AM (13 years ago)
Author:
sewpafly
Message:

3.0: Media Library hook

Location:
post-thumbnail-extras
Files:
9 added
3 edited
5 copied

Legend:

Unmodified
Added
Removed
  • post-thumbnail-extras/tags/3.0/php/options.php

    r697354 r705217  
    88    public function admin_init() {
    99        add_settings_field( 'ptx-post-thumbnails'
    10             , '<b>' . __('Post Thumbnail Extra Sizes', PTX_DOMAIN) . '</b>'
     10            , '<b>' . __( 'Post Thumbnail Extra Sizes', PTX_DOMAIN ) . '</b>'
    1111                . '&nbsp;<a class="ptx-add-thumb" href="#">+</a>'
    1212            , array( $this, 'create_post_thumbnails_html' )
     
    1414            , 'default'
    1515        );
     16        // Register the settings to be handled by wordpress and the callback function
    1617        register_setting( 'media'
    1718            , 'ptx_post_thumbnails'
    1819            , array( $this, 'sanitize_post_thumbnails' ) );
     20
     21        // Add a section for displaying other Post Thumbnails and their metadata
     22        // (e.g. width, height, crop)
     23        add_settings_section( 'ptx-other-post-thumbnails'
     24            , __( 'Post Thumbnail Extras - Display other post thumbnails', PTX_DOMAIN )
     25            , array( $this, 'other_post_thumbnails_html' )
     26            , 'media'
     27        );
    1928    }
    2029
     
    121130     */
    122131    public function sanitize_post_thumbnails( $input ) {
     132        $too_large = 2000;
    123133        //add_settings_error( 'ptx-post-thumbnails'
    124134        //    , 'not-really-helpful'
     
    126136        //    , 'updated'
    127137        //);
    128 
    129138        $new_input = array();
     139
     140        if ( ! is_array( $input ) )
     141            return $new_input;
     142
    130143        $counter = 0;
    131144        $pattern = "/[^[:alnum:]-]+/";
     
    155168            $thumbnail['crop'] = ( isset( $thumbnail['crop'] ) && $thumbnail['crop'] );
    156169
     170            if ( $too_large < $thumbnail['width'] || $too_large < $thumbnail['height'] ) {
     171                add_settings_error( 'ptx-post-thumbnails'
     172                    , NULL
     173                    , sprintf( __( "Consider using 0 for an unlimited size side (%s)", PTX_DOMAIN ), $thumbnail['name'] )
     174                    , 'updated');
     175            }
     176
     177
    157178            $new_input[] = $thumbnail;
    158179        }
     
    168189        return $new_input;
    169190    }
     191
     192    /**
     193     * Display post thumbnail metadata for other post thumbnails defined elsewhere
     194     */
     195    public function other_post_thumbnails_html() {
     196        $thumbnails = $this->get_other_intermediate_image_sizes();
     197
     198        if ( ! isset( $thumbnails ) || 0 == count( $thumbnails ) ) {
     199            _e( "No additional image sizes defined", PTX_DOMAIN );
     200            return;
     201        }
     202
     203        $name = __( 'Name', PTX_DOMAIN );
     204        $width = __( 'Width', PTX_DOMAIN );
     205        $height = __( 'Height', PTX_DOMAIN );
     206        $crop = __( 'Crop', PTX_DOMAIN );
     207        $output = <<<EOT
     208<style type="text/css" media="all">
     209    #ptx-other-post-thumbnails {
     210        width: 50%%;
     211    }
     212    .widefat thead th:first-of-type {
     213        padding-left: 8px;
     214    }
     215    .widefat.media .check-column {
     216        padding-bottom: 8px;
     217    }
     218</style>
     219<table id="ptx-other-post-thumbnails" class="wp-list-table widefat fixed media" cellspacing="0">
     220    <thead>
     221        <tr>
     222            <th class="manage-column column-name check-column" style="">
     223                $name
     224            </th>
     225            <th class="manage-column check-column" style="">
     226                $width
     227            </th>
     228            <th class="manage-column check-column" style="">
     229                $height
     230            </th>
     231            <th class="manage-column check-column" style="">
     232                $crop
     233            </th>
     234        </tr>
     235    </thead>
     236    <tbody>
     237        %s
     238    </tbody>
     239</table>
     240EOT;
     241        $body = "";
     242        $row = "<tr><td>%s</td><td>%d</td><td>%d</td><td>%s</td></tr>";
     243        foreach ( $thumbnails as $name => $thumbnail ) {
     244            $body .= sprintf( $row
     245                , $name
     246                , $thumbnail['width']
     247                , $thumbnail['height']
     248                , ( true == $thumbnail['crop'] ) ? __( 'True' ) : __( 'False' )
     249            );
     250        }
     251
     252        print( sprintf( $output, $body ) );
     253    }
     254
     255    /**
     256     * Ignore 'large', 'medium', 'thumbnail' and any ptx_thumbs
     257     */
     258    public function get_other_intermediate_image_sizes() {
     259        global $_wp_additional_image_sizes;
     260        $filter = array( 'large', 'medium', 'thumbnail' );
     261
     262        $ptx_post_thumbnails = get_option( 'ptx_post_thumbnails' );
     263        if ( isset( $ptx_post_thumbnails ) and is_array( $ptx_post_thumbnails ) ){
     264            foreach ( get_option( 'ptx_post_thumbnails' ) as $thumb ) {
     265                $filter[] = $thumb['name'];
     266            }
     267        }
     268
     269        foreach ( $_wp_additional_image_sizes as $name => $thumb ) {
     270            if ( ! in_array( $name, $filter ) ) {
     271                $return[$name] = $thumb;
     272            }
     273        }
     274        return $return;
     275    }
    170276}
    171277
     278$PTX_OPTIONS = new PTXOptions();
  • post-thumbnail-extras/tags/3.0/php/shortcode.php

    r696870 r705217  
    44    public function __construct() {
    55        add_shortcode( 'pt', array( $this, 'parse_shortcode' ) );
     6        //add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
     7        add_action( 'add_meta_boxes_post', array( $this, 'enqueue_scripts' ) );
     8        add_filter( 'media_view_strings', array( $this, 'media_strings' ), 10, 2 );
     9        add_filter( 'image_size_names_choose', array( $this, 'image_sizes' ) );
     10        add_filter( 'wp_prepare_attachment_for_js', array( $this, 'fix_attachment' ), 10, 3 );
    611    }
    712
     
    2530        return wp_get_attachment_image( $id, $size, false, array( 'class' => $class ) );
    2631    }
     32
     33    /**
     34     * Enqueue scripts for the Edit Post page
     35     */
     36    public function enqueue_scripts() {
     37        wp_enqueue_script( 'ptx-shortcode'
     38            , PTX_PLUGINURL . 'js/media-shortcode.js'
     39            , array('media-views', 'media-editor')
     40            , false, true
     41        );
     42    }
     43
     44    /**
     45     * The i18n strings for the Edit Post page
     46     */
     47    public function media_strings( $strings, $post ) {
     48        $strings['PTXInsertShortcode'] = __( 'Insert Shortcode', PTX_DOMAIN );
     49        return $strings;
     50    }
     51
     52    /**
     53     * Get the list of image sizes
     54     */
     55    public function image_sizes ( $sizes ) {
     56        $potential_sizes = get_intermediate_image_sizes();
     57        foreach ( $potential_sizes as $size ) {
     58            if ( 'post-thumbnail' != $size && ! in_array( $size, $sizes ) ) {
     59                $sizes[$size] = $size;
     60            }
     61        }
     62        return $sizes;
     63    }
     64
     65    /**
     66     * In the prepare_attachment_for_js ajax call, it marshalls a list of
     67     * attachment metadata used for adding to the media-library sidebar. If we want
     68     * the other post thumbnails to show up, we have to add them here.
     69     *
     70     * _Sidenotes_:
     71     *
     72     * 1. If the post-thumbnail has not yet been generated, and the user selects
     73     *    a post-thumbnail and does an "Insert into Post", it will link to the fullsize
     74     *    image and the browser will resize it inefficiently.
     75     *
     76     * 2. I tried using the image_downsize filter, but that caused an infinite loop
     77     *    with the wp_get_attachment_* methods.
     78     *
     79     * See <wp-includes/media.php> for more information.
     80     *
     81     * @param $response - the metadata being returned to the client
     82     * @param $attachment = wp_get_attachment_metadata for the post
     83     * @param $meta - Other metadata
     84     */
     85    public function fix_attachment( $response, $attachment, $meta ) {
     86        $predefined = array( 'thumbnail', 'medium', 'large', 'full' );
     87        $possible_sizes = apply_filters( 'image_size_names_choose', array() );
     88        foreach ( $predefined as $size ) {
     89            if ( isset( $possible_sizes[$size] ) ) {
     90                unset( $possible_sizes[$size] );
     91            }
     92        }
     93
     94
     95        foreach ( $possible_sizes as $size => $label ) {
     96            if ( isset( $response['sizes'][$size] ) )
     97                continue;
     98            $img = wp_get_attachment_image_src( $response['id'], $size );
     99            $response['sizes'][$size] = array(
     100                'height' => $img[2],
     101                'width' => $img[1],
     102                'url' => $img[0],
     103                'orientation' => ( $img[2] > $img[1] ) ? 'portrait' : 'landscape'
     104            );
     105        }
     106        return $response;
     107    }
    27108}
    28109
     110$PTX_SHORTCODE = new PTXShortcode();
  • post-thumbnail-extras/tags/3.0/post-thumbnail-extras.php

    r697354 r705217  
    55Author: sewpafly
    66Author URI: http://sewpafly.github.io/post-thumbnail-editor/
    7 Version: 2.1
     7Version: 3.0
    88Description: Little things that make using post thumbnails easier
    99*/
     
    1313 */
    1414define( 'PTX_DOMAIN', 'post-thumbnail-extras' );
     15define( 'PTX_PLUGINURL', plugins_url(basename( dirname(__FILE__))) . "/");
    1516
    1617class PostThumbnailExtras {
     
    3334         */
    3435        $this->load_requires();
    35         $s = new PTXShortcode();
    36         $o = new PTXOptions();
    3736    }
    3837
  • post-thumbnail-extras/trunk/php/options.php

    r697354 r705217  
    88    public function admin_init() {
    99        add_settings_field( 'ptx-post-thumbnails'
    10             , '<b>' . __('Post Thumbnail Extra Sizes', PTX_DOMAIN) . '</b>'
     10            , '<b>' . __( 'Post Thumbnail Extra Sizes', PTX_DOMAIN ) . '</b>'
    1111                . '&nbsp;<a class="ptx-add-thumb" href="#">+</a>'
    1212            , array( $this, 'create_post_thumbnails_html' )
     
    1414            , 'default'
    1515        );
     16        // Register the settings to be handled by wordpress and the callback function
    1617        register_setting( 'media'
    1718            , 'ptx_post_thumbnails'
    1819            , array( $this, 'sanitize_post_thumbnails' ) );
     20
     21        // Add a section for displaying other Post Thumbnails and their metadata
     22        // (e.g. width, height, crop)
     23        add_settings_section( 'ptx-other-post-thumbnails'
     24            , __( 'Post Thumbnail Extras - Display other post thumbnails', PTX_DOMAIN )
     25            , array( $this, 'other_post_thumbnails_html' )
     26            , 'media'
     27        );
    1928    }
    2029
     
    121130     */
    122131    public function sanitize_post_thumbnails( $input ) {
     132        $too_large = 2000;
    123133        //add_settings_error( 'ptx-post-thumbnails'
    124134        //    , 'not-really-helpful'
     
    126136        //    , 'updated'
    127137        //);
    128 
    129138        $new_input = array();
     139
     140        if ( ! is_array( $input ) )
     141            return $new_input;
     142
    130143        $counter = 0;
    131144        $pattern = "/[^[:alnum:]-]+/";
     
    155168            $thumbnail['crop'] = ( isset( $thumbnail['crop'] ) && $thumbnail['crop'] );
    156169
     170            if ( $too_large < $thumbnail['width'] || $too_large < $thumbnail['height'] ) {
     171                add_settings_error( 'ptx-post-thumbnails'
     172                    , NULL
     173                    , sprintf( __( "Consider using 0 for an unlimited size side (%s)", PTX_DOMAIN ), $thumbnail['name'] )
     174                    , 'updated');
     175            }
     176
     177
    157178            $new_input[] = $thumbnail;
    158179        }
     
    168189        return $new_input;
    169190    }
     191
     192    /**
     193     * Display post thumbnail metadata for other post thumbnails defined elsewhere
     194     */
     195    public function other_post_thumbnails_html() {
     196        $thumbnails = $this->get_other_intermediate_image_sizes();
     197
     198        if ( ! isset( $thumbnails ) || 0 == count( $thumbnails ) ) {
     199            _e( "No additional image sizes defined", PTX_DOMAIN );
     200            return;
     201        }
     202
     203        $name = __( 'Name', PTX_DOMAIN );
     204        $width = __( 'Width', PTX_DOMAIN );
     205        $height = __( 'Height', PTX_DOMAIN );
     206        $crop = __( 'Crop', PTX_DOMAIN );
     207        $output = <<<EOT
     208<style type="text/css" media="all">
     209    #ptx-other-post-thumbnails {
     210        width: 50%%;
     211    }
     212    .widefat thead th:first-of-type {
     213        padding-left: 8px;
     214    }
     215    .widefat.media .check-column {
     216        padding-bottom: 8px;
     217    }
     218</style>
     219<table id="ptx-other-post-thumbnails" class="wp-list-table widefat fixed media" cellspacing="0">
     220    <thead>
     221        <tr>
     222            <th class="manage-column column-name check-column" style="">
     223                $name
     224            </th>
     225            <th class="manage-column check-column" style="">
     226                $width
     227            </th>
     228            <th class="manage-column check-column" style="">
     229                $height
     230            </th>
     231            <th class="manage-column check-column" style="">
     232                $crop
     233            </th>
     234        </tr>
     235    </thead>
     236    <tbody>
     237        %s
     238    </tbody>
     239</table>
     240EOT;
     241        $body = "";
     242        $row = "<tr><td>%s</td><td>%d</td><td>%d</td><td>%s</td></tr>";
     243        foreach ( $thumbnails as $name => $thumbnail ) {
     244            $body .= sprintf( $row
     245                , $name
     246                , $thumbnail['width']
     247                , $thumbnail['height']
     248                , ( true == $thumbnail['crop'] ) ? __( 'True' ) : __( 'False' )
     249            );
     250        }
     251
     252        print( sprintf( $output, $body ) );
     253    }
     254
     255    /**
     256     * Ignore 'large', 'medium', 'thumbnail' and any ptx_thumbs
     257     */
     258    public function get_other_intermediate_image_sizes() {
     259        global $_wp_additional_image_sizes;
     260        $filter = array( 'large', 'medium', 'thumbnail' );
     261
     262        $ptx_post_thumbnails = get_option( 'ptx_post_thumbnails' );
     263        if ( isset( $ptx_post_thumbnails ) and is_array( $ptx_post_thumbnails ) ){
     264            foreach ( get_option( 'ptx_post_thumbnails' ) as $thumb ) {
     265                $filter[] = $thumb['name'];
     266            }
     267        }
     268
     269        foreach ( $_wp_additional_image_sizes as $name => $thumb ) {
     270            if ( ! in_array( $name, $filter ) ) {
     271                $return[$name] = $thumb;
     272            }
     273        }
     274        return $return;
     275    }
    170276}
    171277
     278$PTX_OPTIONS = new PTXOptions();
  • post-thumbnail-extras/trunk/php/shortcode.php

    r696870 r705217  
    44    public function __construct() {
    55        add_shortcode( 'pt', array( $this, 'parse_shortcode' ) );
     6        //add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
     7        add_action( 'add_meta_boxes_post', array( $this, 'enqueue_scripts' ) );
     8        add_filter( 'media_view_strings', array( $this, 'media_strings' ), 10, 2 );
     9        add_filter( 'image_size_names_choose', array( $this, 'image_sizes' ) );
     10        add_filter( 'wp_prepare_attachment_for_js', array( $this, 'fix_attachment' ), 10, 3 );
    611    }
    712
     
    2530        return wp_get_attachment_image( $id, $size, false, array( 'class' => $class ) );
    2631    }
     32
     33    /**
     34     * Enqueue scripts for the Edit Post page
     35     */
     36    public function enqueue_scripts() {
     37        wp_enqueue_script( 'ptx-shortcode'
     38            , PTX_PLUGINURL . 'js/media-shortcode.js'
     39            , array('media-views', 'media-editor')
     40            , false, true
     41        );
     42    }
     43
     44    /**
     45     * The i18n strings for the Edit Post page
     46     */
     47    public function media_strings( $strings, $post ) {
     48        $strings['PTXInsertShortcode'] = __( 'Insert Shortcode', PTX_DOMAIN );
     49        return $strings;
     50    }
     51
     52    /**
     53     * Get the list of image sizes
     54     */
     55    public function image_sizes ( $sizes ) {
     56        $potential_sizes = get_intermediate_image_sizes();
     57        foreach ( $potential_sizes as $size ) {
     58            if ( 'post-thumbnail' != $size && ! in_array( $size, $sizes ) ) {
     59                $sizes[$size] = $size;
     60            }
     61        }
     62        return $sizes;
     63    }
     64
     65    /**
     66     * In the prepare_attachment_for_js ajax call, it marshalls a list of
     67     * attachment metadata used for adding to the media-library sidebar. If we want
     68     * the other post thumbnails to show up, we have to add them here.
     69     *
     70     * _Sidenotes_:
     71     *
     72     * 1. If the post-thumbnail has not yet been generated, and the user selects
     73     *    a post-thumbnail and does an "Insert into Post", it will link to the fullsize
     74     *    image and the browser will resize it inefficiently.
     75     *
     76     * 2. I tried using the image_downsize filter, but that caused an infinite loop
     77     *    with the wp_get_attachment_* methods.
     78     *
     79     * See <wp-includes/media.php> for more information.
     80     *
     81     * @param $response - the metadata being returned to the client
     82     * @param $attachment = wp_get_attachment_metadata for the post
     83     * @param $meta - Other metadata
     84     */
     85    public function fix_attachment( $response, $attachment, $meta ) {
     86        $predefined = array( 'thumbnail', 'medium', 'large', 'full' );
     87        $possible_sizes = apply_filters( 'image_size_names_choose', array() );
     88        foreach ( $predefined as $size ) {
     89            if ( isset( $possible_sizes[$size] ) ) {
     90                unset( $possible_sizes[$size] );
     91            }
     92        }
     93
     94
     95        foreach ( $possible_sizes as $size => $label ) {
     96            if ( isset( $response['sizes'][$size] ) )
     97                continue;
     98            $img = wp_get_attachment_image_src( $response['id'], $size );
     99            $response['sizes'][$size] = array(
     100                'height' => $img[2],
     101                'width' => $img[1],
     102                'url' => $img[0],
     103                'orientation' => ( $img[2] > $img[1] ) ? 'portrait' : 'landscape'
     104            );
     105        }
     106        return $response;
     107    }
    27108}
    28109
     110$PTX_SHORTCODE = new PTXShortcode();
  • post-thumbnail-extras/trunk/post-thumbnail-extras.php

    r697354 r705217  
    55Author: sewpafly
    66Author URI: http://sewpafly.github.io/post-thumbnail-editor/
    7 Version: 2.1
     7Version: 3.0
    88Description: Little things that make using post thumbnails easier
    99*/
     
    1313 */
    1414define( 'PTX_DOMAIN', 'post-thumbnail-extras' );
     15define( 'PTX_PLUGINURL', plugins_url(basename( dirname(__FILE__))) . "/");
    1516
    1617class PostThumbnailExtras {
     
    3334         */
    3435        $this->load_requires();
    35         $s = new PTXShortcode();
    36         $o = new PTXOptions();
    3736    }
    3837
Note: See TracChangeset for help on using the changeset viewer.