PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Shared / DataProvider / ProductsData.php

ProductsData.php in Extendify 3.0.5, at app/Shared/DataProvider/ProductsData.php

79 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Cache data.
5 */
6
7 namespace Extendify\Shared\DataProvider;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\PartnerData;
12 use Extendify\Shared\Services\Sanitizer;
13
14 /**
15 * The product data class.
16 */
17
18 class ProductsData
19 {
20 /**
21 * Gets the recommended products based on partner and current language.
22 *
23 * @return array
24 */
25 public static function get()
26 {
27 // Check cache before fetching.
28 $products = get_transient('extendify_recommendations_products');
29
30 // Return products from cache if not empty.
31 if ($products !== false) {
32 return $products;
33 }
34
35 // Otherwise fetch products.
36 $response = \wp_remote_get(
37 \add_query_arg(
38 [
39 'disabled_products' => PartnerData::setting('productRecommendations')['disabledProducts'],
40 'custom_products' => PartnerData::setting('productRecommendations')['customProducts'],
41 'wp_language' => \get_locale(),
42 ],
43 'https://dashboard.extendify.com/api/recommendations/products'
44 ),
45 [
46 'headers' => ['Accept' => 'application/json'],
47 ]
48 );
49
50 if (\is_wp_error($response)) {
51 return [];
52 }
53
54 $result = json_decode(\wp_remote_retrieve_body($response), true);
55
56 if (!isset($result['success']) || !$result['success']) {
57 return [];
58 }
59
60 $products = $result['data'];
61 $sanitizedProducts = [];
62
63 foreach ($products as $product) {
64 // We are escaping the original price tag separately because we are using HTML tags
65 // inside it and they are removed when going through the `sanitizeArray` function.
66 $originalPriceTag = $product['priceTag'] ?? '';
67 $sanitizedPriceTag = Sanitizer::sanitizeTextWithFormattingTags($originalPriceTag);
68 $sanitizedProduct = Sanitizer::sanitizeArray($product);
69 $sanitizedProduct['priceTag'] = $sanitizedPriceTag;
70 $sanitizedProducts[] = $sanitizedProduct;
71 }
72
73 // Cache products.
74 set_transient('extendify_recommendations_products', $sanitizedProducts, DAY_IN_SECONDS);
75
76 return $sanitizedProducts;
77 }
78 }
79