| 1 |
<?php |
| 2 |
|
| 3 |
class WPUPG_Template { |
| 4 |
|
| 5 |
public $blocks; |
| 6 |
public $container; |
| 7 |
public $fonts; |
| 8 |
|
| 9 |
public function __construct( $blocks, $fonts = array() ) |
| 10 |
{ |
| 11 |
$this->fonts = $fonts; |
| 12 |
|
| 13 |
// Set object blocks before sorting to maintain index |
| 14 |
$this->blocks = $blocks; |
| 15 |
|
| 16 |
// Sort according to block order |
| 17 |
usort( $blocks, array( $this, 'sortByOrder' ) ); |
| 18 |
|
| 19 |
// Generate children, children will be sorted correctly due to above sort |
| 20 |
foreach( $blocks as $block ) |
| 21 |
{ |
| 22 |
if( isset( $block->parent ) && isset( $block->row ) && isset( $block->column ) ) |
| 23 |
{ |
| 24 |
// Set container block, there should only be one |
| 25 |
if( $block->type == 'container' ) { |
| 26 |
$this->container = $block; |
| 27 |
} |
| 28 |
// Add child to parent |
| 29 |
else |
| 30 |
{ |
| 31 |
$this->blocks[$block->parent]->add_child( $block ); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
public function sortByOrder( $a, $b ) |
| 39 |
{ |
| 40 |
return $a->order > $b->order ? 1 : -1; |
| 41 |
} |
| 42 |
|
| 43 |
public function serialize() |
| 44 |
{ |
| 45 |
return serialize( $this ); |
| 46 |
} |
| 47 |
|
| 48 |
public function encode() |
| 49 |
{ |
| 50 |
return base64_encode( $this->serialize() ); |
| 51 |
} |
| 52 |
|
| 53 |
public function output_string( $post, $classes ) |
| 54 |
{ |
| 55 |
$args = array( |
| 56 |
'classes' => $classes, |
| 57 |
); |
| 58 |
|
| 59 |
return $this->container->output( $post, $args ); |
| 60 |
} |
| 61 |
|
| 62 |
public function output( $post, $classes ) |
| 63 |
{ |
| 64 |
echo $this->output_string( $post, $classes ); |
| 65 |
} |
| 66 |
} |