Additional Overwrite options

This topic contains 12 replies, has 2 voices, and was last updated by  Stephen Harris 8 years, 5 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #22489

    I read the previous post regarding preventing specific Post data overwrites. We have a similar situation, but different circumstances.

    We setup the ical feed to pull each feed from Google into a WP Category. That works well. After we received the data, we apply updates to the event. Specifically, we add a thumbnail and a second “special” category to some events that we want to feature. The next time the feed runs, our client suggests he’s seeing inconsistent activity. Specifically:

    1. The event updates, the “special” category remains intact, and the featured image remains intact
    2. The event updates, the “special” category is no longer checked, but the featured image IS still there.
    3. The event updates, but both the category and the featured image are gone.

    I can only reproduce activity from situation #2. After briefly reviewing your code, I can’t see how situations #1 and #3 could occur, but I may have overlooked something. It appears that when the feed is associated with a category, as is the case with all of our feeds, it will overwrite any categories previously present with the new category ($cats = array( $category );). Lastly, I do not see where the code could delete the thumbnail meta data.

    In response, I have the following questions:

    1. Do you think situations #1 and #3 are possible? If so, can you provide a code snippet, like the previous post, that will prevent updating specific post meta data, i.e. Category and Thumbnail.
    2. If the category update needs to work as it does now, we can either add a new custom field to the event post type or use tags. In either case, I want to confirm that the meta data won’t reset.
    3. If options 1 and 2 aren’t possible, do you have a suggestion regarding how we might be able to manage the thumbnails and/or special event types?

    Thank you,
    Marten

    Marten Quadland
    #22539

    I can see 1 and 3 potentially occurring.

    Specifically thumbnails are not imported/updated by the plugin, so I can’t see how it would be removed.

    However, importing a feed could overwrite the category. If the event has one or more categories in the feed it will override the events current category. If it doesnt ,it will do nothing, This is in fact a bug: in such instances it should remove existing categories.

    So depending on how your code handles updated events, I think 1 and 3 could occur.

    Metadata is updated but not removed (it should arguably remove it if not present). But the only meta key registered by core is for the a URL custom field

    I think the underlying issue is that the hooks for adding/updating an event from a feed are fired too early. Specifically they are fired before venues/categories/meta are updated.

    Really it should fire after so you can retrospectively add a category or assign a thumbnail.

    Can you provide the code you are using, as that will help me see how you’re handling feed updates now.

    Stephen Harris
    #22540

    Stephen,

    We do not currently have any custom code that handles feed updates, just the main Event plugin and the iCal Feed plugin.

    On the General event setting tab, we’ve enabled :

    Organiser ( Author )
    Thumbnail
    Excerpt
    Custom Fields
    Revisions
    Event Tags

    On the ICal Import/Export Tab, we added feeds with:

    Name, Google Calendar slug, Assigned Event to author, Category, and Event Status (either set to “Use status specified in feed” or “Published”, I’m not sure what the difference is b/t the two)

    After we receive the feed, we add the thumbnail, tag, and/or category.

    Ideally, I am looking for a hook and code sample to ensure we don’t update the thumbnail and tag. Regarding the category, the client has some feeds that they would like to associate with more than one category. In those case, I figured we could hard code the exceptions in the theme function.php file.

    Marten Quadland
    #22541

    I see, this is a manual process.

    Thumbnails and tags should be unaffected. Categories will be trickier but I shall see what can be done.

    R.e. status – iCal feeds can have their own status (confirmed, tentative etc) these will be mapped to publish / draft / trash etc or you can assign a status.

    Stephen Harris
    #22542

    Regarding categories, thank you.

    FYI, I’m not sure it matters, but I want to clarify that we have the sync scheduled for “Once Hourly”. In other words, we’re expecting the data to update on a regular basis.

    Marten Quadland
    #22571

    Stephen,

    Checking in to see if you had any luck with the categories.

    Thanks.

    Marten Quadland
    #22588

    Stephen,

    Can you give me a sense of when you will be able to look into the categories? We’re hoping to launch the site this weekend and this is the last piece of functionality that we are waiting on.

    Thank you.

    Marten Quadland
    #22590

    Hi Marten,

    The only solution I can see is to move the hooks eventorganiser_ical_sync_event_updated and eventorganiser_ical_sync_event_inserted to immediately beneath this line:

    if ( $cats ) {
        wp_set_object_terms( $event_id, $cats, 'event-category' );
    }
    

    (~line 507), and trigger either depending on whether an event is being created or updated:

    if ( $found_event ) { 
        do_action( 'eventorganiser_ical_sync_event_updated', $event_id, $event, $feed_id );
    } else {
        do_action( 'eventorganiser_ical_sync_event_inserted', $event_id, $event, $feed_id );
    }
    

    That will be included in the next update. What this allows you to do is to update an event to add the ‘special’ category:

     add_action( 'eventorganiser_ical_sync_event_updated', function ( $event_id ) {
          $cats = wp_get_object_terms( $event_id, 'event-category', array( 'fields' => 'ids' ) );
          if ( /* event had 'special category' */ ) {
               $cats[] = 123; //change to ID of 'special 'category'
               wp_set_object_terms( $event_id, $cats, 'event-category' );
          }
     }, 10 );
    

    (you’ll need to specify the term ID of the ‘special’ category. The only problem to solve is how to check an event had a special category (as it would have been removed with the sync).

    One way to do that is to hook into save_post and check if the event has that category, if so, add a post meta to act a ‘flag’, and if not, delete it (if it exists). You can use a check for the existence of that flag as the conditiona..

    Stephen Harris
    #22674

    Stephen,

    Thank you, your code helps.

    The client recently expanded their request to prevent removing categories that they add for the event and prevent updating the event as whole.

    Regarding preventing the sync from updating the categories they add, I’m not clear what you mean by hooking into the “save_post”. On the other hand, I believe I have a simple fix to your code to accomplish what we need. I merely changed:

    wp_set_object_terms( $event_id, $cats, 'event-category' );
    

    to

    wp_set_object_terms( $event_id, $cats, 'event-category', true );
    

    That allows the category to append without overriding the updates applied since the last sync.

    It would be nice to provide an override feature to prevent updating specific fields or the event as a whole. If you see a better way, please let me know.

    If there is a good reason to avoid doing what we want to do, please let me know.

    Thank you.

    Marten Quadland
    #22675

    The client recently expanded their request to prevent removing categories that they add for the event and prevent updating the event as whole.

    So they want to allow the importer to importer the event, and then leave it completely alone?

    Regarding the solution you proposed, unfortunately this won’t work in general. That would append the category terms to the event. If the categories in the iCal feed change then they will be added, but the original categories in the feed not removed. Similarly if the categories are removed from the iCal feed, that won’t be reflected after the next sync.

    If you wanted to prevent the sync from doing anything to already-imported events then you can add the line continue; in between these two lines:

    $parsed_events[] = $event_id;
    update_post_meta( $event_id, '_eventorganiser_feed', $feed_id );
    

    I’m happy to insert a filter that in the next update to make it possible to do that without editing the plug-in files.

    Stephen Harris
    #22676

    Stephen,

    Thank you, that is very helpful. If you could provide the filter, that would be great.

    Regarding my proposed Category solution, to clarify, that would only be an issue if we set the feed to use the “categories specified in the feed”, correct? If so, that won’t be an issue for us because we are setting each feed to a specific category.

    Regards

    Marten Quadland
    #22685

    That’s correct (though obviously maybe an issue for other users).

    There’ll be an update for the filter at some point next week.

    Stephen Harris
    #22916

    2.2 has just been released. It add the eventorganiser_ical_sync_update_event filter that allows you to prevent events from being updated in a sync:

    add_filter( 'eventorganiser_ical_sync_update_event', '__return_false' );
    

    The eventorganiser_ical_sync_event_inserted and eventorganiser_ical_sync_event_updated hooks have been moved so that the they are triggered after the event categories / venues / meta data has been updated.

    Stephen Harris
Viewing 13 posts - 1 through 13 (of 13 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.