| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail; |
| 4 |
|
| 5 |
use YayMail\Models\TemplateModel; |
| 6 |
use YayMail\Utils\Helpers; |
| 7 |
|
| 8 |
/** |
| 9 |
* GlobalHeaderFooter Class |
| 10 |
* |
| 11 |
* 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. |
| 12 |
* |
| 13 |
* @since 4.1.0 |
| 14 |
* @method static GlobalHeaderFooter get_instance() |
| 15 |
*/ |
| 16 |
class GlobalHeaderFooter { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get global header and footer elements |
| 20 |
* Each result is an array of elements |
| 21 |
* If empty, it means the global header or footer is hidden or not enabled |
| 22 |
* |
| 23 |
* @param string|YayMailTemplate $template |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public static function get_elements( $template ) { |
| 28 |
|
| 29 |
$fallback_result = [ |
| 30 |
'global_header_elements' => [], |
| 31 |
'global_footer_elements' => [], |
| 32 |
]; |
| 33 |
|
| 34 |
if ( ! self::is_global_header_footer_enabled() ) { |
| 35 |
return $fallback_result; |
| 36 |
} |
| 37 |
|
| 38 |
$template = self::get_template_from_input( $template ); |
| 39 |
|
| 40 |
if ( empty( $template ) ) { |
| 41 |
return $fallback_result; |
| 42 |
} |
| 43 |
|
| 44 |
$global_header_footer_elements = TemplateModel::get_instance()->get_global_header_and_footer( $template->get_language() ); |
| 45 |
|
| 46 |
if ( empty( $global_header_footer_elements ) ) { |
| 47 |
return $fallback_result; |
| 48 |
} |
| 49 |
|
| 50 |
if ( self::is_global_header_hidden( $template ) ) { |
| 51 |
$global_header_footer_elements['global_header_elements'] = []; |
| 52 |
} |
| 53 |
|
| 54 |
if ( self::is_global_footer_hidden( $template ) ) { |
| 55 |
$global_header_footer_elements['global_footer_elements'] = []; |
| 56 |
} |
| 57 |
|
| 58 |
return $global_header_footer_elements; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get template from input |
| 63 |
* |
| 64 |
* @param string|YayMailTemplate $template |
| 65 |
* |
| 66 |
* @return YayMailTemplate|null |
| 67 |
*/ |
| 68 |
private static function get_template_from_input( $template ) { |
| 69 |
|
| 70 |
if ( is_string( $template ) ) { |
| 71 |
$template = new YayMailTemplate( $template ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! ( $template instanceof YayMailTemplate ) ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
if ( empty( $template ) ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
return $template; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if global header is hidden |
| 87 |
* |
| 88 |
* @param string|YayMailTemplate $template |
| 89 |
* |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public static function is_global_header_hidden( $template ) { |
| 93 |
$template = self::get_template_from_input( $template ); |
| 94 |
|
| 95 |
if ( empty( $template ) ) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
$global_header_settings = $template->get_global_header_settings(); |
| 100 |
$hidden_value = $global_header_settings['hidden']; |
| 101 |
|
| 102 |
// validate boolean |
| 103 |
return filter_var( $hidden_value, FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Check if global footer is hidden |
| 108 |
* |
| 109 |
* @param string|YayMailTemplate $template |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
public static function is_global_footer_hidden( $template ) { |
| 114 |
$template = self::get_template_from_input( $template ); |
| 115 |
|
| 116 |
if ( empty( $template ) ) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
$global_footer_settings = $template->get_global_footer_settings(); |
| 121 |
$hidden_value = $global_footer_settings['hidden']; |
| 122 |
|
| 123 |
// validate boolean |
| 124 |
return filter_var( $hidden_value, FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get global header override heading content |
| 129 |
* |
| 130 |
* @param string|YayMailTemplate $template |
| 131 |
* |
| 132 |
* @return string|null |
| 133 |
*/ |
| 134 |
public static function get_global_header_override_heading_content( $template ) { |
| 135 |
|
| 136 |
if ( ! self::is_global_header_footer_enabled() ) { |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
$template = self::get_template_from_input( $template ); |
| 141 |
|
| 142 |
if ( empty( $template ) ) { |
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
$global_header_settings = $template->get_global_header_settings(); |
| 147 |
|
| 148 |
if ( ! Helpers::is_true( $global_header_settings['content_override'] ) ) { |
| 149 |
return null; |
| 150 |
} |
| 151 |
|
| 152 |
return $global_header_settings['heading_content'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings']['heading_content']; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get global footer override content |
| 157 |
* |
| 158 |
* @param string|YayMailTemplate $template |
| 159 |
* |
| 160 |
* @return string|null |
| 161 |
*/ |
| 162 |
public static function get_global_footer_override_content( $template ) { |
| 163 |
|
| 164 |
if ( ! self::is_global_header_footer_enabled() ) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
|
| 168 |
$template = self::get_template_from_input( $template ); |
| 169 |
|
| 170 |
if ( empty( $template ) ) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
$global_footer_settings = $template->get_global_footer_settings(); |
| 175 |
|
| 176 |
if ( ! Helpers::is_true( $global_footer_settings['content_override'] ) ) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
|
| 180 |
return $global_footer_settings['footer_content'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings']['footer_content']; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Check if element is in global header |
| 185 |
* |
| 186 |
* @param array $element |
| 187 |
* @param string|YayMailTemplate $template |
| 188 |
* |
| 189 |
* @return bool |
| 190 |
*/ |
| 191 |
public static function is_element_in_global_header( $element, $template ) { |
| 192 |
$elements = self::get_elements( $template ); |
| 193 |
|
| 194 |
if ( empty( $elements['global_header_elements'] ) ) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
return count( |
| 199 |
array_filter( |
| 200 |
$elements['global_header_elements'], |
| 201 |
function( $el ) use ( $element ) { |
| 202 |
return $el['id'] === $element['id']; |
| 203 |
} |
| 204 |
) |
| 205 |
) > 0; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Check if element is in global footer |
| 210 |
* |
| 211 |
* @param array $element |
| 212 |
* @param string|YayMailTemplate $template |
| 213 |
* |
| 214 |
* @return bool |
| 215 |
*/ |
| 216 |
public static function is_element_in_global_footer( $element, $template ) { |
| 217 |
$elements = self::get_elements( $template ); |
| 218 |
|
| 219 |
if ( empty( $elements['global_footer_elements'] ) ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
return count( |
| 224 |
array_filter( |
| 225 |
$elements['global_footer_elements'], |
| 226 |
function( $el ) use ( $element ) { |
| 227 |
return $el['id'] === $element['id']; |
| 228 |
} |
| 229 |
) |
| 230 |
) > 0; |
| 231 |
} |
| 232 |
|
| 233 |
public static function is_global_header_footer_enabled() { |
| 234 |
return yaymail_settings()['global_header_footer_enabled'] ?? false; |
| 235 |
} |
| 236 |
} |
| 237 |
|