| 1 |
<?php |
| 2 |
namespace ABlocks\Blocks\Logout; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\BlockBaseAbstract; |
| 9 |
use ABlocks\Classes\CssGeneratorV2; |
| 10 |
use ABlocks\Controls\Alignment; |
| 11 |
use ABlocks\Controls\Range; |
| 12 |
use ABlocks\Controls\Border; |
| 13 |
use ABlocks\Controls\Typography; |
| 14 |
use ABlocks\Controls\TextShadow; |
| 15 |
use ABlocks\Controls\TextStroke; |
| 16 |
use ABlocks\Controls\Color; |
| 17 |
|
| 18 |
|
| 19 |
class Block extends BlockBaseAbstract { |
| 20 |
protected $block_name = 'logout'; |
| 21 |
|
| 22 |
public function build_css( $attributes ) { |
| 23 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 24 |
|
| 25 |
$css_generator->add_class_styles( |
| 26 |
'{{WRAPPER}} .ablocks-block-logout__label', |
| 27 |
$this->get_log_out_label_color( $attributes ) |
| 28 |
); |
| 29 |
|
| 30 |
$css_generator->add_class_styles( |
| 31 |
'{{WRAPPER}} .ablocks-block-logout', |
| 32 |
$this->get_log_out_css( $attributes ), |
| 33 |
$this->get_log_out_css( $attributes, 'Tablet' ), |
| 34 |
$this->get_log_out_css( $attributes, 'Mobile' ) |
| 35 |
); |
| 36 |
$css_generator->add_class_styles( |
| 37 |
'{{WRAPPER}} .ablocks-block-logout__name', |
| 38 |
$this->get_name_css( $attributes ) |
| 39 |
); |
| 40 |
|
| 41 |
$css_generator->add_class_styles( |
| 42 |
'{{WRAPPER}} .ablocks-block-logout__avatar', |
| 43 |
$this->get_avatar_css( $attributes ), |
| 44 |
$this->get_avatar_css( $attributes, 'Tablet' ), |
| 45 |
$this->get_avatar_css( $attributes, 'Mobile' ) |
| 46 |
); |
| 47 |
$css_generator->add_class_styles( |
| 48 |
'{{WRAPPER}} .ablocks-block-logout__avatar:hover', |
| 49 |
$this->get_avatar_border_hover_css( $attributes ), |
| 50 |
$this->get_avatar_border_hover_css( $attributes, 'Tablet' ), |
| 51 |
$this->get_avatar_border_hover_css( $attributes, 'Mobile' ) |
| 52 |
); |
| 53 |
|
| 54 |
return $css_generator->generate_css(); |
| 55 |
} |
| 56 |
|
| 57 |
public function get_log_out_label_color( $attributes, $device = '' ) { |
| 58 |
$typography = isset( $attributes['labelTypography'] ) ? $attributes['labelTypography'] : ''; |
| 59 |
$text_stroke = isset( $attributes['labelTextStroke'] ) ? $attributes['labelTextStroke'] : ''; |
| 60 |
$text_shadow = isset( $attributes['labelTextShadow'] ) ? $attributes['labelTextShadow'] : ''; |
| 61 |
$typographyglobal = ! empty( $attributes['labelTypographyGlobal'] ) ? $attributes['labelTypographyGlobal'] : array(); |
| 62 |
return array_merge( |
| 63 |
[ 'color' => Color::get_css( isset( $attributes['logOutLabelColor'] ) ? $attributes['logOutLabelColor'] : '' ) ], |
| 64 |
[ 'background' => Color::get_css( isset( $attributes['logOutLabelBgColor'] ) ? $attributes['logOutLabelBgColor'] : '' ) ], |
| 65 |
Typography::get_css( $typography, '', $device, $typographyglobal ), |
| 66 |
TextStroke::get_css( $text_stroke, '', $device ), |
| 67 |
TextShadow::get_css( $text_shadow, '', $device ), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function get_log_out_css( $attributes, $device = '' ) { |
| 72 |
$log_out_css = []; |
| 73 |
if ( ! empty( $attributes['direction'][ 'value' . $device ] ) ) { |
| 74 |
$log_out_css['flex-direction'] = $attributes['direction'][ 'value' . $device ]; |
| 75 |
} |
| 76 |
|
| 77 |
if ( isset( $attributes['labelAlignment'][ 'value' . $device ] ) ) { |
| 78 |
$log_out_css['justify-content'] = $attributes['labelAlignment'][ 'value' . $device ]; |
| 79 |
} |
| 80 |
|
| 81 |
return array_merge( |
| 82 |
[ 'background' => Color::get_css( isset( $attributes['logOutLabelBgColor'] ) ? $attributes['logOutLabelBgColor'] : '' ) ], |
| 83 |
$log_out_css |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public function get_avatar_css( $attributes, $device = '' ) { |
| 89 |
|
| 90 |
return array_merge( |
| 91 |
Range::get_css([ |
| 92 |
'attributeValue' => $attributes['avatarWidth'], |
| 93 |
'attribute_object_key' => 'value', |
| 94 |
'isResponsive' => true, |
| 95 |
'defaultValue' => 40, |
| 96 |
'hasUnit' => true, |
| 97 |
'unitDefaultValue' => 'px', |
| 98 |
'property' => 'width', |
| 99 |
'device' => $device, |
| 100 |
]), |
| 101 |
Range::get_css([ |
| 102 |
'attributeValue' => $attributes['avatarHeight'], |
| 103 |
'attribute_object_key' => 'value', |
| 104 |
'isResponsive' => true, |
| 105 |
'defaultValue' => 40, |
| 106 |
'hasUnit' => true, |
| 107 |
'unitDefaultValue' => 'px', |
| 108 |
'property' => 'height', |
| 109 |
'device' => $device, |
| 110 |
]), |
| 111 |
isset( $attributes['avatarBorder'] ) ? Border::get_css( $attributes['avatarBorder'], '', $device ) : [], |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function get_avatar_border_hover_css( $attributes, $device = '' ) { |
| 116 |
return array_merge( |
| 117 |
isset( $attributes['avatarBorder'] ) ? Border::get_hover_css( $attributes['avatarBorder'], '', $device ) : [] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public function get_name_css( $attributes, $device = '' ) { |
| 122 |
$typography = isset( $attributes['nameTypography'] ) ? $attributes['nameTypography'] : ''; |
| 123 |
$text_stroke = isset( $attributes['nameTextStroke'] ) ? $attributes['nameTextStroke'] : ''; |
| 124 |
$text_shadow = isset( $attributes['nameTextShadow'] ) ? $attributes['nameTextShadow'] : ''; |
| 125 |
$typographyglobal = ! empty( $attributes['nameTypographyGlobal'] ) ? $attributes['nameTypographyGlobal'] : array(); |
| 126 |
return array_merge( |
| 127 |
[ 'color' => Color::get_css( isset( $attributes['nameColor'] ) ? $attributes['nameColor'] : '' ) ], |
| 128 |
Typography::get_css( $typography, '', $device, $typographyglobal ), |
| 129 |
TextStroke::get_css( $text_stroke, '', $device ), |
| 130 |
TextShadow::get_css( $text_shadow, '', $device ) |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
public function render_block_content( $attributes, $content, $block_instance ) { |
| 135 |
$logout_redirect_option = isset( $attributes['logoutRedirect'] ) ? $attributes['logoutRedirect'] : 'current-url'; |
| 136 |
|
| 137 |
if ( $logout_redirect_option === 'current-url' ) { |
| 138 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 139 |
$logout_redirect_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 140 |
} elseif ( $logout_redirect_option === 'custom-url' ) { |
| 141 |
$logout_redirect_url = isset( $attributes['logoutCustomUrl'] ) && ! empty( $attributes['logoutCustomUrl'] ) |
| 142 |
? esc_url( $attributes['logoutCustomUrl'] ) |
| 143 |
: home_url(); |
| 144 |
} |
| 145 |
|
| 146 |
$login_redirect_option = isset( $attributes['loginRedirect'] ) ? $attributes['loginRedirect'] : 'current-url'; |
| 147 |
|
| 148 |
if ( $login_redirect_option === 'current-url' ) { |
| 149 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 150 |
$login_redirect_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 151 |
} elseif ( $login_redirect_option === 'custom-url' ) { |
| 152 |
$login_redirect_url = isset( $attributes['loginCustomUrl'] ) && ! empty( $attributes['loginCustomUrl'] ) |
| 153 |
? esc_url( $attributes['loginCustomUrl'] ) |
| 154 |
: home_url(); |
| 155 |
} |
| 156 |
|
| 157 |
$current_user = wp_get_current_user(); |
| 158 |
$profile_picture = esc_url( get_avatar_url( $current_user->ID ) ); |
| 159 |
$display_name = sanitize_text_field( $current_user->display_name ); |
| 160 |
|
| 161 |
$button_icon_url = isset( $attributes['buttonIconUrl'] ) && ! empty( $attributes['buttonIconUrl'] ) |
| 162 |
? esc_url( $attributes['buttonIconUrl'] ) |
| 163 |
: ''; |
| 164 |
|
| 165 |
$button_class = isset( $attributes['buttonClass'] ) ? sanitize_html_class( $attributes['buttonClass'] ) : 'ablocks-block-logout__label'; |
| 166 |
$is_logged_in = is_user_logged_in(); |
| 167 |
$is_show_avatar = isset( $attributes['isShowAvatar'] ) && $attributes['isShowAvatar']; |
| 168 |
$is_show_name = isset( $attributes['isShowName'] ) && $attributes['isShowName']; |
| 169 |
|
| 170 |
$button_text = $is_logged_in |
| 171 |
? ( isset( $attributes['logOutLabel'] ) ? sanitize_text_field( $attributes['logOutLabel'] ) : __( '(Log Out)', 'ablocks' ) ) |
| 172 |
: ( isset( $attributes['logInLabel'] ) ? sanitize_text_field( $attributes['logInLabel'] ) : __( '(Log In)', 'ablocks' ) ); |
| 173 |
|
| 174 |
$action_url = $is_logged_in ? wp_logout_url( $logout_redirect_url ) : wp_login_url( $login_redirect_url ); |
| 175 |
|
| 176 |
ob_start(); |
| 177 |
?> |
| 178 |
<div class="ablocks-block-logout"> |
| 179 |
<?php if ( $is_logged_in && $is_show_avatar ) : ?> |
| 180 |
<img |
| 181 |
src="<?php echo esc_url( $profile_picture ); ?>" |
| 182 |
alt="<?php echo esc_attr( $display_name ); ?>" |
| 183 |
class="ablocks-block-logout__avatar" |
| 184 |
/> |
| 185 |
<?php endif; ?> |
| 186 |
<?php if ( $is_logged_in && $is_show_name ) : ?> |
| 187 |
<span class="ablocks-block-logout__name"><?php echo esc_html( $display_name ); ?></span> |
| 188 |
<?php endif; ?> |
| 189 |
<a href="<?php echo esc_url( $action_url ); ?>" class="<?php echo esc_attr( $button_class ); ?>"> |
| 190 |
(<span><?php echo esc_html( $button_text ); ?></span>) |
| 191 |
</a> |
| 192 |
</div> |
| 193 |
<?php |
| 194 |
|
| 195 |
return ob_get_clean(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
} |
| 200 |
|