Skip to content

Instantly share code, notes, and snippets.

@gschoppe
Last active October 12, 2018 10:28
Show Gist options
  • Save gschoppe/d5755574f3c3ec8c9078a7229e3bad18 to your computer and use it in GitHub Desktop.
Save gschoppe/d5755574f3c3ec8c9078a7229e3bad18 to your computer and use it in GitHub Desktop.
Adds a "Translate" button to the admin bar, on post-edit and front end pages, that links directly to the WeGlot visual editor for that page.
<?php if(!defined('ABSPATH')) { die(); }
/*
Plugin Name: Admin Bar for WeGlot
Plugin URI: https://gschoppe.com
Description: Adds an admin bar link to translate any page on your site
Version: 0.1.0
Author: Greg Schoppe
Author URI: https://gschoppe.com
Text Domain: weglotadminbar
Domain Path: /languages
*/
if( !class_exists('WeGlot_AdminBarButton') ) {
class WeGlot_AdminBarButton {
public static function Instance() {
static $instance = null;
if ($instance === null) {
$instance = new self();
}
return $instance;
}
private function __construct() {
add_action( 'wp_before_admin_bar_render', array( $this, 'add_admin_button' ) );
add_action( 'admin_head', array( $this, 'admin_bar_styles' ) );
add_action( 'wp_head', array( $this, 'admin_bar_styles' ) );
}
public function add_admin_button() {
global $wp_admin_bar;
$url = $this->get_current_page_url();
if( $url ) {
$wp_admin_bar->add_menu( array(
'parent' => false,
'id' => 'weglot_translate',
'title' => __('Translate', 'weglotadminbar'),
'href' => 'http://dashboard.weglot.com/visual-editor/?url=' . urlencode( $url ),
'meta' => array(
//'html' => '',
//'class' => '',
//'onclick' => '',
'target' => '_blank',
'title' => __('Translate with WeGlot', 'weglotadminbar' )
)
) );
}
}
public function admin_bar_styles() {
$weglot_icon = plugins_url() . "/weglot/images/weglot_fav_bw.png";
?>
<style type="text/css">
body.admin-bar #wpadminbar #wp-admin-bar-weglot_translate a:before {
content: ' ';
display: inline-block;
width: 20px;
height: 20px;
margin-top: 3px;
background-image: url('<?php echo $weglot_icon; ?>') !important;
background-size: contain;
background-position: center center;
background-repeat: no-repeat;
}
body.admin-bar #wpadminbar #wp-admin-bar-weglot_translate a:hover:before {
opacity: 0.6;
}
</style>
<?php
}
private function get_current_page_url() {
if( !is_admin() ) {
$domain = $this->get_site_domain();
$url = $domain . $_SERVER['REQUEST_URI'];
return $url;
} else {
if( function_exists( "get_current_screen" ) )
$screen = get_current_screen();
if( empty( $screen->base ) || $screen->base != 'post' ) {
return false;
}
$id = get_the_ID();
$permalink = get_the_permalink( $id );
if( !$id || !$permalink ) {
return false;
}
return $permalink;
}
}
private function get_site_domain() {
$url = untrailingslashit( get_site_url() );
$path = parse_url( $url, PHP_URL_PATH );
if( $path ) {
$url = preg_replace('/'. preg_quote($path, '/') . '$/', '', $url);
$url = untrailingslashit( $url );
}
return $url;
}
}
WeGlot_AdminBarButton::Instance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment