function getFeedData($category, $remote_category, $file_name, $saved_feed = null)
 {
     $this->logActivity('Initializing...');
     global $message;
     global $pfcore;
     $providers = new PProviderList();
     $this->logActivity('Loading paths...');
     if (!$this->checkFolders()) {
         return;
     }
     $file_url = PFeedFolder::uploadFolder() . $this->providerName . '/' . $file_name . '.' . $this->fileformat;
     $file_path = PFeedFolder::uploadURL() . $this->providerName . '/' . $file_name . '.' . $this->fileformat;
     //Special (WordPress): where admin is https and site is http, path to wp-uploads works out incorrectly as https
     //  we check the content_url() for https... if not present, patch the file_path
     if ($pfcore->cmsName == 'WordPress' && strpos($file_path, 'https://') !== false && strpos(content_url(), 'https') === false) {
         $file_path = str_replace('https://', 'http://', $file_path);
     }
     //Create the Feed
     $this->logActivity('Creating feed data');
     $this->filename = $file_url;
     $this->productCount = 0;
     $content = 'Place-Holder File, This file will be replaced next refresh, To manually fill this data, go to manage feeds and click [Update Now]';
     file_put_contents($this->filename, $content);
     $this->logActivity('Updating Feed List');
     PFeedActivityLog::updateFeedList($category, $remote_category, $file_name, $file_path, $this->providerName, $this->productCount);
     //Save the feedlist
     $id = PFeedActivityLog::feedDataToID($file_name, $this->providerName);
     $pfcore->settingSet('cpf_aggrfeedlist_' . $id, implode(',', $this->feed_list));
     if ($this->productCount == 0) {
         //$this->message .= '<br>No products returned';
         //return;
     }
     $this->success = true;
 }
 function initializeAggregateFeed($id, $file_name)
 {
     $this->filename = PFeedFolder::uploadFolder() . $this->providerName . '/' . $file_name . '.' . $this->fileformat;
     $this->file_url = PFeedFolder::uploadURL() . $this->providerName . '/' . $file_name . '.' . $this->fileformat;
     $this->productCount = 0;
     $this->file_name_short = $file_name;
     if (!isset($this->feeds) || count($this->feeds) == 0) {
         global $pfcore;
         $data = $pfcore->settingGet('cpf_aggrfeedlist_' . $id);
         $data = explode(',', $data);
         $this->feeds = array();
         foreach ($data as $datum) {
             $this->feeds[$datum] = true;
         }
     }
 }
function feeds_main_table()
{
    global $wpdb;
    $feed_table = $wpdb->prefix . 'cp_feeds';
    $providerList = new PProviderList();
    // Read the feeds
    $sql_feeds = "SELECT f.*,description FROM {$feed_table} as f LEFT JOIN {$wpdb->term_taxonomy} on ( f.category=term_id and taxonomy='product_cat'  ) ORDER BY f.id";
    $list_of_feeds = $wpdb->get_results($sql_feeds, ARRAY_A);
    // Find the ordering method
    $reverse = false;
    if (isset($_GET['order_by'])) {
        $order = $_GET['order_by'];
    } else {
        $order = '';
    }
    if ($order == '') {
        $order = get_option('cp_feed_order');
        $reverse = get_option('cp_feed_order_reverse');
    } else {
        $old_order = get_option('cp_feed_order');
        $reverse = get_option('cp_feed_order_reverse');
        if ($old_order == $order) {
            $reverse = !$reverse;
        } else {
            $reverse = FALSE;
        }
        update_option('cp_feed_order', $order);
        if ($reverse) {
            update_option('cp_feed_order_reverse', TRUE);
        } else {
            update_option('cp_feed_order_reverse', FALSE);
        }
    }
    if (!empty($list_of_feeds)) {
        // Setup the sequence array
        $seq = false;
        $num = false;
        foreach ($list_of_feeds as $this_feed) {
            $this_feed_ex = new PSavedFeed($this_feed['id']);
            switch ($order) {
                case 'name':
                    $seq[] = strtolower(stripslashes($this_feed['filename']));
                    break;
                case 'description':
                    $seq[] = strtolower(stripslashes($this_feed_ex->local_category));
                    break;
                case 'url':
                    $seq[] = strtolower($this_feed['url']);
                    break;
                case 'category':
                    $seq[] = $this_feed['category'];
                    $num = true;
                    break;
                case 'google_category':
                    $seq[] = $this_feed['remote_category'];
                    break;
                case 'type':
                    $seq[] = $this_feed['type'];
                    break;
                default:
                    $seq[] = $this_feed['id'];
                    $num = true;
                    break;
            }
        }
        // Sort the seq array
        if ($num) {
            asort($seq, SORT_NUMERIC);
        } else {
            asort($seq, SORT_REGULAR);
        }
        // Reverse ?
        if ($reverse) {
            $t = $seq;
            $c = count($t);
            $tmp = array_keys($t);
            $seq = false;
            for ($i = $c - 1; $i >= 0; $i--) {
                $seq[$tmp[$i]] = '0';
            }
        }
        $image['down_arrow'] = '<img src="' . esc_url(plugins_url('images/down.png', __FILE__)) . '" alt="down" style=" height:12px; position:relative; top:2px; " />';
        $image['up_arrow'] = '<img src="' . esc_url(plugins_url('images/up.png', __FILE__)) . '" alt="up" style=" height:12px; position:relative; top:2px; " />';
        ?>
        <!--	<div class="table_wrapper">	-->
        <table class="widefat" style="margin-top:12px;" >
            <thead>
                <tr>
                    <?php 
        $url = get_admin_url() . 'admin.php?page=cart-product-feed-manage-page&amp;order_by=';
        ?>
                    <th scope="col" style="min-width: 40px;" >
                        <a href="<?php 
        echo $url . "id";
        ?>
">
                            <?php 
        _e('ID', 'cart-product-strings');
        if ($order == 'id') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 120px;">
                        <a href="<?php 
        echo $url . "name";
        ?>
">
                            <?php 
        _e('Name', 'cart-product-strings');
        if ($order == 'name') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col">
                        <a href="<?php 
        echo $url . "category";
        ?>
">
                            <?php 
        _e('Local category', 'cart-product-strings');
        if ($order == 'category') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 100px;">
                        <a href="<?php 
        echo $url . "google_category";
        ?>
">
                            <?php 
        _e('Export category', 'cart-product-strings');
        if ($order == 'google_category') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 50px;" >
                        <a href="<?php 
        echo $url . "type";
        ?>
">
                            <?php 
        _e('Type', 'cart-product-strings');
        if ($order == 'type') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="width: 120px;">
                        <a href="<?php 
        echo $url . "url";
        ?>
">
                            <?php 
        _e('URL', 'cart-product-strings');
        if ($order == 'url') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="width: 80px;"><?php 
        _e('Last Updated', 'cart-product-strings');
        ?>
</th>
                    <th scope="col" width="50px"><?php 
        //_e( 'View', 'cart-product-strings' );
        ?>
</th>
										<th scope="col" width="50px"><?php 
        _e('Options', 'cart-product-strings');
        ?>
</th>
                    <th scope="col" width="50px"><?php 
        //_e( 'Delete', 'cart-product-strings' );
        ?>
</th>
										<th scope="col"><?php 
        _e('Products', 'cart-product-strings');
        ?>
</th>
                </tr>
            </thead>
            <tbody>
                <?php 
        $alt = ' class="alternate" ';
        ?>

                <?php 
        $idx = '0';
        foreach (array_keys($seq) as $s) {
            $this_feed = $list_of_feeds[$s];
            $this_feed_ex = new PSavedFeed($this_feed['id']);
            $pendcount = FALSE;
            ?>
                    <tr <?php 
            echo $alt;
            if ($pendcount) {
                echo 'style="background-color:#ffdddd"';
            }
            ?>
>
                        <td><?php 
            echo $this_feed['id'];
            ?>
</td>
                        <td><?php 
            echo $this_feed['filename'];
            ?>
</td>
                        <td><small><?php 
            echo esc_attr(stripslashes($this_feed_ex->local_category));
            ?>
</small></td>
                        <td><?php 
            echo str_replace(".and.", " & ", str_replace(".in.", " > ", esc_attr(stripslashes($this_feed['remote_category']))));
            ?>
</td>
                        <td><?php 
            echo $providerList->getPrettyNameByType($this_feed['type']);
            ?>
</td>
                        <td><?php 
            echo $this_feed['url'];
            ?>
</td>
                        <?php 
            //$url = get_admin_url() . 'admin.php?page=??? ( edit feed ) &amp;tab=edit&amp;edit_id=' . $this_feed['id'];
            ?>
                        <td><?php 
            $ext = '.' . $providerList->getExtensionByType($this_feed['type']);
            $feed_file = PFeedFolder::uploadFolder() . $this_feed['type'] . '/' . $this_feed['filename'] . $ext;
            if (file_exists($feed_file)) {
                echo date("d-m-Y H:i:s", filemtime($feed_file));
            } else {
                echo 'DNE';
            }
            ?>
</td>

                        <td><a href="<?php 
            echo $this_feed['url'];
            ?>
" target="_blank" class="purple_xmlsedit"><?php 
            _e('View', 'cart-product-strings');
            ?>
</a></td>
						<?php 
            $url_edit = get_admin_url() . 'admin.php?page=cart-product-feed-admin&action=edit&id=' . $this_feed['id'];
            ?>
						<td><a href="<?php 
            echo $url_edit;
            ?>
" class="purple_xmlsedit"><?php 
            _e('Edit', 'cart-product-strings');
            ?>
</a></td>
                        <?php 
            $url = get_admin_url() . 'admin.php?page=cart-product-feed-manage-page&action=delete&id=' . $this_feed['id'];
            ?>
                        <td><a href="<?php 
            echo $url;
            ?>
" class="purple_xmlsedit"><?php 
            _e('Delete', 'cart-product-strings');
            ?>
</a></td>
						<td><?php 
            echo $this_feed['product_count'];
            ?>
</td>

                    </tr>
                    <?php 
            if ($alt == '') {
                $alt = ' class="alternate" ';
            } else {
                $alt = '';
            }
            $idx++;
        }
        ?>
            </tbody>
            <tfoot>
                <tr>
                    <?php 
        $url = get_admin_url() . 'admin.php?page=cart-product-manage-page&amp;order_by=';
        $order = '';
        ?>
                    <th scope="col" style="min-width: 40px;" >
                        <a href="<?php 
        echo $url . "id";
        ?>
">
                            <?php 
        _e('ID', 'cart-product-strings');
        if ($order == 'id') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 120px;">
                        <a href="<?php 
        echo $url . "name";
        ?>
">
                            <?php 
        _e('Name', 'cart-product-strings');
        if ($order == 'name') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col">
                        <a href="<?php 
        echo $url . "category";
        ?>
">
                            <?php 
        _e('Local Category', 'cart-product-strings');
        if ($order == 'category') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 100px;">
                        <a href="<?php 
        echo $url . "google_category";
        ?>
">
                            <?php 
        _e('Export category', 'cart-product-strings');
        if ($order == 'google_category') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="min-width: 50px;" >
                        <a href="<?php 
        echo $url . "type";
        ?>
">
                            <?php 
        _e('Type', 'cart-product-strings');
        if ($order == 'type') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="width: 120px;">
                        <a href="<?php 
        echo $url . "url";
        ?>
">
                            <?php 
        _e('URL', 'cart-product-strings');
        if ($order == 'url') {
            if ($reverse) {
                echo $image['up_arrow'];
            } else {
                echo $image['down_arrow'];
            }
        }
        ?>
                        </a>
                    </th>
                    <th scope="col" style="width: 80px;"><?php 
        _e('Last Updated', 'cart-product-strings');
        ?>
</th>
                    <th scope="col"><?php 
        //_e( 'View', 'cart-product-strings' );
        ?>
</th>
										<th scope="col"><?php 
        _e('Options', 'cart-product-strings');
        ?>
</th>
                    <th scope="col"><?php 
        //_e( 'Delete', 'cart-product-strings' );
        ?>
</th>
										<th scope="col"><?php 
        _e('Products', 'cart-product-strings');
        ?>
</th>
                </tr>
            </tfoot>

        </table>

		<input class="button-primary" type="submit" value="Update Now" id="submit" name="submit" onclick="doUpdateAllFeeds()">
		<div id="update-message">&nbsp;</div>
        <!--	</div> -->
        <?php 
    } else {
        ?>
        <p><?php 
        _e('No feeds yet!', 'cart-product-strings');
        ?>
</p>
        <?php 
    }
}
function get_feed_main()
{
    $requestCode = safeGetPostData('provider');
    $local_category = safeGetPostData('local_category');
    $remote_category = safeGetPostData('remote_category');
    $file_name = safeGetPostData('file_name');
    $feedIdentifier = safeGetPostData('feed_identifier');
    $saved_feed_id = safeGetPostData('feed_id');
    $feed_list = safeGetPostData('feed_ids');
    //For Aggregate Feed Provider
    $output = new stdClass();
    $output->url = '';
    if (strlen($requestCode) * strlen($local_category) == 0) {
        $output->errors = 'Error: error in AJAX request. Insufficient data or categories supplied.';
        doOutput($output);
        return;
    }
    if (strlen($remote_category) == 0) {
        $output->errors = 'Error: Insufficient data. Please fill in "' . $requestCode . ' category"';
        doOutput($output);
        return;
    }
    // Check if form was posted and select task accordingly
    $dir = PFeedFolder::uploadRoot();
    if (!is_writable($dir)) {
        $output->errors = "Error: {$dir} should be writeable";
        doOutput($output);
        return;
    }
    $dir = PFeedFolder::uploadFolder();
    if (!is_dir($dir)) {
        mkdir($dir);
    }
    if (!is_writable($dir)) {
        $output->errors = "Error: {$dir} should be writeable";
        doOutput($output);
        return;
    }
    $providerFile = 'feeds/' . strtolower($requestCode) . '/feed.php';
    if (!file_exists(dirname(__FILE__) . '/../../' . $providerFile)) {
        if (!class_exists('P' . $requestCode . 'Feed')) {
            $output->errors = 'Error: Provider file not found.';
            doOutput($output);
            return;
        }
    }
    $providerFileFull = dirname(__FILE__) . '/../../' . $providerFile;
    if (file_exists($providerFileFull)) {
        require_once $providerFileFull;
    }
    //Load form data
    $file_name = sanitize_title_with_dashes($file_name);
    if ($file_name == '') {
        $file_name = 'feed' . rand(10, 1000);
    }
    $saved_feed = null;
    if (strlen($saved_feed_id) > 0 && $saved_feed_id > -1) {
        require_once dirname(__FILE__) . '/../../data/savedfeed.php';
        $saved_feed = new PSavedFeed($saved_feed_id);
    }
    $providerClass = 'P' . $requestCode . 'Feed';
    $x = new $providerClass();
    $x->feed_list = $feed_list;
    //For Aggregate Provider only
    if (strlen($feedIdentifier) > 0) {
        $x->activityLogger = new PFeedActivityLog($feedIdentifier);
    }
    $x->getFeedData($local_category, $remote_category, $file_name, $saved_feed);
    if ($x->success) {
        $output->url = PFeedFolder::uploadURL() . $x->providerName . '/' . $file_name . '.' . $x->fileformat;
    }
    $output->errors = $x->getErrorMessages();
    doOutput($output);
}