Skip to content

Instantly share code, notes, and snippets.

@chrisdempsey
Last active August 29, 2015 14:05
Show Gist options
  • Save chrisdempsey/9f11bdcda8885da859f3 to your computer and use it in GitHub Desktop.
Save chrisdempsey/9f11bdcda8885da859f3 to your computer and use it in GitHub Desktop.
Trustpilot RSS Reader for MODX
Collection of code blocks for the Trustpilot RSS Reader example.
<!-- getFeed can process this -->
<item>
<title>
<![CDATA[An awesome review]]>
</title>
</item>
<!-- This cannot be accessed by getFeed -->
<item>
<tp:authorname>
<![CDATA[Some User]]>
</tp:authorname>
</item>
[[!getFeedPlus? &url=`http://trustbox.trustpilot.com/r/wearerhino.com.xml` &tpl=`trustpilot.tpl` &limit=`30` ]]
<?php
/**
* getFeedPlus
*
* A simple snippet to retrieve an RSS feed and iterate the feed items using a Chunk.
*
* Updated by jiripavlicek to add support for Feeds containg fields with fields containing ":" char eg. <tp:authorname>
*
* @author Jason Coward <jason@modxcms.com>
* @author Shaun McCormick <shaun@modxcms.com>
*
* @version 1.0.0-beta
* @copyright Copyright 2010 by Jason Coward
* @license http://www.gnu.org/licenses/gpl.txt GPLv3
*/
if (!defined('MAGPIE_OUTPUT_ENCODING')) {
$outputEncoding = $modx->getOption('outputEncoding',$scriptProperties,'UTF-8');
define('MAGPIE_OUTPUT_ENCODING',$outputEncoding);
}
$limit = isset($limit) ? (integer) $limit : 0;
$offset = isset($offset) ? (integer) $offset : 0;
$totalVar = !empty($totalVar) ? $totalVar : 'total';
$total = 0;
$output = array();
if (!empty($url) && $modx->getService('rss', 'xmlrss.modRSSParser')) {
$rss = $modx->rss->parse($url);
if (!empty($rss) && isset($rss->items)) {
$total = count($rss->items);
$modx->setPlaceholder($totalVar, $total);
$itemIdx = 0;
$idx = 0;
while (list($itemKey, $item) = each($rss->items)) {
if ($idx >= $offset) {
foreach ($item as $k => $v) {
// original: $item[$k] = str_replace(array('[',']'),array('&#91;','&#93;'),$item[$k]);
// updated with new if statement below (5 lines)
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
$item[$k . '-' . $k2] = $v2;
}
}
}
if (!empty($tpl)) {
$output[] = $modx->getChunk($tpl, $item);
} else {
$output[] = '<pre>'.$idx.': ' . print_r($item, true) . '</pre>';
}
$itemIdx++;
if ($limit > 0 && $itemIdx+1 > $limit) break;
}
$idx++;
}
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "Error parsing RSS feed at {$url}", '', 'getFeed', __FILE__, __LINE__);
}
}
$output = implode("\n", $output);
if (!empty($scriptProperties['toPlaceholder'])) {
$modx->setPlaceholder($scriptProperties['toPlaceholder'],$output);
return '';
}
return $output;
<div class="trust-review-wrapper">
<span class="pull-right"><a href="[[+link]]" title="View on Trustpilot">[[!trustpilot-stars? stars=`[[+tp-stars]]` &tpl=`trustpilot-star`]]</a> [[+pubdate:limit=`16`]]</span>
<h1>[[+tp-authorname]]</h1>
<strong>[[+title]]</strong>
<p>[[+description]]</p>
</div>
<?php
/**
* trustpilot-stars
*
* DESCRIPTION
*
* This Snippet outputs the appropriate number of stars for a Trustpilot review
*
* PROPERTIES:
*
* &stars - string supplied by Trustpilot RSS feed indicating no. stars awarded by reviewer. default: 0
* &tpl - name of chunk to use as template for each star
*
* USAGE:
*
* [[!trustpilot-stars? stars=`[[+tp-stars]]` &tpl=`trustpilot-star`]]
*
*/
if (!isset($modx)) return '';
$loop = 0;
$stars = isset($stars) && intval($stars) ? intval($stars) : 0;
if (empty($stars)) return 'no stars';
$output = '';
while ( $loop <= $stars - 1 ) {
$output .= $modx->getChunk($tpl);
$loop++;
}
return $output;
<i class="fa fa-star trust-star"></i>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment