Solution for WPML + Events Calendar PRO Recurring Events Compatibility

Home Forums Calendar Products Events Calendar PRO Solution for WPML + Events Calendar PRO Recurring Events Compatibility

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #944883
    Robert
    Participant

    In reference to this post, we have come up with a solution that works for us to force newly-created Events Calendar PRO recurring events to appear when using WPML Sitepress MultiLingual CMS.

    I am happy to share the full source code (developed as a single-file plugin) in hopes that it will a) potentially help other users encountering this frustrating incompatibility and b) support Modern Tribe in finding an official, permanent solution within their codebase as soon as possible.

    WPML and Events Calendar PRO are two great plugins, biggest and best of their kind. They really should work together.

    Here is the code:

    <?php
    /*
    Plugin Name: WPML Events Calendar PRO Sync
    Plugin URI: http://www.wordpress.org/plugins/
    Description: Keeps recurring events created in The Events Calendar PRO in sync with WPML Multilingual CMS translation data so that the events show up properly. This plugin is not endorsed by or affiliated with OnTheGoSystems, Inc., makers of WPML Multilingual CMS, or Modern Tribe, Inc., makers of The Events Calendar PRO.
    Version: 0.1a
    Author: Robert Peake
    Author URI: http://www.peakepro.com/
    Text Domain: wpml-ecp-sync
    Domain Path: /languages/
    */
    if ( !function_exists( 'add_action' ) ) {
        die();
    }
    
    register_activation_hook(__FILE__, 'wpml_ecp_sync_activate');
    
    function wpml_ecp_sync_activate() {
    	if (!class_exists('SitePress')) {
    		wpml_ecp_sync_trigger_error_message(__('This plugin depends upon WPML Multilingual CMS plugin being active','wpml-ecp-sync'));
    	}		
    	if (!class_exists('TribeEventsPro')) {
    		wpml_ecp_sync_trigger_error_message(__('This plugin depends upon the The Events Calendar PRO plugin being active','wpml-ecp-sync'));
    	}		
    }
    function wpml_ecp_sync_trigger_error_message($message) {
        if(isset($_GET['action']) && $_GET['action'] == 'error_scrape') {
            echo '<div class="wrap"><p style="font-family: \'Open Sans\',sans-serif; font-size: 13px; color: #444;">' . $message . '</p></div>';
            exit;
        } else {
            throw new Exception($message);
        }
    }
    /* Inserts translation data for everything, but only works if no translations are yet present */
    function wpml_ecp_init_all() {
        $s = new SitePress();
        $languages = $s->get_languages();
        foreach($languages as $language) {
            if (isset($language['code']) && $s->is_active_language($language['code'])) {
                $result = $s->prepopulate_translations($language['code']);
            }
        }
        return $result;
    }
    /* Inserts translation data for post children that do not yet have translation data */
    function wpml_ecp_sync_all($post_type = TribeEvents::POSTTYPE) {
        global $wpdb;
        $prefix = $wpdb->prefix;
        $results = array();
        $one_translation = $wpdb->get_var( "SELECT translation_id FROM {$prefix}icl_translations LIMIT 1" );
        if (!is_numeric($one_translation) ) { //no translations yet, init
            wpml_ecp_init_all();
        } else { //translations exist, sync
            $query =    "SELECT ID,post_parent from {$prefix}posts p
                        LEFT JOIN {$prefix}icl_translations t 
                        ON p.ID = t.element_id AND t.element_type = 'post_{$post_type}'
                        where post_parent!=0 AND post_type='{$post_type}'
                        and post_status IN ('draft', 'publish','schedule','future','private', 'pending')
                        and t.element_id is null";
            $result = $wpdb->get_results($query);
            foreach( $result as $row) {
                $post_id = $row->ID;
                $parent_id = $row->post_parent;
                $results[] = wpml_ecp_update_child($post_id,$parent_id);
            }
            return $results;
        }
    }
    /* updates a single child post with correct translation data based on its parent */
    function wpml_ecp_update_child($post_id,$parent_id) {
        global $wpdb;
        $prefix = $wpdb->prefix;
        $results = array();
        $parent_obj = $wpdb->get_row($wpdb->prepare("select element_type, language_code 
                                                    from {$prefix}icl_translations 
                                                    where element_id=%d",
                                                    $parent_id));
        if($parent_obj && strlen($parent_obj->element_type) > 0 && strlen($parent_obj->language_code) > 0) {
            $trid = 1 + $wpdb->get_var( "SELECT MAX(trid) FROM {$wpdb->prefix}icl_translations" );
            $data = array('element_id' => $post_id);
            $data['element_type'] = $parent_obj->element_type;
            $data['trid'] = $trid;
            $data['language_code'] = $parent_obj->language_code;
            $query_result = $wpdb->insert($prefix.'icl_translations', $data, array('%d','%s','%d','%s') );
            if (!$query_result) {
                $results[] = array('status' => 'error', 'parent_id' => $parent_id, 'child_id' => $post_id, 'parent_obj' => $parent_obj);
            } else {
                $results[] = array('status' => 'OK', 'parent_id' => $parent_id, 'child_id' => $post_id, 'parent_obj' => $parent_obj);
            }
        } else {
            $results[] = array('status' => 'error', 'parent_id' => $parent_id, 'child_id' => $post_id, 'parent_obj' => $parent_obj);
        }
        return $results;
    }
    /* Takes a parent post ID and updates those children who do not yet have translation data */
    /* @TODO: this function currently only handles inserts; modify to handle delete/update for optimal data tidiness */
    function wpml_ecp_update_after_save($parent_post_id) {
        global $wpdb;
        $prefix = $wpdb->prefix;
        $post_type = TribeEvents::POSTTYPE;
        $results = array();
        if ( $real_parent_post_id = wp_is_post_revision( $parent_post_id ) ) {
            $parent_post_id = $real_parent_post_id;
        }
        $query =    "SELECT ID,post_parent from {$prefix}posts p
                    LEFT JOIN {$prefix}icl_translations t 
                    ON p.ID = t.element_id AND t.element_type = 'post_{$post_type}'
                    where post_parent=%s AND post_type='{$post_type}'
                    and post_status IN ('draft', 'publish','schedule','future','private', 'pending')
                    and t.element_id is null";
        $result = $wpdb->get_results($wpdb->prepare($query,$parent_post_id));
        foreach( $result as $row) {
            $child_post_id = $row->ID;
            wpml_ecp_update_child($child_post_id,$parent_post_id);
        }
    }
    add_action( TribeEvents::POSTTYPE.'_update_meta', 'wpml_ecp_update_after_save', 30, 1);
    #945016
    Brian
    Keymaster

    Great! Thank you for sharing the coding with us.

    It is something we are exploring and I hope this can help us get going in that direction.

    Thanks Again

    #945682
    Robert
    Participant

    Hi Brian,

    Would be great to get an update from the development team at some point as to whether and when they plan to support WPML fully. Also happy to answer any questions they might have about the sample code.

    Best,
    Robert

    #945962
    Brian
    Keymaster

    All I can saw is it is on our roadmap and we are looking to add it, but have no timeline. We cannot release any more information then that until the feature is ready to be released as we do not want to have someone rely on a release date and us miss it.

    I have brought up your coding to the team and we are going to take a look at it.

    Thanks for providing the coding again.

    #946125
    Robert
    Participant

    Thanks, Brian. As I say, if I can be of any help in moving the timeline along on an official release (answering questions, etc.) please don’t hesitate to contact me.

    #946133
    Brian
    Keymaster

    Will do I have added this to our WPML ticket with your message to let the Dev’s know.

    I am going to close this ticket for now as there is nothing more to do here, but if you have any questions related to this or something new, please create a new ticket for us to help you out 🙂

    #1001952
    Leah
    Member

    Hello,

    We are excited to report that our upcoming release, The Events Calendar 3.12, includes changes that greatly improve the compatibility between The Events Calendar and WPML. We’ve been working hard on this integration for some time and we want to specifically thank all of our users who requested and waited for this feature. We appreciate all of the support and patience!

    Version 3.12 of The Events Calendar will greatly improve compatibility with WPML, but we still have work to do. If you run into any problems integrating the two plugins, please post in our forums so that we can look into the issues. We’ll be on the look out for tweaks and fixes that will improve things further.

    Thank you again to everyone who has been waiting for this! The Events Calendar 3.12 is coming soon- watch your WordPress Updates page!

    Best,
    Leah
    and The Events Calendar team

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Solution for WPML + Events Calendar PRO Recurring Events Compatibility’ is closed to new replies.