AlkantarClanX12
| Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/ |
| Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/vdp.php |
<?php
use function Roots\app;
use function Roots\view;
function leadbox_render_vdp($options)
{
$options = shortcode_atts(array(
'stock_vdp' =>'',
), $options, 'inventory');
$json_path = wp_get_upload_dir()['basedir'] . '/data/inventory';
$idiom_path = makeUrl($json_path);
if (file_exists($idiom_path)) {
$data = json_decode(file_get_contents($idiom_path), true);
return parse_vdp($data['vehicles'], $options);
}
return parse_vdp(array(), $options);
}
function parse_vdp($vehicles, $options)
{
$vehicle = filter_vehicle($vehicles, $options);
$data = json_decode(file_get_contents(wp_get_upload_dir()['basedir'] . '/data/'.$vehicle[0]['id'].".json"), true);
return view('partials.custom-vdp', ['data' => $data, 'options' => $options])->render();
}
function filter_vehicle($vehicles, $options)
{
$_vehicles = [];
$_stockWildcard = [];
$stockrange = parse_range($options['stock_vdp']);
foreach($stockrange as $stockvalue){
if(strpos($stockvalue, '*') !== false){
$_stockWildcard[] = str_replace("*","",$stockvalue);
}
}
foreach ($vehicles as $vehicle) {
$matcher = new VehicleMatcherVDP($vehicle, $options,$_stockWildcard);
$is_match = $matcher->match('stocknumber', $stockrange, 'stock');
if ($is_match) {
$_vehicles[] = $vehicle;
}
}
return $_vehicles;
}
class VehicleMatcherVDP
{
function __construct($vehicle, $options, $_stockWildcard)
{
$this->vehicle = $vehicle;
$this->options = $options;
$this->stockWildcard = $_stockWildcard;
}
public function match(string $property, array $values, string $propertyOptions = null)
{
$propertyOptions = $propertyOptions ?? $property;
$value = (isset($this->vehicle) && isset($this->vehicle[$property])) ? $this->vehicle[$property] : "";
if($propertyOptions == "stock_vdp"){
if(sizeof($this->stockWildcard)>0){
foreach($this->stockWildcard as $wildcard){
if(strpos($value, $wildcard)!==false){
return true;
}
}
}
}
return $this->options[$propertyOptions] == 'all' || in_array($value, $values);
}
}