/**
  * Renders a displayed gallery on the frontend
  * @param C_Displayed_Gallery|stdClass $displayed_gallery
  */
 public function render($displayed_gallery, $return = FALSE, $mode = null)
 {
     $retval = '';
     $lookup = TRUE;
     // Simply throwing our rendered gallery into a feed will most likely not work correctly.
     // The MediaRSS option in NextGEN is available as an alternative.
     if (!C_NextGen_Settings::get_instance()->galleries_in_feeds && is_feed()) {
         return sprintf(__(' [<a href="%s">See image gallery at %s</a>] ', 'nggallery'), esc_url(apply_filters('the_permalink_rss', get_permalink())), $_SERVER['SERVER_NAME']);
     }
     if ($mode == null) {
         $mode = 'normal';
     }
     if (apply_filters('ngg_cache_displayed_galleries', FALSE)) {
         // Save the displayed gallery as a transient if it hasn't already. Allows for ajax operations
         // to add or modify the gallery without losing a retrievable ID
         if (!$displayed_gallery->apply_transient()) {
             $displayed_gallery->to_transient();
         }
     } else {
         if (is_null($displayed_gallery->id())) {
             $displayed_gallery->id(md5(json_encode($displayed_gallery->get_entity())));
         }
     }
     // Get the display type controller
     $controller = $this->get_registry()->get_utility('I_Display_Type_Controller', $displayed_gallery->display_type);
     // Get routing info
     $router = C_Router::get_instance();
     $url = $router->get_url($router->get_request_uri(), TRUE);
     // Should we lookup in cache?
     if (is_array($displayed_gallery->container_ids) && in_array('All', $displayed_gallery->container_ids)) {
         $lookup = FALSE;
     } elseif ($displayed_gallery->source == 'albums' && $controller->param('gallery') or $controller->param('album')) {
         $lookup = FALSE;
     } elseif ($controller->param('show')) {
         $lookup = FALSE;
     } elseif ($controller->is_cachable() === FALSE) {
         $lookup = FALSE;
     } elseif (!NGG_RENDERING_CACHE_ENABLED) {
         $lookup = FALSE;
     }
     // Enqueue any necessary static resources
     if (!defined('NGG_SKIP_LOAD_SCRIPTS') || !NGG_SKIP_LOAD_SCRIPTS) {
         $controller->enqueue_frontend_resources($displayed_gallery);
     }
     // Try cache lookup, if we're to do so
     $key = NULL;
     $html = FALSE;
     if ($lookup) {
         // The display type may need to output some things
         // even when serving from the cache
         if ($controller->has_method('cache_action')) {
             $retval = $controller->cache_action($displayed_gallery);
         }
         // Output debug message
         $retval .= $this->debug_msg('Lookup!');
         // Some settings affect display types
         $settings = C_NextGen_Settings::get_instance();
         $key_params = apply_filters('ngg_displayed_gallery_cache_params', array($displayed_gallery->get_entity(), $url, $mode, $settings->activateTags, $settings->appendType, $settings->maxImages, $settings->thumbEffect, $settings->thumbCode, $settings->galSort, $settings->galSortDir));
         // Any displayed gallery links on the home page will need to be regenerated if the permalink structure
         // changes
         if (is_home() or is_front_page()) {
             $key_params[] = get_option('permalink_structure');
         }
         // Try getting the rendered HTML from the cache
         $key = C_Photocrati_Transient_Manager::create_key('displayed_gallery_rendering', $key_params);
         $html = C_Photocrati_Transient_Manager::fetch($key, FALSE);
         // Output debug messages
         if ($html) {
             $retval .= $this->debug_msg('HIT!');
         } else {
             $retval .= $this->debug_msg('MISS!');
         }
         // TODO: This is hack. We need to figure out a more uniform way of detecting dynamic image urls
         if (strpos($html, C_Photocrati_Settings_Manager::get_instance()->dynamic_thumbnail_slug . '/') !== FALSE) {
             $html = FALSE;
         }
     } else {
         $retval .= $this->debug_msg('Not looking up in cache as per rules');
     }
     // If we're displaying a variant, I want to know it
     if (isset($displayed_gallery->variation) && is_numeric($displayed_gallery->variation) && $displayed_gallery->variation > 0) {
         $retval .= $this->debug_msg("Using variation #{$displayed_gallery->variation}!");
     }
     // If a cached version doesn't exist, then create the cache
     if (!$html) {
         $retval .= $this->debug_msg('Rendering displayed gallery');
         $current_mode = $controller->get_render_mode();
         $controller->set_render_mode($mode);
         $html = apply_filters('ngg_displayed_gallery_rendering', $controller->index_action($displayed_gallery, TRUE), $displayed_gallery);
         if ($key != null) {
             C_Photocrati_Transient_Manager::update($key, $html, NGG_RENDERING_CACHE_TTL);
         }
     }
     $retval .= $html;
     if (!$return) {
         echo $retval;
     }
     return $retval;
 }