| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Partner Settings |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify; |
| 7 |
|
| 8 |
/** |
| 9 |
* Controller for handling partner settings |
| 10 |
*/ |
| 11 |
class PartnerData |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* The partner id |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public static $id = 'no-partner'; |
| 20 |
|
| 21 |
/** |
| 22 |
* The partner logo |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public static $logo = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* The partner display name |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public static $name = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* The partner display name |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public static $colors = []; |
| 41 |
|
| 42 |
/** |
| 43 |
* The partner display name |
| 44 |
* |
| 45 |
* @var boolean |
| 46 |
*/ |
| 47 |
public static $disableRecommendations = false; |
| 48 |
|
| 49 |
/** |
| 50 |
* Set up and collect partner data |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function __construct() |
| 55 |
{ |
| 56 |
if (isset($GLOBALS['extendify_sdk_partner']) && $GLOBALS['extendify_sdk_partner']) { |
| 57 |
self::$id = $GLOBALS['extendify_sdk_partner']; |
| 58 |
} |
| 59 |
|
| 60 |
// Always use the partner ID if set as a constant. |
| 61 |
if (defined('EXTENDIFY_PARTNER_ID')) { |
| 62 |
self::$id = constant('EXTENDIFY_PARTNER_ID'); |
| 63 |
} |
| 64 |
|
| 65 |
// If the plugin has no partner, don't fetch data. |
| 66 |
if (self::$id === 'no-partner') { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$data = self::getPartnerData(); |
| 71 |
|
| 72 |
if (isset($data['disableRecommendations'])) { |
| 73 |
self::$disableRecommendations = $data['disableRecommendations']; |
| 74 |
unset($data['disableRecommendations']); |
| 75 |
} |
| 76 |
|
| 77 |
if (isset($data['logo'])) { |
| 78 |
self::$logo = $data['logo'][0]['thumbnails']['large']['url']; |
| 79 |
unset($data['logo']); |
| 80 |
} |
| 81 |
|
| 82 |
if (isset($data['Name'])) { |
| 83 |
self::$name = $data['Name']; |
| 84 |
unset($data['Name']); |
| 85 |
} |
| 86 |
|
| 87 |
self::$colors = $data; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Retrieve partner data from a transient or from the API. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public static function getPartnerData() |
| 96 |
{ |
| 97 |
// If the transient is already set, don't fetch again. |
| 98 |
$transientData = get_transient('extendify_partner_data'); |
| 99 |
// Check the secondaryColor as the Launch Command does not add this in some versions. |
| 100 |
if ($transientData !== false && isset($transientData['secondaryColor'])) { |
| 101 |
return get_option('extendify_partner_data', []); |
| 102 |
} |
| 103 |
|
| 104 |
$response = wp_remote_get( |
| 105 |
add_query_arg( |
| 106 |
['partner' => self::$id], |
| 107 |
'https://dashboard.extendify.com/api/onboarding/partner-data/' |
| 108 |
), |
| 109 |
['headers' => ['Accept' => 'application/json']] |
| 110 |
); |
| 111 |
|
| 112 |
if (is_wp_error($response)) { |
| 113 |
// If the request fails, try again in 24 hours. |
| 114 |
set_transient('extendify_partner_data', [], DAY_IN_SECONDS); |
| 115 |
return get_option('extendify_partner_data', []); |
| 116 |
} |
| 117 |
|
| 118 |
$result = json_decode(wp_remote_retrieve_body($response), true); |
| 119 |
|
| 120 |
$data = []; |
| 121 |
if (isset($result['data'])) { |
| 122 |
$keys = ['foregroundColor', 'backgroundColor', 'secondaryColor', 'logo', 'Name', 'disableRecommendations']; |
| 123 |
$data = array_intersect_key($result['data'], array_flip($keys)); |
| 124 |
} |
| 125 |
|
| 126 |
$data['secondaryColorText'] = '#ffffff'; |
| 127 |
if (!isset($data['secondaryColor'])) { |
| 128 |
$data['secondaryColor'] = $data['backgroundColor']; |
| 129 |
} |
| 130 |
|
| 131 |
// Transient is used to mark the time, but the data is put into an option, |
| 132 |
// so that in case of network issues, we can still retrun old data. |
| 133 |
set_transient('extendify_partner_data', $data, MONTH_IN_SECONDS); |
| 134 |
update_option('extendify_partner_data', $data); |
| 135 |
return $data; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Return colors mapped as css variables |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public static function cssVariableMapping() |
| 144 |
{ |
| 145 |
$mapping = [ |
| 146 |
'backgroundColor' => '--ext-banner-main', |
| 147 |
'foregroundColor' => '--ext-banner-text', |
| 148 |
'secondaryColor' => '--ext-design-main', |
| 149 |
'secondaryColorText' => '--ext-design-text', |
| 150 |
]; |
| 151 |
$cssVariables = []; |
| 152 |
$colors = self::$colors; |
| 153 |
foreach ($mapping as $color => $variable) { |
| 154 |
if (isset($colors[$color])) { |
| 155 |
$cssVariables[$variable] = $colors[$color]; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return $cssVariables; |
| 160 |
} |
| 161 |
} |
| 162 |
|