• Resolved ssvtom

    (@ssvtom)


    I’m creating events via Rest-API for quite a while. Probably since some update of the plugin the events created via REST do no show up in the list of upcoming events anymore. Originally I did NOT set hide_from_listings in the POST-JSON. Investigating it a bit, I found out, that it does not matter what I set as hide_from_listings in the POST-JSON.

    I investigated a bit further and found _EventHideFromUpcoming in the database table wp_postmeta being linked to the hide_from_listings in JSON:

    1. if I omit hide_from_listings in JSON it defaults to a BLANK character in the meta_value corresponding to _EventHideFromUpcoming of this event.

    2. if I put false into hide_from_listings it also results in a blank character in the database table

    3. if I put true in it, it gets correctly put into _EventHideFromUpcoming (yes).

    The blank setting of _EventHideFromUpcoming is then treated as true, which means that the event does not show up in upcoming events. So no matter how I set (or not set) hide_from_listings in JSON during the creation of the event via REST it will not be displayed in any upcoming events.

    Further investigating into this I ended up at line 626 of src/Tribe/REST/V1/Endpoints/Single_Event.php of the plugin:

    $postarr[‘EventHideFromUpcoming’] = tribe_is_truthy( $request[‘hide_from_listings’] ) ? ‘yes’ : false;

    which seems to cause the trouble with setting it to the value of false. If I replace this line with:

    if (tribe_is_truthy( $request[‘hide_from_listings’] )) {
    $postarr[‘EventHideFromUpcoming’] = ‘yes’;
    }

    all works as desired.

    Is that a bug or do I make a mistake in the usage of the REST-API?

    Thanks,
    Tom

Viewing 2 replies - 1 through 2 (of 2 total)
  • I have further investigated this bug and it appears that there is a misunderstanding between the REST API and the rest of the plugin concerning the meaning of ‘EventHideFromUpcoming’.

    Those are excerpts from some source files showing that the event is only considered hidden when ‘EventHideFromUpcoming’ isn’t present at all:

    
    Tribe/Main.php # get_closest_event
                                            array(
                                                    'key'     => '_EventHideFromUpcoming',
                                                    'compare' => 'NOT EXISTS',
                                            ),
    
    Tribe/Adjacent_Events.php # get_closest_event - the same thing
                                    [
                                            'key'     => '_EventHideFromUpcoming',
                                            'compare' => 'NOT EXISTS',
                                    ],
    
    Tribe/Repositories/Event.php # filter_by_hidden
                    $this->by( (bool) $hidden ? 'meta_exists' : 'meta_not_exists', '_EventHideFromUpcoming', '#' );
    

    This works quite well for normal use of the events calendar as the HTTP request sent on save from the web UI doesn’t include the ‘EventHideFromUpcoming’ query param if it isn’t set (browsers seem to handle form ‘checkbox’ inputs like this).

    But the REST API handles this field a bit differently:

    
    Tribe/REST/V1/Endpoints/Single_Event.php:
    		$postarr['EventHideFromUpcoming'] = tribe_is_truthy( $request['hide_from_listings'] ) ? 'yes' : false;
    

    where ‘false’ is converted to an empty string when saved to the db.

    I have already written a fix for this which I’ll open a PR on Github for in a second. It modifies Tribe/REST/V1/Endpoints/Single_Event.php so that it doesn’t set ‘EventHideFromUpcoming’ on events at all.

    You can find the PR here.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Events created via Rest-API do not show up in list of upcoming events’ is closed to new replies.