private function _getScript_MediaUploader($sReferrer)
{
$_sThickBoxTitle = esc_js($this->oMsg->get('upload_file'));
$_sThickBoxButtonUseThis = esc_js($this->oMsg->get('use_this_file'));
$_sInsertFromURL = esc_js($this->oMsg->get('insert_from_url'));
if (!function_exists('wp_enqueue_media')) {
return <<<JAVASCRIPTS
/**
* Bind/rebinds the thickbox script the given selector element.
* The fMultiple parameter does not do anything. It is there to be consistent with the one for the WordPress version 3.5 or above.
*/
setAdminPageFrameworkMediaUploader = function( sInputID, fMultiple, fExternalSource ) {
jQuery( '#select_media_' + sInputID ).unbind( 'click' ); // for repeatable fields
jQuery( '#select_media_' + sInputID ).click( function() {
var sPressedID = jQuery( this ).attr( 'id' );
window.sInputID = sPressedID.substring( 13 ); // remove the select_media_ prefix and set a property to pass it to the editor callback method.
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = hfAdminPageFrameworkSendToEditorMedia;
var fExternalSource = jQuery( this ).attr( 'data-enable_external_source' );
tb_show( '{$_sThickBoxTitle}', 'media-upload.php?post_id=1&enable_external_source=' + fExternalSource + '&referrer={$sReferrer}&button_label={$_sThickBoxButtonUseThis}&type=image&TB_iframe=true', false );
return false; // do not click the button after the script by returning false.
});
}
var hfAdminPageFrameworkSendToEditorMedia = function( sRawHTML, param ) {
var sHTML = '<div>' + sRawHTML + '</div>'; // This is for the 'From URL' tab. Without the wrapper element. the below attr() method don't catch attributes.
var src = jQuery( 'a', sHTML ).attr( 'href' );
var classes = jQuery( 'a', sHTML ).attr( 'class' );
var id = ( classes ) ? classes.replace( /(.*?)wp-image-/, '' ) : ''; // attachment ID
// If the user wants to save relavant attributes, set them.
var sInputID = window.sInputID;
jQuery( '#' + sInputID ).val( src ); // sets the image url in the main text field. The url field is mandatory so it does not have the suffix.
jQuery( '#' + sInputID + '_id' ).val( id );
// restore the original send_to_editor
window.send_to_editor = window.original_send_to_editor;
// close the thickbox
tb_remove();
}
JAVASCRIPTS;
}
return <<<JAVASCRIPTS
// Global Function Literal
/**
* Binds/rebinds the uploader button script to the specified element with the given ID.
*/
setAdminPageFrameworkMediaUploader = function( sInputID, fMultiple, fExternalSource ) {
var _bEscaped = false;
var _oMediaUploader;
jQuery( '#select_media_' + sInputID ).unbind( 'click' ); // for repeatable fields
jQuery( '#select_media_' + sInputID ).click( function( e ) {
// Reassign the input id from the pressed element ( do not use the passed parameter value to the caller function ) for repeatable sections.
var sInputID = jQuery( this ).attr( 'id' ).substring( 13 ); // remove the select_image_ prefix and set a property to pass it to the editor callback method.
window.wpActiveEditor = null;
e.preventDefault();
// If the uploader object has already been created, reopen the dialog
if ( 'object' === typeof _oMediaUploader ) {
_oMediaUploader.open();
return;
}
// Store the original select object in a global variable
oAdminPageFrameworkOriginalMediaUploaderSelectObject = wp.media.view.MediaFrame.Select;
// Assign a custom select object.
wp.media.view.MediaFrame.Select = fExternalSource ? getAdminPageFrameworkCustomMediaUploaderSelectObject() : oAdminPageFrameworkOriginalMediaUploaderSelectObject;
_oMediaUploader = wp.media({
title: fExternalSource
? '{$_sInsertFromURL}'
: '{$_sThickBoxTitle}',
button: {
text: '{$_sThickBoxButtonUseThis}'
},
multiple: fMultiple, // Set this to true to allow multiple files to be selected
metadata: {},
});
// When the uploader window closes,
_oMediaUploader.on( 'escape', function() {
_bEscaped = true;
return false;
});
_oMediaUploader.on( 'close', function() {
var state = _oMediaUploader.state();
// Check if it's an external URL
if ( typeof( state.props ) != 'undefined' && typeof( state.props.attributes ) != 'undefined' ) {
// 3.4.2+ Somehow the image object breaks when it is passed to a function or cloned or enclosed in an object so recreateing it manually.
var _oMedia = {}, _sKey;
for ( _sKey in state.props.attributes ) {
_oMedia[ _sKey ] = state.props.attributes[ _sKey ];
}
}
// If the image variable is not defined at this point, it's an attachment, not an external URL.
if ( typeof( _oMedia ) !== 'undefined' ) {
setMediaPreviewElementWithDelay( sInputID, _oMedia );
} else {
var _oNewField;
_oMediaUploader.state().get( 'selection' ).each( function( oAttachment, iIndex ) {
var _oAttributes = oAttachment.hasOwnProperty( 'attributes' )
? oAttachment.attributes
: {};
if( 0 === iIndex ){
// place first attachment in field
setMediaPreviewElementWithDelay( sInputID, _oAttributes );
return true;
}
var _oFieldContainer = 'undefined' === typeof _oNewField
? jQuery( '#' + sInputID ).closest( '.admin-page-framework-field' )
: _oNewField;
_oNewField = jQuery( this ).addAdminPageFrameworkRepeatableField( _oFieldContainer.attr( 'id' ) );
var sInputIDOfNewField = _oNewField.find( 'input' ).attr( 'id' );
setMediaPreviewElementWithDelay( sInputIDOfNewField, _oAttributes );
});
}
// Restore the original select object.
wp.media.view.MediaFrame.Select = oAdminPageFrameworkOriginalMediaUploaderSelectObject;
});
// Open the uploader dialog
_oMediaUploader.open();
return false;
});
var setMediaPreviewElementWithDelay = function( sInputID, oImage, iMilliSeconds ) {
iMilliSeconds = 'undefiend' === typeof iMilliSeconds ? 100 : iMilliSeconds;
setTimeout( function (){
if ( ! _bEscaped ) {
setMediaPreviewElement( sInputID, oImage );
}
_bEscaped = false;
}, iMilliSeconds );
}
}
/**
* Removes the set values to the input tags.
*
* @since 3.2.0
*/
removeInputValuesForMedia = function( oElem ) {
var _oImageInput = jQuery( oElem ).closest( '.admin-page-framework-field' ).find( '.media-field input' );
if ( _oImageInput.length <= 0 ) {
return;
}
// Find the input tag.
var _sInputID = _oImageInput.first().attr( 'id' );
// Remove the associated values.
setMediaPreviewElement( _sInputID, {} );
}
/**
* Sets the preview element.
*
* @since 3.2.0 Changed the scope to global.
*/
setMediaPreviewElement = function( sInputID, oSelectedFile ) {
// If the user want the attributes to be saved, set them in the input tags.
jQuery( '#' + sInputID ).val( oSelectedFile.url ); // the url field is mandatory so it does not have the suffix.
jQuery( '#' + sInputID + '_id' ).val( oSelectedFile.id );
jQuery( '#' + sInputID + '_caption' ).val( jQuery( '<div/>' ).text( oSelectedFile.caption ).html() );
jQuery( '#' + sInputID + '_description' ).val( jQuery( '<div/>' ).text( oSelectedFile.description ).html() );
}
JAVASCRIPTS;
}