Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/css/msls.css

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

28 changes: 28 additions & 0 deletions assets/css/msls.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,38 @@ select.msls-translations {
input.msls_title, select {
flex-grow: 1;
}
.msls-create-new,
.msls-edit-link {
text-decoration: none;
margin-left: 4px;
color: #2271b1;
&:hover {
color: #135e96;
}
}
}
}
}

.msls-quick-create {
background: none;
border: none;
padding: 0;
margin: 0;
cursor: pointer;
color: inherit;
font: inherit;
line-height: inherit;
&.msls-loading .dashicons {
animation: msls-spin 1s linear infinite;
}
}

@keyframes msls-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}

#msls-content-import {
.button-primary {
margin: 1em auto;
Expand Down
98 changes: 90 additions & 8 deletions includes/MslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@
if ( $blogs ) {
global $post;

$type = get_post_type( $post->ID );
$mydata = new MslsOptionsPost( $post->ID );
$type = get_post_type( $post->ID );
$mydata = new MslsOptionsPost( $post->ID );
$origin_language = MslsBlogCollection::get_blog_language();
$is_saved = 'auto-draft' !== get_post_status( $post );

$this->maybe_set_linked_post( $mydata );

Expand All @@ -198,8 +200,10 @@
$icon_type = $this->options->get_icon_type();
$icon = MslsAdminIcon::create( $type )->set_language( $language )->set_icon_type( $icon_type );

$linked_post_id = null;
if ( $mydata->has_value( $language ) ) {
$icon->set_href( (int) $mydata->$language );
$linked_post_id = (int) $mydata->$language;
$icon->set_href( $linked_post_id );
}

$selects = '';
Expand Down Expand Up @@ -234,11 +238,17 @@
);
}

$action = '';
if ( $is_saved ) {
$action = $this->get_create_new_link( $type, $language, $post->ID, $origin_language, $linked_post_id );
}

$lis .= sprintf(
'<li><label for="msls_input_%1$s msls-icon-wrapper %4$s">%2$s</label>%3$s</li>',
'<li><label for="msls_input_%1$s" class="msls-icon-wrapper %5$s">%2$s</label>%3$s%4$s</li>',
esc_attr( $language ),
$icon, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$selects, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$action, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_attr( $icon_type )
);

Expand Down Expand Up @@ -311,8 +321,10 @@
if ( $blogs ) {
global $post;

$post_type = get_post_type( $post->ID );
$my_data = new MslsOptionsPost( $post->ID );
$post_type = get_post_type( $post->ID );
$my_data = new MslsOptionsPost( $post->ID );
$origin_language = MslsBlogCollection::get_blog_language();
$is_saved = 'auto-draft' !== get_post_status( $post );

$this->maybe_set_linked_post( $my_data );

Expand All @@ -330,19 +342,27 @@
$value = '';
$title = '';

$linked_post_id = null;
if ( $my_data->has_value( $language ) ) {
$icon->set_href( (int) $my_data->$language );
$linked_post_id = (int) $my_data->$language;
$icon->set_href( $linked_post_id );
$value = $my_data->$language;
$title = get_the_title( $value );
}

$action = '';
if ( $is_saved ) {
$action = $this->get_create_new_link( $post_type, $language, $post->ID, $origin_language, $linked_post_id );
}

$items .= sprintf(
'<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>',
'<li class=""><label for="msls_title_%1$s" class="msls-icon-wrapper %7$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"/>%6$s</li>',
$blog->userblog_id,
$icon,
$language,
$value,
$title,
$action,
esc_attr( $icon_type )
);

Expand Down Expand Up @@ -372,6 +392,68 @@
}
}

/**
* Renders the action element for a language row in the metabox.
*
* Returns a "+" create button (Quick Create or classic link) when no
* translation is linked, or an external-link icon when one exists.
*
* @param string $type Post type slug.
* @param string $language Target language code.
* @param int $post_id Current (source) post ID.
* @param string $origin_language Source blog language code.
* @param ?int $linked_post_id Linked translation post ID, or null.
*
* @return string
*/
private function get_create_new_link( string $type, string $language, int $post_id, string $origin_language, ?int $linked_post_id ): string {
if ( null !== $linked_post_id ) {
$href = (string) get_edit_post_link( $linked_post_id );

/* translators: %s: language code */
$title = sprintf(
__( 'Edit the translation in the %s-blog', 'multisite-language-switcher' ),

Check failure on line 415 in includes/MslsMetaBox.php

View workflow job for this annotation

GitHub Actions / test

WordPress.WP.I18n.MissingTranslatorsComment

A function call to __() with texts containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.

Check failure on line 415 in includes/MslsMetaBox.php

View workflow job for this annotation

GitHub Actions / test

WordPress.WP.I18n.MissingTranslatorsComment

A function call to __() with texts containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
$language
);

return sprintf(
'<a class="msls-edit-link" href="%1$s" target="_blank" title="%2$s"><span class="dashicons dashicons-external"></span></a>',
esc_url( $href ),
esc_attr( $title )
);
}

if ( msls_options()->activate_quick_create ) {
$action_icon = ( new MslsAdminIcon( $type ) )
->set_language( $language )
->set_icon_type( 'action' )
->set_id( $post_id )
->set_origin_language( $origin_language );

return $action_icon->get_a();
}

$action_icon = ( new MslsAdminIcon( $type ) )
->set_language( $language )
->set_icon_type( 'action' )
->set_id( $post_id )
->set_origin_language( $origin_language );

$href = $action_icon->get_edit_new();
Comment on lines +426 to +442
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The instantiation of MslsAdminIcon is duplicated and uses the constructor directly instead of the preferred factory method. Refactoring this to use MslsAdminIcon::create() once will improve maintainability and ensure consistency with how icons are handled elsewhere in the plugin.

		$action_icon = MslsAdminIcon::create( $type )
			->set_language( $language )
			->set_icon_type( 'action' )
			->set_id( $post_id )
			->set_origin_language( $origin_language );

		if ( msls_options()->activate_quick_create ) {
			return $action_icon->get_a();
		}

		$href = $action_icon->get_edit_new();


/* translators: %s: language code */
$title = sprintf(
__( 'Create a new translation in the %s-blog', 'multisite-language-switcher' ),

Check failure on line 446 in includes/MslsMetaBox.php

View workflow job for this annotation

GitHub Actions / test

WordPress.WP.I18n.MissingTranslatorsComment

A function call to __() with texts containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.

Check failure on line 446 in includes/MslsMetaBox.php

View workflow job for this annotation

GitHub Actions / test

WordPress.WP.I18n.MissingTranslatorsComment

A function call to __() with texts containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
$language
);

return sprintf(
'<a class="msls-create-new" href="%1$s" target="_blank" title="%2$s"><span class="dashicons dashicons-plus"></span></a>',
esc_url( $href ),
esc_attr( $title )
);
}

/**
* Set
*
Expand Down
8 changes: 7 additions & 1 deletion src/msls-quick-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ jQuery( document ).ready(
}
).then(
function ( response ) {
var isMetabox = $button.closest( '#msls' ).length > 0;
var $link = $( '<a>' )
.attr( 'href', response.edit_url )
.attr( 'title', $button.attr( 'title' ).replace( /Create/, 'Edit' ) )
.html( $button.html() );

$link.find( '.dashicons' ).removeClass( 'dashicons-update dashicons-plus' ).addClass( 'dashicons-edit' );
if ( isMetabox ) {
$link.addClass( 'msls-edit-link' ).attr( 'target', '_blank' );
}

var successIcon = isMetabox ? 'dashicons-external' : 'dashicons-edit';
$link.find( '.dashicons' ).removeClass( 'dashicons-update dashicons-plus' ).addClass( successIcon );

$button.replaceWith( $link );

Expand Down
4 changes: 4 additions & 0 deletions src/msls.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ jQuery( document ).ready(
select: function ( event, ui ) {
$( event.target ).val( ui.item.label );
hid_field.val( ui.item.value );
$( event.target ).siblings( '.msls-create-new, .msls-quick-create' ).hide();
$( event.target ).siblings( '.msls-edit-link' ).show();
return false;
},
change: function ( event, ui ) {
if ( ! $( event.target ).val() ) {
hid_field.val( '' );
$( event.target ).siblings( '.msls-create-new, .msls-quick-create' ).show();
$( event.target ).siblings( '.msls-edit-link' ).hide();
} else if (
mslsinput.id === hid_field.val() &&
mslsinput.title !== $( event.target ).val()
Expand Down
Loading
Loading