Created
December 11, 2015 15:22
-
-
Save Clorith/ea360c12b75d63a4cb66 to your computer and use it in GitHub Desktop.
Encourage your users to submit translations for your WordPress plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function language_detector_admin_notices() { | |
// Get the current language locale | |
$language = get_locale(); | |
// Check if the nag screen has been disabled for this language | |
if ( false === get_option( 'plugin_slug_language_detector_' . $language, false ) ) { | |
// Check if a translation file already exists for this language | |
if ( $loaded = load_plugin_textdomain( 'text_domain', false, plugin_dir_path( __FILE__ ) . '/languages/' ) ) { | |
// If a translation file is discovered, we disable all future nag screens for this language | |
update_option( 'plugin_slug_language_detector_' . $language, true, true ); | |
return; | |
} | |
// We need the translation data from core to display human readable locale names | |
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); | |
$translations = wp_get_available_translations(); | |
// Gather information about the plugin (in this case we really only watn the name and text domain/slug) | |
$plugin = get_plugin_data( __FILE__ ); | |
printf( | |
'<div class="notice notice-info"><p><strong>%s</strong></p><p>%s</p></div>', | |
esc_html( $plugin['Name'] ), | |
sprintf( | |
__( 'This plugin does not have a translation for <strong>%1$s</strong>, you can help translating the plugin at <a href="%2$s" target="_blank">%2$s</a>.' ), | |
$translations[ $language ]['native_name'], // The native name of the language currently in use | |
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/' . $plugin['TextDomain'] ) // The URL to the translation overview page for our plugin | |
) | |
); | |
} | |
} | |
// Hook into admin notices | |
add_action( 'admin_notices', 'language_detector_admin_notices' ); | |
// Output JavaScript for handling dismissals in the footer | |
function language_detector_admin_footer() { | |
$language = get_locale(); | |
// We only add our JavaScript if the nag notice is being displayed any way | |
if ( false === get_option( '$plugin_slug_language_detector_' . $language, false ) ) { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function ($) { | |
$( ".notice.is-dismissible.language-detect-nag-dismiss" ).on( "click", ".notice-dismiss", function() { | |
var data = { | |
action : 'language_nag_dismiss' | |
}; | |
$.post( | |
ajaxurl, | |
data | |
); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'admin_footer', 'language_detector_admin_footer' ); | |
// Trigger function for when the ajax call goes through | |
function language_detector_dismiss() { | |
$language = get_locale(); | |
update_option( '$plugin_slug_language_detector_' . $language, true ); | |
wp_die(); | |
} | |
add_action( 'wp_ajax_language_nag_dismiss', 'language_detector_dismiss' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment