AlkantarClanX12
| Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/ |
| Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/line-up.php |
<?php
use function Roots\app;
use function Roots\view;
function get_line_up()
{
return json_decode(file_get_contents(wp_get_upload_dir()['basedir'] . '/data/line-up.json'), true);
}
function parse_line_up($data, $options) {
$vehicles = get_search_inventory();
return view('partials.line-up', ['data' => $data, 'options' => $options, 'vehicles' => $vehicles])->render();
}
function get_unparsed_line_up ($options) {
$urlUp = "https://v5websitescdn.blob.core.windows.net/v5cdn/components/lineup/";
$url = "https://v5websitescdn.blob.core.windows.net/v5cdn/components/lineup/ford/";
$imgURl = "https://v5websitescdn.blob.core.windows.net/v5cdn/components/lineup/ford/";
$filename = "lineupv2.json";
$lbsettings = get_option('leadbox-settings');
$dealer = get_option('dealer-settings-contact-options');
if(isset($lbsettings) && isset($lbsettings["lineup"]) && !empty($lbsettings["lineup"]) && strtolower($lbsettings["lineup"]) == 'custom')
{
if(isset($dealer) && !empty($dealer) && isset($dealer["dealer_name"]) && !empty($dealer["dealer_name"])){
$dealername = preg_replace('/[^A-Za-z0-9\-]/', '', $dealer["dealer_name"]);
if(_isFrench()) {
$filename = "lineup-".strtolower(str_replace(" ","",$dealername))."-frv2.json";
}
else {
$filename = "lineup-".strtolower(str_replace(" ","",$dealername))."v2.json";
}
}
}
else{
if(_isFrench()) {
$filename = "lineup-frv2.json";
}
}
if(isset($lbsettings ) && !empty($lbsettings ) && isset($lbsettings ["manufacturer"]) && !empty($lbsettings ["manufacturer"])){
$url = $urlUp . strtolower(trim(str_replace(" ","",$lbsettings["manufacturer"]))) . "/". $filename;
if(_isFrench()) {
$url = $urlUp . strtolower(trim(str_replace(" ","",$lbsettings["manufacturer"]))) . "/". $filename;
}
$imgURl = $urlUp . strtolower(trim(str_replace(" ","",$lbsettings["manufacturer"])))."/";
}
else {
$url = $url.$filename;
}
return ['vehicles' => lbx_get_line_up_data($url), 'imgURL' => $imgURl];
}
/**
* Fetch the remote line-up JSON with transient caching (positive + negative).
*
* The line-up JSON lives on the Azure CDN and used to be pulled with a raw,
* un-cached, un-timed file_get_contents() on every render of any page that
* includes the line-up component (the home page included). A manufacturer
* without a JSON file on the CDN (a permanent 404 — e.g. dodge/chrysler/ram/
* jeep; only ford exists today) therefore meant a remote request AND a PHP
* warning on every single page view.
*
* We now cache the decoded payload for an hour and — critically — cache
* FAILURES for 15 minutes (negative caching), so a permanent 404 or a timeout
* stops hitting the CDN and spamming the error log until the cache expires.
* SSL verification is left at wp_remote_get()'s secure default (no more
* verify_peer=false) and a 5s timeout bounds the worst-case wait.
*
* Always returns an array so the templates degrade gracefully: line-up/1..6
* json_encode($data) it, and the legacy lbx-iag partial does count($data[1])
* — an empty array is safe for both, null would fatal count() on PHP 8.
*
* @param string $url Fully-built line-up JSON URL.
* @return array Decoded vehicle list on success, or [] when unavailable.
*/
function lbx_get_line_up_data($url) {
$cache_key = 'lbx_lineup_' . md5($url);
$cached = get_transient($cache_key);
// Negative cache hit: a recent fetch failed — do NOT retry until it expires.
if ($cached === 'lbx_lineup_failed') {
return [];
}
// Positive cache hit.
if (is_array($cached)) {
return $cached;
}
$response = wp_remote_get($url, ['timeout' => 5]);
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
$decoded = json_decode(wp_remote_retrieve_body($response), true);
if (is_array($decoded)) {
set_transient($cache_key, $decoded, HOUR_IN_SECONDS);
return $decoded;
}
}
// Failure (404, timeout, WP_Error or invalid JSON): remember it briefly so we
// don't re-request on every render while the CDN file is missing/unreachable.
set_transient($cache_key, 'lbx_lineup_failed', 15 * MINUTE_IN_SECONDS);
return [];
}
function leadbox_render_line_up ($options) {
$data = get_unparsed_line_up($options);
return parse_line_up([$data['imgURL'],$data['vehicles']], $options);
}
function _isFrench() {
$idiom = get_locale();
if(strpos( $idiom, 'en' ) !== false ) {
return false;
}
return true;
}
function get_fileIn($route) {
$childRoute = get_stylesheet_directory();
$parentRoute = get_template_directory();
if(file_exists($childRoute . $route)) {
return $childRoute;
}
return $parentRoute;
}