1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | Plugin Name: Reuse Media Proof of Concept |
---|
5 | Plugin URI: http://simonwheatley.co.uk/wordpress/rum-poc/ |
---|
6 | Description: Adds an upload screen to the edit categories screen to prove how cool this would all be. |
---|
7 | Version: 0.1 |
---|
8 | Author: Simon Wheatley |
---|
9 | Author URI: http://simonwheatley.co.uk/wordpress/ |
---|
10 | */ |
---|
11 | |
---|
12 | /* Copyright 2010 Simon Wheatley |
---|
13 | |
---|
14 | This program is free software; you can redistribute it and/or modify |
---|
15 | it under the terms of the GNU General Public License as published by |
---|
16 | the Free Software Foundation; either version 2 of the License, or |
---|
17 | (at your option) any later version. |
---|
18 | |
---|
19 | This program is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | GNU General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | * @package RumPoc |
---|
32 | **/ |
---|
33 | class RumPoc { |
---|
34 | |
---|
35 | /** |
---|
36 | * Initiate! |
---|
37 | * |
---|
38 | * @return void |
---|
39 | **/ |
---|
40 | public function __construct() { |
---|
41 | if ( is_admin() ) { |
---|
42 | add_action( 'edit_category_form_fields', array( & $this, 'edit_category_form_fields' ) ); |
---|
43 | add_action( 'load-edit-tags.php', array( & $this, 'load_edit_tags' ) ); |
---|
44 | add_action( 'admin_head-edit-tags.php', array( & $this, 'print_scripts_edit_cat' ), 11 ); |
---|
45 | add_action( 'admin_head-media-upload.php', array( & $this, 'print_scripts_media_up' ), 11 ); |
---|
46 | add_action( 'admin_print_scripts-media-upload-popup', array( & $this, 'print_scripts_media_up' ), 11 ); |
---|
47 | add_filter( 'media_upload_tabs', array( & $this, 'media_upload_tabs' ) ); |
---|
48 | add_filter( 'gettext', array( & $this, 'translate' ), null, 3 ); |
---|
49 | add_action( 'post-flash-upload-ui', array( & $this, 'post_upload_ui' ) ); |
---|
50 | add_action( 'post-html-upload-ui', array( & $this, 'post_upload_ui' ) ); |
---|
51 | add_filter( 'get_media_item_args', array( & $this, 'get_media_item_args' ) ); |
---|
52 | add_filter( 'swfupload_post_params', array( & $this, 'swfupload_post_params' ) ); |
---|
53 | add_filter( 'swfupload_success_handler', array( & $this, 'swfupload_success_handler' ) ); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Hooks the WP load-edit-tags.php action |
---|
59 | * |
---|
60 | * @return void |
---|
61 | **/ |
---|
62 | public function load_edit_tags() { |
---|
63 | $tax = @ $_GET[ 'taxonomy' ]; |
---|
64 | if ( $tax != 'category' ) |
---|
65 | return; |
---|
66 | wp_enqueue_script( 'jquery' ); |
---|
67 | wp_enqueue_script( 'thickbox' ); |
---|
68 | wp_enqueue_style( 'thickbox' ); |
---|
69 | wp_enqueue_style( 'media' ); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Hooks the WP admin_print_scripts-edit-tags.php action to print scripts on the cat edit screen |
---|
74 | * |
---|
75 | * @return void |
---|
76 | **/ |
---|
77 | public function print_scripts_edit_cat() { |
---|
78 | // Some JS to resize the Thickbox file upload overlay and commandeer the reply from |
---|
79 | // the overlay (which is currently expecting a post edit window JS function) |
---|
80 | echo <<<END |
---|
81 | <script type="text/javascript"> |
---|
82 | //<![CDATA[ |
---|
83 | |
---|
84 | // Hackety Hack: Hijack the function name which WP is expecting to choose the image |
---|
85 | function send_to_editor(h) { |
---|
86 | // Obviously need to do more stuff here |
---|
87 | alert( "Received the object (we'd want to do things slightly differently here'): " ); |
---|
88 | alert( h ); |
---|
89 | tb_remove(); |
---|
90 | } |
---|
91 | |
---|
92 | // thickbox settings |
---|
93 | var tb_cat_position; |
---|
94 | (function($) { |
---|
95 | tb_cat_position = function() { |
---|
96 | var tbWindow = $('#TB_window'), width = $(window).width(), H = $(window).height(), W = ( 720 < width ) ? 720 : width; |
---|
97 | |
---|
98 | if ( tbWindow.size() ) { |
---|
99 | tbWindow.width( W - 50 ).height( H - 45 ); |
---|
100 | $('#TB_iframeContent').width( W - 50 ).height( H - 75 ); |
---|
101 | tbWindow.css({'margin-left': '-' + parseInt((( W - 50 ) / 2),10) + 'px'}); |
---|
102 | if ( typeof document.body.style.maxWidth != 'undefined' ) |
---|
103 | tbWindow.css({'top':'20px','margin-top':'0'}); |
---|
104 | }; |
---|
105 | |
---|
106 | return $('a.thickbox').each( function() { |
---|
107 | var href = $(this).attr('href'); |
---|
108 | if ( ! href ) return; |
---|
109 | href = href.replace(/&width=[0-9]+/g, ''); |
---|
110 | href = href.replace(/&height=[0-9]+/g, ''); |
---|
111 | $(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 ) ); |
---|
112 | }); |
---|
113 | }; |
---|
114 | |
---|
115 | $(window).resize(function(){ tb_cat_position(); }); |
---|
116 | |
---|
117 | })(jQuery); |
---|
118 | |
---|
119 | jQuery( document ).ready( function($){ tb_cat_position(); } ); |
---|
120 | |
---|
121 | //]]> |
---|
122 | </script> |
---|
123 | END; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Hooks the WP admin_print_scripts-edit-tags.php action to print scripts on the cat edit screen |
---|
128 | * |
---|
129 | * @return void |
---|
130 | **/ |
---|
131 | public function print_scripts_media_up() { |
---|
132 | error_log( "Print scripts on media up" ); |
---|
133 | echo <<<END |
---|
134 | <script type="text/javascript"> |
---|
135 | //<![CDATA[ |
---|
136 | |
---|
137 | function rmupoc_uploadSuccess(fileObj, serverData) { |
---|
138 | // if async-upload returned an error message, place it in the media item div and return |
---|
139 | if ( serverData.match('media-upload-error') ) { |
---|
140 | jQuery('#media-item-' + fileObj.id).html(serverData); |
---|
141 | return; |
---|
142 | } |
---|
143 | |
---|
144 | rmupoc_prepareMediaItem(fileObj, serverData); |
---|
145 | updateMediaForm(); |
---|
146 | |
---|
147 | // Increment the counter. |
---|
148 | if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) ) |
---|
149 | jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1); |
---|
150 | } |
---|
151 | |
---|
152 | function rmupoc_prepareMediaItem(fileObj, serverData) { |
---|
153 | var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id); |
---|
154 | // Move the progress bar to 100% |
---|
155 | jQuery('.bar', item).remove(); |
---|
156 | jQuery('.progress', item).hide(); |
---|
157 | |
---|
158 | try { |
---|
159 | if ( typeof topWin.tb_remove != 'undefined' ) |
---|
160 | topWin.jQuery('#TB_overlay').click(topWin.tb_remove); |
---|
161 | } catch(e){} |
---|
162 | |
---|
163 | // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server |
---|
164 | item.load('async-upload.php', { attachment_id:serverData, fetch:f, app: 'rmupoc', cat_id: 1 }, function(){ |
---|
165 | prepareMediaItemInit(fileObj); |
---|
166 | updateMediaForm(); |
---|
167 | }); |
---|
168 | } |
---|
169 | |
---|
170 | //]]> |
---|
171 | </script> |
---|
172 | END; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Hooks the (new) WP filter get_media_item_args |
---|
177 | * |
---|
178 | * @param array $args The args for get_media_item, after they've had the defaults applied |
---|
179 | * @return array The args, after we've amended them |
---|
180 | * @author Simon Wheatley |
---|
181 | **/ |
---|
182 | public function get_media_item_args( $args ) { |
---|
183 | $args[ 'send' ] = true; |
---|
184 | return $args; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Hooks the (new) WP swfupload_post_params filter |
---|
189 | * |
---|
190 | * @param array $params An associative array of post params to pass to SWFUpload |
---|
191 | * @return array An associative array of post params |
---|
192 | * @author Simon Wheatley |
---|
193 | **/ |
---|
194 | public function swfupload_post_params( $params ) { |
---|
195 | if ( isset( $_REQUEST[ 'app' ] ) && $_REQUEST[ 'app' ] == 'rmupoc' ) { |
---|
196 | $params[ 'app' ] = 'rmupoc'; |
---|
197 | $params[ 'cat_id' ] = (int) @ $_REQUEST[ 'cat_id' ]; |
---|
198 | } |
---|
199 | return $params; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Hooks the (new) WP swfupload_success_handler filter |
---|
204 | * |
---|
205 | * @param string $handler The current handler name (JS function name) |
---|
206 | * @return string The (amended) handler name |
---|
207 | * @author Simon Wheatley |
---|
208 | **/ |
---|
209 | public function swfupload_success_handler( $handler ) { |
---|
210 | $handler = 'rmupoc_uploadSuccess'; |
---|
211 | return $handler; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Hook the WP edit_category_form_fields action |
---|
216 | * |
---|
217 | * @param object $cat The WP taxonomy object for the category being edited |
---|
218 | * @return void |
---|
219 | **/ |
---|
220 | public function edit_category_form_fields( $cat ) { |
---|
221 | // Hopefully some media upload JS will add sizes on |
---|
222 | $media_upload_iframe_src = "media-upload.php?cat_id={$cat->term_id}&app=rmupoc&TB_iframe=true&cb=" . rand(); |
---|
223 | $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image"); |
---|
224 | $image_link = __( 'Add a Category Image', 'category-images-ii' ); |
---|
225 | echo "<tr><th>Category Image</th><td><a href='$image_upload_iframe_src' id='add_image' class='thickbox' title='$image_link'>$image_link</a></td></tr>"; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Hooks the WP media_upload_tabs filter |
---|
230 | * |
---|
231 | * @param array $tabs An associative array of tab names |
---|
232 | * @return array An associative array of tab names |
---|
233 | **/ |
---|
234 | public function media_upload_tabs( $tabs ) { |
---|
235 | if ( isset( $_REQUEST[ 'app' ] ) && $_REQUEST[ 'app' ] == 'rmupoc' ) |
---|
236 | unset( $tabs[ 'type_url' ] ); |
---|
237 | return $tabs; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * Hooks the WP post-flash-upload-ui & post-html-upload-ui actions |
---|
242 | * |
---|
243 | * @return void |
---|
244 | **/ |
---|
245 | public function post_upload_ui() { |
---|
246 | echo "<input type='hidden' name='app' value='rmupoc' />"; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * Hooks the WP gettext function to monkey with translations |
---|
251 | * |
---|
252 | * @param string $translation The current translation |
---|
253 | * @param string $original The text before translation |
---|
254 | * @param string $domain The gettext domain |
---|
255 | * @return void |
---|
256 | **/ |
---|
257 | public function translate( $translation, $original, $domain ) { |
---|
258 | // Dirty dirty hack |
---|
259 | if ( $original == 'Insert into Post' ) |
---|
260 | error_log( "Change button: " . print_r( $_REQUEST, true ) ); |
---|
261 | if ( isset( $_REQUEST[ 'app' ] ) && $_REQUEST[ 'app' ] == 'rmupoc' ) |
---|
262 | if ( $original == 'Insert into Post' ) |
---|
263 | return __( 'Set as Category Image', 'category-images-ii' ); |
---|
264 | return $translation; |
---|
265 | } |
---|
266 | |
---|
267 | } // END RumPoc class |
---|
268 | |
---|
269 | $rum_poc = new RumPoc(); |
---|
270 | |
---|
271 | |
---|
272 | ?> |
---|