/** * 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 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 function render($template = "", $data = array()) { return kp_recommender::renderPosts($this->posts, $template, $data); }