• Resolved thelastcookie

    (@thelastcookie)


    Hi again,

    It seems like the plugin breaks the functionality to add media (images) via the default “Add Media” button above the post content edit field.

    I’m able to select the image file in the overlay but when I click “insert” the post content is not updated and I get the following console output:

    ReferenceError: exhibition_image_id is not defined

    in scripts.min.js:2 (wp-content/plugins/exhibition/assets/js/scripts.min.js)

    The issue remains when I disable the plugin for that post type.
    The issue resolves when I disable the plugin completely.

    Upon encountering this I updated the plugin to the latest version but that did not fix the issue.

    I will be looking into this and see if I can find more info.

    https://wordpress.org/plugins/exhibition/

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Matt Watson

    (@mwtsn)

    Hi there,

    Could you tell me if you have upgraded to the latest version?

    I patched this bug last night.

    Matt πŸ™‚

    Thread Starter thelastcookie

    (@thelastcookie)

    Yes, I did update. I am using version 1.1.3 of the plugin.

    This is the script that is on my server.

    It seems you are overriding window.send_to_editor() and basically replacing the default image upload functionality

    Thread Starter thelastcookie

    (@thelastcookie)

    I just reinstalled the plugin and the issue persists, scripts.js seems to have the same contents, too.

    Plugin Author Matt Watson

    (@mwtsn)

    Thanks,

    Looks like my patch didn’t upload properly :-/

    I will get this fixed, but I’m afraid it will not be until this afternoon.

    Sorry about this πŸ™

    Thread Starter thelastcookie

    (@thelastcookie)

    No problem!

    If you still have a few seconds to spare then you could outline how the patch works and I can hack something together myself in the meantime.

    The problem doesn’t look too complicated, I just dont have the time myself right now to go through all of the plugin and half of wordpress to figure out how it works. (Also, why do the same work twice.)

    Anyway I thank you for your quick replies, I’m actually impressed.

    Plugin Author Matt Watson

    (@mwtsn)

    Hi there,

    The patch should be replacing the scripts.min.js with the following:

    jQuery('.exhibition-image-upload').live( "click", function( e ) {
    
    	var id = jQuery(this).attr('id')
    	var id_split = id.split("-");
    	exhibition_image_id = id_split[1];
    	exhibition_src = jQuery(this).find('img').attr('src');
    
    	jQuery(this).removeClass('exhibition-image-upload').addClass('exhibition-image-remove');
    	jQuery(this).val('Remove Image');
    
    	tb_show( '', ' media-upload.php?type=image&TB_iframe=true' );
    
    	e.preventDefault();
    	return false;
    });
    
    jQuery('.exhibition-image-remove').live( 'click', function( e ) {
    
    	jQuery(this).remove();
    
    	e.preventDefault();
    	return false;
    });
    
    window.send_to_editor = function( html ) {
    
    	var parent 			= jQuery( '#exhibition_image-' + exhibition_image_id ).parent();
    	var image_url 		= jQuery( 'img', html ).attr('src');
    	var image_id 		= jQuery( 'img', html ).attr('class');
    
    	if( !image_url )
    	{
    		image_url = jQuery( html ).attr('src');
    	}
    
    	if( !image_id )
    	{
    		image_id = jQuery( html ).attr('class');
    	}
    
    	var image_output 	= '';
    	image_id 			= image_id.replace(/[^0-9]/g,'');
    
    	jQuery.post( ajaxurl, {
    		action: 'exhibition_gallery',
    		'image_id': image_id,
    	},
    	function(response) {
    		image_output = response.substring(0, response.length - 1);
    
    		jQuery( '#exhibition_image-' + exhibition_image_id ).val( image_url ).find('img').attr( 'src', image_url ).replaceWith( image_output );
    		jQuery( '#exhibition_image-' + exhibition_image_id ).val( image_url ).find('input').val( image_id );
    		jQuery( '#exhibition_image-' + exhibition_image_id ).append('<img class="exhibition-image-remove-hover" src="'+exhibition_src.replace( '/add.png', '/remove.png' )+'" alt="Remove image" />');
    
    		parent.append('<button id="exhibition_image-' + ( parseInt( exhibition_image_id ) + 1 ) + '" class="exhibition-image-upload" value="Add Image" type="submit"><img src="'+exhibition_src+'" height="150" width="150" alt=""/><input type="hidden" name="exhibition_image-' + ( parseInt( exhibition_image_id ) + 1 ) + '" value=""/></button>');
    
    		tb_remove();
    
    	});
    };

    The key fix in this is the following block:

    if( !image_url )
    {
    	image_url = jQuery( html ).attr('src');
    }
    
    if( !image_id )
    {
    	image_id = jQuery( html ).attr('class');
    }

    Basically the html variable that is passed into the window.send_to_editor function is an image tag, but sometimes WordPress puts it in a wrapper (which is what my original broken plugin checks for).

    These two ifs, check to see if the variables have been set, and if they haven’t assumes that the html variable is not in a wrapper, and must be the img tag itself.

    This was in the patch, but it looks like the minified version dosn’t contain the fix :-/

    Give this a go, and if it dosnt work, will you dump out the contents of the html variable, as you may have a case that I am not taking into consideration.

    Cheers πŸ™‚

    Thread Starter thelastcookie

    (@thelastcookie)

    Weird fix you have there – That wasnt even the issue πŸ˜‰

    It is the variable exhibition_image_id which is defined in the $().live() event and is left undefined when the default uploader is used and therefore breaks the script.

    I think we just need to only override window.send_to_editor() when we actually use the gallery uploader.

    PS: I was not even looking at the minified versions.

    Thread Starter thelastcookie

    (@thelastcookie)

    Either way, just for the sake of completeness here are the outputs of the html variable.

    Using default uploader to add to post:

    <a href="http://www.redacted.de/wp-content/uploads/2014/03/karte.jpg"><img src="http://www.redacted.de/wp-content/uploads/2014/03/karte.jpg" alt="Karte" width="630" height="472" class="alignleft size-full wp-image-541" /></a>

    Adding to gallery:
    <a href="http://www.redacted.de/konzept/erlebnisorientierte-musikerziehung/karte/" rel="attachment wp-att-541"><img src="http://www.redacted.de/wp-content/uploads/2014/03/karte.jpg" alt="" width="630" height="472" class="alignleft size-full wp-image-541" /></a>

    Plugin Author Matt Watson

    (@mwtsn)

    Ahhh! You are right!

    Back to the drawing board. I’m afraid I won’t get chance to patch this until later today, but I’ll get to it.

    Plugin Author Matt Watson

    (@mwtsn)

    Actually, I’m going to try and fix it now, so if I do, I’ll post the fix here, and patch it later.

    Thread Starter thelastcookie

    (@thelastcookie)

    Haha, did I wake your developer’s pride? πŸ˜€

    I would look into this myself but unfortunately I have a million other todos on this site that need to be taken care of by tonight too.

    Plugin Author Matt Watson

    (@mwtsn)

    Hi again,

    Right, I think I have a fix in place.

    Try putting this in your min.scripts.js file:

    jQuery('.exhibition-image-upload').live( "click", function( e ) {
    
    	send_to_editor_temp = window.send_to_editor;
    
    	var id = jQuery(this).attr('id')
    	var id_split = id.split("-");
    
    	exhibition_image_id = id_split[1];
    	exhibition_src = jQuery(this).find('img').attr('src');
    
    	jQuery(this).removeClass('exhibition-image-upload').addClass('exhibition-image-remove');
    	jQuery(this).val('Remove Image');
    
    	tb_show( '', ' media-upload.php?type=image&TB_iframe=true' );
    
    	window.send_to_editor = function( html ) {
    
    		var parent 			= jQuery( '#exhibition_image-' + exhibition_image_id ).parent();
    		var image_url 		= jQuery( 'img', html ).attr('src');
    		var image_id 		= jQuery( 'img', html ).attr('class');
    
    		if( !image_url )
    		{
    			image_url = jQuery( html ).attr('src');
    		}
    
    		if( !image_id )
    		{
    			image_id = jQuery( html ).attr('class');
    		}
    
    		var image_output 	= '';
    		image_id 			= image_id.replace(/[^0-9]/g,'');
    
    		jQuery.post( ajaxurl, {
    			action: 'exhibition_gallery',
    			'image_id': image_id,
    		},
    		function(response) {
    			image_output = response.substring(0, response.length - 1);
    
    			jQuery( '#exhibition_image-' + exhibition_image_id ).val( image_url ).find('img').attr( 'src', image_url ).replaceWith( image_output );
    			jQuery( '#exhibition_image-' + exhibition_image_id ).val( image_url ).find('input').val( image_id );
    			jQuery( '#exhibition_image-' + exhibition_image_id ).append('<img class="exhibition-image-remove-hover" src="'+exhibition_src.replace( '/add.png', '/remove.png' )+'" alt="Remove image" />');
    
    			parent.append('<button id="exhibition_image-' + ( parseInt( exhibition_image_id ) + 1 ) + '" class="exhibition-image-upload" value="Add Image" type="submit"><img src="'+exhibition_src+'" height="150" width="150" alt=""/><input type="hidden" name="exhibition_image-' + ( parseInt( exhibition_image_id ) + 1 ) + '" value=""/></button>');
    
    			tb_remove();
    		});
    		window.send_to_editor = send_to_editor_temp;
    	};
    
    	e.preventDefault();
    	return false;
    });
    
    jQuery('.exhibition-image-remove').live( 'click', function( e ) {
    
    	jQuery(this).remove();
    
    	e.preventDefault();
    	return false;
    });

    Now when you click the button window.send_to_editor() is stored in a temporary variable, and then reassigned when the whole thing is over.

    It seems to work for me, but if you have any more trouble let me know and I’ll jump on it.

    I will put this in an official fix tonight after I’ve done some more testing.

    Thread Starter thelastcookie

    (@thelastcookie)

    This works for me so far!

    Thank you very much.

    Plugin Author Matt Watson

    (@mwtsn)

    Fantastic πŸ™‚

    I will push the patch later tonight and close the thread πŸ™‚

    Thanks for all your help in getting this working.

    Thread Starter thelastcookie

    (@thelastcookie)

    My pleasure!

    Keep up the good work.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Plugin breaks default "Add Media" functionality.’ is closed to new replies.