WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Problem when adding Media to Event on the frontend (6 posts)

  1. leoloso
    Member
    Posted 1 year ago #

    Hi again Marcus!

    I discovered a problem which, in part, has no easy fix. Actually it happens with other plugins too, I use WP User Frontend and it happens with it too.

    The problem is that, when creating or editing an Event in the frontend, and adding Media, this uploaded media does not get uploaded with post_parent = the event->post_id, but instead with the Edit Event Page id ( get_option('dbem_edit_events_page') )

    Then, later on, this generates the problem that on the Media Library, when changing from "All media items" to "Uploaded to this page", it shows all media uploaded by all users (check that it says "Uploaded to this page" and not "Uploaded to this post" since the post_parent is this Edit Event page and not the actual post)

    This can be split into 2: Add new Event (no easy solution), and Edit Event (easy solution)

    1. Edit Event:
    we only need to change the "current post" id through hook "media_view_settings":

    add_filter( 'media_view_settings', 'em_media_view_settings', 10, 2);
    function em_media_view_settings ($settings, $post) {
    
    	// Change it if we're in frontend and the ID of the post is the one of the Edit Event page
    	if (!is_admin() && $post->ID == get_option('dbem_edit_events_page')) {
    
    		// Do it only when Editing Event. When creating the Event, keep it with this id, since we don't have yet the id of the Event to be
    		if ($_REQUEST['event_id']) {
    
    			$event = em_get_event($_REQUEST['event_id']);
    			$settings['post']['id'] = $event->post_id;
    		}
    	}
    
    	return $settings;
    }

    2. Create new Event
    The problem here is that we don't know in advance the id of the post to be created, so we can't change from the wrong page id to this post id, as in the previous case. So my strategy here is:
    - Upload the event with the wrong post_parent id
    - Immediately after saving the event, so we got the event id, change the wrong post_parent id to the right one

    The problem here is that all users can be uploading Media and creating events at the same time, so the first one to save will also update the post_parent for all these other media files.

    The solution I thought of is to create a list of uploaded Media, put it in $_SESSION, so when saving the event we know what media files were uploaded by that user. Unluckily I saw no hook in WP to do any action after a media file was uploaded, so I had to hack it. I know it's wrong, if you think of another solution, please let me know. Or in any case, I'd say that this bug is caused by the way WP works, so possibly it makes sense to ask them to add the following hook in WP?

    - Hack a hook in wp-admin/includes/ajax-actions.php, function wp_ajax_upload_attachment() (at the end, just before the last wp_die()):

    do_action('wp_ajax_upload_attachment_success', $attachment_id, $post_id);

    - Then, each time a media file is uploaded, save the id in $_SESSION

    add_action('wp_ajax_upload_attachment_success', 'em_add_media_to_list', 10, 2);
    function em_add_media_to_list($attachment_id, $post_id = null) {
    
    	$wrong_post_parent = get_option('dbem_edit_events_page');
    	$session_key = 'em_new_event_added_media';
    
    	if ($post_id && $post_id == $wrong_post_parent) {
    
    		if (!isset($_SESSION[$session_key]))
    			$_SESSION[$session_key] = array();
    
    		$_SESSION[$session_key][] = $attachment_id;
    	}
    }

    - Finally, after saving the event, change these ids to the newly created $event->post_id

    add_action('em_event_added', 'em_event_added_change_media_post_parent', 0, 1);
    function em_event_added_change_media_post_parent($event) {
    
    	$post_id = $event->post_id;
    	$wrong_post_parent = get_option('dbem_edit_events_page');
    	$session_key = 'em_new_event_added_media';
    
    		$attachments = get_children(array(
    		'post_parent' => $wrong_post_parent,
    //		'post_type' => 'attachment',
    		'post__in' => $_SESSION[$session_key]
    		)); 
    
    	foreach($attachments as $attachment_id => $attachment) {
    
    		$attachment_update = array(
    			'ID' => $attachment_id,
    			'post_parent' => $right_post_parent
    		);
    
    		wp_update_post($attachment_update);
    	}		
    
    	$_SESSION[$session_key] = null;
    }

    Cheers,
    Leo

    http://wordpress.org/extend/plugins/events-manager/

  2. leoloso
    Member
    Posted 1 year ago #

    I see another possibility, which looks like too much trouble, but at least there's no need to hack WP:

    - Override jquery function window.send_to_editor, save the id of the just added media in a hidden input
    - Then when submitting the new event, send these ids along and do the rest as explained above

    This also fixes a bug from my solution above: if the user creates a new event and uploads media, but he doesn't submit the event, then he creates a second event and Submits, then this second event will have the media attached from the first event

  3. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    I see what you mean, will test and get back to you.

    however, could it be you're overcomplicating this though? we do know the post id when adding an image. See EM_Event::save() - $this->upload_image();

  4. leoloso
    Member
    Posted 1 year ago #

    you know the post_id of the image, what you don't know is the post_parent of the image (that is the post you're creating). You get that value only when you do $post_id = wp_insert_post($post_array); in save(), but all the Media you add to the page is saved before.

    Check this sql and you'll see what I mean:
    SELECT ID, post_parent FROM wp_posts where post_type = 'attachment' order by id desc;

    so when you create an event in the frontend and add an image through Media Library, run the above sql, you'll see the post_parent in the just added image points to the Edit Event Page id

  5. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    ok, will check this out asap

  6. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    hey Leo, I just checked this out and I still think you're over-thinking this :)

    In fact, I don't even see a problem... testing out the front-end works as expected, visiting the event post and viewing the media shows the post correctly associated with the image and the event admin page has no associated images.

    For sure we do know the post_id of the event/location in question too, because $this->image_upload() is only executed when a post is successfully saved on the front-end, and at that point $this->post_id is updated with a valid id, which is then used in the EM_Object::image_upload function

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.