/var/www/html_fr/wp-content/plugins/yaymail/src/Elements/ElementsLoader.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

namespace YayMail\Elements;

use 
YayMail\Abstracts\BaseElement;
use 
YayMail\Shortcodes\ShortcodesExecutor;
use 
YayMail\Utils\SingletonTrait;
use 
YayMail\Utils\TemplateHelpers;

/**
 * Email Loader Class
 *
 * @method static ElementsLoader get_instance()
 */
class ElementsLoader {

    use 
SingletonTrait;

    private 
$elements = [];

    private function 
__construct() {
        
$dir = new \DirectoryIteratorYAYMAIL_PLUGIN_PATH '/src/Elements' );
        foreach ( 
$dir as $fileinfo ) {
            if ( ! 
$fileinfo->isDot() ) {
                
$file_name  $fileinfo->getFilename();
                
$class_name basename$file_name'.php' );
                
$class      'YayMail\\Elements\\' $class_name;
                if ( 
__CLASS__ === $class || 'ElementsHelper' === $class_name ) {
                    continue;
                }
                if ( 
class_exists$class ) ) {
                    
$instance $class::get_instance();
                    
$this->register_element$instance );
                }
            }
        }

        
do_action'yaymail_register_elements'$this );

        
$emails yaymail_get_emails();

        foreach ( 
$this->elements as $element ) {
            foreach ( 
$emails as $email ) {
                if ( 
$element->is_available_in_email$email ) ) {
                    
$email->register_element$element );
                }
            }
        }
    }

    public function 
register_element$element ) {
        if ( ! ( 
$element instanceof BaseElement ) ) {
            return;
        }
        
$this->elements[] = $element;
    }

    public function 
get_all() {
        return 
$this->elements;
    }

    public function 
get_element_instance_by_type$type ) {
        foreach ( 
$this->elements as $element ) {
            if ( 
$element->get_type() === $type ) {
                return 
$element;
            }
        }
        return 
null;
    }

    public static function 
load_elements$elements ) {
        
$content = [];
        if ( ! 
is_array$elements ) ) {
            return [];
        }
        foreach ( 
$elements as $element ) {
            if ( isset( 
$element['integration'] ) && '3rd' === $element['integration'] ) {
                
$class 'YayMail\Integrations\\' $element['type'];
            } elseif ( ! empty( 
$element['addon_namespace'] ) ) {
                
$class $element['addon_namespace'] . '\\Elements\\' $element['type'];
            } elseif ( ! empty( 
$element['caller_class'] ) ) {
                
$class $element['caller_class'];
            } else {
                
$class 'YayMail\Elements\\' $element['type'];
            }
            if ( ! 
class_exists$class ) ) {
                continue;
            }

            
$attributes   = isset( $element['attributes'] ) ? $element['attributes'] : [];
            
$element_info $class::get_data$attributes );

            
// Map data to get the 'default_value' only
            
$mapped_data          array_map(
                function( 
$attribute ) {
                    
// If the attribute has default value, get the default value
                    // Else leave the whole value as is
                    
return $attribute['default_value'] ?? $attribute;
                },
                
$element_info['data']
            );
            
$element_info['data'] = $mapped_data;

            
// Remove unneeded attributes
            
$unneeded_attributes = [ 'icon''group''position' ];
            foreach ( 
$unneeded_attributes as $attribute ) {
                unset( 
$element_info$attribute ] );
            }

            
$content[] = $element_info;
        }
//end foreach
        
return $content;
    }

    
/**
     * Render list elements
     *
     * @param $args includes
     * $render_data
     * $template
     * $settings
     * $is_nested
     * ...
     */
    
public static function render_elements$elements$args ) {
        
$is_nested     = isset( $args['is_nested'] ) ? $args['is_nested'] : false;
        
$template_name $args['template']->get_name();

        
$shortcodes yaymail_get_email_shortcodes$template_name );

        if ( ! empty( 
$args['render_data']['order'] ) ) {
            
$order $args['render_data']['order'];
            if ( 
$order instanceof \WC_Order ) {
                
$order_id $order->get_id();
            } elseif ( 
is_numeric$order ) ) {
                
$order_id $order;
            } else {
                
$order_id null;
            }
            
// $shortcodes = apply_filters( 'yaymail_extra_shortcodes', $shortcodes, $template_name, $order_id );
        
}

        
$args apply_filters'yaymail_template_rendering_args'$args$template_name$elements );

        foreach ( 
$elements as $element ) {
            if ( empty( 
$element['available'] ) ) {
                continue;
            }

            if ( ! 
apply_filters'yaymail_validate_element_before_sending'true$element$args ) ) {
                continue;
            }

            
$args['element'] = $element;

            new 
ShortcodesExecutor$shortcodes$args );

            
$element_instance yaymail_get_element$element['type'] );

            
$layout '';
            if ( 
$element_instance ) {
                
$layout $element_instance->get_layout$element$args );
            }

            
$layout TemplateHelpers::remove_empty_shortcodes$layout );

            if ( 
'column' === $element['type'] || $is_nested ) {
                
yaymail_kses_post_e$layout );
            } else {
                
?>
            <tr>
                <td style="padding: 0;"><?php yaymail_kses_post_e$layout ); ?></td>
            </tr>
                <?php
            
}
        }
//end foreach
    
}
}