Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 414 lines (337 sloc) 9.93 kB
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
1 <?php
2
3 /**
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
4 * @author Dimas Begunoff
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
5 * @copyright Copyright (c) 2011, Dimas Begunoff, http://farinspace.com/
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
6 * @license http://en.wikipedia.org/wiki/MIT_License The MIT License
7 * @package WPAlchemy
7e32edc @herewithme Fix preg match when a media have a simple/double quotes on this title…
herewithme authored
8 * @version 0.2.1
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
9 * @link http://github.com/farinspace/wpalchemy/
10 * @link http://farinspace.com/
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
11 */
12
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
13 class WPAlchemy_MediaAccess
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
14 {
15 /**
16 * User defined identifier for the css class name of the HTML button element,
17 * used when pairing the field and button elements
18 *
19 * @since 0.1
20 * @access public
21 * @var string required
22 */
23 public $button_class_name = 'mediabutton';
24
25 /**
26 * User defined identifier for the css class name of the HTML field element,
27 * used when pairing the field and button elements
28 *
29 * @since 0.1
30 * @access public
31 * @var string required
32 */
33 public $field_class_name = 'mediafield';
34
35 /**
36 * User defined label for the insert button in the media upload box, this
37 * can be set once or per field and button pair.
38 *
39 * @since 0.1
40 * @access public
41 * @var string optional
42 * @see setInsertButtonLabel()
43 */
44 public $insert_button_label = null;
45
46 /**
47 * Used to track the current groupname for pairing a field and button.
48 *
49 * @since 0.1
50 * @access private
51 * @var string
52 * @see setGroupName()
53 */
54 private $groupname = null;
55
56 /**
57 * Used to track the current tab for the media upload box.
58 *
59 * @since 0.1
60 * @access private
61 * @var string
62 * @see setTab()
63 */
64 private $tab = null;
65
66 /**
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
67 * MediaAccess class
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
68 *
69 * @since 0.1
70 * @access public
71 * @param array $a
72 */
73 public function __construct(array $a = array())
74 {
75 foreach ($a as $n => $v)
76 {
77 $this->$n = $v;
78 }
79
80 if ( ! defined('WPALCHEMY_SEND_TO_EDITOR_ENABLED'))
81 {
82 add_action('admin_footer', array($this, 'init'));
83
84 define('WPALCHEMY_SEND_TO_EDITOR_ENABLED', true);
85 }
86 }
87
88 /**
89 * Used to generate short unique/random names
90 *
91 * @since 0.1
92 * @access public
93 * @return string
94 */
95 private function getName()
96 {
97 return substr(md5(microtime() . rand()), rand(0,25), 6);
98 }
99
100 /**
101 * Used to set the insert button label in the media upload box, this can be
102 * set once or per field and button pair.
103 *
104 * @since 0.1
105 * @access public
106 * @param string $label button label/title
107 * @return object $this
108 * @see setGroupName()
109 */
110 public function setInsertButtonLabel($label = 'Insert')
111 {
112 $this->insert_button_label = $label;
113
114 return $this;
115 }
116
117 public function setTab($name)
118 {
119 $this->tab = $name;
120
121 $this;
122 }
123
124 /**
125 * Used before calls to getField(), getButton() or getButtonClass() to set
126 * the groupname to pair a field and button element.
127 *
128 * @since 0.1
129 * @access public
130 * @param string $name unique name per pair of field and button
131 * @return object $this
132 * @see setInsertButtonLabel()
133 */
134 public function setGroupName($name)
135 {
136 $this->groupname = $name;
137
138 return $this;
139 }
140
141 /**
142 * Used to insert a form field of type "text", this should be paired with a
143 * button element. The name and value attributes are required.
144 *
145 * @since 0.1
146 * @access public
147 * @param array $attr INPUT tag parameters
148 * @return HTML
149 * @see getButton()
150 */
151 public function getField(array $attr)
152 {
153 $groupname = isset($attr['groupname']) ? $attr['groupname'] : $this->groupname ;
154
155 $attr_default = array
156 (
157 'type' => 'text',
158 'class' => $this->field_class_name . '-' . $groupname,
159 );
160
161 ###
162
163 if (isset($attr['class']))
164 {
165 $attr['class'] = $attr_default['class'] . ' ' . trim($attr['class']);
166 }
167
168 $attr = array_merge($attr_default, $attr);
169
170 ###
171
172 $elem_attr = array();
173
174 foreach ($attr as $n => $v)
175 {
176 array_push($elem_attr, $n . '="' . $v . '"');
177 }
178
179 ###
180
181 return '<input ' . implode(' ', $elem_attr) . '/>';
182 }
183
184 /**
185 * Used to get the link used for the button element. If creating custom
186 * buttons, this method should be used to get the link needed for proper
187 * functionality.
188 *
189 * @since 0.1
190 * @access public
191 * @param string $tab name that the media upload box will initially load
192 * @return string link
193 * @see getButtonClass(), getButton()
194 */
195 public function getButtonLink($tab = null)
196 {
197 // this is set even for new posts/pages
198 global $post_ID; //wp
199
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
200 $tab = ! empty($tab) ? $tab : $this->tab ;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
201
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
202 $tab = ! empty($tab) ? $tab : 'library' ;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
203
204 return 'media-upload.php?post_id=' . $post_ID . '&tab=' . $tab . '&TB_iframe=1';
205 }
206
207 /**
208 * Used to get the CSS class name(s) used for the button element. If
209 * creating custom buttons, this method should be used to get the css class
210 * names needed for proper functionality.
211 *
212 * @since 0.1
213 * @access public
214 * @param string $groupname name used when pairing a text field and button
215 * @return string css class(es)
216 * @see getButtonLink(), getButton()
217 */
218 public function getButtonClass($groupname = null)
219 {
220 $groupname = isset($groupname) ? $groupname : $this->groupname ;
221
222 return $this->button_class_name . '-' . $groupname . ' thickbox';
223 }
224
225 /**
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
226 * Used to get the CSS class name used for the field element. If
227 * creating a custom field, this method should be used to get the css class
228 * name needed for proper functionality.
229 *
230 * @since 0.2
231 * @access public
232 * @param string $groupname name used when pairing a text field and button
233 * @return string css class(es)
234 * @see getButtonClass(), getField()
235 */
236 public function getFieldClass($groupname = null)
237 {
238 $groupname = isset($groupname) ? $groupname : $this->groupname ;
239
240 return $this->field_class_name . '-' . $groupname;
241 }
242
243 /**
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
244 * Used to insert a WordPress styled button, should be paired with a text
245 * field element.
246 *
247 * @since 0.1
248 * @access public
249 * @return HTML
250 * @see getField(), getButtonClass(), getButtonLink()
251 */
252 public function getButton(array $attr = array())
253 {
254 $groupname = isset($attr['groupname']) ? $attr['groupname'] : $this->groupname ;
255
256 $tab = isset($attr['tab']) ? $attr['tab'] : $this->tab ;
257
258 $attr_default = array
259 (
98fa6c5 @farinspace changed button text label from "text" to "label"
authored
260 'label' => 'Add Media',
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
261 'href' => $this->getButtonLink($tab),
262 'class' => $this->getButtonClass($groupname) . ' button',
263 );
264
265 if (isset($this->insert_button_label))
266 {
267 $attr_default['class'] .= " {label:'" . $this->insert_button_label . "'}";
268 }
269
270 ###
271
272 if (isset($attr['class']))
273 {
274 $attr['class'] = $attr_default['class'] . ' ' . trim($attr['class']);
275 }
276
277 $attr = array_merge($attr_default, $attr);
278
98fa6c5 @farinspace changed button text label from "text" to "label"
authored
279 $label = $attr['label'];
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
280
98fa6c5 @farinspace changed button text label from "text" to "label"
authored
281 unset($attr['label']);
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
282
283 ###
284
285 $elem_attr = array();
286
287 foreach ($attr as $n => $v)
288 {
289 array_push($elem_attr, $n . '="' . $v . '"');
290 }
291
292 ###
293
98fa6c5 @farinspace changed button text label from "text" to "label"
authored
294 return '<a ' . implode(' ', $elem_attr) . '>' . $label . '</a>';
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
295 }
296
297 /**
298 * Used to insert global STYLE or SCRIPT tags into the footer, called on
299 * WordPress admin_footer action.
300 *
301 * @since 0.1
302 * @access public
303 * @return HTML/Javascript
304 */
305 public function init()
306 {
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
307 $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
308
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
309 $file = basename(parse_url($uri, PHP_URL_PATH));
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
310
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
311 if ($uri AND in_array($file, array('post.php', 'post-new.php')))
312 {
313 // include javascript for special functionality
314 ?><script type="text/javascript">
315 /* <![CDATA[ */
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
316
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
317 var interval = null;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
318
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
319 jQuery(function($)
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
320 {
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
321 if (typeof send_to_editor === 'function')
322 {
323 var wpalchemy_insert_button_label = '';
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
324
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
325 var wpalchemy_mediafield = null;
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
326
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
327 var wpalchemy_send_to_editor_default = send_to_editor;
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
328
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
329 send_to_editor = function(html)
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
330 {
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
331 clearInterval(interval);
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
332
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
333 if (wpalchemy_mediafield)
334 {
7e32edc @herewithme Fix preg match when a media have a simple/double quotes on this title…
herewithme authored
335 var src = html.match(/src=['|"](.*?)['|"] alt=/i);
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
336 src = (src && src[1]) ? src[1] : '' ;
e5621de @farinspace using timer vs iframe load event, tab=type behaves differently from t…
authored
337
7e32edc @herewithme Fix preg match when a media have a simple/double quotes on this title…
herewithme authored
338 var href = html.match(/href=['|"](.*?)['|"]/i);
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
339 href = (href && href[1]) ? href[1] : '' ;
e5621de @farinspace using timer vs iframe load event, tab=type behaves differently from t…
authored
340
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
341 var url = src ? src : href ;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
342
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
343 wpalchemy_mediafield.val(url);
344
345 // reset insert button label
346 setInsertButtonLabel(wpalchemy_insert_button_label);
347
348 wpalchemy_mediafield = null;
349 }
350 else
351 {
352 wpalchemy_send_to_editor_default(html);
353 }
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
354
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
355 tb_remove();
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
356 }
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
357
358 function getInsertButtonLabel()
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
359 {
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
360 return $('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val();
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
361 }
362
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
363 function setInsertButtonLabel(label)
364 {
365 $('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val(label);
366 }
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
367
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
368 $('[class*=<?php echo $this->button_class_name; ?>]').live('click', function()
369 {
370 var name = $(this).attr('class').match(/<?php echo $this->button_class_name; ?>-([a-zA-Z0-9_-]*)/i);
371 name = (name && name[1]) ? name[1] : '' ;
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
372
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
373 var data = $(this).attr('class').match(/({.*})/i);
374 data = (data && data[1]) ? data[1] : '' ;
375 data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
376
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
377 wpalchemy_mediafield = $('.<?php echo $this->field_class_name; ?>-' + name, $(this).closest('.postbox'));
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
378
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
379 function iframeSetup()
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
380 {
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
381 if ($('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').length)
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
382 {
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
383 // run once
384 if ( ! wpalchemy_insert_button_label.length)
385 {
386 wpalchemy_insert_button_label = getInsertButtonLabel();
387 }
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
388
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
389 setInsertButtonLabel((data && data.label)?data.label:'Insert');
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
390
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
391 // tab "type" needs a timer in order to properly change the button label
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
392
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
393 //clearInterval(interval);
e5621de @farinspace using timer vs iframe load event, tab=type behaves differently from t…
authored
394
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
395 // setup iframe.load as soon as it becomes available
396 // prevent multiple binds
397 //$('#TB_iframeContent').unbind('load', iframeSetup).bind('load', iframeSetup);
398 }
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
399 }
400
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
401 clearInterval(interval);
e5621de @farinspace using timer vs iframe load event, tab=type behaves differently from t…
authored
402
29e78dd @farinspace fixed issue when using repeating fields and multiple meta boxes, adde…
authored
403 interval = setInterval(iframeSetup, 500);
404 });
405 }
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
406 });
6cacd7f @farinspace renamed MediaButton to MediaAccess, also adjusted javascript as ifram…
authored
407
408 /* ]]> */
409 </script><?php
410 }
7a6d9cb @farinspace added WPAlchemy_MediaButton class (alpha), good enough for review
authored
411 }
412 }
413
414 /* End of file */
Something went wrong with that request. Please try again.