Skip to content

Commit

Permalink
Merge pull request #155 from hatsumatsu/text-based-labels
Browse files Browse the repository at this point in the history
Add setting to represent languages as labels instead of flags in backend.
  • Loading branch information
lloc authored Nov 29, 2023
2 parents 44d50f4 + 1f546f0 commit ec1281b
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 27 deletions.
2 changes: 1 addition & 1 deletion css/msls.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 85 additions & 4 deletions css/msls.less
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@select-width: 226px;
div#msls.postbox {
label {
width: 16px;
margin-right: 6px;
}
select {
Expand All @@ -18,10 +17,9 @@ select.msls-translations {
align-items: center;
label {
display: flex;
flex-grow: .1;
}
input.msls_title, select{
flex-grow: .9;
flex-grow: 1;
}
}
}
Expand All @@ -32,8 +30,91 @@ select.msls-translations {
margin: 1em auto;
}
}

.flag-icon {
height: 1em;
width: 1.3333em !important;
height: 1em !important;
vertical-align: middle;
overflow: hidden;
line-height: 1 !important;
color: transparent;
}

.msls-icon-wrapper {
display: inline-flex;
justify-content: center;
align-items: center;

text-align: center;

&.flag {
min-width: 36px;
}

&.label {
min-width: 48px;
}

label & {
text-align: left;
}
}

.language-badge,
#wpadminbar .language-badge,
#wpadminbar * .language-badge {
display: inline-block;
min-width: 32px;
height: auto;
padding: 4px 6px;
white-space: nowrap;

font-size: 10px;
line-height: 1;
text-align: center;

background-color: currentColor;
border-radius: 9px;

user-select: none;

& > span {
display: inline-block;
vertical-align: top;
margin: 0 1px;

font-size: 10px;
font-weight: 600;
line-height: 1;
text-transform: uppercase;
color: #fff;
text-align: center;
line-height: 1;

&:nth-child( 2 ) {
opacity: 0.5;
}
}
}

.column-mslscol .language-badge {
margin: 0 1px !important;
}

#wpadminbar .language-badge,
#wpadminbar * .language-badge {
position: relative;
top: -1px;

// margin-left: 4px;
// margin-right: 4px;
padding-top: 3px;
padding-bottom: 3px;

background-color: transparent;
border: 1px currentColor solid;

& > span {
color: currentColor;
}
}
4 changes: 3 additions & 1 deletion includes/Component/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ abstract class Icon {
* Icon constructor
*/
public function __construct() {
$this->map = include $this->get_include();
if( $this->get_include() ) {
$this->map = include $this->get_include();
}
}

/**
Expand Down
34 changes: 34 additions & 0 deletions includes/Component/Icon/IconLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace lloc\Msls\Component\Icon;

use lloc\Msls\Component\Icon;
use lloc\Msls\MslsPlugin;

/**
* Class IconLabel
* @package lloc\Msls\Component
*/
class IconLabel extends Icon {

/**
* @return string
*/
protected function get_include(): string {
return '';
}

/**
* @param string $language
*
* @return string
*/
public function get( string $language ): string {
// if ( isset( $this->map[ $language ] ) ) {
// return $this->map[ $language ];
// }

return '<span>' . implode( '</span><span>', explode( '_', $language ) ) . '</span>';
}

}
8 changes: 8 additions & 0 deletions includes/MslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function language_section(): int {
public function main_section(): int {
$map = [
'display' => __( 'Display', 'multisite-language-switcher' ),
'admin_display' => __( 'Admin Display', 'multisite-language-switcher' ),
'sort_by_description' => __( 'Sort languages', 'multisite-language-switcher' ),
'output_current_blog' => __( 'Current language link', 'multisite-language-switcher' ),
'only_with_translation' => __( 'Translation links', 'multisite-language-switcher' ),
Expand Down Expand Up @@ -317,6 +318,13 @@ public function display() {
echo ( new Select( 'display', MslsLink::get_types_description(), $this->options->display ) )->render();
}

/**
* Shows the select-form-field 'admin_display'
*/
public function admin_display() {
echo ( new Select( 'admin_display', array( 'flag' => __( 'Flag', 'multisite-language-switcher' ), 'label' => __( 'Label', 'multisite-language-switcher' ) ), $this->options->admin_display ) )->render();
}

/**
* Shows the select-form-field 'reference_user'
*/
Expand Down
8 changes: 8 additions & 0 deletions includes/MslsAdminIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace lloc\Msls;

use lloc\Msls\Component\Icon\IconSvg;
use lloc\Msls\Component\Icon\IconLabel;

/**
* Handles the icon links in the backend
Expand Down Expand Up @@ -233,6 +234,13 @@ public function get_icon(): string {
);
}

if ( 'label' === $this->iconType ) {
return sprintf( '<span class="language-badge %s">%s</span>',
$this->language,
( new IconLabel() )->get( $this->language )
);
}

if ( empty( $this->href ) ) {
return '<span class="dashicons dashicons-plus"></span>';
}
Expand Down
12 changes: 11 additions & 1 deletion includes/MslsBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function __construct( $obj, $description ) {
$this->language = MslsBlogCollection::get_blog_language( $this->obj->userblog_id );
}

$this->options = MslsOptions::instance();

$this->description = (string) $description;
}

Expand Down Expand Up @@ -72,7 +74,15 @@ public function get_description(): string {
* @return string
*/
public function get_title(): string {
return sprintf( '%1$s (%2$s)', $this->obj->blogname, $this->get_description() );
$icon = new MslsAdminIcon( null );
$icon->set_language( $this->language );
if( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}

return sprintf( '%1$s %2$s', $this->obj->blogname, '<span class="msls-icon-wrapper flag">' . $icon->get_icon() . '</span>' );
}

/**
Expand Down
19 changes: 14 additions & 5 deletions includes/MslsCustomColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,28 @@ public static function init() {
public function th( $columns ) {
$blogs = $this->collection->get();
if ( $blogs ) {
$arr = [];
$html;
foreach ( $blogs as $blog ) {
$language = $blog->get_language();

$icon = new MslsAdminIcon( null );
$icon->set_language( $language )->set_icon_type( 'flag' );
$icon->set_language( $language );
if( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}

if ( $post_id = get_the_ID() ) {
$icon->set_id( $post_id );
$icon->set_origin_language( 'it_IT' );
}

$arr[] = $icon->get_icon();
$html .= '<span class="msls-icon-wrapper ' . esc_attr( $this->options->admin_display ) . '">';
$html .= $icon->get_icon();
$html .= '</span>';
}
$columns['mslscol'] = implode( '&nbsp;', $arr );
$columns['mslscol'] = $html;
}

return $columns;
Expand Down Expand Up @@ -97,7 +104,9 @@ public function td( $column_name, $item_id ) {
$icon->set_href( $mydata->$language );
}

echo $icon->get_a();
echo '<span class="msls-icon-wrapper ' . esc_attr( $this->options->admin_display ) . '">';
echo $icon->get_a();
echo '</span>';

restore_current_blog();
}
Expand Down
33 changes: 22 additions & 11 deletions includes/MslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,16 @@ public function render_select() {
switch_to_blog( $blog->userblog_id );

$language = $blog->get_language();
$icon = MslsAdminIcon::create( $type )
->set_language( $language )
->set_icon_type( 'flag' );
$icon = MslsAdminIcon::create()->set_language( $language );

if ( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}

if ( $mydata->has_value( $language ) ) {
$icon->set_href( $mydata->$language );
$icon->set_href( $mydata->$language );
}

$selects = '';
Expand Down Expand Up @@ -224,10 +228,11 @@ public function render_select() {
}

$lis .= sprintf(
'<li><label for="msls_input_%s">%s</label>%s</li>',
'<li><label for="msls_input_%s msls-icon-wrapper %4$s">%s</label>%s</li>',
$language,
$icon,
$selects
$selects,
esc_attr( $this->options->admin_display )
);

restore_current_blog();
Expand Down Expand Up @@ -307,8 +312,13 @@ public function render_input( $echo = true ) {

$language = $blog->get_language();
$icon = MslsAdminIcon::create()
->set_language( $language )
->set_icon_type( 'flag' );
->set_language( $language );

if( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}

$value = $title = '';

Expand All @@ -319,16 +329,17 @@ public function render_input( $echo = true ) {
}

$items .= sprintf(
'<li>
<label for="msls_title_%1$s">%2$s</label>
'<li class="">
<label for="msls_title_%1$s msls-icon-wrapper %6$s">%2$s</label>
<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/>
<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>
</li>',
$blog->userblog_id,
$icon,
$language,
$value,
$title
$title,
esc_attr( $this->options->admin_display )
);

restore_current_blog();
Expand Down
8 changes: 6 additions & 2 deletions includes/MslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ public function the_input( $tag, $title_format, $item_format ) {

$language = $blog->get_language();
$icon = MslsAdminIcon::create()
->set_language( $language )
->set_icon_type( 'flag' );
->set_language( $language );
if( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}

$value = $title = '';
if ( $my_data->has_value( $language ) ) {
Expand Down
9 changes: 7 additions & 2 deletions includes/MslsPostTagClassic.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $myda

$language = $blog->get_language();
$icon = MslsAdminIcon::create()
->set_language( $language )
->set_icon_type( 'flag' );
->set_language( $language );

if( $this->options->admin_display === 'label' ) {
$icon->set_icon_type( 'label' );
} else {
$icon->set_icon_type( 'flag' );
}
$options = '';
$terms = get_terms( [ 'taxonomy' => $type, 'hide_empty' => false ] );

Expand Down

0 comments on commit ec1281b

Please sign in to comment.