Advertisement
Guest User

widget-image-field.php multiple images

a guest
Jun 10th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2. /*
  3.     Plugin Name: Widget Image Field
  4.     Plugin URI: http://mondaybynoon.com/wordpress/widget-image-field/
  5.     Description: This prepares an image field for use within your own Widgets
  6.     Author: Jonathan Christopher
  7.     Version: 0.3
  8.     Author URI: http://mondaybynoon.com/
  9. */
  10.  
  11. if( !defined( 'IS_ADMIN' ) )
  12.     define( 'IS_ADMIN',  is_admin() );
  13.  
  14. define( 'WIDGET_IMAGE_FIELD_VERSION', '0.3' );
  15. define( 'WIDGET_IMAGE_FIELD_DIR', WP_PLUGIN_DIR . '/' . basename( dirname( __FILE__ ) ) );
  16. define( 'WIDGET_IMAGE_FIELD_URL', rtrim( plugin_dir_url( __FILE__ ), '/' ) );
  17.  
  18.  
  19. class WidgetImageField
  20. {
  21.     private $image_id;
  22.     private $src;
  23.     private $width;
  24.     private $height;
  25.     private $widget_field;
  26.     private $widget = null;
  27.  
  28.     function __construct( $widget = null, $image_id = 0 )
  29.     {
  30.         $uri        = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : NULL ;
  31.         $file       = basename( parse_url( $uri, PHP_URL_PATH ) );
  32.  
  33.         // if we're on the Widgets page
  34.         if( $uri && in_array( $file, array( 'widgets.php' ) ) && IS_ADMIN )
  35.         {
  36.             wp_enqueue_script( 'jquery-ui-core' );
  37.             wp_enqueue_script( 'thickbox' );
  38.             wp_enqueue_script( 'media-upload' );
  39.             wp_enqueue_script( 'widgetimagefield', WIDGET_IMAGE_FIELD_URL . '/script.js', array( 'jquery', 'jquery-ui-core', 'thickbox', 'media-upload' ), false, true );
  40.  
  41.             wp_enqueue_style( 'thickbox' );
  42.             wp_enqueue_style( 'widgetimagefield', WIDGET_IMAGE_FIELD_URL . '/style.css' );
  43.         }
  44.  
  45.         // set our properties
  46.         $this->widget = $widget;
  47.         if( $image_id )
  48.         {
  49.             $this->image_id         = intval( $image_id );
  50.             $this->src              = $this->get_image_src( $this->image_id );
  51.             $this->width            = $this->get_image_width( $this->image_id );
  52.             $this->height           = $this->get_image_height( $this->image_id );
  53.             $this->widget_field     = $this->get_widget_field( $widget, $this->image_id );
  54.         }
  55.         else
  56.             $this->image_id = $image_id;
  57.     }
  58.  
  59.     function get_image( $size = 'thumbnail' )
  60.     {
  61.         $image = false;
  62.  
  63.         if( $this->image_id )
  64.         {
  65.             $image = wp_get_attachment_image_src( $this->image_id, $size );
  66.         }
  67.  
  68.         return $image;
  69.     }
  70.  
  71.     function get_image_src( $size = 'thumbnail' )
  72.     {
  73.         $src = false;
  74.  
  75.         if( $this->image_id )
  76.         {
  77.             $image      = $this->get_image( $size );
  78.             $src        = $image[0];
  79.         }
  80.  
  81.         return $src;
  82.     }
  83.  
  84.     function get_image_dimensions( $size = 'thumbnail' )
  85.     {
  86.         $dimensions = array( null, null );
  87.  
  88.         if( $this->image_id )
  89.         {
  90.             $image          = $this->get_image( $size );
  91.             $dimensions     = array( $image[1], $image[2] );
  92.         }
  93.  
  94.         return $dimensions;
  95.     }
  96.  
  97.     function get_image_width( $size = 'thumbnail' )
  98.     {
  99.         $width = false;
  100.  
  101.         if( $this->image_id )
  102.         {
  103.             $dimensions     = $this->get_image_dimensions( $size );
  104.             $width          = $dimensions[0];
  105.         }
  106.  
  107.         return $width;
  108.     }
  109.  
  110.     function get_image_height( $size = 'thumbnail' )
  111.     {
  112.         $height = false;
  113.  
  114.         if( $this->image_id )
  115.         {
  116.             $dimensions     = $this->get_image_dimensions( $size );
  117.             $height         = $dimensions[1];
  118.         }
  119.  
  120.         return $height;
  121.     }
  122.  
  123.     function get_widget_field( $field_name = null )
  124.     {
  125.         $field = false;
  126.         if( $this->widget && ( isset( $this->widget->image_field ) || $field_name ) )
  127.         {
  128.             $fieldnumber = is_int( $field_name ) ? $field_name : 0;
  129.             $field  = "<div class='iti-image-widget-field'><div class='iti-image-widget-image' id='" . $this->widget->id . "'>";
  130.             $field .= "<input type='hidden' style='display:none;' id='" . $this->widget->get_field_id( $this->widget->image_field . $fieldnumber ) . "' name='" . $this->widget->get_field_name( $this->widget->image_field . $fieldnumber ) . "' value='" . $this->image_id . "' />";
  131.                        
  132.             if( $this->image_id )
  133.             {
  134.                 $field .= "<img src='" . $this->src . "' width='" . $this->width . "' height='" . $this->height . "' />";
  135.             }
  136.  
  137.             $field .= "</div>";
  138.  
  139.             $field .= "<a class='button iti-image-widget-trigger' href='media-upload.php?TB_iframe=1&amp;width=640&amp;height=1500' title='" . __( 'Choose Image' ) . "'>";
  140.             $field .= __( 'Choose Image' );
  141.             $field .= "</a></div>";
  142.         }
  143.         return $field;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement