function gallery_shortcode($output, $attr)
 {
     global $post;
     if (!isset($attr['royalslider'])) {
         if (NewRoyalSliderMain::$override_all_default_galleries) {
             $rsid = NewRoyalSliderMain::$override_all_default_galleries;
         } else {
             return $output;
         }
     } else {
         $rsid = $attr['royalslider'];
     }
     // $rsdata = NewRoyalSliderMain::query_slider_data( $rsid );
     // if(!$rsdata || !$rsdata[0]) {
     // 	return NewRoyalSliderMain::frontend_error(__('Incorrect royalslider ID in gallery shortcode (or in Global, or problem with query.', 'new_royalslider'));
     // }
     // $rsdata = $rsdata[0];
     if (isset($attr['orderby'])) {
         $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
         if (!$attr['orderby']) {
             unset($attr['orderby']);
         }
     }
     extract(shortcode_atts(array('order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post->ID, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'ids' => '', 'include' => '', 'exclude' => ''), $attr));
     $id = intval($id);
     if ('RAND' == $order) {
         $orderby = 'none';
     }
     if (!empty($ids)) {
         // 'ids' is explicitly ordered
         $orderby = 'post__in';
         $include = $ids;
     }
     if (!empty($include)) {
         $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
         $attachments = array();
         foreach ($_attachments as $key => $val) {
             $attachments[$val->ID] = $_attachments[$key];
         }
     } elseif (!empty($exclude)) {
         $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
     } else {
         $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
     }
     if (empty($attachments)) {
         return NewRoyalSliderMain::frontend_error(__('No post attachments found.', 'new_royalslider'));
     }
     if (is_feed()) {
         $output = "\n";
         foreach ($attachments as $att_id => $attachment) {
             $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
         }
         return $output;
     }
     require_once 'rsgenerator/NewRoyalSliderGenerator.php';
     return NewRoyalSliderGenerator::generateSlides(true, true, $rsid, 'gallery', null, $attachments, null, null, null, true);
     return $output;
 }
    static function generateSlides($fetch_data, $refresh_cache, $id, $type, $markup = null, $slides = null, $options = null, $template = null, $skin = null, $disable_cache = false)
    {
        $arr = false;
        $refresh_hours = NewRoyalSliderMain::$refresh_hours;
        if (!$disable_cache) {
            if ($refresh_hours > 0) {
                $disable_cache = false;
            } else {
                $disable_cache = true;
            }
        }
        if (!$disable_cache) {
            $transient_key = NewRoyalSliderMain::get_transient_key($id, $type);
            if (!$id) {
                $refresh_cache = true;
            }
            if ($refresh_cache) {
                // delete cached version
                delete_transient($transient_key);
            } else {
                $arr = get_transient($transient_key);
            }
        }
        if ($arr === false) {
            if ($fetch_data) {
                if ($type != 'nextgen') {
                    $rsdata = NewRoyalSliderMain::query_slider_data($id);
                } else {
                    $rsdata = NewRoyalSliderMain::query_nextgen_slider_config();
                }
                if (!$rsdata || !$rsdata[0]) {
                    if ($type == 'nextgen') {
                        return NewRoyalSliderMain::frontend_error(__('NextGEN configuration not found. Likely you just haven\'t created it yet, please go to "RoyalSlider admin page > Create New Slider > NextGEN config" and create it.', 'new_royalslider'));
                    } else {
                        return NewRoyalSliderMain::frontend_error(__('Incorrect RoyalSlider ID or problem with query.', 'new_royalslider'));
                    }
                }
                $rsdata = $rsdata[0];
                $rsdata = apply_filters('new_rs_slider_data', $rsdata);
                $type = $rsdata['type'];
                $markup = $rsdata['template_html'];
                if (!$slides) {
                    $slides = $rsdata['slides'];
                }
                $options = $rsdata['options'];
                $template = $rsdata['template'];
                $skin = $rsdata['skin'];
            }
            if (!is_array($slides)) {
                $slides = json_decode($slides, ARRAY_A);
            }
            if (!class_exists('Mustache_Autoloader')) {
                require_once NEW_ROYALSLIDER_PLUGIN_PATH . 'lib/Mustache/Autoloader.php';
            }
            require_once NEW_ROYALSLIDER_PLUGIN_PATH . 'classes/NewRoyalSliderOptions.php';
            Mustache_Autoloader::register();
            $m = new Mustache_Engine(array('escape' => array('NewRoyalSliderGenerator', 'escapeMustache')));
            $css_id = 'new-royalslider-' . $id;
            $t = NewRoyalSliderOptions::getRsTemplates();
            $add_js = '';
            $curr_template;
            $selector = '';
            if (!isset($t[$template])) {
                $template = ' noTemplate';
            } else {
                $curr_template = $t[$template];
                if (isset($curr_template['add_js'])) {
                    $add_js = $curr_template['add_js'];
                    $add_js = str_replace('{{selector}}', '.' . $css_id, $add_js);
                }
                $template = ' ' . $curr_template['template-css-class'];
            }
            if ($options) {
                if (!is_array($options)) {
                    $options = json_decode($options, ARRAY_A);
                } else {
                }
                $gen_opts = self::preParseOpts($options);
                if (isset($options['sopts'])) {
                    $o = array_merge($options, $options['sopts']);
                    $to_unset = array('sopts', 'posts', 'rs_500px', 'flickr', 'rs_instagram');
                    foreach ($to_unset as $key => $value) {
                        if (isset($o[$value])) {
                            unset($o[$value]);
                        }
                    }
                    foreach ($o as $key => $option) {
                        if (is_array($option)) {
                            foreach ($option as $subkey => $suboption) {
                                if (is_numeric($suboption)) {
                                    $o[$key][$subkey] = (double) $suboption;
                                }
                            }
                        } else {
                            if (is_numeric($option)) {
                                $o[$key] = (double) $option;
                            }
                        }
                    }
                    //return;
                    $init_opts = json_encode($o);
                    $init_opts = str_replace(':"true"', ':!0', $init_opts);
                    $init_opts = str_replace(':"false"', ':!1', $init_opts);
                    $init_opts = str_replace('"', '\'', $init_opts);
                    $init_opts = str_replace(',\'', ',', $init_opts);
                    $init_opts = str_replace('\':', ':', $init_opts);
                    $init_opts = str_replace('{\'', '{', $init_opts);
                } else {
                    $init_opts = $options;
                }
            } else {
                $options = array();
                $gen_opts = self::preParseOpts(null);
                $init_opts = '';
            }
            $js_init_code = "\t\$('." . $css_id . "').royalSlider(" . $init_opts . ");\n" . $add_js;
            if (!isset($skin)) {
                $skin = 'rsDefault';
            }
            $skin = ' ' . $skin;
            $out = '';
            if (NewRoyalSliderMain::$include_style_tag) {
                if ($gen_opts['thumb_width'] != 96 || $gen_opts['thumb_height'] != 72) {
                    $out .= "\n<style type=\"text/css\">\n";
                    $out .= '.' . $css_id . ' .rsThumbsHor { height:' . $gen_opts['thumb_height'] . 'px; }' . "\n";
                    $out .= '.' . $css_id . ' .rsThumbsVer { width:' . $gen_opts['thumb_width'] . 'px; } 
			.' . $css_id . ' .rsThumb { width: ' . $gen_opts['thumb_width'] . 'px; height: ' . $gen_opts['thumb_height'] . 'px; }';
                    $out .= "\n</style>\n";
                }
            }
            if (isset($curr_template) && isset($curr_template['wrapHTML'])) {
                $out .= str_replace('%width%', $gen_opts['width'], $curr_template['wrapHTML']['before']);
            }
            $options['id'] = $id;
            $slides = apply_filters('new_rs_slides_filter', $slides, $options, $type);
            if ($type === 'custom') {
                require_once 'renderers/DefaultRenderer.php';
            } else {
                if ($type === 'gallery') {
                    require_once 'renderers/PostGalleryRenderer.php';
                } else {
                    if ($type === 'flickr' || $type === '500px') {
                        require_once 'renderers/CustomSourceRenderer.php';
                    } else {
                        if ($type === 'posts') {
                            require_once 'renderers/PostsListRenderer.php';
                        } else {
                            if ($type === 'nextgen') {
                                require_once 'renderers/NextGenRenderer.php';
                            } else {
                                if ($type === 'instagram') {
                                    require_once 'renderers/InstagramRenderer.php';
                                }
                            }
                        }
                    }
                }
            }
            if (is_array($slides) && count($slides) > 0) {
                $width = $gen_opts['width'];
                if (is_numeric($width)) {
                    $width .= 'px';
                }
                if ($width) {
                    $width = 'width:' . $width . ';';
                } else {
                    $width = '';
                }
                $height = $gen_opts['height'];
                if (is_numeric($height)) {
                    $height .= 'px';
                }
                if ($height) {
                    $height = 'height:' . $height . ';';
                } else {
                    $height = '';
                }
                $out .= '<div id="' . $css_id . '" class="royalSlider ' . $css_id . $skin . $template . '" style="' . $width . $height . ';">' . "\n";
                foreach ($slides as $key => $slide) {
                    if ($type === 'custom') {
                        $renderer = new NewRoyalSliderDefaultRenderer($slide, $gen_opts, $options);
                    } else {
                        if ($type === 'gallery') {
                            $renderer = new NewRoyalSliderPostGalleryRenderer($key, $slide, $gen_opts, $options);
                        } else {
                            if ($type === 'flickr' || $type === '500px') {
                                $renderer = new NewRoyalSliderCustomSourceRenderer($slide, $gen_opts, $options);
                            } else {
                                if ($type === 'posts') {
                                    $renderer = new NewRoyalSliderPostsRenderer($slide, $gen_opts, $options);
                                } else {
                                    if ($type === 'nextgen') {
                                        $renderer = new NewRoyalSliderNextGenRenderer($slide, $gen_opts, $options);
                                    } else {
                                        if ($type === 'instagram') {
                                            $renderer = new NewRoyalSliderInstagramRenderer($slide, $gen_opts, $options);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $m->getHelpers()->clear();
                    apply_filters('new_rs_slides_renderer_helper', $m, $slide, $options);
                    $out .= $m->render($markup, $renderer) . "\n";
                }
                $out = apply_filters('new_rs_slides_output_before_end', $out, $id, $type);
                $out .= "\n" . '</div>' . "\n";
            } else {
                if ($type !== 'posts') {
                    $error_message = print_r($slides, true);
                    if (strlen($error_message) > 5) {
                        $out .= NewRoyalSliderMain::frontend_error(print_r($slides, true));
                    } else {
                        $out .= NewRoyalSliderMain::frontend_error(__('Slides are missing. ', 'new_royalslider') . print_r($slides, true));
                    }
                } else {
                    $out .= NewRoyalSliderMain::frontend_error(__('No posts found matching your criteria.', 'new_royalslider'));
                }
            }
            if (isset($curr_template) && isset($curr_template['wrapHTML'])) {
                $out .= $curr_template['wrapHTML']['after'];
            }
            if ($type != '500px' && $type != 'flickr' && $type != 'instagram') {
                $pattern = '\\[(\\[?)(gallery|new_royalslider)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
                preg_match_all('/' . $pattern . '/s', $out, $matches);
                $out = preg_replace_callback("/{$pattern}/s", array('NewRoyalSliderGenerator', 'strip_shortcode_tag'), $out);
                $out = do_shortcode($out);
            }
            $arr = array('out' => $out, 'js_init' => $js_init_code);
            if (!$disable_cache) {
                set_transient($transient_key, $arr, 60 * 60 * $refresh_hours);
            }
        }
        NewRoyalSliderMain::register_slider($id, $arr['js_init']);
        return $arr['out'];
    }
 function get_slider($id, $shortcode_atts = null)
 {
     $rsdata = self::query_slider_data($id);
     if (!$rsdata || !$rsdata[0]) {
         return NewRoyalSliderMain::frontend_error(__('Incorrect RoyalSlider ID in shortcode or problem with query.', 'new_royalslider'));
     }
     $rsdata = $rsdata[0];
     require_once 'rsgenerator/NewRoyalSliderGenerator.php';
     $rsdata = apply_filters('new_rs_slider_data', $rsdata, $shortcode_atts);
     $output = '';
     $output .= NewRoyalSliderGenerator::generateSlides(false, $rsdata['id'], $rsdata['type'], $rsdata['template_html'], $rsdata['slides'], $rsdata['options'], $rsdata['template'], $rsdata['skin']);
     return $output;
 }
    static function generateSlides($refresh_cache, $id, $type, $markup, $slides, $options, $template, $skin)
    {
        $transient_key = NewRoyalSliderMain::get_transient_key($id, $type);
        if (!$id) {
            $refresh_cache = true;
        }
        if ($refresh_cache) {
            // delete cached version
            delete_transient($transient_key);
            $arr = false;
        } else {
            $arr = get_transient($transient_key);
        }
        if ($arr === false) {
            if (!is_array($slides)) {
                $slides = json_decode($slides, ARRAY_A);
            }
            require_once NEW_ROYALSLIDER_PLUGIN_PATH . 'lib/Mustache/Autoloader.php';
            require_once 'renderers/DefaultRenderer.php';
            require_once 'renderers/PostGalleryRenderer.php';
            require_once 'renderers/CustomSourceRenderer.php';
            require_once 'renderers/PostsListRenderer.php';
            require_once NEW_ROYALSLIDER_PLUGIN_PATH . 'classes/NewRoyalSliderOptions.php';
            Mustache_Autoloader::register();
            $m = new Mustache_Engine(array('escape' => array('NewRoyalSliderGenerator', 'escapeMustache')));
            $css_id = 'new-royalslider-' . $id;
            $t = NewRoyalSliderOptions::getRsTemplates();
            $add_js = '';
            $curr_template;
            $selector = '';
            if (!isset($t[$template])) {
                $template = ' noTemplate';
            } else {
                $curr_template = $t[$template];
                if (isset($curr_template['add_js'])) {
                    $add_js = $curr_template['add_js'];
                    $add_js = str_replace('{{selector}}', '.' . $css_id, $add_js);
                }
                $template = ' ' . $curr_template['template-css-class'];
            }
            if ($options) {
                if (!is_array($options)) {
                    $options = json_decode($options, ARRAY_A);
                } else {
                }
                $gen_opts = self::preParseOpts($options);
                if (isset($options['sopts'])) {
                    $o = array_merge($options, $options['sopts']);
                    $to_unset = array('sopts', 'posts', 'rs_500px', 'flickr');
                    foreach ($to_unset as $key => $value) {
                        if (isset($o[$value])) {
                            unset($o[$value]);
                        }
                    }
                    foreach ($o as $key => $option) {
                        if (is_array($option)) {
                            foreach ($option as $subkey => $suboption) {
                                if (is_numeric($suboption)) {
                                    $o[$key][$subkey] = (double) $suboption;
                                }
                            }
                        } else {
                            if (is_numeric($option)) {
                                $o[$key] = (double) $option;
                            }
                        }
                    }
                    //return;
                    $init_opts = json_encode($o);
                    $init_opts = str_replace(':"true"', ':!0', $init_opts);
                    $init_opts = str_replace(':"false"', ':!1', $init_opts);
                    $init_opts = str_replace('"', '\'', $init_opts);
                    $init_opts = str_replace(',\'', ',', $init_opts);
                    $init_opts = str_replace('\':', ':', $init_opts);
                    $init_opts = str_replace('{\'', '{', $init_opts);
                } else {
                    $init_opts = $options;
                }
            } else {
                $options = array();
                $gen_opts = self::preParseOpts(null);
                $init_opts = '';
            }
            $js_init_code = "\t\$('." . $css_id . "').royalSlider(" . $init_opts . ");\n" . $add_js;
            if (!isset($skin)) {
                $skin = 'rsDefault';
            }
            $skin = ' ' . $skin;
            $out = '';
            if ($gen_opts['thumb_width'] != 96 || $gen_opts['thumb_height'] != 72) {
                $out .= "\n<style type=\"text/css\">\n";
                $out .= '.' . $css_id . ' .rsThumbsVer { width:' . $gen_opts['thumb_width'] . 'px; } 
		.' . $css_id . ' .rsThumb { width: ' . $gen_opts['thumb_width'] . 'px; height: ' . $gen_opts['thumb_height'] . 'px; }';
                $out .= "\n</style>\n";
            }
            if (isset($curr_template) && isset($curr_template['wrapHTML'])) {
                $out .= str_replace('%width%', $gen_opts['width'], $curr_template['wrapHTML']['before']);
            }
            $options['id'] = $id;
            $slides = apply_filters('new_rs_slides_filter', $slides, $options, $type);
            if (is_array($slides)) {
                $out .= '<div id="' . $css_id . '" class="royalSlider ' . $css_id . $skin . $template . '" style="width:' . $gen_opts['width'] . '; height:' . $gen_opts['height'] . ';">';
                foreach ($slides as $key => $slide) {
                    if ($type === 'custom') {
                        $renderer = new NewRoyalSliderDefaultRenderer($slide, $gen_opts, $options);
                    } else {
                        if ($type === 'gallery') {
                            $renderer = new NewRoyalSliderPostGalleryRenderer($key, $slide, $gen_opts, $options);
                        } else {
                            if ($type === 'flickr' || $type === '500px') {
                                $renderer = new NewRoyalSliderCustomSourceRenderer($slide, $gen_opts, $options);
                            } else {
                                if ($type === 'posts') {
                                    $renderer = new NewRoyalSliderPostsRenderer($slide, $gen_opts, $options);
                                }
                            }
                        }
                    }
                    $m->getHelpers()->clear();
                    apply_filters('new_rs_slides_renderer_helper', $m, $slide, $options);
                    $out .= $m->render($markup, $renderer);
                }
                $out .= '</div>';
            } else {
                $out .= NewRoyalSliderMain::frontend_error(__('Slides are missing: ', 'new_royalslider') . print_r($slides, true));
            }
            if (isset($curr_template) && isset($curr_template['wrapHTML'])) {
                $out .= $curr_template['wrapHTML']['after'];
            }
            if ($type != '500px' && $type != 'flickr') {
                $pattern = '\\[(\\[?)(gallery|new_royalslider)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
                preg_match_all('/' . $pattern . '/s', $out, $matches);
                $out = preg_replace_callback("/{$pattern}/s", array('NewRoyalSliderGenerator', 'strip_shortcode_tag'), $out);
                $out = do_shortcode($out);
            }
            $arr = array('out' => $out, 'js_init' => $js_init_code);
            $refresh_hours = NewRoyalSliderMain::$refresh_hours;
            if ($refresh_hours > 0) {
                set_transient($transient_key, $arr, 60 * 60 * $refresh_hours);
            }
        }
        NewRoyalSliderMain::register_slider($id, $arr['js_init']);
        return $arr['out'];
    }