Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion wp-content/themes/mytheme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

include_once 'metaboxes/select-spec.php';

/* eof */
include_once 'metaboxes/media-spec.php';

/* eof */
20 changes: 20 additions & 0 deletions wp-content/themes/mytheme/metaboxes/media-meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control metabox">

<?php $mb->the_field('imgurl'); ?>
<?php $wpalchemy_media_access->setGroupName('nn')->setInsertButtonLabel('Add File to Text Box')->setButtonLabel('Upload File'); ?>

<p>
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
<?php echo $wpalchemy_media_access->getButton(); ?>
</p>

<?php $mb->the_field('imgurl2'); ?>
<?php $wpalchemy_media_access->setGroupName('nn2')->setButtonLabel('Add Image From Library')->setInsertButtonLabel('Insert This')->setTab('gallery'); ?>

<p>
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
<?php echo $wpalchemy_media_access->getButton(); ?>
</p>

</div>
13 changes: 13 additions & 0 deletions wp-content/themes/mytheme/metaboxes/media-spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
include_once WP_CONTENT_DIR . '/wpalchemy/MediaAccess.php';
$wpalchemy_media_access = new WPAlchemy_MediaAccess();

$custom_metabox = $media_mb = new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta',
'title' => 'My Custom Media Meta',
'types' => array('book'),
'template' => get_stylesheet_directory() . '/metaboxes/media-meta.php',
));

/* eof */
194 changes: 100 additions & 94 deletions wp-content/wpalchemy/MediaAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ class WPAlchemy_MediaAccess
*/
public $insert_button_label = null;

/**
* User defined label for the "Add Media" button, this
* can be set once or per field and button pair.
*
* @since 0.1
* @access public
* @var string optional
* @see setInsertButtonLabel()
*/
public $button_label = null;

/**
* Used to track the current groupname for pairing a field and button.
*
Expand Down Expand Up @@ -76,13 +87,14 @@ public function __construct(array $a = array())
{
$this->$n = $v;
}

if ( ! defined('WPALCHEMY_SEND_TO_EDITOR_ENABLED'))
{
add_action('admin_footer', array($this, 'init'));

define('WPALCHEMY_SEND_TO_EDITOR_ENABLED', true);
}

}

/**
Expand Down Expand Up @@ -113,6 +125,13 @@ public function setInsertButtonLabel($label = 'Insert')

return $this;
}

public function setButtonLabel($button = 'Add Media')
{
$this->button_label = $button;

return $this;
}

public function setTab($name)
{
Expand Down Expand Up @@ -201,7 +220,14 @@ public function getButtonLink($tab = null)

$tab = ! empty($tab) ? $tab : 'library' ;

return 'media-upload.php?post_id=' . $post_ID . '&tab=' . $tab . '&TB_iframe=1';
$update_href = esc_url( add_query_arg( array(
'post_id' => $post_ID,
'tab' => $tab,
'TB_iframe' => 1,
'_wpnonce' => wp_create_nonce('shiba_gallery_options'),
), admin_url('upload.php') ) );

return $update_href;
}

/**
Expand All @@ -219,7 +245,7 @@ public function getButtonClass($groupname = null)
{
$groupname = isset($groupname) ? $groupname : $this->groupname ;

return $this->button_class_name . '-' . $groupname . ' thickbox';
return $this->button_class_name . '-' . $groupname . ' ';
}

/**
Expand Down Expand Up @@ -257,14 +283,13 @@ public function getButton(array $attr = array())

$attr_default = array
(
'label' => 'Add Media',
'href' => $this->getButtonLink($tab),
'label' => $this->button_label,
'class' => $this->getButtonClass($groupname) . ' button',
);

if (isset($this->insert_button_label))
{
$attr_default['class'] .= " {label:'" . $this->insert_button_label . "'}";
$attr_default['data-update'] = $this->insert_button_label;
}

###
Expand All @@ -288,10 +313,13 @@ public function getButton(array $attr = array())
{
array_push($elem_attr, $n . '="' . $v . '"');
}

$link = $this->getButtonLink();

###

return '<a ' . implode(' ', $elem_attr) . '>' . $label . '</a>';
###
//return '<input type="button" ' . implode(' ', $elem_attr) . ' value="' .$label. '" />';
return '<a href="'.$link.'" ' . implode(' ', $elem_attr) . '>'.$label.'</a>';
}

/**
Expand All @@ -304,111 +332,89 @@ public function getButton(array $attr = array())
*/
public function init()
{
global $post_ID;
$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">


// Get current post ID

$post = get_post( $post_ID );

// Check post type support for thumbnails, call wp_enqueue_media() scripts if thumbnails not supported.

if(!post_type_supports( $post->post_type, 'thumbnail' ) && function_exists('wp_enqueue_media')) {
if(!wp_script_is('custom-header')){
wp_enqueue_script('custom-header');
}
wp_enqueue_media();
}
// 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_send_to_editor_default = send_to_editor;

send_to_editor = function(html)
{
clearInterval(interval);

if (wpalchemy_mediafield)
{
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 ;

wpalchemy_mediafield.val(url);

// 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()

//Create WP media frame.

var customMediaManager;

var formlabel = 0;

$('[class*=<?php echo $this->button_class_name; ?>]').live('click', function(e)
{
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'));

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);
}
e.preventDefault();

// If the frame already exists, re-open it.
if ( customMediaManager ) {
customMediaManager.open();
return;
}

clearInterval(interval);

interval = setInterval(iframeSetup, 500);
});
}

// Get our Button element
formlabel = jQuery(this);

// Get our Form element
form = jQuery(this).parent();

var customMediaManager = wp.media.frames.customMediaManager = wp.media({
//Title of media manager frame
title: "Upload Document",
library: {
type: ''
},
frame: 'select',
button: {
//Set Button text
text: formlabel.attr("data-update")
},
//Do not allow multiple files, if you want multiple, set true
multiple: false
});
customMediaManager.on('select', function(){
//Set text box value
var media_attachment = customMediaManager.state().get('selection').first().toJSON();
form.find('input[type="text"]').val(media_attachment.url);
});

customMediaManager.open();
})
});

/* ]]> */
</script><?php
</script>
<?php
}
}
}

/* End of file */
/* End of file */
Loading