Skip to content

Commit

Permalink
Merge pull request #139 from brenoalvs/develop
Browse files Browse the repository at this point in the history
Releasing 0.2.0
  • Loading branch information
brenoalvs authored Feb 22, 2017
2 parents fe9887a + cd6768e commit 2e66f52
Show file tree
Hide file tree
Showing 15 changed files with 1,411 additions and 180 deletions.
512 changes: 427 additions & 85 deletions admin/class-monk-admin.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions admin/css/monk-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ th.column-languages {
padding: 0 0 7px;
z-index: 9999;
}

div.monk-language-field {
padding-top: 8px;
}

.monk-language-name {
margin-right: 10px;
}
38 changes: 38 additions & 0 deletions admin/js/monk-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@
e.preventDefault();
});

$( document ).on( 'click', '.monk-attach', function( event ) {
event.preventDefault();
var form_data = {
action : 'monk_add_attachment_translation',
monk_id : $( this ).siblings( '.monk-id' ).val(),
current_post_id : $( this ).siblings( '.current-post-id' ).val(),
lang : $( this ).siblings( '.monk-lang' ).val(),
};

$.ajax({
type: 'POST',
url: monk.ajax_url,
data: form_data,
success: function( response ) {
window.location.replace( response.data );
}
});

return false;
});

$( document ).on( 'click', 'button.monk-change-post-language', function( e ) {
e.preventDefault();
$( '.monk-change-current-language' ).slideUp( 150 );
Expand All @@ -70,6 +91,23 @@
$( document ).ajaxComplete( function() {
$( location ).attr( 'href', url[0] );
});
};

/**
* Insert language for new attachments added through media modal.
*
* Hook into uploader success callback getting the uploaded attachment ID and trigger
* a change event on its language hidden field to dispatch an AJAX request provided by core
* to update attachment language.
*/
if ( typeof wp.Uploader !== 'undefined' ) {
$.extend( wp.Uploader.prototype, {
success : function( file_attachment ){
var attachment_id = file_attachment.attributes.id;
$( '.attachments li[data-id="' + attachment_id + '"] .attachment-preview' ).click();
$( '[name="attachments[' + attachment_id + '][language]"]' ).change();
}
});
}
});
})( jQuery );
28 changes: 28 additions & 0 deletions admin/partials/admin-monk-language-name-render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Show flags in Languages column on posts list.
*
* @since 0.1.0
*
* @package Monk
* @subpackage Monk/Widgets/Partials
*/

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! $language ) : ?>
<?php if ( monk_get_url_args( 'lang' ) ) : ?>
<?php $language = monk_get_url_args( 'lang' ); ?>
<?php elseif ( $_REQUEST['post_id'] && get_post_meta( $_REQUEST['post_id'], '_monk_post_language', true ) ) : ?>
<?php $language = get_post_meta( $_REQUEST['post_id'], '_monk_post_language', true ); ?>
<?php else : ?>
<?php $language = get_option( 'monk_default_language', false ); ?>
<?php endif; ?>
<?php endif; ?>
<div class="monk-language-field">
<span class="monk-language-name"><?php echo esc_html( $monk_languages[ $language ]['name'] ); ?></span>
<span class="monk-selector-flag flag-icon <?php echo esc_attr( 'flag-icon-' . $monk_languages[ $language ]['slug'] ); ?>"></span>
</div>
<input type="hidden" name="<?php echo sprintf( 'attachments[%d][language]', $post_id ); ?>" id="<?php echo sprintf( 'attachments[%d][language]', $post_id ); ?>" value="<?php echo esc_attr( $language ); ?>">
56 changes: 56 additions & 0 deletions admin/partials/admin-monk-language-selector-render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Show flags in Languages column on posts list.
*
* @since 0.1.0
*
* @package Monk
* @subpackage Monk/Widgets/Partials
*/

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

$attach = 'monk-attach';
if ( $available_languages ) :
?>
<select class="monk-lang">
<?php
foreach ( $active_languages as $lang_code ) {
if ( array_key_exists( $lang_code, $monk_languages ) && $language_code !== $lang_code && ! array_key_exists( $lang_code, $post_translations ) ) {
if ( $lang_code === $language ) :
?>
<option value='<?php echo esc_attr( $lang_code ); ?>' selected="selected">
<?php echo esc_html( $monk_languages[ $lang_code ]['name'] ); ?>
</option>
<?php
elseif ( ! $language && $lang_code === $default_language ) :
?>
<option value='<?php echo esc_attr( $lang_code ); ?>' selected="selected">
<?php echo esc_html( $monk_languages[ $lang_code ]['name'] ); ?>
</option>
<?php
else :
?>
<option value="<?php echo esc_attr( $lang_code ); ?>">
<?php echo esc_html( $monk_languages[ $lang_code ]['name'] )?>
</option>;
<?php
endif;
}
}
?>
</select>

<input type="hidden" name="monk_id" class="monk-id" value="<?php echo esc_attr( $monk_id ); ?>">
<input type="hidden" class="current-post-id" value="<?php echo esc_attr( $post_id ); ?>">
<button class="button <?php echo esc_attr( $attach ); ?>">
<?php esc_html_e( 'Ok', 'monk' ); ?>
</button>
<?php else : ?>
<div class="monk-language-field">
<span class="monk-language-name"><?php esc_html_e( 'No more languages available.', 'monk' ); ?></span>
</div>
<?php endif; ?>
31 changes: 28 additions & 3 deletions admin/partials/admin-monk-post-meta-box-field-render.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

?>
<input type="hidden" name="monk_id" value="<?php echo esc_attr( $monk_id ); ?>" />
<input type="hidden" class="current-post-id" value="<?php echo esc_attr( $post->ID ); ?>">
<?php if ( 'add' === $current_screen->action || '' === $post_default_language ) : ?>
<div>
<strong><?php esc_html_e( 'Post language', 'monk' ); ?></strong>
Expand Down Expand Up @@ -76,10 +77,10 @@
</div>
<div class="monk-post-meta-add-translation">
<?php if ( count( $active_languages ) !== $translation_counter ) : ?>
<select name="monk_post_translation_id">
<select name="monk_post_translation_id" class='monk-lang'>
<?php
$post_type = get_post_type( $post->ID );
if ( 'post' !== $post_type ) :
if ( 'post' !== $post_type && 'attachment' !== $post_type ) :
foreach ( $active_languages as $lang_code ) :
$language_url = add_query_arg( array(
'post_type' => $post_type,
Expand All @@ -96,6 +97,19 @@
<?php
endif;
endforeach;
elseif ( 'attachment' === $post_type ) :
$monk_translation_url = admin_url( 'media-new.php' );
foreach ( $active_languages as $lang_code ) :
$lang_id = sanitize_title( $lang_code );
if ( array_key_exists( $lang_code, $monk_languages ) && ! array_key_exists( $lang_code, $post_translations ) ) :
$lang_name = $monk_languages[ $lang_code ]['name'];
?>
<option value="<?php echo esc_attr( $lang_code ); ?>"/>
<?php echo esc_html( $lang_name ); ?>
</option>
<?php
endif;
endforeach;
else :
foreach ( $active_languages as $lang_code ) :
$language_url = add_query_arg( array(
Expand All @@ -115,7 +129,18 @@
endif;
?>
</select>
<button class="monk-submit-translation button"><?php esc_html_e( 'Ok', 'monk' ); ?></button>
<?php
$attach = ( 'attachment' === $post_type ) ? 'monk-attach' : '';
if ( $attach ) :
?>
<input type="hidden" name="monk_id" class="monk-id" value="<?php echo esc_attr( $monk_id ); ?>">
<input type="hidden" class="current-post-id" value="<?php echo esc_attr( $post->ID ); ?>">
<button class="button <?php echo esc_attr( $attach ); ?>"><?php esc_html_e( 'Ok', 'monk' ); ?></button>
<?php
else :
?>
<button class="monk-submit-translation button" id="<?php echo esc_attr( $attach ); ?>"><?php esc_html_e( 'Ok', 'monk' ); ?></button>
<?php endif; ?>
<a class="monk-cancel-submit-translation hide-if-no-js button-cancel"><?php esc_html_e( 'Cancel', 'monk' ); ?></a>
<?php endif; ?>
</div>
Expand Down
7 changes: 6 additions & 1 deletion admin/partials/monk-language-column.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
<?php if ( $monk_language ) : ?>
<?php if ( $available_languages ) : ?>
<div class="monk-button-wrapper">
<a class="monk-new-translation-link button" href="<?php echo esc_url( $new_url ); ?>"><?php esc_html_e( 'Add+', 'monk' ) ?></a>
<?php $is_attachment = 'attachment' === $post_type ? 'monk-attach' : ''; ?>
<?php if ( 'monk-attach' === $is_attachment ) : ?>
<input type="hidden" class="monk-id" value="<?php echo esc_attr( $monk_translations_id ); ?>">
<input type="hidden" class="current-post-id" value="<?php echo esc_attr( $post_id ); ?>">
<?php endif; ?>
<a class="monk-new-translation-link button <?php echo esc_attr( $is_attachment ); ?>" href="<?php echo esc_url( $new_url ); ?>"><?php esc_html_e( 'Add+', 'monk' ) ?></a>
</div>
<?php endif; ?>
<div class="monk-flag-wrapper">
Expand Down
2 changes: 2 additions & 0 deletions includes/class-monk-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Monk_Activator {
*/
public static function activate() {
global $monk_languages;

$locale = get_locale();
if ( array_key_exists( $locale, $monk_languages ) ) {
$language = $locale;
Expand All @@ -39,6 +40,7 @@ public static function activate() {
update_option( 'monk_default_language', $language );
update_option( 'monk_active_languages', array( $language ) );
update_option( 'monk_settings_notice', true );
update_option( 'monk_first_media_list_access', true );
set_transient( '_monk_redirect', true, 30 );
}

Expand Down
36 changes: 25 additions & 11 deletions includes/class-monk-i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,42 @@ public function load_plugin_textdomain() {
}

/**
* Change locale accordingly Language Switcher.
* Changes locale accordingly requested URL.
*
* @since 0.1.0
*
* @param string $locale Language of locale.
* @return string $locale New language of locale.
*/
public function monk_define_locale( $locale ) {
global $monk_languages;
$active_languages = get_option( 'monk_active_languages', false );
if ( is_admin() ) {
return $locale;
}

if ( isset( $_GET['lang'] ) ) {
$lang = sanitize_text_field( wp_unslash( $_GET['lang'] ) );
foreach ( $active_languages as $lang_code ) {
if ( $lang === $monk_languages[ $lang_code ]['slug'] ) {
$locale = $lang_code;
}
global $monk_languages;

/**
* As locale is defined before WordPress parse the request.
* We need get language directly from URL.
*/
$path = $_SERVER['REQUEST_URI'];
$has_args = strpos( $path, '?' );

if ( false !== $has_args ) {
$path_parts = str_split( $path, $has_args );
$path = $path_parts[0];
}

$matches = preg_split( '/(\/)/', $path, 0, PREG_SPLIT_NO_EMPTY );

if ( ! empty( $matches ) ) {
$slug = $matches[0];
$locale = monk_get_locale_by_slug( $slug );
$active_languages = get_option( 'monk_active_languages', false );

if ( ! in_array( $locale, $active_languages, true ) ) {
$locale = get_option( 'monk_default_language', false );
}
} else {
$locale = get_option( 'monk_default_language', false );
}

return $locale;
Expand Down
Loading

0 comments on commit 2e66f52

Please sign in to comment.