Skip to content

Instantly share code, notes, and snippets.

@jackmahoney
Created July 31, 2012 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackmahoney/3214107 to your computer and use it in GitHub Desktop.
Save jackmahoney/3214107 to your computer and use it in GitHub Desktop.
Extension of WP_Alchemy mediaaccess class
<?php
include('MediaAccessBase.php');
/**
* @author Jack Mahoney
*/
class WPAlchemy_MediaAccess extends WPAlchemy_MediaAccess_Base{
public $meta_field_class_name = 'media-meta-field';
public function __construct(){
parent::__construct();
}
public function getMetaField(array $attr)
{
$groupname = isset($attr['groupname']) ? $attr['groupname'] : $this->groupname ;
$attr_default = array
(
'type' => 'text',
'class' => $this->meta_field_class_name . '-' . $groupname,
);
###
if (isset($attr['class']))
{
$attr['class'] = $attr_default['class'] . ' ' . trim($attr['class']);
}
$attr = array_merge($attr_default, $attr);
###
$elem_attr = array();
foreach ($attr as $n => $v)
{
array_push($elem_attr, $n . '="' . $v . '"');
}
###
return '<input type="hidden"' . implode(' ', $elem_attr) . '/>';
}
/**
* Warning this code may become redundant
* @overide parent
*/
public function init(){
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
$file = basename(parse_url($uri, PHP_URL_PATH));
if ($uri AND in_array($file, array('post.php', 'post-new.php')))
{
// include javascript for special functionality
?><script type="text/javascript">
/* <![CDATA[ */
var interval = null;
jQuery(function($)
{
if (typeof send_to_editor === 'function')
{
var wpalchemy_insert_button_label = '';
var wpalchemy_mediafield = null;
var wpalchemy_mediafield_metafield = null;
var wpalchemy_send_to_editor_default = send_to_editor;
send_to_editor = function(html)
{
clearInterval(interval);
if (wpalchemy_mediafield)
{
alert('hello');
var src = html.match(/src=['|"](.*?)['|"] alt=/i);
src = (src && src[1]) ? src[1] : '' ;
var href = html.match(/href=['|"](.*?)['|"]/i);
href = (href && href[1]) ? href[1] : '' ;
var url = src ? src : href ;
//!important
var title = html.match(/title=['|"](.*?)['|"]/i);
title = (title && title[1]) ? title[1] : '' ;
wpalchemy_mediafield.val(url);
wpalchemy_mediafield_metafield.val(title);
// reset insert button label
setInsertButtonLabel(wpalchemy_insert_button_label);
wpalchemy_mediafield = null;
}
else
{
wpalchemy_send_to_editor_default(html);
}
tb_remove();
}
function getInsertButtonLabel()
{
return $('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val();
}
function setInsertButtonLabel(label)
{
$('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').val(label);
}
$('[class*=<?php echo $this->button_class_name; ?>]').live('click', function()
{
var name = $(this).attr('class').match(/<?php echo $this->button_class_name; ?>-([a-zA-Z0-9_-]*)/i);
name = (name && name[1]) ? name[1] : '' ;
var data = $(this).attr('class').match(/({.*})/i);
data = (data && data[1]) ? data[1] : '' ;
data = eval("(" + (data.indexOf('{') < 0 ? '{' + data + '}' : data) + ")");
wpalchemy_mediafield = $('.<?php echo $this->field_class_name; ?>-' + name, $(this).closest('.postbox'));
wpalchemy_mediafield_metafield = $('.<?php echo $this->meta_field_class_name; ?>-' + name, $(this).closest('.postbox'));
function iframeSetup()
{
if ($('#TB_iframeContent').contents().find('.media-item .savesend input[type=submit], #insertonlybutton').length)
{
// run once
if ( ! wpalchemy_insert_button_label.length)
{
wpalchemy_insert_button_label = getInsertButtonLabel();
}
setInsertButtonLabel((data && data.label)?data.label:'Insert');
// tab "type" needs a timer in order to properly change the button label
//clearInterval(interval);
// setup iframe.load as soon as it becomes available
// prevent multiple binds
//$('#TB_iframeContent').unbind('load', iframeSetup).bind('load', iframeSetup);
}
}
clearInterval(interval);
interval = setInterval(iframeSetup, 500);
});
}
});
/* ]]> */
</script><?php
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment