Exemple #1
0
 /**
  * Helper method to prepare the Context stack.
  *
  * Adds the Mustache HelperCollection to the stack's top context frame if helpers are present.
  *
  * @param mixed $context Optional first context frame (default: null)
  *
  * @return Mustache_Context
  */
 protected function prepareContextStack($context = null)
 {
     $stack = new Mustache_Context();
     $helpers = $this->mustache->getHelpers();
     if (!$helpers->isEmpty()) {
         $stack->push($helpers);
     }
     if (!empty($context)) {
         $stack->push($context);
     }
     return $stack;
 }
    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'];
    }
 public function testHelpers()
 {
     $foo = array($this, 'getFoo');
     $bar = 'BAR';
     $mustache = new Mustache_Engine(array('helpers' => array('foo' => $foo, 'bar' => $bar)));
     $helpers = $mustache->getHelpers();
     $this->assertTrue($mustache->hasHelper('foo'));
     $this->assertTrue($mustache->hasHelper('bar'));
     $this->assertTrue($helpers->has('foo'));
     $this->assertTrue($helpers->has('bar'));
     $this->assertSame($foo, $mustache->getHelper('foo'));
     $this->assertSame($bar, $mustache->getHelper('bar'));
     $mustache->removeHelper('bar');
     $this->assertFalse($mustache->hasHelper('bar'));
     $mustache->addHelper('bar', $bar);
     $this->assertSame($bar, $mustache->getHelper('bar'));
     $baz = array($this, 'wrapWithUnderscores');
     $this->assertFalse($mustache->hasHelper('baz'));
     $this->assertFalse($helpers->has('baz'));
     $mustache->addHelper('baz', $baz);
     $this->assertTrue($mustache->hasHelper('baz'));
     $this->assertTrue($helpers->has('baz'));
     // ... and a functional test
     $tpl = $mustache->loadTemplate('{{foo}} - {{bar}} - {{#baz}}qux{{/baz}}');
     $this->assertEquals('foo - BAR - __qux__', $tpl->render());
     $this->assertEquals('foo - BAR - __qux__', $tpl->render(array('qux' => "won't mess things up")));
 }
    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'];
    }