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