/var/www/html_fr/wp-content/plugins/yaymail/src/Builder/ElementBuilder.php


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
<?php
namespace YayMail\Builder;

/**
 * Builder class for constructing email elements
 */
class ElementBuilder {
    
/**
     * Array of elements to build
     *
     * @var array
     */
    
protected $elements = [];

    
/**
     * Constructor
     */
    
public function __construct() {}

    public function 
add_element$type$attributes ) {
        
$this->elements[] = [
            
'type'       => $type,
            
'attributes' => $attributes,
        ];

        return 
$this;
    }

    public function 
build() {
        
$built_elements = [];

        foreach ( 
$this->elements as $element ) {
            
$element_type $element['type'];
            
$attributes   $element['attributes'];

            if ( isset( 
$element['integration'] ) && '3rd' === $element['integration'] ) {
                
$class 'YayMail\Integrations\\' $element_type;
            } elseif ( ! empty( 
$element['addon_namespace'] ) ) {
                
$class $element['addon_namespace'] . '\\Elements\\' $element_type;
            } else {
                
$class 'YayMail\Elements\\' $element_type;
            }

            if ( ! 
class_exists$class ) ) {
                continue;
            }

            if ( 
'ColumnLayout' === $element_type ) {
                
$amount_of_columns $attributes['amount_of_columns'] ?? 1;
                unset( 
$attributes['amount_of_columns'] );
                
$element_data $class::get_data$amount_of_columns$attributes );
            } elseif ( 
'Column' === $element_type ) {
                
$width $attributes['column_width'] ?? 5;
                unset( 
$attributes['column_width'] );
                
$element_data $class::get_data$width$attributes );
            } else {
                
$element_data $class::get_data$attributes );
            }

            if ( isset( 
$attributes['children'] ) && is_array$attributes['children'] ) ) {
                
$element_data['children'] = array_merge( ...array_values$attributes['children'] ) );
            }

            
$built_elements[] = $element_data;
        }
//end foreach

        
return $built_elements;
    }
}