Skip to content

Commit

Permalink
Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
koen12344 committed Nov 7, 2024
1 parent fcf60c6 commit 59d5902
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 49 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
**Requires at least:** 5.1
**Tested up to:** 6.6.1
**Requires PHP:** 7.0
**Stable tag:** 0.1.1
**Stable tag:** 0.2.0
**License:** GPLv2 or later
**License URI:** https://www.gnu.org/licenses/gpl-2.0.html

A beautiful way to display products from affiliate network product feeds on your website. Currently supports TradeTracker and AdTraction.
A beautiful way to display products from affiliate network product feeds on your website. Currently supports Daisycon, TradeTracker and AdTraction.

## Description ##

Expand All @@ -19,6 +19,7 @@ products from product feeds of various affiliate networks.
Networks currently supported:
* AdTraction
* TradeTracker
* Daisycon

**Features:**

Expand Down Expand Up @@ -83,6 +84,12 @@ Colors can be adjusted by overriding the default CSS variables:

## Changelog ##

### 0.2.0 ###
* Added: Uninstall function
* Added: Daisycon support
* Added: Shortcode for direct links to products
* Improved: SQL logic

### 0.1.1 ###
* Added: Product ID and Feed in selection section
* Added: AdTraction sale price
Expand Down
3 changes: 2 additions & 1 deletion affiliate-product-highlights.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://koenreus.com
* Text Domain: affiliate-product-highlights
* Domain Path: /languages
* Version: 0.1.1
* Version: 0.2.0
*
* @package Affiliate_Product_Highlights
*/
Expand All @@ -18,6 +18,7 @@

register_activation_hook(__FILE__, ['Koen12344\AffiliateProductHighlights\Plugin', 'activate']);
register_deactivation_hook(__FILE__, ['Koen12344\AffiliateProductHighlights\Plugin', 'deactivate']);
register_uninstall_hook(__FILE__, ['Koen12344\AffiliateProductHighlights\Plugin', 'uninstall']);

$affiliate_product_highlights = new Plugin(__FILE__);

Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Tags: tradetracker, adtraction, affiliate, feed, products
Requires at least: 5.1
Tested up to: 6.6.1
Requires PHP: 7.0
Stable tag: 0.1.1
Stable tag: 0.2.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

A beautiful way to display products from affiliate network product feeds on your website. Currently supports TradeTracker and AdTraction.
A beautiful way to display products from affiliate network product feeds on your website. Currently supports Daisycon, TradeTracker and AdTraction.

== Description ==

Expand All @@ -19,6 +19,7 @@ products from product feeds of various affiliate networks.
Networks currently supported:
* AdTraction
* TradeTracker
* Daisycon

**Features:**

Expand Down Expand Up @@ -79,6 +80,12 @@ Colors can be adjusted by overriding the default CSS variables:

== Changelog ==

= 0.2.0 =
* Added: Uninstall function
* Added: Daisycon support
* Added: Shortcode for direct links to products
* Improved: SQL logic

= 0.1.1 =
* Added: Product ID and Feed in selection section
* Added: AdTraction sale price
Expand Down
76 changes: 50 additions & 26 deletions src/php/BackgroundProcessing/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Koen12344\AffiliateProductHighlights\BackgroundProcessing;

use Koen12344\AffiliateProductHighlights\Provider\AdTraction\ProductMapping;
use Exception;
use InvalidArgumentException;
use Koen12344\AffiliateProductHighlights\Provider\AdTraction\ProductMapping as AdtractionProductMapping;
use Koen12344\AffiliateProductHighlights\Provider\Daisycon\ProductMapping as DaisyconProductMapping;
use Koen12344\AffiliateProductHighlights\Provider\TradeTracker\ProductMapping as TradeTrackerProductMapping;
use SimpleXMLElement;
use WP_Http;
use XMLReader;
Expand Down Expand Up @@ -55,7 +59,7 @@ public function import_images(int $feed_id, int $product_id, $images){

public function download_xml_file($url) {
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
throw new InvalidArgumentException(__('Invalid feed URL', 'affiliate-product-highlights'));
}

if(!function_exists('wp_tempnam')){
Expand All @@ -66,7 +70,7 @@ public function download_xml_file($url) {
$handle = fopen($tmp_file, 'w');

if ($handle === false) {
return false;
throw new Exception(__('Unable to to get write access to store temporary file.', 'affiliate-product-highlights'));
}

$http = new WP_Http();
Expand All @@ -79,7 +83,7 @@ public function download_xml_file($url) {
if (is_wp_error($response)) {
@fclose($handle);
@unlink($tmp_file);
return false;
throw new Exception(sprintf(__('Unable to download the feed: %s', 'affiliate-product-highlights'), $response->get_error_message()));
}

fclose($handle);
Expand All @@ -93,7 +97,7 @@ public function import_xml($file, int $feed_id, $feed_type) {
$reader->open($file);

while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'product') {
if ($reader->nodeType == XMLReader::ELEMENT && ($reader->name === 'product' || $reader->name === 'product_info')) {
$product = new SimpleXMLElement($reader->readOuterXML());

if($feed_type == 'tradetracker'){
Expand All @@ -103,14 +107,21 @@ public function import_xml($file, int $feed_id, $feed_type) {
(int)$product->campaignID,
$feed_id
));
$mapped_product = new \Koen12344\AffiliateProductHighlights\Provider\TradeTracker\ProductMapping($product);
$mapped_product = new TradeTrackerProductMapping($product);
}elseif($feed_type == 'adtraction'){
$existing_product = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM " . $wpdb->prefix . 'phft_products' . " WHERE sku = %s AND feed_id = %d",
(string)$product->SKU,
$feed_id
));
$mapped_product = new ProductMapping($product);
$mapped_product = new AdtractionProductMapping($product);
}elseif($feed_type == 'daisycon'){
$existing_product = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM " . $wpdb->prefix . 'phft_products' . " WHERE sku = %s AND feed_id = %d",
(string)$product->sku,
$feed_id
));
$mapped_product = new DaisyconProductMapping($product);
}else{
break;
}
Expand All @@ -132,29 +143,30 @@ public function import_xml($file, int $feed_id, $feed_type) {
} else {
$product_slug = sanitize_title($product_data['product_name']);
$product_data['slug'] = $product_slug;
$inserted = false;
$suffix = 1;

while(!$inserted){
while($suffix <= 20){ //Try 20 times then bail
$result = @$wpdb->insert($wpdb->prefix . 'phft_products', $product_data);
if($result){
$inserted = true;
}else{
if($wpdb->last_error && strpos($wpdb->last_error, 'Duplicate entry') !== false){
$product_data['slug'] = $product_slug . '-' . $suffix;
$suffix++;
}else{
break;
}
if($result) {
break;
}

if($wpdb->last_error && strpos($wpdb->last_error, "for key 'slug_unique'") !== false){
$product_data['slug'] = $product_slug . '-' . $suffix;
$suffix++;
continue;
}

break;
}


$inserted_id = $wpdb->insert_id;
}
if($inserted_id > 0){
$this->import_images($feed_id, (int)$inserted_id, $mapped_product->get_product_images());
}

$this->import_images($feed_id, (int)$inserted_id, $mapped_product->get_product_images());
}
}

Expand All @@ -174,7 +186,7 @@ private function split_feed($item){

$output_xml = new SimpleXMLElement('<products/>');
while($reader->read()){
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name === 'product') {
if ($reader->nodeType == XMLReader::ELEMENT && ($reader->name === 'product' || $reader->name === 'product_info')) {
$product = new SimpleXMLElement($reader->readOuterXML());
$node = dom_import_simplexml($output_xml->addChild('product'));
$node->parentNode->replaceChild($node->ownerDocument->importNode(dom_import_simplexml($product), true), $node);
Expand Down Expand Up @@ -223,14 +235,25 @@ private function download_feed($item){

$xml_url = get_post_meta($item['feed_id'], '_phft_feed_url', true);

$temp_file = $this->download_xml_file($xml_url);
$network = $this->get_affiliate_network($xml_url);
if(!$network){
update_post_meta($item['feed_id'], '_phft_last_error', __('The affiliate network this feed belongs to is unrecognized'));
return false;
}

update_post_meta($item['feed_id'], '_phft_last_import', time());
try{
$temp_file = $this->download_xml_file($xml_url);
}catch (Exception $e){
update_post_meta($item['feed_id'], '_phft_last_error', $e->getMessage());
return false;
}

$item['action'] = 'split_feed';
$item['feed_type'] = $this->get_affiliate_network($xml_url);
$item['feed_type'] = $network;
$item['temp_file'] = $temp_file;

update_post_meta($item['feed_id'], '_phft_last_import', time());

return $item;
}

Expand All @@ -249,8 +272,9 @@ private function get_affiliate_network($url) {


$networks = [
'adtraction' => 'adtraction.com',
'tradetracker' => 'tradetracker.net',
'adtraction' => 'adtraction.com',
'tradetracker' => 'tradetracker.net',
'daisycon' => 'daisycon.io',
];


Expand All @@ -260,6 +284,6 @@ private function get_affiliate_network($url) {
}
}

return 'unknown';
return false;
}
}
6 changes: 6 additions & 0 deletions src/php/Metabox/FeedMetabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ public function get_title(): string {
}

public function render(WP_Post $post) {

$last_error = get_post_meta($post->ID, '_phft_last_error', true);
$feed_url = get_post_meta($post->ID, '_phft_feed_url', true);

$value = $feed_url ?: '';

if(!empty($last_error)){
echo sprintf(__('Last error: %s', 'affiliate-product-highlights'), $last_error);
}

echo "Feed Url: <input type='text' name='_phft_feed_url' value='{$value}' />";
}
}
Loading

0 comments on commit 59d5902

Please sign in to comment.