PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/image-widget/lib/ImageWidgetDeprecated.php

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