File: /home/dtptviut/public_html/wp-content/plugins/comparisonfeed-wp/compare-feed-wp-shortcode.php
<?php
add_shortcode('adservice-comparison-feed', 'doAdserviceComparisonFeedShortcode');
add_shortcode('adservice-comparison-feed-iframe', 'doAdserviceComparisonFeedIframeShortcode');
add_shortcode('adservice-comparison-filter','doAdserviceComparisonFilterShortcode');
add_shortcode('adservice-product-feed', 'doAdserviceProductFeedShortcode');
function correctFormat($string, $startChar, $endChar) {
if (substr($string, 0, 1) !== $startChar) {
$string = $startChar . $string . $endChar;
}
return $string;
}
function enqueueScripts() {
wp_enqueue_style( 'compare-feed' );
if (locate_template( 'compare-list-modified.css' )) {
wp_enqueue_style( 'compare-list-modified' );
}
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jsrender' );
wp_enqueue_script( 'star-rating' );
wp_enqueue_script( 'jquery-spinner' );
wp_enqueue_script( 'bootstrap-slider' );
wp_enqueue_script( 'bootstrap-tooltips' );
wp_enqueue_script( 'compare-feed' );
}
/*
Show saved comparison feeds built using Feed Builder presented using
The [adservice-comparison-feed feed=FEED-ID] shortcode.
*/
function showComparisonFeed($atts) {
// Include settings.php to get access to $categoryNames.
include('settings.php');
$adservice_options = get_option("adservice_options");
$containerClass = '';
$id = $atts['feed'];
$searchForm = '#searchTable';
$template = (isset($atts['template'])) ? $atts['template'] : 'template';
if (!isset($adservice_options['pid']) || !isset($adservice_options['token'])) {
return "<div style='display:none'>Adservice Comparison Feed: You haven't connected an Adservice account on the settings page - please connect your Adservice account and try again.</div>
<script>console.log('Adservice Comparison Feed: You haven\'t connected an Adservice account on the settings page - please connect your Adservice account and try again.');</script>";
}
$feed = json_remote_get("https://api.adservice.com/v2/publisher/tools/comparisonfeed/feeds/" . $id, $adservice_options);
if (!isset($feed) || !isset($feed->data->id)) {
return "<div style='display:none'>Adservice Comparison Feed: Could not find feed with ID \"" . $id . "\"</div>
<script>console.log('Adservice Comparison Feed: Could not find feed with ID \"" . $id . "\"');</script>";
}
$feed = $feed->data;
// Unique id appended to filter, feed and template containers.
$uuid = GUID();
// Feed settings exists.
ob_start();
if (isset($feed->type) && $feed->type === 'IFRAME') {
// Display iframe feed.
// Register iframe resizer script in shortcode to make sure we can add
// the inline script later on that initialize the iframe resizer.
// Source: https://core.trac.wordpress.org/ticket/54958
wp_register_script(
'iframe-resizer',
'//feedcontentcloud.com/comparisonfeed/resources/js/iframeResizer.min.js',
false,
CURRENT_VERSION,
true
);
wp_enqueue_script( 'iframe-resizer' );
// Build iframe parameters from shortcode attributes.
$iframeParams = '';
foreach(array_keys($atts) as $key) {
// Don't include the feed parameter.
if ($key !== 'feed') {
$iframeParams .= $key . '=' . $atts[$key] . '&';
}
}
$iframeParams = substr($iframeParams, 0, -1);
// Print iframe tag.
// Class 'fitvidsignore' is needed to avoid FitVids.js wrapping
// our iframe in a container that breaks page layout.
// Source: https://github.com/davatron5000/FitVids.js
echo '<iframe id="as-comparison-iframe-'.$uuid.'"
class="fitvidsignore"
style="border: none; width: 1px; min-width: 100%; overflow: hidden;"
scrolling="no"
src="//feedcontentcloud.com/v2/public/publisher/comparisonfeed/iframe/'.$id.'?'.$iframeParams.'">
</iframe>';
// Add inline script for initializing iframe resizing after iframe-resizer
// to make sure it's loaded first.
$script = "iFrameResize({heightCalculationMethod: 'taggedElement', checkOrigin: false}, '#as-comparison-iframe-$uuid');";
wp_add_inline_script('iframe-resizer', $script);
} else {
// Display regular feed.
enqueueScripts();
$settings = json_decode($feed->settings);
$settings->removedItems = (isset($settings->removedItems)) ? json_encode($settings->removedItems) : '[]';
$settings->conditions = (isset($settings->conditions)) ? json_encode($settings->conditions) : '[]';
$settings->customOrder = (isset($settings->customOrder)) ? json_encode($settings->customOrder) : '{}';
$settings->customRating = (isset($settings->customRating)) ? json_encode($settings->customRating) : '{}';
$settings->labels = (isset($settings->labels)) ? json_encode($settings->labels) : '{}';
$settings->sortBy = (isset($settings->sortBy)) ? $settings->sortBy : '{}';
$settings->includeNewCampaigns =
(isset($settings->includeNewCampaigns) && $settings->includeNewCampaigns) ? 'true' : 'false';
$settings->lazyLoading =
(isset($settings->lazyLoading) && $settings->lazyLoading) ? 'true' : 'false';
$settings->oneItemPerCampaign =
(isset($settings->oneItemPerCampaign) && $settings->oneItemPerCampaign) ? 'true' : 'false';
$settings->sub .= '|||CPL';
$settings->showRating =
(isset($settings->showRating) && $settings->showRating) ? 'true' : 'false';
// Override settings with supported shortcode attributes.
foreach(array_keys($atts) as $key) {
if ($key !== 'feed' &&
$key !== 'removedItems' &&
$key !== 'conditions' &&
$key !== 'customOrder' &&
$key !== 'customRating' &&
$key !== 'labels' &&
$key !== 'includeNewCampaigns' &&
$key !== 'lazyLoading') {
$settings->{$key} = $atts[$key];
}
}
// Override settings that aren't relevant/supported for selected template.
// Also check that the template exists for the given category.
if ($template === 'top3' && file_exists( plugin_dir_path( __FILE__ ) . 'templates-dist/' .
$categoryNames[$feed->categoryId] . '/' . $template . '.html')) {
$containerClass = 'as-top-3-container';
$settings->initialRows = 0;
$settings->lazyLoading = 'true'; // Make sure IntersectionObserver gets polyfilled.
$settings->maxRows = 3;
$settings->showFilter = 0;
$settings->title = '';
$searchForm = '';
} else {
$containerClass = '';
# Set to default template for non-existing templates.
$template = 'template';
}
include( plugin_dir_path( __FILE__ ) . '/compare-feed-wp-main-template.php');
}
$output = ob_get_clean();
return $output;
}
/*
Handles the [adservice-comparison-feed] shortcode.
*/
function doAdserviceComparisonFeedShortcode($atts) {
// Normalize attribute keys to lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// If a feed attribute is defined then hand over presentation
// to showComparisonFeed().
if (isset($atts['feed']) && strlen($atts['feed']) > 0) {
return showComparisonFeed($atts);
}
$adservice_options = get_option("adservice_options");
if ($adservice_options['login']) {
enqueueScripts();
ob_start();
global $post;
$postID = get_the_ID();
$custom_content = get_post_custom($postID);
if (isset($custom_content["compare-template"])) {
$template = $custom_content["compare-template"][0];
}
if (isset($custom_content["compare-account"])) {
$account = $custom_content["compare-account"][0];
}
//$cleanLink = $adservice_options['cleanLink'] || "true";
// Use the custom min rating defined on the post or use the default of 60.
$minRating = get_post_meta($postID , 'min-rating', true);
if (!(strlen($minRating) > 0 && intval($minRating) >= 0)) {
$minRating = 60;
}
$sub = get_post_meta($postID , 'subID', true);
$media = get_post_meta($postID, 'mediaID',true);
$removedItems = get_post_meta($postID , 'removed-items', true);
$conditions = get_post_meta($postID , 'conditions', true);
$customRating = get_post_meta($postID , 'custom-rating', true);
$customOrder = get_post_meta($postID , 'custom-order', true);
$initialRows = get_post_meta($postID , 'initial-rows', true);
$maxRows = get_post_meta($postID , 'max-rows', true);
$includeNewCampaigns = get_post_meta($postID , 'include-new-campaigns', true);
$lazyLoading = get_post_meta($postID , 'lazy-loading', true);
$sortBy = get_post_meta($postID , 'sortBy', true);
// Make sure removedItems, customRating, and customOrder have proper format.
$removedItems = correctFormat($removedItems, '[', ']');
$customRating = correctFormat($customRating, '{', '}');
$customOrder = correctFormat($customOrder, '{', '}');
echo '<div id="tableHolder" class="comparisson-plg"
data-minRating="'.$minRating.'"
data-adserviceFeed="true"
data-cleanLink="true"
data-sortBy="'.$sortBy.'"
data-sortOrder=""
data-template="#outputTmpl"
data-publisherId="'.$account.'"
data-categoryId="'.$template.'"
data-searchForm="#searchTable"
data-sub="'.$sub.'|||CPL"
data-media="'.$media.'"
data-initialRows="'.$initialRows.'"
data-maxRows="'.$maxRows.'"
data-conditions=\''.$conditions.'\'
data-customRating="'.$customRating.'"
data-customOrder="'.$customOrder.'"
data-removedItems="'.$removedItems.'"
data-includeNewCampaigns="'.$includeNewCampaigns.'"
data-lazyLoading="'.$lazyLoading.'">
</div> ';
// Include template for 'View more' button if initial rows is set.
if (intval($initialRows) > 0) {
include( plugin_dir_path( __FILE__ ) . '/templates-dist/view-more.html');
}
include( plugin_dir_path( __FILE__ ) . '/compare-feed-wp-template.php');
$output = ob_get_clean();
return $output;
} else {
return "<div style='display:none'>You haven't connected an Adservice account on the settings page - please connect your Adservice account and try again.</div>";
}
}
/*
Handles the [adservice-comparison-feed-iframe] shortcode.
*/
function doAdserviceComparisonFeedIframeShortcode($atts) {
$adservice_options = get_option("adservice_options");
if ($adservice_options['login']) {
ob_start();
global $post;
$postID = get_the_ID();
$custom_content = get_post_custom($postID);
if (isset($custom_content["compare-template"])) {
$template = $custom_content["compare-template"][0];
}
if (isset($custom_content["compare-account"])) {
$account = $custom_content["compare-account"][0];
}
//$cleanLink = $adservice_options['cleanLink'] || "true";
// Use the custom min rating defined on the post or use the default of 60.
$minRating = get_post_meta($postID , 'min-rating', true);
if (!(strlen($minRating) > 0 && intval($minRating) >= 0)) {
$minRating = 60;
}
$sub = str_replace(' ', '',get_post_meta($postID , 'subID', true));
$media = str_replace(' ', '',get_post_meta($postID, 'mediaID',true));
$removedItems = get_post_meta($postID , 'removed-items', true);
$conditions = str_replace('"', '"', get_post_meta($postID , 'conditions', true));
$customRating = str_replace(' ', '', get_post_meta($postID , 'custom-rating', true));
$customOrder = str_replace(' ', '', get_post_meta($postID , 'custom-order', true));
$initialRows = get_post_meta($postID , 'initial-rows', true);
$maxRows = get_post_meta($postID , 'max-rows', true);
$buttonColor = str_replace('#', '', get_post_meta($postID , 'button-color', true));
$buttonHoverColor = str_replace('#', '', get_post_meta($postID , 'button-hover-color', true));
$fontUrl = get_post_meta($postID , 'font-url', true);
$fontName = get_post_meta($postID , 'font-name', true);
$showFilter = get_post_meta($postID , 'show-filter', true);
$includeNewCampaigns = get_post_meta($postID , 'include-new-campaigns', true);
$lazyLoading = get_post_meta($postID , 'lazy-loading', true);
// Make sure removedItems, customRating, and customOrder have proper format.
$removedItems = correctFormat($removedItems, '[', ']');
$customRating = correctFormat($customRating, '{', '}');
$customOrder = correctFormat($customOrder, '{', '}');
/*
Old way of loading iframe feed through router.
It's more efficient to not use the router and we avoid the
problem where Google index feed.php.
echo '<script type="text/javascript" src="' . plugins_url( '/js/iframeResizer.min.js', __FILE__ ) . '"></script>
<iframe id="as-comparison-iframe" style="border: none; width: 1px; min-width: 100%; overflow: hidden;" scrolling="no" src="'.$plugins_url.'routers/feed.php?location='.$plugins_url.'&path=/cgi-bin/publisher/comparisoniFramePreview.pl?id='.$account.'&categoryId='.$template.'&showFilter='.$showFilter.'&removedItems='.$removedItems.'&customOrder='.$customOrder.'&customRating='.$customRating.'&title=&color='.$buttonColor.'&hColor='.$buttonHoverColor.'&minRating='.$minRating.'&maxRows='.$maxRows.'&fontUrl='.$fontUrl.'&fontName='.$fontName.'&sub='.$sub.'|||CPL&media_id='.$media.'" onload="iFrameResize({heightCalculationMethod: \'taggedElement\', checkOrigin: false}, this);"></iframe>';
*/
echo '<script type="text/javascript" src="//feedcontentcloud.com/comparisonfeed/resources/js/iframeResizer.min.js"></script>
<iframe id="as-comparison-iframe"
style="border: none; width: 1px; min-width: 100%; overflow: hidden;" scrolling="no"
src="//feedcontentcloud.com/cgi-bin/publisher/comparisoniFramePreview.pl?id='.$account
.'&categoryId='.$template
.'&showFilter='.$showFilter
.'&includeNewCampaigns='.$includeNewCampaigns
.'&removedItems='.$removedItems
.'&conditions='.$conditions
.'&customOrder='.$customOrder
.'&customRating='.$customRating
.'&title=&color='.$buttonColor
.'&hColor='.$buttonHoverColor
.'&minRating='.$minRating
.'&initialRows='.$initialRows
.'&maxRows='.$maxRows
.'&fontUrl='.$fontUrl
.'&fontName='.$fontName
.'&sub='.$sub.'|||CPL'
.'&media_id='.$media
.'&lazyLoading='.$lazyLoading.'"
onload="iFrameResize({heightCalculationMethod: \'taggedElement\', checkOrigin: false}, this);">
</iframe>';
$output = ob_get_clean();
return $output;
} else {
return "<div style='display:none'>You haven't connected an Adservice account on the settings page - please connect your Adservice account and try again.</div>";
}
}
/*
Handles the [adservice-comparison-filter] shortcode.
*/
function doAdserviceComparisonFilterShortcode($atts) {
// Include settings.php to get access to $categoryNames.
include('settings.php');
$adservice_options = get_option("adservice_options");
if ($adservice_options['login']) {
enqueueScripts();
ob_start();
global $post;
$custom_content = get_post_custom($post->ID);
if (isset($custom_content["compare-template"])) {
$template = $custom_content["compare-template"][0];
}
// For 'All loans' we just show the Loans filter
// Otherwise use the category to find the correct filter.
if ($template === '180,147') {
include( plugin_dir_path( __FILE__ ) . 'templates-dist/' .
$categoryNames[147] . '/filter.html');
} else {
include( plugin_dir_path( __FILE__ ) . 'templates-dist/' .
$categoryNames[$template] . '/filter.html');
}
$output = ob_get_clean();
return $output;
} else {
return "<div style='display:none'>You haven't connected an Adservice account on the settings page - please connect your Adservice account and try again.</div>";
}
}
function doAdserviceProductFeedShortcode($atts) {
$id = $atts['feed'];
$template = (isset($atts['template'])) ? $atts['template'] : 'template';
if (!isset($id) || intval($id) <= 0) {
return "<div style='display:none'>Adservice Comparison Feed: Invalid or missing feed ID \"" . $id . "\"</div>
<script>console.log('Adservice Comparison Feed: Invalid or missing feed ID \"" . $id . "\"');</script>";
}
// Unique id appended to filter, feed and template containers.
$uuid = GUID();
// Feed settings exists.
ob_start();
// Display iframe feed.
// Register iframe resizer script in shortcode to make sure we can add
// the inline script later on that initialize the iframe resizer.
// Source: https://core.trac.wordpress.org/ticket/54958
wp_register_script(
'iframe-resizer',
'//feedcontentcloud.com/comparisonfeed/resources/js/iframeResizer.min.js',
false,
CURRENT_VERSION,
true
);
wp_enqueue_script( 'iframe-resizer' );
// Build iframe parameters from shortcode attributes.
$iframeParams = '';
foreach(array_keys($atts) as $key) {
// Don't include the feed parameter.
if ($key !== 'feed') {
$iframeParams .= $key . '=' . $atts[$key] . '&';
}
}
$iframeParams = substr($iframeParams, 0, -1);
// Print iframe tag.
// Class 'fitvidsignore' is needed to avoid FitVids.js wrapping
// our iframe in a container that breaks page layout.
// Source: https://github.com/davatron5000/FitVids.js
echo '<iframe id="as-comparison-iframe-'.$uuid.'"
class="fitvidsignore"
style="border: none; width: 1px; min-width: 100%; overflow: hidden;"
scrolling="no"
src="//feedcontentcloud.com/v2/public/publisher/productfeeds/'.$id.'/iframe?'.$iframeParams.'">
</iframe>';
// Add inline script for initializing iframe resizing after iframe-resizer
// to make sure it's loaded first.
$script = "iFrameResize({heightCalculationMethod: 'taggedElement', checkOrigin: false}, '#as-comparison-iframe-$uuid');";
wp_add_inline_script('iframe-resizer', $script);
$output = ob_get_clean();
return $output;
}
?>