class-block.php
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Abstract class, other block must extend from this.
*
* @file
* @package Learndash_Certificate_Builer
*/
namespace LearnDash_Certificate_Builder\Component\Builder\Pdf\Blocks;
use LearnDash_Certificate_Builder\Component\Pdf\Builder\Html_Builder;
use LearnDash_Certificate_Builder\Component\Pdf\Builder\Style_Builder;
use LearnDash_Certificate_Builder\Component\Pdf\Pdf_Content;
use simplehtmldom\HtmlDocument;
/**
* Class Block
*
* @package LearnDash_Certificate_Builder\Component\Builder\Pdf\Blocks
*/
abstract class Block {
/**
* The class that calling it
*
* @var Pdf_Content
*/
public $caller;
/**
* The style builder class.
*
* @var Style_Builder
*/
public $style;
/**
* /**
* The Html builder class
*
* @var Html_Builder
*/
public $html;
/**
* The root block
*
* @var array
*/
public $block;
/**
* A flag to know if this element is inside a column, mostly for deal with margin bottom issue.
*
* @var bool
*/
public $is_nested = false;
/**
* Block constructor.
*
* @param array $block The root block.
* @param Style_Builder $style The style builder class.
* @param Html_Builder $html The html builder class.
* @param Pdf_Content $caller The caller class, we use this for accessing to parent data.
*/
public function __construct( array $block, Style_Builder $style, Html_Builder $html, Pdf_Content $caller ) {
$this->block = $block;
$this->html = $html;
$this->style = $style;
$this->caller = $caller;
$this->fix_id();
}
/**
* Trigger code
*/
abstract public function run();
/**
* Append the id into innerHtml
*/
protected function fix_id() {
$client = new HtmlDocument();
if ( empty( $this->block['innerHTML'] ) ) {
return;
}
$client->load( $this->block['innerHTML'] );
$element = $client->lastChild();
if ( is_object( $element ) ) {
$client->lastChild()->setAttribute( 'id', $this->block['id'] );
$class = $client->lastChild()->getAttribute( 'class' );
$client->lastChild()->setAttribute( 'class', $class . ' cb-block' );
$this->block['innerHTML'] = $client->save();
}
}
}