/var/www/html_fr/wp-content/plugins/yaymail/src/GlobalHeaderFooter.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

namespace YayMail;

use 
YayMail\Models\TemplateModel;

/**
 * GlobalHeaderFooter Class
 *
 * This is an YayMail element, not an email template. But its customizer page (for editing, saving, etc...) shares the same logic as email template customizer.
 *
 * @since 4.1.0
 * @method static GlobalHeaderFooter get_instance()
 */
class GlobalHeaderFooter {

    
/**
     * Get global header and footer elements
     * Each result is an array of elements
     * If empty, it means the global header or footer is hidden or not enabled
     *
     * @param string|YayMailTemplate $template
     *
     * @return array
     */
    
public static function get_elements$template ) {

        
$fallback_result = [
            
'global_header_elements' => [],
            
'global_footer_elements' => [],
        ];

        if ( ! 
self::is_global_header_footer_enabled() ) {
            return 
$fallback_result;
        }

        
$template self::get_template_from_input$template );

        if ( empty( 
$template ) ) {
            return 
$fallback_result;
        }

        
$global_header_footer_elements TemplateModel::get_instance()->get_global_header_and_footer$template->get_language() );

        if ( empty( 
$global_header_footer_elements ) ) {
            return 
$fallback_result;
        }

        if ( 
self::is_global_header_hidden$template ) ) {
            
$global_header_footer_elements['global_header_elements'] = [];
        }

        if ( 
self::is_global_footer_hidden$template ) ) {
            
$global_header_footer_elements['global_footer_elements'] = [];
        }

        return 
$global_header_footer_elements;
    }

    
/**
     * Get template from input
     *
     * @param string|YayMailTemplate $template
     *
     * @return YayMailTemplate|null
     */
    
private static function get_template_from_input$template ) {

        if ( 
is_string$template ) ) {
            
$template = new YayMailTemplate$template );
        }

        if ( ! ( 
$template instanceof YayMailTemplate ) ) {
            return 
null;
        }

        if ( empty( 
$template ) ) {
            return 
null;
        }

        return 
$template;
    }

    
/**
     * Check if global header is hidden
     *
     * @param string|YayMailTemplate $template
     *
     * @return bool
     */
    
public static function is_global_header_hidden$template ) {
        
$template self::get_template_from_input$template );

        if ( empty( 
$template ) ) {
            return 
true;
        }

        
$global_header_settings $template->get_global_header_settings();

        return ! ( empty( 
$global_header_settings['hidden'] ) ); // phpcs:ignore
    
}

    
/**
     * Check if global footer is hidden
     *
     * @param string|YayMailTemplate $template
     *
     * @return bool
     */
    
public static function is_global_footer_hidden$template ) {
        
$template self::get_template_from_input$template );

        if ( empty( 
$template ) ) {
            return 
true;
        }

        
$global_footer_settings $template->get_global_footer_settings();

        return ! ( empty( 
$global_footer_settings['hidden'] ) ); // phpcs:ignore
    
}

    
/**
     * Get global header override heading content
     *
     * @param string|YayMailTemplate $template
     *
     * @return string|null
     */
    
public static function get_global_header_override_heading_content$template ) {

        if ( ! 
self::is_global_header_footer_enabled() ) {
            return 
null;
        }

        
$template self::get_template_from_input$template );

        if ( empty( 
$template ) ) {
            return 
null;
        }

        
$global_header_settings $template->get_global_header_settings();

        if ( 
'true' != $global_header_settings['content_override'] ) { // phpcs:ignore
            
return null;
        }

        return 
$global_header_settings['heading_content'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings']['heading_content'];
    }

    
/**
     * Get global footer override content
     *
     * @param string|YayMailTemplate $template
     *
     * @return string|null
     */
    
public static function get_global_footer_override_content$template ) {

        if ( ! 
self::is_global_header_footer_enabled() ) {
            return 
null;
        }

        
$template self::get_template_from_input$template );

        if ( empty( 
$template ) ) {
            return 
null;
        }

        
$global_footer_settings $template->get_global_footer_settings();

        if ( 
'true' != $global_footer_settings['content_override'] ) { // phpcs:ignore
            
return null;
        }

        return 
$global_footer_settings['footer_content'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings']['footer_content'];
    }

    
/**
     * Check if element is in global header
     *
     * @param array                  $element
     * @param string|YayMailTemplate $template
     *
     * @return bool
     */
    
public static function is_element_in_global_header$element$template ) {
        
$elements self::get_elements$template );

        if ( empty( 
$elements['global_header_elements'] ) ) {
            return 
false;
        }

        return 
count(
            
array_filter(
                
$elements['global_header_elements'],
                function( 
$el ) use ( $element ) {
                    return 
$el['id'] === $element['id'];
                }
            )
        ) > 
0;
    }

    
/**
     * Check if element is in global footer
     *
     * @param array                  $element
     * @param string|YayMailTemplate $template
     *
     * @return bool
     */
    
public static function is_element_in_global_footer$element$template ) {
        
$elements self::get_elements$template );

        if ( empty( 
$elements['global_footer_elements'] ) ) {
            return 
false;
        }

        return 
count(
            
array_filter(
                
$elements['global_footer_elements'],
                function( 
$el ) use ( $element ) {
                    return 
$el['id'] === $element['id'];
                }
            )
        ) > 
0;
    }

    public static function 
is_global_header_footer_enabled() {
        return 
yaymail_settings()['global_header_footer_enabled'] ?? false;
    }
}