/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Content/Image.php
protected array $sizes;
protected array $map;
public function __construct(protected int $attachment_id, $sizes = ['hero'] ) {
$this->sizes = $sizes;
$this->map = wp_is_mobile() ? Image::MAP['mobile'] : Image::MAP['desktop'];
$this->data = $this->getImageData();
}
protected function getImageData(): array {
$attachment = get_post($this->attachment_id);
return [
'id' => $this->attachment_id,
'title' => $attachment->post_title,
'sizes' => $this->getSizes(),
'alt' => get_post_meta($this->attachment_id, '_wp_attachment_image_alt', true),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
];
}
protected function getUrl( $args ) : string {
$url = wp_get_attachment_image_url( $this->attachment_id, 'full');
return $url === false ? '' : $this->getThumborUrl( $url, $args );
}
protected function getOriginalUrl( $args ) : string {
$url = wp_get_original_image_url( $this->attachment_id );
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Content/Image.php
protected array $sizes;
protected array $map;
public function __construct(protected int $attachment_id, $sizes = ['hero'] ) {
$this->sizes = $sizes;
$this->map = wp_is_mobile() ? Image::MAP['mobile'] : Image::MAP['desktop'];
$this->data = $this->getImageData();
}
protected function getImageData(): array {
$attachment = get_post($this->attachment_id);
return [
'id' => $this->attachment_id,
'title' => $attachment->post_title,
'sizes' => $this->getSizes(),
'alt' => get_post_meta($this->attachment_id, '_wp_attachment_image_alt', true),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
];
}
protected function getUrl( $args ) : string {
$url = wp_get_attachment_image_url( $this->attachment_id, 'full');
return $url === false ? '' : $this->getThumborUrl( $url, $args );
}
protected function getOriginalUrl( $args ) : string {
$url = wp_get_original_image_url( $this->attachment_id );
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Content/Image.php
'lightbox' => ['width' => 1920, 'height' => 1080, 'quality' => 100 ],
'thumbnail' => ['width' => 160, 'height' => 120, 'quality' => 80 ],
]
];
const DEFAULT_WIDTH = 1200;
const DEFAULT_HEIGHT = 675;
const DEFAULT_QUALITY = 80;
protected array $sizes;
protected array $map;
public function __construct(protected int $attachment_id, $sizes = ['hero'] ) {
$this->sizes = $sizes;
$this->map = wp_is_mobile() ? Image::MAP['mobile'] : Image::MAP['desktop'];
$this->data = $this->getImageData();
}
protected function getImageData(): array {
$attachment = get_post($this->attachment_id);
return [
'id' => $this->attachment_id,
'title' => $attachment->post_title,
'sizes' => $this->getSizes(),
'alt' => get_post_meta($this->attachment_id, '_wp_attachment_image_alt', true),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
];
}
protected function getUrl( $args ) : string {
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Components/ContentSplitters/Image.php
$image_id = (int) $image_split[2][0];
return [
[ 'type' => 'content', 'content' => $image_split[1][0] ],
[ 'type' => 'image', 'content' => $this->getImage($image_id) ],
[ 'type' => 'content', 'content' => $image_split[3][0] ]
];
}
/**
* Return the collection of the gallery images
* of a given gallery id
*
* @var int gallery_id
* @return array
*/
protected function getImage( $attachment_id ) : array {
$image = new InTextImage( $attachment_id, ['intext', 'lightbox'] );
return $image->data;
}
}
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Components/ContentSplitters/Image.php
* split the content section and return a new image section
* preserve the section title, if any
*
* @var array $section
* @return array that defines a section
*/
public function split( array $section ) : array {
// takes the image shortcodes and the positions they start
$content = str_replace( ["”","″"], "\"" ,$section['content'] );
preg_match_all('/(.*?){{image=(\d+)}}(.*)/s', $content, $image_split);
if ( empty( $image_split[0] ) ) return [];
$image_id = (int) $image_split[2][0];
return [
[ 'type' => 'content', 'content' => $image_split[1][0] ],
[ 'type' => 'image', 'content' => $this->getImage($image_id) ],
[ 'type' => 'content', 'content' => $image_split[3][0] ]
];
}
/**
* Return the collection of the gallery images
* of a given gallery id
*
* @var int gallery_id
* @return array
*/
protected function getImage( $attachment_id ) : array {
$image = new InTextImage( $attachment_id, ['intext', 'lightbox'] );
return $image->data;
}
}
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Components/SplittedContent.php
* @param Integer $post_id
* @return mixed
*/
/**
* Recursive method that will progressively split the contents into
* smaller, specialized sections according with the given splitter
*/
protected function splitSections( Splitter $splitter) {
$splits = [];
// split content sections and
// keep track of their positions in the array
foreach ($this->sections as $idx => $section) {
if ($section['type'] == 'content') {
$split = $splitter->split($section);
if (!empty($split)) {
$splits[] = (object) ['idx' => $idx , 'sections' => $split ];
}
}
}
// replace old sections with the splitted ones
// starting from the bottom to the top
foreach (array_reverse($splits) as $split)
array_splice($this->sections, $split->idx, 1, $split->sections);
// no more splits exit the recursive loop
if (empty($splits))
return;
// else do another ride
else
$this->splitSections($splitter);
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Components/SplittedContent.php
protected function init($content) {
$content = strip_tags( $content, ['<h1>','<h2>','<h3>','<h4>','<h5>','<h6>','<p>','<div>','<span>','<header>','<footer>','<section>','<article>','<nav>','<aside>','<main>','<ul>','<ol>','<li>','<a>','<img>','<audio>','<video>','<source>','<table>','<thead>','<tbody>','<tfoot>','<tr>','<td>','<th>','<form>','<input>','<textarea>','<button>','<select>','<option>','<label>','<fieldset>','<legend>','<b>','<i>','<u>','<strong>','<em>','<mark>','<small>','<del>','<ins>','<script>','<noscript>','<style>','<br>','<hr>','<blockquote>','<cite>','<code>','<pre>','<abbr>','<address>','<time>']);
// cambiare gli shortcode "mutati"
foreach($this->splitters as $splitter){
$content = $splitter->convertShortCode($content);
}
// initialize the content as a big section to split
$this->sections = [
[ 'type' => 'content', 'content' => apply_filters('the_content', $content) ]
];
foreach ($this->splitters as $splitter) {
$this->splitSections($splitter);
}
}
/**
* Return the list of the splitters
*/
protected function getSplitters() : array {
$splitters = [
ContentSplitters\Gallery::class,
ContentSplitters\Headings::class,
ContentSplitters\Faq::class,
ContentSplitters\Image::class,
ContentSplitters\Video::class,
ContentSplitters\Toc::class,
ContentSplitters\InTextRelateds::class,
];
return array_map( function( $splitter ) { return new $splitter(); } , $splitters );
/data/websites/motori/web/app/themes/motori/src/ViewModel/Post/Components/SplittedContent.php
use THEME\ViewModel\Common\Component;
use THEME\ViewModel\Common\ComponentData;
use THEME\ViewModel\Post\Components\ContentSplitters;
use THEME\ViewModel\Post\Components\ContentSplitters\Splitter;
class SplittedContent implements Component {
const NAME = 'content';
protected \WP_Post $post;
private $splitters;
private $sections = [];
public function __construct( $data = null, array $context = []) {
$this->post = $data;
$this->splitters = $this->getSplitters();
$this->init( $this->post->post_content );
}
public function getData(): ComponentData {
return new ComponentData( static::NAME, $this->getSections() );
}
/**
* Expose the sections
*
* @return Array sections
*/
protected function getSections() : array {
return $this->sections;
}
/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.php
<?php
use THEME\Utils\Timber;
use THEME\ViewModel\Single;
use THEME\ViewModel\Post\Content\Templates;
if (have_posts()) {
// load data
$data = (new Single($posts[0], Templates\Single::class))->getContext();
Timber::render('single.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';