| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pixelavo; |
| 4 |
|
| 5 |
/* Exit if accessed directly */ |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class PixelEddFeed{ |
| 11 |
|
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Instance. |
| 16 |
* Initializes a singleton instance. |
| 17 |
* @return self class |
| 18 |
*/ |
| 19 |
static function instance() { |
| 20 |
if ( is_null( self::$_instance ) ) { |
| 21 |
self::$_instance = new self(); |
| 22 |
} |
| 23 |
return self::$_instance; |
| 24 |
} |
| 25 |
|
| 26 |
function __construct() { |
| 27 |
add_action('init', [$this, 'init_feed']); |
| 28 |
add_filter('feed_content_type', [$this, 'feed_content_type'], 10, 2); |
| 29 |
} |
| 30 |
|
| 31 |
function feed_content_type($content_type, $type) { |
| 32 |
if ('pixelavo-edd' === $type) { |
| 33 |
return feed_content_type( 'rss-http' ); |
| 34 |
} |
| 35 |
return $content_type; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Feed. |
| 40 |
* Run when user search for any product and go to `search` page. |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function init_feed() { |
| 44 |
add_feed( 'pixelavo-edd', [$this, 'pixel_feed'] ); |
| 45 |
} |
| 46 |
|
| 47 |
function pixel_feed() { |
| 48 |
|
| 49 |
/** |
| 50 |
* Settings |
| 51 |
*/ |
| 52 |
$settings = get_option('pixelavo_settings'); |
| 53 |
$exclude_categories = array_key_exists('edd_exclude_categories', $settings) ? $settings['edd_exclude_categories'] : []; |
| 54 |
$exclude_tags = array_key_exists('edd_exclude_tags', $settings) ? $settings['edd_exclude_tags'] : []; |
| 55 |
|
| 56 |
/** |
| 57 |
* Products |
| 58 |
*/ |
| 59 |
$args = [ |
| 60 |
'fields' => 'ids', |
| 61 |
'post_type' => 'download', |
| 62 |
'post_status' => 'publish', |
| 63 |
'posts_per_page' => -1, |
| 64 |
'orderby' => 'ID', |
| 65 |
'order' => 'ASC', |
| 66 |
'tax_query' => [ |
| 67 |
'relation' => 'AND', |
| 68 |
[ |
| 69 |
'taxonomy' => 'download_category', |
| 70 |
'field' => 'term_id', |
| 71 |
'terms' => $exclude_categories, |
| 72 |
'operator' => 'NOT IN', |
| 73 |
], |
| 74 |
[ |
| 75 |
'taxonomy' => 'download_tag', |
| 76 |
'field' => 'term_id', |
| 77 |
'terms' => $exclude_tags, |
| 78 |
'operator' => 'NOT IN', |
| 79 |
] |
| 80 |
], |
| 81 |
]; |
| 82 |
$download_ids = get_posts($args); |
| 83 |
|
| 84 |
/** |
| 85 |
* Feed Namespace |
| 86 |
*/ |
| 87 |
$namespace = 'http://base.google.com/ns/1.0'; |
| 88 |
|
| 89 |
/** |
| 90 |
* Feed RSS |
| 91 |
*/ |
| 92 |
$rss = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><rss xmlns:g='{$namespace}'></rss>"); |
| 93 |
$rss->addAttribute('version', '2.0'); |
| 94 |
|
| 95 |
/** |
| 96 |
* Feed Channel |
| 97 |
*/ |
| 98 |
$channel = $rss->addChild('channel'); |
| 99 |
$channel->addChild('title', get_bloginfo('name')); |
| 100 |
if(!empty(get_bloginfo('description'))) { |
| 101 |
$channel->addChild('description', get_bloginfo('description')); |
| 102 |
} |
| 103 |
$channel->addChild('link', get_bloginfo('url')); |
| 104 |
|
| 105 |
/** |
| 106 |
* Feed Item |
| 107 |
*/ |
| 108 |
foreach ($download_ids as $id) { |
| 109 |
$download = edd_get_download($id); |
| 110 |
$dowl['id'] = $settings['edd_product_identifier'] == 'post_id' ? $download->ID : $download->get_sku(); |
| 111 |
$dowl['title'] = $download->post_title; |
| 112 |
$dowl['desc'] = $settings['edd_description_field'] == 'description' ? $download->post_content : $download->post_excerpt; |
| 113 |
$dowl['link'] = get_permalink($dowl['id']); |
| 114 |
$dowl['image_link'] = $this->get_image_link($dowl['id']); |
| 115 |
$dowl['brand'] = $this->get_brand($dowl['id']); |
| 116 |
$dowl['condition'] = 'new'; |
| 117 |
$dowl['availability'] = 'in stock'; |
| 118 |
$dowl['price'] = $download->get_price() . ' ' . edd_get_option('currency', 'USD'); |
| 119 |
$dowl['google_product_category'] = '5032'; |
| 120 |
$this->feed_item($channel, $dowl, $namespace); |
| 121 |
} |
| 122 |
echo $rss->asXML(); // phpcs:ignore |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Feed item function |
| 127 |
*/ |
| 128 |
function feed_item($channel, $download, $namespace) { |
| 129 |
|
| 130 |
$item = $channel->addChild('item'); |
| 131 |
$item->addChild("g:id", $download['id'], $namespace); |
| 132 |
$item->addChild("g:title", $download['title'], $namespace); |
| 133 |
if(!empty($download['desc'])) { |
| 134 |
$item->addChild("g:description", $download['desc'], $namespace); |
| 135 |
} |
| 136 |
$item->addChild("g:availability", $download['availability'], $namespace); |
| 137 |
$item->addChild("g:condition", $download['condition'], $namespace); |
| 138 |
if($download['price']) { |
| 139 |
$item->addChild("g:price", $download['price'], $namespace); |
| 140 |
} |
| 141 |
$item->addChild("g:link", $download['link'], $namespace); |
| 142 |
if($download['image_link']) { |
| 143 |
$item->addChild("g:image_link", $download['image_link'], $namespace); |
| 144 |
} |
| 145 |
if(!empty($download['brand'])) { |
| 146 |
$item->addChild("g:brand", $download['brand'], $namespace); |
| 147 |
} |
| 148 |
$item->addChild("g:google_product_category", $download['google_product_category'], $namespace); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get product thumbnail image link |
| 153 |
*/ |
| 154 |
function get_image_link($download_id, $variation = null) { |
| 155 |
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $download_id ), 'single-post-thumbnail' ); |
| 156 |
if($image) { |
| 157 |
return $image[0]; |
| 158 |
} |
| 159 |
return $image; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get brand name |
| 164 |
*/ |
| 165 |
function get_brand($download_id) { |
| 166 |
$settings = get_option('pixelavo_settings'); |
| 167 |
return isset($settings['edd_product_feed_brand']) && !empty($settings['edd_product_feed_brand']) ? $settings['edd_product_feed_brand'] : ''; |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Initialize PixelEddFeed Class |
| 174 |
*/ |
| 175 |
PixelEddFeed::instance(); |
| 176 |
|