/data/websites/motori/web/app/themes/motori/src/CarPriceList/Components/CarData.php
protected \WP_Post $car;
public function __construct( $data, array $context) {
$this->car = $data;
}
public function getData() : ComponentData {
return new ComponentData( self::NAME, $this->getCarData() );
}
protected function getCarData() : array {
// Trova il termine genitore
$termModel = $this->getModelTerm( $this->car->ID );
$termBrand = $this->getBrandTerm( $termModel->parent );
$brandSlug = $termBrand->slug;
$modelSlug = $termModel->slug;
$carSlug = get_post_meta( $this->car->ID, 'dbauto_car_slug', true);
$apiBase = BLZ_DB_AUTO;
$response = wp_remote_get( "{$apiBase}/motori.it/brands/{$brandSlug}/models/{$modelSlug}/cars/{$carSlug}", [ 'sslverify' => false ] );
if ( is_wp_error( $response ) ) {
return [];
}
$data = wp_remote_retrieve_body( $response );
return json_decode($data, true);
}
/data/websites/motori/web/app/themes/motori/src/CarPriceList/Components/CarData.php
protected \WP_Post $car;
public function __construct( $data, array $context) {
$this->car = $data;
}
public function getData() : ComponentData {
return new ComponentData( self::NAME, $this->getCarData() );
}
protected function getCarData() : array {
// Trova il termine genitore
$termModel = $this->getModelTerm( $this->car->ID );
$termBrand = $this->getBrandTerm( $termModel->parent );
$brandSlug = $termBrand->slug;
$modelSlug = $termModel->slug;
$carSlug = get_post_meta( $this->car->ID, 'dbauto_car_slug', true);
$apiBase = BLZ_DB_AUTO;
$response = wp_remote_get( "{$apiBase}/motori.it/brands/{$brandSlug}/models/{$modelSlug}/cars/{$carSlug}", [ 'sslverify' => false ] );
if ( is_wp_error( $response ) ) {
return [];
}
$data = wp_remote_retrieve_body( $response );
return json_decode($data, true);
}
/data/websites/motori/web/app/themes/motori/src/CarPriceList/Components/CarData.php
use THEME\ViewModel\Common\Component;
use THEME\ViewModel\Common\ComponentData;
class CarData implements Component {
const NAME = 'car_data';
protected \WP_Post $car;
public function __construct( $data, array $context) {
$this->car = $data;
}
public function getData() : ComponentData {
return new ComponentData( self::NAME, $this->getCarData() );
}
protected function getCarData() : array {
// Trova il termine genitore
$termModel = $this->getModelTerm( $this->car->ID );
$termBrand = $this->getBrandTerm( $termModel->parent );
$brandSlug = $termBrand->slug;
$modelSlug = $termModel->slug;
$carSlug = get_post_meta( $this->car->ID, 'dbauto_car_slug', true);
$apiBase = BLZ_DB_AUTO;
$response = wp_remote_get( "{$apiBase}/motori.it/brands/{$brandSlug}/models/{$modelSlug}/cars/{$carSlug}", [ 'sslverify' => false ] );
if ( is_wp_error( $response ) ) {
return [];
/data/websites/motori/web/app/themes/motori/src/ViewModel/Common/HasComponents.php
<?php
namespace THEME\ViewModel\Common;
trait HasComponents {
protected array $components = []; // list of components that yield data
protected object|null $data = null; // basic data of the current object ( post, term, queried_object, etc.)
protected array $context = []; // data gathered while loading components from the compontent's list
protected function loadComponents() {
foreach($this->components as $component) {
$component_instance = ( new $component( $this->data, $this->context) )->getData();
$this->context[$component_instance->name] = $component_instance->data;
}
}
public function getContext() {
return $this->context;
}
}
/data/websites/motori/web/app/themes/motori/src/ViewModel/Base.php
* expose getContext() to the view
*
*/
abstract class Base {
use HasComponents;
use CanPurgeCachedComponents;
public function __construct() {
$this->context = $this->initContext();
$this->components = array_merge(
$this->listBasicComponents(),
$this->listComponents(),
$this->listStructureComponents()
);
$this->loadComponents();
}
protected function listBasicComponents() : array {
return [
Components\Domain::class,
Components\IsMobile::class,
Components\AssetsDir::class,
Components\GoogleTagManager::class,
Components\Menus::class,
BrandModelList::class,
];
}
/**
* componenti da calcolare sempre dopo l'esecuzione degli
* altri hook
*
* @return array
/data/websites/motori/web/app/themes/motori/src/ViewModel/Single.php
<?php
namespace THEME\ViewModel;
use THEME\ViewModel\Post\Content\Templates\Template;
use THEME\ViewModel\Post\Components;
use THEME\ViewModel\Post\CachedComponents;
class Single extends Base {
protected Template $postTemplate;
public function __construct(\WP_Post $post, $postTemplate) {
$this->data = $post;
parent::__construct();
// assign the post template (default is 'Single')
$this->postTemplate = (new $postTemplate);
// init the post template
// (includes content details as well)
$this->setPost();
}
/**
* Writes the basic page info
*/
protected function initContext(): array
{
$context = [
'type' => 'single_post',
'is_electric' => false,
'infinite' => 'false'
];
/data/websites/motori/web/app/themes/motori/single-car.php
<?php
use THEME\Utils\Timber;
use THEME\CarPriceList\ViewModel\SingleCar;
use THEME\ViewModel\Post\Content\Templates;
if (have_posts()) {
// load data
$data = (new SingleCar($posts[0], Templates\Minimal::class))->getContext();
Timber::render('single-version.twig', $data, 600);
}
/data/websites/motori/web/wp/wp-includes/template-loader.php
}
break;
}
}
if ( ! $template ) {
$template = get_index_template();
}
/**
* Filters the path of the current template before including it.
*
* @since 3.0.0
*
* @param string $template The path of the template to include.
*/
$template = apply_filters( 'template_include', $template );
if ( $template ) {
include $template;
} elseif ( current_user_can( 'switch_themes' ) ) {
$theme = wp_get_theme();
if ( $theme->errors() ) {
wp_die( $theme->errors() );
}
}
return;
}
/data/websites/motori/web/wp/wp-blog-header.php
<?php
/**
* Loads the WordPress environment and template.
*
* @package WordPress
*/
if ( ! isset( $wp_did_header ) ) {
$wp_did_header = true;
// Load the WordPress library.
require_once __DIR__ . '/wp-load.php';
// Set up the WordPress query.
wp();
// Load the theme template.
require_once ABSPATH . WPINC . '/template-loader.php';
}
/data/websites/motori/web/index.php
<?php
/**
* WordPress View Bootstrapper
*/
define('WP_USE_THEMES', true);
require __DIR__ . '/wp/wp-blog-header.php';