eo_get_the_occurrences_of() not working with {$wpdb->eo_events}

WordPress Event Management, Calendars & Registration Forums Report A Bug eo_get_the_occurrences_of() not working with {$wpdb->eo_events}

This topic contains 7 replies, has 2 voices, and was last updated by  Stephen Harris 9 years, 4 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #17929

    Hi Stephen,

    I think I may have discovered a bug. I say ‘may’ because I’ve seen other people using eo_get_the_occurrences_of() before successfully – so it suggests to me that either I’m doing something wrong or there’s something weird with my local environment.

    I’m trying to use eo_get_the_occurrences_of() to get all the occurrences of an event (unsurprisingly) – and it was simply returning false even when passed an event that had occurrences.

    I got under the covers a bit and found the query that it uses to return the results:

    $results = $wpdb->get_results($wpdb->prepare("
    SELECT event_id, StartDate,StartTime,EndDate,FinishTime FROM {$wpdb->eo_events}
    WHERE {$wpdb->eo_events}.post_id=%d ORDER BY StartDate ASC",post_id));

    I took this and tried mucking around with it in my code. Eventually, I discovered that if I replaced{$wpdb->eo_events} with a hard-coded 'wp_eo_events', it worked. So for some reason or another, $wpdb isn’t generating eo_events as a property of $wpdb.

    It’s not a major issue for me now as I can write my own wrapper around the query, but I thought you might like to take a look at it.

    Best wishes,

    Andrew

    Andrew Shankie
    #17937

    Hi Andrew,

    The code itself works (passes the unit test), so the issue must be to do with the assumption it’s making, that is $wpdb->eo_events is (correctly) populated. I can only think of three reasons why that might happen:

    a) A plug-in/theme is over-riding $wdpb
    b) Your code is being called before Event Organiser has had a chance to define that property.
    c) The property is not being defined

    (a) seems unlikely – most plug-in’s would have no business doing that, unless you’ve intentionally decided to provide your own database connection class.

    (b) This is done fairly early (see source: https://github.com/stephenharris/Event-Organiser/blob/2.13.3/includes/event-organiser-cpt.php#L850-L851) though it could perhaps be done sooner. Do you know when your code is called?

    (c) Probably the most likely is that the function responsible for defining that property is not being called. This could be because of this bug: https://core.trac.wordpress.org/ticket/17817 – one way to test this is to start switching stuff off until it works.

    Having said all that, the plug-in uses $wpdb->eo_events in all its database interactions regarding events. If it were (a) or (c), you would probably seeing some really odd behaviour by the plug-in, (i.e. no events anywhere). That would suggest that it might be related to the context in which you’re calling that function (i.e. before the property is defined).

    Stephen Harris
    #17946

    Hi Stephen,

    As always, thanks for your great response.

    I agree that (a) is unlikely. And I also agree that (c) is probably not the answer as the plugin otherwise works.

    Which leaves us with (b). My code is running in an include called as part of functions.php in a theme. According to the plugin API, functions.php is indeed run before init – but that doesn’t really make sense to me – I write plenty of stuff in functions.php that uses functions defined in plugins.

    Would a reasonable test of this be to move EO into mu-plugins and see if the same issue occurs? Must-use plugins look to load earlier.

    Have a good weekend,

    Andrew

    Andrew Shankie
    #17947

    I think that might be why. The plug-in files are indeed loaded before init and before your theme’s functions.php, which means the functions are available to you – however, in this case $wpdb->eo_events is not defined until init.

    If you’re invoking that function inside functions.php (i.e. not inside a callback hooked onto an action/filter) then that would explain the problem:

     //functions.php
    
     //Won't work:
     $occurrences = eo_get_the_occurrences_of( 5 );
    
     //Will work
     add_action( 'init', 'my_init_callback' );
     function my_init_callback(){
         $occurrences = eo_get_the_occurrences_of( 5 );
     }
    
    Stephen Harris
    #17992

    Hi Stephen,

    That does indeed seem to be the problem. I’ve not tested it with the particular issue I was having above (as it’s a real pig to test – it’s on a hook that’s initiated by an AJAX call, meaning lots of fun with xedebug)… but as it happens I came across the same issue again in another context and solved it by hooking the action onto admin_menus as I’m building some admin screens.

    On reflection, I’ve had this issue in a number of times over the couple years I’ve been working with EO and never really got to the bottom of it – that is, if you add any function to functions.php without putting it on a hook, EO functions that depend on $wpdb->eo_events won’t work.

    Can you think of a general solution so that theme developers don’t run into the problem in the future? If you’re building some theme functionality that works in the dashboard screens, I think that it’s quite likely that you’ll come across this problem.

    I’ll also accept that perhaps there’s a better way to do it!

    All the best,

    Andrew

    Andrew Shankie
    #18020

    Hi Andrew,

    The function which populates $wpdb could be called earlier: If you change init to plugins_loaded here: https://github.com/stephenharris/Event-Organiser/blob/2.13.3/includes/event-organiser-cpt.php#L850-L851

    does that fix the issue for you?

    Stephen Harris
    #18027

    Hi Stephen,

    That did it. That would be a useful tweak in the next version – it just removes some headscratching.

    Cheers,

    Andrew

    Andrew Shankie
    #18086

    Should be fixed in Event Organiser 2.13.5.

    Stephen Harris
Viewing 8 posts - 1 through 8 (of 8 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.