_ok = parent::isValidAid( true );
// Set prefix comparator
$this->current_options = get_option( $this->option_name );
$this->prefix = $this->current_options['prefix'];
// URL and Rewriting
add_action('init', array(&$this, 'rewriteInit') );
add_filter('query_vars' , array(&$this, 'addQueryVars'));
add_action('parse_query', array(&$this,'parseQuery'));
$this->checkJS();
// Home ? page ?
add_action( 'template_redirect', array(&$this, 'checkHomePage') );
if ( !is_admin() ) {
// Check for CSS, load CSS and exit !
$this->checkCSS();
}
}
function &getInstance()
{
static $singleton;
if (!$singleton)
$singleton = new AdfeverClient_Comparator();
return $singleton;
}
/**
* Be tolerant with users URL, allow to redirect the adresse : monblog.com/adfever-comparator to the adfever home if rewriting is enabled.
**/
function checkHomePage() {
global $wp_query;
if ( $wp_query->query_vars['pagename'] == $this->prefix && $wp_query->is_404 == true && $this->rewriting == true ) {
wp_redirect( $this->link('home') );
exit();
}
return false;
}
/**
* Add a robots meta HTML for allow or not crowlings... Adfever option.
**/
function displayRobots() {
if ( $this->current_options['noindex'] != 1 )
echo "\n" . ' ' . "\n";
}
/**
* Determine if adfever rewriting must be init and build base URL for adfever
**/
function rewriteInit() {
global $wp_rewrite;
// Detect permalink type & construct base URL for local links
$this->base_url = get_option('home') . '/';
if (isset($wp_rewrite) && $wp_rewrite->using_permalinks()) { // Permalink
$this->rewriting = true;
//$this->base_url .= ( substr($wp_rewrite->front, 0, 1) == '/' ) ? substr($wp_rewrite->front, 1, strlen($wp_rewrite->front)) : $wp_rewrite->front;
//wp_die($this->base_url);
$this->base_url .= $wp_rewrite->root; // set to "index.php/" if using that style
$this->base_url .= $this->prefix . '/';
add_filter('generate_rewrite_rules', array(&$this, 'createRewriteRules' ) );
} else { // Old school links
$this->rewriting = false;
$this->base_url .= '?' . $this->prefix . '=';
}
}
/**
* Add rewrite rules for allow adfever rewriting with order param
**/
function createRewriteRules() {
global $wp_rewrite;
// Add rewrite tokens for main rewriting
$token_adfever = '%'.$this->prefix.'%';
$wp_rewrite->add_rewrite_tag($token_adfever, '(.+?)', $this->prefix.'=');
// Add seconds parameter, for order by example
$key_adfever_order = '%'.$this->prefix.'-order%';
$wp_rewrite->add_rewrite_tag($key_adfever_order, '([^/]+?)', $this->prefix.'-order=');
// Build rules
$adfever_rewrite = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root . "$this->prefix/$token_adfever/$this->prefix-order/$key_adfever_order/", EP_NONE, true, false );
// Add new rules in WP array
$wp_rewrite->rules = $adfever_rewrite + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
/**
* Add Adfever keywords in WordPress query vars to be accessible with WordPress fonction
**/
/*function addQueryVars( $query_vars = array() ) {
// Execute hook once !
remove_filter('query_vars', array(&$this, 'addQueryVars'));
$query_vars[] = $this->prefix;
$query_vars[] = $this->prefix.'-order';
return $query_vars;
}*/
/**
* Parse query and determine if adfever process must be launch...
**/
function parseQuery() {
// Execute hook once !
remove_action('parse_query', array(&$this, 'parseQuery'));
$this->raw_item = stripslashes(get_query_var($this->prefix));
$this->raw_order = stripslashes(get_query_var($this->prefix.'-order'));
if ( get_magic_quotes_gpc() ) { // why so many freakin' slashes?
$this->raw_item = stripslashes($this->raw_item);
$this->raw_order = stripslashes($this->raw_order);
}
if ( !empty($this->raw_item) ) {
// Parse query
$this->is_adfever = true;
$this->analyseQuery();
// Add JS for product
if ( $this->is_product == true ) {
wp_enqueue_script( 'jquery-tablesorter', ADFEVER_URL . '/lib/jquery-tablesorter/jquery.tablesorter.min.js', array('jquery'), '2.0.3' );
wp_enqueue_script( 'adfever-comparator', ADFEVER_URL . '/inc/js/comparator.js', array('jquery', 'jquery-tablesorter'), $this->version );
wp_enqueue_style ( 'jquery-tablesorter', ADFEVER_URL . '/lib/jquery-tablesorter/style.css', array(), '2.0.3', 'all' );
}
// Add CSS on header
wp_enqueue_style( 'adfever-comparator', get_bloginfo('siteurl').'/?adfever-css=comparator', array(), $this->version, 'all' );
wp_enqueue_script('loadimages', get_bloginfo('siteurl').'/?adfever-js=comparator', array(), $this->version, 'all' );
// Redirect to specific template
add_action('template_redirect', array(&$this, 'templateRedirect'), 1);
add_filter('wp_title', array(&$this, 'addTitle') );
add_action( 'wp_head', array(&$this, 'displayRobots') );
return true;
}
return false;
}
function addQueryVars($query_vars = array()) {
$current_options = get_option( 'adfever' );
$prefix = $current_options['prefix'];
remove_filter('query_vars', 'addQueryVars');
$query_vars[] = $prefix;
$query_vars[] = $prefix.'-order';
return $query_vars;
}
/**
* Specify a title for adfever comparator, optimization SEO
**/
function addTitle( $title = '', $sep = '»' ) {
if ( !empty($this->pagename) ) {
return $sep . wp_specialchars($this->pagename);
}
return __('Home', 'adfever') . ' ' . $sep . ' ' . __('Price comparator', 'adfever') . ' ' . $sep;
}
/**
* Redirect view to adfever template if it exist...
**/
function templateRedirect() {
if ( !empty($this->raw_item) ) {
$template = '';
if ( is_file( TEMPLATEPATH . '/' . ADFEVER_THEME_FILE ) ) {
$template = TEMPLATEPATH . '/' . ADFEVER_THEME_FILE;
}
else if ( is_file( ADFEVER_DIR . '/samples/' . ADFEVER_THEME_FILE ) ) {
$template = ADFEVER_DIR . '/samples/' . ADFEVER_THEME_FILE;
}
else {
wp_die( sprintf(__('Template file %s are required for this plugin.', 'adfever'), ADFEVER_THEME_FILE ) );
}
if ( !empty($template) ) {
load_template($template);
exit();
}
}
return false;
}
/**
* Count how many univers and categories is selected
**/
function countUserSelection() {
$this->count_univers = count($this->current_options['multiples_cat']['univers']);
$this->count_categories = count($this->current_options['multiples_cat']['categories']);
}
/**
* Analyse the query for determine wich template must be loaded...
**/
function analyseQuery() {
// Check for valid AID
$result = parent::isValidAid( true );
if ( isset($this->current_options['categories_selection']) && $this->current_options['categories_selection'] == 'sel' ) $this->countUserSelection();
// Init shopping before API Call
// Init only if Shopping isn't already loaded
$shop_obj = $this->getShopObj();
if ( $result == true ) {
if ( $this->raw_item == 'home') {
$this->is_front_page = true;
if ( $this->current_options['categories_selection'] == 'sel' ) {
// Load pre-saved configuration.
foreach( (array) $this->current_options[$this->option_field_home] as $class_var_name => $value ) {
$this->{$class_var_name} = $value;
}
}
else { // All categories, no filter, too easy !
$this->is_home = true;
$this->tpl_file = 'home.php';
}
}
else if( $this->raw_item == 'search' ) {
$this->is_search = true;
$this->tpl_file = 'search.php';
$this->current_item = stripslashes( $_GET['terms'] );
$this->current_cat_id = intval( $_GET['cat_id'] );
// Search on univers, but only selected category
if ( $this->current_options[$this->option_field_home]['is_home_type2'] == true &&
in_array( $this->current_cat_id, $this->current_options['multiples_cat']['univers'] ) ) {
$this->search_cat_id = $this->current_options['multiples_cat']['categories'];
}
else if ( $this->current_options[$this->option_field_home]['is_home_type1'] == true && in_array( $this->current_cat_id, $this->current_options['multiples_cat']['univers'] ) ) {
if ( in_array( $this->current_cat_id, $this->current_options['multiples_cat']['categories'] ) ) {
$this->search_cat_id = $this->current_cat_id;
}
else {
$this->search_cat_id = $this->getCategoryHierarchy( $this->current_cat_id );
}
}
else {
$this->search_cat_id = $this->current_cat_id;
}
}
else {
$parts = explode('/', $this->raw_item);
if ( $parts[0] == 'category' ) {
$this->tpl_file = 'category.php';
$this->is_category = true;
$this->current_item = $parts[1];
$cat_id = $this->isUnivers($this->current_item, true);
if ( $cat_id !== false ) {
$this->current_cat_id = $cat_id;
$this->tpl_file = 'univers.php';
$this->is_univers = true;
}
}
else if ( $parts[0] == 'product' ) {
$this->tpl_file = 'product.php';
$this->is_product = true;
$this->current_item = $parts[1];
}
}
}
else {
$this->tpl_file = 'maintenance.php';
$this->is_maintenance = true;
}
// If empty template, load 404.
if ( empty($this->tpl_file) ) {
$this->tpl_file = '404.php';
$this->is_404 = true;
}
}
function isUnivers( $current_item = 0, $sub_category = false ) {
$shop_obj = $this->getShopObj();
$all_categories = $this->getCategories();
$all_categories = $all_categories['shopping']['category'][0]['categories']['category'];
foreach( (array) $all_categories as $category ) {
if ( $sub_category == true && isset( $category["categories"]["category"] ) && is_array( $category["categories"]["category"] ) && !empty($category["categories"]["category"]) ) {
foreach( $category["categories"]["category"] as $sub_category ) {
if ( isset( $sub_category["categories"]["category"] ) && is_array( $sub_category["categories"]["category"] ) && !empty($sub_category["categories"]["category"]) ) {
if ( $sub_category['kwd'] == $current_item || (int) $current_item == $sub_category['id'] ) {
return $sub_category['id'];
}
}
}
}
if ( $category['kwd'] == $current_item || (int) $current_item == $category['id'] ) {
return $category['id'];
}
}
return false;
}
function render() {
$shop_obj = $this->getShopObj();
// Fixed paged
$paged = (int) get_query_var('paged');
if ( $paged < 1 ) $paged = 1;
// Prepare datas for template
if ( $this->is_home == true ) {
$all_categories = $this->filterCategories( $this->getCategories(), 'both', 'render_home' );
$max_cat_to_display = parent::getOption( 'c-sub-categories', 'integer', 'comparator', 'design' ); // Default 3
}
else if ( $this->is_univers == true ) {
$categories = $this->filterCategories( $this->getCategories($this->current_cat_id), 'categories', 'render_univers' );
}
else if ( $this->is_category == true ) {
$products = $shop_obj->findForCategory( $this->current_item, $paged, $this->current_options['c-products-per-page'], SORT_PRICE, (( $this->raw_order == 'less' ) ? 'DESC' : 'ASC') );
$this->current_cat_id = $products["shopping"]["meta"]["category_id"];
$this->total_items = $products["shopping"]["meta"]["total"];
$products = $products["shopping"]["products"]["product"];
}
else if ( $this->is_search == true ) {
if($this->raw_order) {
$sort = SORT_PRICE;
}
else {
$sort = false;
}
$products = $shop_obj->search( $this->current_item, $this->search_cat_id, $paged, $this->current_options['c-products-per-page'], $sort, (( $this->raw_order == 'less' ) ? 'ASC' : 'DESC') );
$this->total_items = $products["shopping"]["meta"]["total"];
$quantity_results = $this->total_items;
$products = $products["shopping"]["products"]["product"];
$terms = $this->current_item;
}
else if( $this->is_product == true ) {
$product = $shop_obj->find( $this->current_item );
$external = (parent::getOption( 'link-target', 'integer', 'general', 'account' )==1 ? 1 : 0);
$nofollow = (parent::getOption('nofollow', 'integer', 'general', 'account')==1 ? 1 : 0);
$product = $product["shopping"]["product"][0];
if ( empty($product) || is_string($product) ) {
$this->tpl_file = '404.php';
$this->is_404 = true;
}
else {
$this->pagename = $product['name'];
$this->current_id = $this->current_item;
$this->current_cat_id = $product['category'][0]['id'];
$stores = $product["offers"]["offer"];
}
}
// Put template in buffer, for work on render
ob_start();
$tpl_file = $this->getFilePathTemplate($this->tpl_file);
if ( !empty($tpl_file) ) {
include( $tpl_file );
}
else {
$tpl_file = $this->getFilePathTemplate('404.php');
if ( !empty($tpl_file) ) {
include( $tpl_file );
}
else {
echo __( 'Adfever - Internal error - Impossible to load a template. Please reinstall the plugin.', 'adfever' );
}
}
$output = ob_get_contents();
ob_end_clean();
// Include bloc
$output = str_replace( '{include:search}', $this->includeTemplate('bloc.search.php'), $output );
$output = str_replace( '{include:top-products}', $this->includeTemplate('bloc.top-products.php'), $output );
$options = get_option('adfever');
if(isset($options['top-category']) && $options['top-category']==1) {
$output = str_replace( '{include:top-categories}', $this->includeTemplate('bloc.top-categories.php'), $output );
}
else {
$output = str_replace('{include:top-categories}', '', $output);
}
$output = str_replace( '{include:breadcrumb}', $this->includeTemplate('bloc.breadcrumb.php'), $output );
$output = str_replace( '{pagination}', $this->getPagination(), $output );
// Add container
$output = "\n