ge => $EM_Object) : array(EM_ML::$current_language => $EM_Object); }else{ return $translated_objects; } } /** * @param EM_Event $EM_Event * @return array[EM_Event] * @see EM_ML::get_translations() */ public static function get_event_translations( $EM_Event ){ return static::get_translations( $EM_Event ); } /** * @param EM_Location $EM_Location * @return array[EM_Location] * @see EM_ML::get_translations() */ public static function get_location_translations( $EM_Location ){ return static::get_translations( $EM_Location ); } /* START original object determining functions - Whilst not crucial to override, this is a good fallback mechanism for any event/location that may not have had the correct language/parent saved yet */ /** * Gets the original event/location. The 'original' means the language the event/location was first created in, irrespective of the default/main language of the currennt blog, subsequent languages of the same are 'translations'. * @param EM_Event|EM_Location $EM_Object * @return EM_Event|EM_Location */ public static function get_original( $EM_Object ){ $object = null; if( static::is_original($EM_Object) ){ $object = $EM_Object; }else{ if( !empty($EM_Object->post_id) && !empty(static::$originals_cache[$EM_Object->blog_id][$EM_Object->post_id]) ){ $original_post_id = static::$originals_cache[$EM_Object->blog_id][$EM_Object->post_id]; } if( !empty($original_post_id) ){ if( em_is_event($EM_Object) ){ $object = em_get_event( $original_post_id, 'post_id' ); }elseif( em_is_location($EM_Object) ){ $object = em_get_location( $original_post_id, 'post_id' ); } }elseif( em_is_event($EM_Object) || em_is_location($EM_Object) ){ $object = $EM_Object->get_parent(); } } $object = apply_filters('em_ml_get_original', $object, $EM_Object ); // return result if( $object ){ // save to cache first if( !empty($object->post_id) ){ static::$originals_cache[$EM_Object->blog_id][$EM_Object->post_id] = $object->post_id; } return $object; }else{ // if we still don't have an original, return the same object return $EM_Object; } } /** * @param $object * @return bool */ public static function is_original( $object ){ $result = null; // assumed original since all non-multilingual content is original if( em_is_event( $object ) || em_is_location( $object ) ){ if( !empty( static::$originals_cache[$object->blog_id][$object->post_id]) ){ $result = static::$originals_cache[$object->blog_id][$object->post_id] == $object->post_id; }elseif( !empty($object->language) && $object->id ){ // we can only determine if something is original without 3rd party intervention if it has been saved previously $result = !$object->translation; } } // if $result is passed as null then it can be assuemd we could not determine originality and multilingual plugins should decide $result = apply_filters('em_ml_is_original', $result, $object); return $result === null || $result; } /** * Shortcut for EM_ML::get_original() to aid IDE semantics. * @see EM_ML::get_original() * @param EM_Event $EM_Event * @return EM_Event */ public static function get_original_event( $EM_Event ){ return self::get_original( $EM_Event ); } /** * Shortcut for EM_ML::get_original() to aid IDE semantics. * @see EM_ML::get_original() * @param EM_Location $EM_Location * @return EM_Location */ public static function get_original_location( $EM_Location ){ return self::get_original( $EM_Location ); } /* END original object determining functions */ /** * Sets the language of an object, shortcut for EM_ML::set_language_by_post_ids() * @param EM_Event|EM_Location $EM_Object * @param string $locale * @return boolean * @uses EM_ML::set_language_by_post_ids() */ public static function set_object_language( $EM_Object, $locale ){ return static::set_language_by_post_ids( $locale, array($EM_Object->post_id), $EM_Object->post_type, $EM_Object->blog_id ); } /** * Inserts or updates locale meta for events and locations by post id. * @param string $locale * @param array $post_ids * @param string $post_type * @param int $blog_id * @param bool $update Determines if these post ids were just added and any extra meta should be bulk added too, * or if they are to be updated and therefore some individual post lookups might be necessary by filters. * @return bool */ public static function set_language_by_post_ids( $locale, $post_ids, $post_type, $blog_id = null, $update = false ){ global $wpdb; if( !preg_match('/^[a-zA-Z_]{2,14}+$/', $locale) ) return false; //valid locale must be supplied if( EM_MS_GLOBAL ){ if( !$blog_id ) $blog_id = get_current_blog_id(); $blog_id = absint($blog_id); switch_to_blog($blog_id); } foreach( $post_ids as $k => $post_id ) $post_ids[$k] = absint($post_id); //sanitize post ids if( em_is_event( $post_type ) || em_is_location( $post_type ) ){ $key_name = em_is_location( $post_type ) ? 'location_language' : 'event_language'; $table_name = em_is_location( $post_type ) ? EM_LOCATIONS_TABLE : EM_EVENTS_TABLE; //save to events/location table - $update is irrelevant here as we must already have a saved event/location $sql = "UPDATE $table_name SET $key_name=%s WHERE post_id IN (". implode(',', $post_ids) .')'; $sql_vars = array( $locale ); if( EM_MS_GLOBAL ){ $sql .= ' AND blog_id=%d'; $sql_vars[] = $blog_id; } $wpdb->query( $wpdb->prepare($sql, $sql_vars) ); //save to meta if( $update ){ $sql = 'UPDATE '.$wpdb->postmeta ." SET meta_value=%s WHERE meta_key=%s AND post_id IN (". implode(',', $post_ids) .')'; $sql = $wpdb->prepare( $sql, array($locale, $key_name) ); }else{ $inserts = array(); foreach( $post_ids as $post_id ){ $inserts[] = $wpdb->prepare('(%d, %s, %s)', array($post_id, $key_name, $locale)); } $sql = 'INSERT INTO '.$wpdb->postmeta.' (post_id, meta_key, meta_value) VALUES '. implode(',', $inserts); } $wpdb->query( $sql ); } if( EM_MS_GLOBAL ){ restore_current_blog(); } return apply_filters('em_ml_set_language_by_post_ids', true, $locale, $post_ids, $post_type, $blog_id, $update); } public static function attach_translations( $locale, $post_ids_map, $post_type, $blog_id = null ){ global $wpdb; if( EM_MS_GLOBAL && !$blog_id ) $blog_id = get_current_blog_id(); //set parents and languages for each post if( em_is_event( $post_type ) || em_is_location( $post_type ) ){ //sanitize any sql vars $prefix = em_is_location( $post_type ) ? 'location' : 'event'; $table_name = em_is_location( $post_type ) ? EM_LOCATIONS_TABLE : EM_EVENTS_TABLE; $post_ids = array(); foreach( $post_ids_map as $original_post_id => $post_id ){ $post_ids[$original_post_id] = absint($post_id); } //get the event/location ids of both new and old event/locations so we can do a bulk update using their ids $sql = "SELECT post_id, {$prefix}_id AS id FROM $table_name WHERE post_id IN (". implode(',', $post_ids + array_keys($post_ids)) .")"; if( EM_MS_GLOBAL ) $sql .= ' AND blog_id='.absint($blog_id); $object_ids = $wpdb->get_results( $sql, OBJECT_K ); //save to events/location table - $update is irrelevant here as we must already have a saved event/location $inserts = array(); foreach( $post_ids as $original_post_id => $post_id ){ if( !empty($object_ids[$post_id]) && !empty($object_ids[$original_post_id]) ){ $inserts[] = $wpdb->prepare('(%d, %d, %s, 1)', $object_ids[$post_id]->id, $object_ids[$original_post_id]->id, $locale); } } $wpdb->query("INSERT INTO $table_name ({$prefix}_id, {$prefix}_parent, {$prefix}_language, {$prefix}_translation) VALUES ".implode(',', $inserts)." ON DUPLICATE KEY UPDATE {$prefix}_parent=VALUES({$prefix}_parent), {$prefix}_language=VALUES({$prefix}_language), {$prefix}_translation=1"); } return apply_filters('em_ml_attach_translations', true, $locale, $post_ids_map, $post_type, $blog_id ); } public static function toggle_languages_index( $force = null ){ global $wpdb; $tables_keys = array(EM_EVENTS_TABLE => 'event_language', EM_LOCATIONS_TABLE => 'location_language'); foreach( $tables_keys as $table_name => $key ){ $language_key = $wpdb->get_row("SHOW KEYS FROM $table_name WHERE Key_name = '$key'"); if( empty($language_key) && ( (!empty(EM_ML::$langs) && !$force) || $force === 'add' ) ){ $wpdb->query("ALTER TABLE $table_name ADD INDEX (`$key`)"); }elseif( !empty($language_key) && ( (empty(EM_ML::$langs) && !$force) ||$force === 'remove' ) ){ $wpdb->query("ALTER TABLE $table_name DROP INDEX `$key`"); } } } } add_action('init','EM_ML::init'); //other plugins may want to do this before we do, that's ok! Activiteiten - Alles voor Elkaar
Menu Sluiten

Activiteiten