| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail; |
| 4 |
|
| 5 |
use YayMail\Models\TemplateModel; |
| 6 |
use YayMail\Platform\PlatformRegistry; |
| 7 |
use YayMail\Platform\YaymailPlatform; |
| 8 |
use YayMail\Utils\Helpers; |
| 9 |
|
| 10 |
/** |
| 11 |
* GlobalHeaderFooter Class |
| 12 |
* |
| 13 |
* 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. |
| 14 |
* |
| 15 |
* @since 4.1.0 |
| 16 |
* @method static GlobalHeaderFooter get_instance() |
| 17 |
*/ |
| 18 |
class GlobalHeaderFooter { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get global header and footer elements |
| 22 |
* Each result is an array of elements |
| 23 |
* If empty, it means the global header or footer is hidden or not enabled |
| 24 |
* |
| 25 |
* @param string|YayMailTemplate $template |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public static function get_elements( $template ) { |
| 30 |
|
| 31 |
$fallback_result = [ |
| 32 |
'global_header_elements' => [], |
| 33 |
'global_footer_elements' => [], |
| 34 |
]; |
| 35 |
|
| 36 |
$template_name = $template->get_name(); |
| 37 |
|
| 38 |
if ( ! self::is_global_header_footer_enabled( $template_name ) ) { |
| 39 |
return $fallback_result; |
| 40 |
} |
| 41 |
|
| 42 |
$template = self::get_template_from_input( $template ); |
| 43 |
|
| 44 |
if ( empty( $template ) ) { |
| 45 |
return $fallback_result; |
| 46 |
} |
| 47 |
|
| 48 |
$ghf_template_name = self::resolve_platform( $template_name )->ghf_option_key(); |
| 49 |
|
| 50 |
$global_header_footer_elements = TemplateModel::get_instance()->get_global_header_and_footer( $template->get_language(), $ghf_template_name ); |
| 51 |
|
| 52 |
if ( empty( $global_header_footer_elements ) ) { |
| 53 |
return $fallback_result; |
| 54 |
} |
| 55 |
|
| 56 |
if ( self::is_global_header_hidden( $template ) ) { |
| 57 |
$global_header_footer_elements['global_header_elements'] = []; |
| 58 |
} |
| 59 |
|
| 60 |
if ( self::is_global_footer_hidden( $template ) ) { |
| 61 |
$global_header_footer_elements['global_footer_elements'] = []; |
| 62 |
} |
| 63 |
|
| 64 |
return $global_header_footer_elements; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Resolve the platform that owns a given global-header-footer template name. |
| 69 |
* |
| 70 |
* Template naming is the only signal available at this call depth ('wp-core*' |
| 71 |
* belongs to the yaymagic-email-builder product, everything else to yaymail), so this |
| 72 |
* is the single place that maps it to a registered platform instead of each |
| 73 |
* caller re-deriving option keys inline. |
| 74 |
* |
| 75 |
* @param string $template_name |
| 76 |
* |
| 77 |
* @return \YayMail\Platform\PlatformInterface |
| 78 |
*/ |
| 79 |
private static function resolve_platform( $template_name ) { |
| 80 |
$platform_key = str_starts_with( $template_name, 'wp-core' ) ? 'yaymagic-email-builder' : 'yaymail'; |
| 81 |
|
| 82 |
return PlatformRegistry::get( $platform_key ) ?? PlatformRegistry::get( 'yaymail' ) ?? new YaymailPlatform(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get template from input |
| 87 |
* |
| 88 |
* @param string|YayMailTemplate $template |
| 89 |
* |
| 90 |
* @return YayMailTemplate|null |
| 91 |
*/ |
| 92 |
private static function get_template_from_input( $template ) { |
| 93 |
|
| 94 |
if ( is_string( $template ) ) { |
| 95 |
$template = new YayMailTemplate( $template ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! ( $template instanceof YayMailTemplate ) ) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
if ( empty( $template ) ) { |
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
return $template; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if global header is hidden |
| 111 |
* |
| 112 |
* @param string|YayMailTemplate $template |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public static function is_global_header_hidden( $template ) { |
| 117 |
$template = self::get_template_from_input( $template ); |
| 118 |
|
| 119 |
if ( empty( $template ) ) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
$global_header_settings = $template->get_global_header_settings(); |
| 124 |
$hidden_value = $global_header_settings['hidden']; |
| 125 |
|
| 126 |
// validate boolean |
| 127 |
return filter_var( $hidden_value, FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check if global footer is hidden |
| 132 |
* |
| 133 |
* @param string|YayMailTemplate $template |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public static function is_global_footer_hidden( $template ) { |
| 138 |
$template = self::get_template_from_input( $template ); |
| 139 |
|
| 140 |
if ( empty( $template ) ) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
$global_footer_settings = $template->get_global_footer_settings(); |
| 145 |
$hidden_value = $global_footer_settings['hidden']; |
| 146 |
|
| 147 |
// validate boolean |
| 148 |
return filter_var( $hidden_value, FILTER_VALIDATE_BOOLEAN ); // phpcs:ignore |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get global header override heading content |
| 153 |
* |
| 154 |
* @param string|YayMailTemplate $template |
| 155 |
* |
| 156 |
* @return string|null |
| 157 |
*/ |
| 158 |
public static function get_global_header_override_heading_content( $template ) { |
| 159 |
|
| 160 |
$template_name = $template->get_name(); |
| 161 |
if ( ! self::is_global_header_footer_enabled( $template_name ) ) { |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
$template = self::get_template_from_input( $template ); |
| 166 |
|
| 167 |
if ( empty( $template ) ) { |
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
$global_header_settings = $template->get_global_header_settings(); |
| 172 |
|
| 173 |
if ( ! Helpers::is_true( $global_header_settings['content_override'] ) ) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
|
| 177 |
$default_heading = self::resolve_platform( $template_name )->is_woo_product() |
| 178 |
? YayMailTemplate::DEFAULT_DATA['global_header_settings']['heading_content'] |
| 179 |
: '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">[yaymail_site_name]</h1>'; |
| 180 |
return $global_header_settings['heading_content'] ?? apply_filters( 'yaymail_default_global_header_override_content', $default_heading ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get global footer override content |
| 185 |
* |
| 186 |
* @param string|YayMailTemplate $template |
| 187 |
* |
| 188 |
* @return string|null |
| 189 |
*/ |
| 190 |
public static function get_global_footer_override_content( $template ) { |
| 191 |
|
| 192 |
$template_name = $template->get_name(); |
| 193 |
if ( ! self::is_global_header_footer_enabled( $template_name ) ) { |
| 194 |
return null; |
| 195 |
} |
| 196 |
|
| 197 |
$template = self::get_template_from_input( $template ); |
| 198 |
|
| 199 |
if ( empty( $template ) ) { |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
$global_footer_settings = $template->get_global_footer_settings(); |
| 204 |
|
| 205 |
if ( ! Helpers::is_true( $global_footer_settings['content_override'] ) ) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
$default_footer = self::resolve_platform( $template_name )->is_woo_product() |
| 210 |
? YayMailTemplate::DEFAULT_DATA['global_footer_settings']['footer_content'] |
| 211 |
: '<p style="font-size: 14px;margin: 0px 0px 16px; text-align: center;">[yaymail_site_name]</p>'; |
| 212 |
return $global_footer_settings['footer_content'] ?? apply_filters( 'yaymail_default_global_footer_override_content', $default_footer ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Check if element is in global header |
| 217 |
* |
| 218 |
* @param array $element |
| 219 |
* @param string|YayMailTemplate $template |
| 220 |
* |
| 221 |
* @return bool |
| 222 |
*/ |
| 223 |
public static function is_element_in_global_header( $element, $template ) { |
| 224 |
$elements = self::get_elements( $template ); |
| 225 |
|
| 226 |
if ( empty( $elements['global_header_elements'] ) ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
return count( |
| 231 |
array_filter( |
| 232 |
$elements['global_header_elements'], |
| 233 |
function( $el ) use ( $element ) { |
| 234 |
return $el['id'] === $element['id']; |
| 235 |
} |
| 236 |
) |
| 237 |
) > 0; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Check if element is in global footer |
| 242 |
* |
| 243 |
* @param array $element |
| 244 |
* @param string|YayMailTemplate $template |
| 245 |
* |
| 246 |
* @return bool |
| 247 |
*/ |
| 248 |
public static function is_element_in_global_footer( $element, $template ) { |
| 249 |
$elements = self::get_elements( $template ); |
| 250 |
|
| 251 |
if ( empty( $elements['global_footer_elements'] ) ) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
return count( |
| 256 |
array_filter( |
| 257 |
$elements['global_footer_elements'], |
| 258 |
function( $el ) use ( $element ) { |
| 259 |
return $el['id'] === $element['id']; |
| 260 |
} |
| 261 |
) |
| 262 |
) > 0; |
| 263 |
} |
| 264 |
|
| 265 |
public static function is_global_header_footer_enabled( $template_name = '' ) { |
| 266 |
if ( empty( $template_name ) ) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
$key = self::resolve_platform( $template_name )->ghf_option_key( 'enabled' ); |
| 271 |
|
| 272 |
return (bool) ( yaymail_settings()[ $key ] ?? false ); |
| 273 |
} |
| 274 |
} |
| 275 |
|