: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Dependencies : jquery, wp media uploader
* Feature added by : Dovy Paukstys
/*global redux_change, wp, redux, colorValidate, jQuery */
redux.field_objects = redux.field_objects || {};
redux.field_objects.background = redux.field_objects.background || {};
redux.field_objects.background.init = function( selector ) {
selector = $.redux.getSelector( selector, 'background' );
if ( ! el.hasClass( 'redux-field-container' ) ) {
parent = el.parents( '.redux-field-container:first' );
if ( parent.is( ':hidden' ) ) {
if ( parent.hasClass( 'redux-field-init' ) ) {
parent.removeClass( 'redux-field-init' );
// Remove the image button.
el.find( '.redux-remove-background' ).off( 'click' ).on(
redux.field_objects.background.removeImage( $( this ).parents( '.redux-container-background:first' ) );
redux.field_objects.background.preview( $( this ) );
el.find( '.redux-background-upload' ).off().on(
redux.field_objects.background.addImage( event, $( this ).parents( '.redux-container-background:first' ) );
el.find( '.redux-background-input' ).on(
redux.field_objects.background.preview( $( this ) );
el.find( '.redux-color' ).wpColorPicker(
change: function( e, ui ) {
$( this ).val( ui.color.toString() );
redux_change( $( this ) );
$( '#' + e.target.id + '-transparency' ).prop( 'checked', false );
redux.field_objects.background.preview( $( this ) );
redux_change( $( this ).parent().find( '.redux-color-init' ) );
redux.field_objects.background.preview( $( this ) );
// Replace and validate field on blur.
el.find( '.redux-color' ).on(
var value = $( this ).val();
var id = '#' + $( this ).attr( 'id' );
if ( 'transparent' === value ) {
$( this ).parent().parent().find( '.wp-color-result' ).css( 'background-color', 'transparent' );
el.find( id + '-transparency' ).prop( 'checked', true );
if ( colorValidate( this ) === value ) {
if ( 0 !== value.indexOf( '#' ) ) {
$( this ).val( $( this ).data( 'oldcolor' ) );
el.find( id + '-transparency' ).prop( 'checked', false );
el.find( '.redux-color' ).on(
$( this ).data( 'oldcolor', $( this ).val() );
el.find( '.redux-color' ).on(
var value = $( this ).val();
var color = colorValidate( this );
var id = '#' + $( this ).attr( 'id' );
if ( 'transparent' === value ) {
$( this ).parent().parent().find( '.wp-color-result' ).css( 'background-color', 'transparent' );
el.find( id + '-transparency' ).prop( 'checked', true );
el.find( id + '-transparency' ).prop( 'checked', false );
if ( color && color !== $( this ).val() ) {
// When transparency checkbox is clicked.
el.find( '.color-transparency' ).on(
if ( $( this ).is( ':checked' ) ) {
el.find( '.redux-saved-color' ).val( $( '#' + $( this ).data( 'id' ) ).val() );
el.find( '#' + $( this ).data( 'id' ) ).val( 'transparent' );
el.find( '#' + $( this ).data( 'id' ) ).parents( '.redux-field-container' ).find( '.wp-color-result' ).css( 'background-color', 'transparent' );
prevColor = $( this ).parents( '.redux-field-container' ).find( '.redux-saved-color' ).val();
if ( '' === prevColor ) {
prevColor = $( '#' + $( this ).data( 'id' ) ).data( 'default-color' );
el.find( '#' + $( this ).data( 'id' ) ).parents( '.redux-field-container' ).find( '.wp-color-result' ).css( 'background-color', prevColor );
el.find( '#' + $( this ).data( 'id' ) ).val( prevColor );
redux_change( $( this ) );
el.find( ' .redux-background-repeat, .redux-background-clip, .redux-background-origin, .redux-background-size, .redux-background-attachment, .redux-background-position' ).select2();
// Update the background preview.
redux.field_objects.background.preview = function( selector ) {
var parent = $( selector ).parents( '.redux-container-background:first' );
var preview = $( parent ).find( '.background-preview' );
if ( ! preview ) { // No preview present.
css = 'height:' + preview.height() + 'px;';
$( parent ).find( '.redux-background-input' ).each(
var data = $( this ).serializeArray();
if ( data && data.name.indexOf( '[background-' ) !== - 1 ) {
if ( '' !== data.value ) {
data.name = data.name.split( '[background-' );
data.name = 'background-' + data.name[1].replace( ']', '' );
if ( 'background-image' === data.name ) {
css += data.name + ':url("' + data.value + '");';
css += data.name + ':' + data.value + ';';
preview.attr( 'style', css ).fadeIn();
// Add a file via the wp.media function.
redux.field_objects.background.addImage = function( event, selector ) {
var jQueryel = $( this );
// If the media frame already exists, reopen it.
// Create the media frame.
title: jQueryel.data( 'choose' ),
text: jQueryel.data( 'update' )
// When an image is selected, run a callback.
// Grab the selected attachment.
var attachment = frame.state().get( 'selection' ).first();
if ( 'image' !== attachment.attributes.type ) {
selector.find( '.upload' ).val( attachment.attributes.url );
selector.find( '.upload-id' ).val( attachment.attributes.id );
selector.find( '.upload-height' ).val( attachment.attributes.height );
selector.find( '.upload-width' ).val( attachment.attributes.width );
redux_change( $( selector ).find( '.upload-id' ) );
thumbSrc = attachment.attributes.url;
if ( 'undefined' !== typeof attachment.attributes.sizes && 'undefined' !== typeof attachment.attributes.sizes.thumbnail ) {
thumbSrc = attachment.attributes.sizes.thumbnail.url;
} else if ( 'undefined' !== typeof attachment.attributes.sizes ) {
height = attachment.attributes.height;
for ( key in attachment.attributes.sizes ) {
if ( attachment.attributes.sizes.hasOwnProperty( key ) ) {
object = attachment.attributes.sizes[key];
if ( object.height < height ) {
thumbSrc = attachment.attributes.icon;
selector.find( '.upload-thumbnail' ).val( thumbSrc );
if ( ! selector.find( '.upload' ).hasClass( 'noPreview' ) ) {
selector.find( '.screenshot' ).empty().hide().append( '<img class="redux-option-image" src="' + thumbSrc + '">' ).slideDown( 'fast' );
selector.find( '.redux-remove-background' ).removeClass( 'hide' );
selector.find( '.redux-background-input-properties' ).slideDown();
redux.field_objects.background.preview( selector.find( '.upload' ) );
// Finally, open the modal.
// Update the background preview.
redux.field_objects.background.removeImage = function( selector ) {
// This shouldn't have been run...
if ( ! selector.find( '.redux-remove-background' ).addClass( 'hide' ) ) {
selector.find( '.redux-remove-background' ).addClass( 'hide' ); // Hide "Remove" button.
selector.find( '.upload' ).val( '' );
selector.find( '.upload-id' ).val( '' );
selector.find( '.upload-height' ).val( '' );
selector.find( '.upload-width' ).val( '' );
redux_change( $( selector ).find( '.upload-id' ) );
selector.find( '.redux-background-input-properties' ).hide();
screenshot = selector.find( '.screenshot' );
selector.find( '.remove-file' ).off();
// We don't display the upload button if .upload-notice is present
// This means the user doesn't have the WordPress 3.5 Media Library Support.
if ( $( '.section-upload .upload-notice' ).length > 0 ) {
$( '.redux-background-upload' ).remove();