/**
  * Render the post object
  *
  * @param string $template: The template to use when rendering the object
  * @param array $data: Additional data we want to pass into the template
  * @return string: The rendered Html
  **/
 public function render($template = "", $data = array())
 {
     global $kp_templates;
     if ($template == "" && isset($kp_templates["kp_recommendedPost"])) {
         $template = $kp_templates["kp_recommendedPost"];
     }
     $data = $this->getPostTemplateData($data);
     return kp_renderer::render($template, $data);
 }
 /**
  * Output the test
  * 
  * @param bool $verbose: Indicates if we should output messages
  * @return string
  **/
 public function render($verbose = true)
 {
     $template = "{TestName}: <span style=\"color:{Color}\">{Msg}</span><br />";
     // Define the template to use when rendering the test object
     $data = array();
     $data["TestName"] = $this->testName;
     if ($this->test) {
         $data["Msg"] = $this->successMsg;
         $data["Color"] = "black";
     } else {
         $data["Msg"] = $this->failureMsg;
         $data["Color"] = "red";
     }
     if ($verbose) {
         echo kp_renderer::render($template, $data);
     }
     return kp_renderer::render($template, $data);
 }
 /**
  * Test Tracking Code
  **/
 public function test5()
 {
     $test = true;
     $currentTrackingCode = get_option("kp_TrackingCode", "");
     $currentTracking = get_option("kp_Tracking", "");
     try {
         $this->rpObj = new kp_recommendedPost($this->post);
         update_option("kp_Tracking", "custom");
         // Test simple tracking: {post_id}
         $template = "{kp:trackingcode}";
         $expected = "" . $this->post->ID . "";
         $trackingCodeTemplate = "{post_id}";
         update_option("kp_TrackingCode", $trackingCodeTemplate);
         $data = kp_renderer::returnTemplateData($this->post);
         $data["kp:trackingcode"] = kp_renderer::render($trackingCodeTemplate, $data);
         $test5a = $this->rpObj->render($template) == kp_renderer::render($template, $data) && kp_renderer::render($template, $data) == $expected;
         $test = $test5a && $test;
         $testObj = new kp_test("Test 5a", $test5a, "kp_widget passed simple Tracking Code test", "kp_widget failed simple Tracking Code test");
         $testObj->render();
         // Test <a href="#" onclick="{post_id}"></a>
         $template = "<a href=\"#\" onclick=\"{kp:trackingcode}\">Empty Link</a>";
         $expected = "<a href=\"#\" onclick=\"" . $this->post->ID . "\">Empty Link</a>";
         $trackingCodeTemplate = "{post_id}";
         update_option("kp_TrackingCode", $trackingCodeTemplate);
         $data = kp_renderer::returnTemplateData($this->post);
         $data["kp:trackingcode"] = kp_renderer::render($trackingCodeTemplate, $data);
         $test5b = $this->rpObj->render($template) == kp_renderer::render($template, $data) && kp_renderer::render($template, $data) == $expected;
         $test = $test5b && $test;
         $testObj = new kp_test("Test 5b", $test5b, "kp_widget passed simple link test", "kp_widget failed simple link test");
         $testObj->render();
         // Test <a href="#" onclick="{post_slug}"></a> with single quote
         $template = "<a href=\"#\" onclick=\"{kp:trackingcode}\">Empty Link</a>";
         $expected = "<a href=\"#\" onclick=\"" . $this->post->post_name . "\">Empty Link</a>";
         $trackingCodeTemplate = "{post_slug}";
         update_option("kp_TrackingCode", $trackingCodeTemplate);
         $data = kp_renderer::returnTemplateData($this->post);
         $data["kp:trackingcode"] = kp_renderer::render($trackingCodeTemplate, $data);
         $test5c = $this->rpObj->render($template) == kp_renderer::render($template, $data) && kp_renderer::render($template, $data) == $expected;
         $test = $test5c && $test;
         $testObj = new kp_test("Test 5c", $test5c, "kp_widget passed post_name test", "kp_widget failed post_name test");
         $testObj->render();
     } catch (Exception $e) {
         $test = false;
     }
     update_option("kp_TrackingCode", $currentTrackingCode);
     update_option("kp_Tracking", $currentTracking);
     $testObj = new kp_test("Test 5", $test, "kp_widget passed Tracking Code tests", "kp_widget failed Tracking Code tests");
     $testObj->render();
 }
/**
 * Render a widget using the template in theme\template.php if $template is blank. Using the default parameters renders a functional widget.
 *
 * @param int $numPostsToRecommend: The number of posts to recommend ($defaultNumPostsToRecommend recommendations will be generated if $recommendedPosts is empty)
 * @param array $recommendedPosts: The posts to recommend (if empty, recommendations will be generated)
 * @param string $template: The template to use to render the widget (if blank, will be generated using theme\template.php)
 * @param string $ip: The ip address of the user to recommend posts for (if blank, this will be found)
 * @param string $ua: The user agent of the user to recommend posts for (if blank, this will be found)
 * @param bool $outputWidgetHtml: Indicates if we should output the html once it is rendered
 * @param string $widgetTitle: The title of the widget 
 * @param string $post_style: Used in rendering the widget
 * @param string $postimage_style: Used in rendering the widget
 * @param string $posttitle_style: Used in rendering the widget
 * @param string $postauthor_style: Used in rendering the widget
 * @param string $postdate_style: Used in rendering the widget
 * @param string $postteaser_style: Used in rendering the widget
 * @param string $before_widget: Used in rendering the widget
 * @param string $after_widget: Used in rendering the widget
 * @param string $before_title: Used in rendering the widget
 * @param string $after_title: Used in rendering the widget
 * @param string $alignment: Used in rendering the widget - defaults to vertical
 * @param bool $show_featuredimage: Used in rendering the widget (shows posts' featured image)
 * @param bool $show_posttitle: Used in rendering the widget (shows posts' title)
 * @param bool $show_postauthor: Used in rendering the widget (shows posts' author)
 * @param bool $show_postdate: Used in rendering the widget (shows posts' post date)
 * @param bool $show_postteaser: Used in rendering the widget (shows posts' teaser, deprecated?)
 * @param array<string> $recommendablePostTypes: An array of post types to recommend (if empty, recommend all post types)
 * @param string $trackingCode: Used in rendering the tracking code for posts
 * @param bool $testModeValue: Indicates if we are in test mode and what value to look for
 * @return array: ("widgetHTML" => the html for the widget, "recommender" => the recommender if it was created)
 **/
function kp_renderWidget($numPostsToRecommend = -1, $recommendedPosts = array(), $template = "", $ip = "", $ua = "", $outputWidgetHtml = true, $widgetTitle = "", $post_style = "padding-top:10px;padding-bottom:10px;", $postimage_style = "display:inline;", $posttitle_style = "display:inline;", $postauthor_style = "display:inline;", $postdate_style = "display:inline;", $postteaser_style = "display:inline;", $before_widget = "", $after_widget = "", $before_title = "", $after_title = "", $alignment = "", $show_featuredimage = false, $show_posttitle = true, $show_postauthor = true, $show_postdate = true, $show_postteaser = false, $recommendablePostTypes = array(), $trackingCode = "", $testModeValue = null)
{
    global $kp_templates, $kp_defaultAlignment;
    // Don't show the widget if we are in test mode and if the user isn't an admin
    if (get_option("kp_AdminTestMode", "false") == "true" && !kp_isUserAdmin()) {
        return array("widgetHtml" => "", "recommender" => null);
    }
    // If $template is blank then use the template in theme\templates.php
    if ($template == "" && isset($kp_templates["kp_widget"])) {
        $template = $kp_templates["kp_widget"];
    }
    // If recommendedPosts have been passed default to those or else recommend posts
    $recommender = null;
    if (count($recommendedPosts) == 0) {
        $recommender = kp_runRecommender($numPostsToRecommend, $ip, $ua, $recommendablePostTypes, $testModeValue);
        $recommendedPosts = $recommender->posts;
    }
    $widgetHtml = "";
    // Stores the widget that we will render/return
    // Render any recommendations to show
    if (count($recommendedPosts) > 0) {
        $widgetTitle = apply_filters("widget_title", $widgetTitle);
        // Start the data for the widget
        $data = array();
        $data["isTestMode"] = get_option("kp_AdminTestMode", "false") == "true" && kp_isUserAdmin();
        $data["kp_widget:before_widget"] = $before_widget;
        $data["kp_widget:after_widget"] = $after_widget;
        $data["kp_widget:title"] = $widgetTitle;
        $data["kp_widget:title_exists"] = !empty($widgetTitle) && $widgetTitle != "";
        $data["kp_widget:before_title"] = $before_title;
        $data["kp_widget:after_title"] = $after_title;
        $data["kp_widget:post_style"] = $post_style;
        $data["kp_widget:postimage_style"] = $postimage_style;
        $data["kp_widget:posttitle_style"] = $posttitle_style;
        $data["kp_widget:postauthor_style"] = $postauthor_style;
        $data["kp_widget:postdate_style"] = $postdate_style;
        $data["kp_widget:postteaser_style"] = $postteaser_style;
        $data["kp:By"] = __('By');
        $data["kp:on"] = __('on');
        $data["kp:On"] = __('On');
        if (!isset($alignment) || $alignment == "") {
            $alignment = $kp_defaultAlignment;
        }
        $data["kp_widget:alignment"] = $alignment;
        $data["kp_widget:orientation-horizontal"] = $instance["orientation"] == "horizontal";
        $data["kp_widget:featureimage"] = $show_featuredimage;
        $data["kp_widget:posttitle"] = $show_posttitle;
        $data["kp_widget:postauthor"] = $show_postauthor;
        $data["kp_widget:postdate"] = $show_postdate;
        $data["kp_widget:postteaser"] = $show_postteaser;
        $data["kp:trackingcode"] = $trackingCode;
        // Render each recommended post
        $data["kp_recommender"] = kp_recommender::renderPosts($recommendedPosts, "", $data);
        // Render the HTML for the widget
        $widgetHtml = kp_renderer::render($template, $data);
    }
    // Echo the results if $outputWidgetHtml is set to true
    if ($outputWidgetHtml) {
        echo $widgetHtml;
    }
    // Return the results and the recommender object
    return array("widgetHtml" => $widgetHtml, "recommender" => $recommender);
}
 /**
  * Render the recommended posts, either using $this->template or the rendered posts
  *
  * @param Array $posts: An array of recommendedPost objects to render
  * @param string $template: The template to use to render (must contain {kp_recommendedPosts} to generate a list of posts)
  * @param Array $data: Data that should be used in rendering
  * @return string: The rendered Html
  **/
 public static function renderPosts($posts, $template = "", $data = array())
 {
     global $kp_templates;
     if ($template == "" && isset($kp_templates["kp_recommender"])) {
         $template = $kp_templates["kp_recommender"];
     }
     $postsHtml = "";
     foreach ($posts as $post) {
         $postsHtml .= $post->render("", $data);
     }
     $data["kp_recommendedPosts"] = $postsHtml;
     return kp_renderer::render($template, $data);
 }
 /**
  * Test if the renderer works with if statements (with regular expressions as keys)
  **/
 public function test16()
 {
     try {
         $expected = "asdf";
         $key = "{\\d}";
         $template = "{if " . $key . "}" . $expected . "{/if " . $key . "}";
         $data = array($key => true);
         $test = kp_renderer::render($template, $data) == $expected;
     } catch (Exception $e) {
         $test = false;
     }
     $testObj = new kp_test("Test 16", $test, "renderer passed if statements (with regular expressions as keys)", "renderer failed if statements (with regular expressions as keys)");
     $testObj->render();
 }