geodir_get_post_info() Not returning updated listing

This topic contains 6 replies, has 3 voices, and was last updated by  Kiran 6 years, 8 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #395253

    mongoose
    Expired Member
    Post count: 8

    Hello,

    I’m building a plugin for GeoDirectory that augments the default ‘Listing Edited’ email with a diff of how the listing has changed. However, I’ve run into a brick wall because when I call geodir_get_post_info() it returns the *old* geodir data, with the *new* post data.

    Here’s how the plugin works:

    First, before updating a post I save it in a post meta:

    
    
    
    function save_previous($post_ID)
    {
        if (get_post_type($post_ID) == 'gd_place') {
            $post = get_post($post_ID);
            $geoDir = geodir_get_post_info($post_ID);
            update_post_meta($post_ID, "previous_version", ["originalPost" => $post, "geodirInfo" => $geoDir]);
        }
    }
    
    add_action("pre_post_update", "save_previous");
    

    This works perfectly and I only add it here for context.

    Then, I hook into

    geodir_sendEmail_message

    :

    `
    add_action(“geodir_sendEmail_message”, “append_to_message”, 10, 10);

    `

    In that method I:

    Check

    $message_type

    to see if it’s

    "listing_edited"

    Check if my

    previous_version

    exists

    Call

    geodir_get_post_info()

    to get the new listing

    Run the diff algorithm

    Here are the relevant parts of the function:

    `
    function append_to_message($message, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id)
    {
    if ($message_type != “listing_edited”) {
    return $message;
    }

    $previousData = get_post_meta($post_id, “trenton_previous_version”, true);
    if ($previousData) {
    return $message;
    }

    //Convert objects to arrays inline.
    $previousGeodir = json_decode(json_encode($previousData[‘geodirInfo’]), true);

    $currentGeodir = json_decode(json_encode(geodir_get_post_info($post_id)), true);

    //Run diff and append to $message

    return $message;
    }
    add_action(“geodir_sendEmail_message”, “append_to_message”, 10, 10);

    `

    Normally, I would just move this to another action and send an email on my own, but since

    wp_insert_post

    is the last action in the post edit actions, I don’t have that option.

    So, is this a bug in geodirectory? Am I using the wrong function? Is there a mistake I can’t see in my code?

    #395345

    Paolo
    Site Admin
    Post count: 31206

    I alerted the developers, they’ll let you know asap.

    Thanks

    #395348

    mongoose
    Expired Member
    Post count: 8

    Thanks for the response Paolo, in the meantime could I get some sort of hotfix? I was supposed to finish this project I’m working on two days ago.

    #395376

    Paolo
    Site Admin
    Post count: 31206

    I wish I could, but all our developers are off for the night already and I’m not in the position to assist you directly with this. You should get an answer within the next 12 hours max though.

    Thanks for your patience,

    #395455

    Kiran
    Moderator
    Post count: 7069

    Hi There,

    We have updated and now listing edited notification will be sent after listing data saved in database. This will solve your problem. Update will be available in next release.
    But you can find patch at here: https://github.com/kprajapatii/geodirectory/commit/11eb913e309cad66121944d36af1a6b24aa12a49

    Update patch to your site and use following code snippet:

    
    
    function _gd_custom_save_previous( $post_ID ) {
        global $_gd_custom_previous;
        if ( get_post_type( $post_ID ) == 'gd_place' && empty( $_gd_custom_previous[ $post_ID ] ) ) {
            $post = get_post( $post_ID );
            $geoDir = geodir_get_post_info( $post_ID );
    
            update_post_meta( $post_ID, "previous_version", ["originalPost" => $post, "geodirInfo" => $geoDir] );
    
            if ( empty( $_gd_custom_previous ) ) {
                $_gd_custom_previous = array();
            }
            $_gd_custom_previous[ $post_ID ] = true;
        }
    }
    add_action( 'pre_post_update', '_gd_custom_save_previous', 1, 1 );
    
    function _gd_custom_append_to_message( $message, $fromEmail, $fromEmailName, $toEmail, $toEmailName, $to_subject, $to_message, $extra, $message_type, $post_id, $user_id ) {
        if ( $message_type = 'listing_edited' && !empty( $post_id ) && get_post_type( $post_id ) == 'gd_place' ) {
            $previousData = get_post_meta( $post_id, 'previous_version', true ); // Previous data
    
            if ( !empty( $previousData ) && !empty( $previousData['geodirInfo'] ) ) {
                // Previous geodir post info
                $previousGeodir = (array)$previousData['geodirInfo'];
    
                // Current geodir post info
                $currentGeodir = (array)geodir_get_post_info( $post_id );
    
                $difference = array_diff( $currentGeodir, $previousGeodir );
                
                // DO STUFF HERE AND APPEND TO MESSAGE
            }
        }
    
        return $message;
    }
    add_filter( 'geodir_sendEmail_message', '_gd_custom_append_to_message', 10, 11 );

    Thanks,
    Kiran

    #395669

    mongoose
    Expired Member
    Post count: 8

    Hi Kiran,

    Thanks for fixing everything so quickly but it seems other things have now broken as well. I downloaded the zip of the repository (I double checked that it was for the commit your referenced), deactivated my version of GeoDirectory and uploaded/Activated yours. Now, when someone goes to confirm the creation of a listing, the site just goes white. No error messages, nothing in console, even the network request just silently returns 200, and this is with WP_DEBUG turned on.

    Here’s the full list of plugins we have activated, all latest released versions:

    GeoDirectory
    GeoDirectory Advance Search Filters
    GeoDirectory BuddyPress Integration
    GeoDirectory Claim Manager
    GeoDirectory Custom Google Maps
    GeoDirectory Custom Post Types
    GeoDirectory Location Manager
    GeoDirectory Marker Cluster
    GeoDirectory Payment Manager
    GeoDirectory Re-Captcha
    My custom Geodirectory plugin, which is nothing but the 2 functions you sent me.

    And the rest:

    AdRotate
    BackupGuard Pro
    BuddyPress
    Code Snippets
    Export User Data
    Google XML Sitemaps
    HTTPS to HTTP Referrer
    Import users from CSV with meta
    Invoicing – Invoice & Payments Plugin
    Ninja Forms
    Ninja Forms – Authorize.net
    Page-list
    Widget Logic
    WordPress Importer
    WordPress Responsive CSS3 Pricing Tables
    WP Crontrol
    WP Easy Updates
    WP Edit
    WP File Manager
    WP Rollback
    Yoast SEO

    #395673

    Kiran
    Moderator
    Post count: 7069

    Just update changes mentioned at here https://github.com/kprajapatii/geodirectory/commit/11eb913e309cad66121944d36af1a6b24aa12a49#diff-86c041744b881cb35af9ed5398f5daae, you do not need to upload all plugin files.
    Update this file only: https://raw.githubusercontent.com/kprajapatii/geodirectory/11eb913e309cad66121944d36af1a6b24aa12a49/geodirectory-functions/general_functions.php

    If you still see error, then turn WP_DEBUG on in wp-config file and check wp-content/debug.log

    Kiran

Viewing 7 posts - 1 through 7 (of 7 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket