ImageWidgetDeprecated.php
203 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Deprecated image upload integration code to support legacy versions of WordPress |
| 4 | * @author Modern Tribe, Inc. |
| 5 | */ |
| 6 | |
| 7 | class ImageWidgetDeprecated { |
| 8 | |
| 9 | private $id_base; |
| 10 | |
| 11 | function __construct( $widget ) { |
| 12 | add_action( 'admin_init', array( $this, 'admin_setup' ) ); |
| 13 | $this->id_base = $widget->id_base; |
| 14 | } |
| 15 | |
| 16 | function admin_setup() { |
| 17 | global $pagenow; |
| 18 | if ( 'widgets.php' == $pagenow ) { |
| 19 | wp_enqueue_style( 'thickbox' ); |
| 20 | wp_enqueue_script( 'tribe-image-widget', plugins_url('resources/js/image-widget.deprecated.js', dirname(__FILE__)), array('thickbox'), Tribe_Image_Widget::VERSION, TRUE ); |
| 21 | } |
| 22 | elseif ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) { |
| 23 | wp_enqueue_script( 'tribe-image-widget-fix-uploader', plugins_url('resources/js/image-widget.deprecated.upload-fixer.js', dirname(__FILE__)), array('jquery'), Tribe_Image_Widget::VERSION, TRUE ); |
| 24 | add_filter( 'image_send_to_editor', array( $this,'image_send_to_editor'), 1, 8 ); |
| 25 | add_filter( 'gettext', array( $this, 'replace_text_in_thickbox' ), 1, 3 ); |
| 26 | add_filter( 'media_upload_tabs', array( $this, 'media_upload_tabs' ) ); |
| 27 | add_filter( 'image_widget_image_url', array( $this, 'https_cleanup' ) ); |
| 28 | } |
| 29 | $this->fix_async_upload_image(); |
| 30 | } |
| 31 | |
| 32 | function fix_async_upload_image() { |
| 33 | if(isset($_REQUEST['attachment_id'])) { |
| 34 | $id = (int) $_REQUEST['attachment_id']; |
| 35 | $GLOBALS['post'] = get_post( $id ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Test context to see if the uploader is being used for the image widget or for other regular uploads |
| 41 | * |
| 42 | * @author Modern Tribe, Inc. (Peter Chester) |
| 43 | */ |
| 44 | function is_sp_widget_context() { |
| 45 | if ( isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$this->id_base) !== false ) { |
| 46 | return true; |
| 47 | } elseif ( isset($_REQUEST['_wp_http_referer']) && strpos($_REQUEST['_wp_http_referer'],$this->id_base) !== false ) { |
| 48 | return true; |
| 49 | } elseif ( isset($_REQUEST['widget_id']) && strpos($_REQUEST['widget_id'],$this->id_base) !== false ) { |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Somewhat hacky way of replacing "Insert into Post" with "Insert into Widget" |
| 57 | * |
| 58 | * @param string $translated_text text that has already been translated (normally passed straight through) |
| 59 | * @param string $source_text text as it is in the code |
| 60 | * @param string $domain domain of the text |
| 61 | * @author Modern Tribe, Inc. (Peter Chester) |
| 62 | */ |
| 63 | function replace_text_in_thickbox($translated_text, $source_text, $domain) { |
| 64 | if ( $this->is_sp_widget_context() ) { |
| 65 | if ('Insert into Post' == $source_text) { |
| 66 | return __('Insert Into Widget', 'image_widget' ); |
| 67 | } |
| 68 | } |
| 69 | return $translated_text; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Filter image_end_to_editor results |
| 74 | * |
| 75 | * @param string $html |
| 76 | * @param int $id |
| 77 | * @param string $alt |
| 78 | * @param string $title |
| 79 | * @param string $align |
| 80 | * @param string $url |
| 81 | * @param array $size |
| 82 | * @return string javascript array of attachment url and id or just the url |
| 83 | * @author Modern Tribe, Inc. (Peter Chester) |
| 84 | */ |
| 85 | function image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { |
| 86 | // Normally, media uploader return an HTML string (in this case, typically a complete image tag surrounded by a caption). |
| 87 | // Don't change that; instead, send custom javascript variables back to opener. |
| 88 | // Check that this is for the widget. Shouldn't hurt anything if it runs, but let's do it needlessly. |
| 89 | if ( $this->is_sp_widget_context() ) { |
| 90 | if ($alt=='') $alt = $title; |
| 91 | ?> |
| 92 | <script type="text/javascript"> |
| 93 | // send image variables back to opener |
| 94 | var win = window.dialogArguments || opener || parent || top; |
| 95 | win.IW_html = '<?php echo addslashes($html); ?>'; |
| 96 | win.IW_img_id = '<?php echo $id; ?>'; |
| 97 | win.IW_alt = '<?php echo addslashes($alt); ?>'; |
| 98 | win.IW_caption = '<?php echo addslashes($caption); ?>'; |
| 99 | win.IW_title = '<?php echo addslashes($title); ?>'; |
| 100 | win.IW_align = '<?php echo esc_attr($align); ?>'; |
| 101 | win.IW_url = '<?php echo esc_url($url); ?>'; |
| 102 | win.IW_size = '<?php echo esc_attr($size); ?>'; |
| 103 | </script> |
| 104 | <?php |
| 105 | } |
| 106 | return $html; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Remove from url tab until that functionality is added to widgets. |
| 111 | * |
| 112 | * @param array $tabs |
| 113 | * @author Modern Tribe, Inc. (Peter Chester) |
| 114 | */ |
| 115 | function media_upload_tabs($tabs) { |
| 116 | if ( $this->is_sp_widget_context() ) { |
| 117 | unset($tabs['type_url']); |
| 118 | } |
| 119 | return $tabs; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adjust the image url on output to account for SSL. |
| 124 | * |
| 125 | * @param string $imageurl |
| 126 | * @return string $imageurl |
| 127 | * @author Modern Tribe, Inc. (Peter Chester) |
| 128 | */ |
| 129 | function https_cleanup( $imageurl = '' ) { |
| 130 | // TODO: 3.5: Document that this is deprecated??? |
| 131 | if( isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ) { |
| 132 | $imageurl = str_replace('http://', 'https://', $imageurl); |
| 133 | } else { |
| 134 | $imageurl = str_replace('https://', 'http://', $imageurl); |
| 135 | } |
| 136 | return $imageurl; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Retrieve resized image URL |
| 143 | * |
| 144 | * @param int $id Post ID or Attachment ID |
| 145 | * @param int $width desired width of image (optional) |
| 146 | * @param int $height desired height of image (optional) |
| 147 | * @return string URL |
| 148 | * @author Modern Tribe, Inc. (Peter Chester) |
| 149 | */ |
| 150 | public static function get_image_url( $id, $width=false, $height=false ) { |
| 151 | /**/ |
| 152 | // Get attachment and resize but return attachment path (needs to return url) |
| 153 | $attachment = wp_get_attachment_metadata( $id ); |
| 154 | $attachment_url = wp_get_attachment_url( $id ); |
| 155 | if (isset($attachment_url)) { |
| 156 | if ($width && $height) { |
| 157 | $uploads = wp_upload_dir(); |
| 158 | $imgpath = $uploads['basedir'].'/'.$attachment['file']; |
| 159 | if (WP_DEBUG) { |
| 160 | error_log(__CLASS__.'->'.__FUNCTION__.'() $imgpath = '.$imgpath); |
| 161 | } |
| 162 | $image = self::resize_image( $imgpath, $width, $height ); |
| 163 | if ( $image && !is_wp_error( $image ) ) { |
| 164 | $image = path_join( dirname($attachment_url), basename($image) ); |
| 165 | } else { |
| 166 | $image = $attachment_url; |
| 167 | } |
| 168 | } else { |
| 169 | $image = $attachment_url; |
| 170 | } |
| 171 | if (isset($image)) { |
| 172 | return $image; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public static function resize_image( $file, $max_w, $max_h ) { |
| 178 | if ( function_exists('wp_get_image_editor') ) { |
| 179 | $dest_file = $file; |
| 180 | if ( function_exists('wp_get_image_editor') ) { |
| 181 | $editor = wp_get_image_editor( $file ); |
| 182 | if ( is_wp_error( $editor ) ) |
| 183 | return $editor; |
| 184 | |
| 185 | $resized = $editor->resize( $max_w, $max_h ); |
| 186 | if ( is_wp_error( $resized ) ) |
| 187 | return $resized; |
| 188 | |
| 189 | $dest_file = $editor->generate_filename(); |
| 190 | $saved = $editor->save( $dest_file ); |
| 191 | |
| 192 | if ( is_wp_error( $saved ) ) |
| 193 | return $saved; |
| 194 | |
| 195 | } |
| 196 | return $dest_file; |
| 197 | } else { |
| 198 | return image_resize( $file, $max_w, $max_h ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | } |
| 203 |