| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controls Recommendations |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Assist\Controllers; |
| 8 |
|
| 9 |
use Extendify\Constants; |
| 10 |
|
| 11 |
defined('ABSPATH') || die('No direct access.'); |
| 12 |
|
| 13 |
/** |
| 14 |
* The controller for fetching recommendations |
| 15 |
*/ |
| 16 |
|
| 17 |
class RecommendationsController |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Return recommendations from source. |
| 21 |
* |
| 22 |
* @return \WP_REST_Response |
| 23 |
*/ |
| 24 |
public static function fetchRecommendations() |
| 25 |
{ |
| 26 |
if (!defined('EXTENDIFY_PARTNER_ID')) { |
| 27 |
return new \WP_REST_Response([], 200); |
| 28 |
} |
| 29 |
|
| 30 |
$partnerData = \get_option('extendify_partner_data_v2', []); |
| 31 |
if (($partnerData['disableRecommendations'] ?? false)) { |
| 32 |
return new \WP_REST_Response([], 200); |
| 33 |
} |
| 34 |
|
| 35 |
$params = http_build_query([ |
| 36 |
'wp_language' => get_locale(), |
| 37 |
'sdk_partner' => defined('EXTENDIFY_PARTNER_ID') ? EXTENDIFY_PARTNER_ID : '', |
| 38 |
]); |
| 39 |
$response = \wp_remote_get( |
| 40 |
sprintf('%s/api/assist/recommendations?%s', Constants::DASHBOARD_HOST, $params), |
| 41 |
[ |
| 42 |
'headers' => [ |
| 43 |
'Content-Type' => 'application/json', |
| 44 |
'Accept' => 'application/json', |
| 45 |
], |
| 46 |
] |
| 47 |
); |
| 48 |
|
| 49 |
if (\is_wp_error($response)) { |
| 50 |
return new \WP_REST_Response([], 500); |
| 51 |
} |
| 52 |
|
| 53 |
$body = json_decode(\wp_remote_retrieve_body($response), true); |
| 54 |
if (!isset($body['success']) || !isset($body['data'])) { |
| 55 |
return new \WP_REST_Response([], 500); |
| 56 |
} |
| 57 |
|
| 58 |
return new \WP_REST_Response($body['data'], \wp_remote_retrieve_response_code($response)); |
| 59 |
} |
| 60 |
} |
| 61 |
|