PluginProbe
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools / 1.0.8
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools v1.0.8
1.5.5 1.5.4 trunk 1.0.0 1.0.1 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 All 47 releases
pixelavo / includes / pixel-feed.php

pixel-feed.php in Pixelavo – Server Side Tracking & Pixel + AI Ads Tools 1.0.8, at includes/pixel-feed.php

287 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pixelavo;
4
5 /* Exit if accessed directly */
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 class PixelFeed{
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' === $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', [$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('exclude_categories', $settings) ? $settings['exclude_categories'] : [];
54 $exclude_tags = array_key_exists('exclude_tags', $settings) ? $settings['exclude_tags'] : [];
55
56 /**
57 * Products
58 */
59 $args = [
60 'status' => 'publish',
61 'limit' => -1,
62 'orderby' => 'ID',
63 'order' => 'ASC',
64 'tax_query' => [
65 'relation' => 'AND',
66 [
67 'taxonomy' => 'product_cat',
68 'field' => 'term_id',
69 'terms' => $exclude_categories,
70 'operator' => 'NOT IN',
71 ],
72 [
73 'taxonomy' => 'product_tag',
74 'field' => 'term_id',
75 'terms' => $exclude_tags,
76 'operator' => 'NOT IN',
77 ]
78 ],
79 ];
80 $products = wc_get_products($args);
81
82 /**
83 * Feed Namespace
84 */
85 $namespace = 'http://base.google.com/ns/1.0';
86
87 /**
88 * Feed RSS
89 */
90 $rss = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><rss xmlns:g='{$namespace}'></rss>");
91 $rss->addAttribute('version', '2.0');
92
93 /**
94 * Feed Channel
95 */
96 $channel = $rss->addChild('channel');
97 $channel->addChild('title', get_bloginfo('name'));
98 $channel->addChild('description', get_bloginfo('description'));
99 $channel->addChild('link', get_bloginfo('url'));
100
101 /**
102 * Feed Item
103 */
104 foreach ($products as $product) {
105 $pro['id'] = $settings['product_identifier'] == 'post_id' ? $product->get_id() : $product->get_sku();
106 $pro['group_id'] = $pro['id'];
107 $pro['title'] = $product->get_title();
108 $pro['desc'] = $settings['description_field'] == 'description' ? $product->get_description() : $product->get_short_description();
109 $pro['link'] = $product->get_permalink();
110 $pro['image_link'] = $this->get_image_link($product->get_id());
111 $pro['brand'] = $this->get_brand($product->get_id());
112 $pro['condition'] = $this->get_condition($product->get_id());
113 $pro['availability'] = $product->is_in_stock() ? 'in stock' : 'out of stock';
114 $pro['price'] = $this->get_price($product);
115 $pro['extra_fields'] = $this->get_extra_fields($product);
116 $pro['category'] = $this->get_categories($product);
117 $pro['additional_images'] = $this->get_additional_images($product);
118
119 if($product->is_type('variable') && pixelavo_get_option('include_variations', 'pixelavo_settings') == 'on') {
120 foreach ($product->get_available_variations() as $variation) {
121 $pro['additional_images'] = $this->get_additional_images($product, $variation);
122 $this->feed_item($channel, $pro, $namespace, $variation);
123 }
124 } else {
125 $this->feed_item($channel, $pro, $namespace);
126 }
127 }
128
129 echo $rss->asXML();
130 }
131
132 /**
133 * Feed item function
134 */
135 function feed_item($channel, $product, $namespace, $variation = false) {
136
137 if($variation) {
138 $product['id'] = $product['id'] . '-' . $variation['variation_id'];
139 $product['title'] = $product['title'] . ' - ' . $this->get_variation_title($variation['attributes']);
140 $product['extra_fields'] = array_merge($product['extra_fields'], $this->get_extra_fields(null,$variation['attributes']));
141 }
142
143 $item = $channel->addChild('item');
144 $item->addChild("g:id", $product['id'], $namespace);
145 $item->addChild("g:item_group_id", $product['group_id'], $namespace);
146 $item->addChild("g:title", $product['title'], $namespace);
147 $item->addChild("g:description", $product['desc'], $namespace);
148 $item->addChild("g:availability", $product['availability'], $namespace);
149 $item->addChild("g:condition", $product['condition'], $namespace);
150 if($product['price']) {
151 $item->addChild("g:price", $product['price'], $namespace);
152 }
153 $item->addChild("g:link", $product['link'], $namespace);
154 if($product['image_link']) {
155 $item->addChild("g:image_link", $product['image_link'], $namespace);
156 }
157 if(!empty($product['brand'])) {
158 $item->addChild("g:brand", $product['brand'], $namespace);
159 }
160 foreach ($product['extra_fields'] as $key => $value) {
161 $item->addChild("g:{$key}", $value, $namespace);
162 }
163 $item->addChild("g:category", $product['category'], $namespace);
164 foreach ($product['additional_images'] as $image) {
165 $item->addChild("g:additional_image_link", $image, $namespace);
166 }
167 }
168
169 /**
170 * Get variation product title
171 */
172 function get_variation_title($variations) {
173 if (is_array($variations)) {
174 $variation_values = [];
175 foreach ($variations as $variation) {
176 if(!empty($variation)) {
177 $variation_values[] = $variation;
178 }
179 }
180 return '(' . implode( '-', $variation_values ) . ')';
181 }
182 return '';
183 }
184
185 /**
186 * Get product thumbnail image link
187 */
188 function get_image_link($product_id, $variation = null) {
189 $image_id = is_array( $variation ) ? $variation[ 'image_id' ] : get_post_thumbnail_id( $product_id );
190 $image = wp_get_attachment_image_src( $image_id, 'single-post-thumbnail' );
191 if($image) {
192 return $image[0];
193 }
194 return $image;
195 }
196
197 /**
198 * Get brand name
199 */
200 function get_brand($product_id) {
201 $brand_terms = wp_get_post_terms( $product_id, 'product_brand' );
202 if (!empty($brand_terms) && !is_wp_error($brand_terms)) {
203 return $brand_terms[0]->name;
204 }
205 return '';
206 }
207
208 /**
209 * Get product condition
210 */
211 function get_condition($product_id) {
212 $condition = get_post_meta($product_id, '_product_condition', true);
213 if (empty($condition)) {
214 return 'new';
215 }
216 return $condition;
217 }
218
219 /**
220 * Get product price
221 */
222 function get_price($product, $variation = null) {
223 $the_price = is_array($variation) ? $variation['display_price'] : wc_get_price_to_display( $product );
224 if ($the_price) {
225 return $the_price . ' ' . get_woocommerce_currency();
226 }
227 return false;
228 }
229
230 /**
231 * Get product extra fields
232 */
233 function get_extra_fields($product, $variation = null) {
234 $fields = ['color', 'gender', 'pattern', 'material', 'size'];
235 $extra_fields = [];
236 forEach ( $fields as $field ) {
237 if ( isSet($variation[ 'attribute_pa_' . $field ]) && !empty($variation[ 'attribute_pa_' . $field ])) {
238 $extra_fields[$field] = $variation[ 'attribute_pa_' . $field ];
239 } else if (isSet($product) && $product->get_attribute($field) && !empty($product->get_attribute($field))) {
240 $extra_fields[$field] = $product->get_attribute($field);
241 }
242 }
243 return $extra_fields;
244 }
245
246 /**
247 * Get product categories
248 */
249 function get_categories($product) {
250 $categories = get_the_terms($product->get_id(), 'product_cat');
251 $category_string = '';
252 if (!empty($categories)) {
253 $categories = wp_list_sort($categories, ['parent' => 'ASC', 'name' => 'ASC'], 'ASC');
254 foreach ($categories as $category) {
255 $category_string = empty($category_string) ? $category_string . $category->name : $category_string .' > '. $category->name;
256 }
257 }
258 return $category_string;
259 }
260
261 /**
262 * Get product additional images
263 */
264 function get_additional_images( $product, $variation = null ) {
265 $images = [];
266 /* Variation Image */
267 $variation_image = is_array($variation) ? wp_get_attachment_image_src($variation['image_id'], 'single-post-thumbnail') : false;
268 if ($variation_image) {
269 $images[] = $variation_image[0];
270 }
271 /* Gallery Images */
272 $gallery_ids = $product->get_gallery_image_ids();
273 if (!empty($gallery_ids)) {
274 foreach($gallery_ids as $gallery_id) {
275 $images[] = wp_get_attachment_url($gallery_id);
276 }
277 }
278 return array_unique($images);
279 }
280
281 }
282
283 /**
284 * Initialize PixelFeed Class
285 */
286 PixelFeed::instance();
287