Skip to content

Instantly share code, notes, and snippets.

@bueltge
Last active October 18, 2015 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bueltge/46766131a3674c12fa5c to your computer and use it in GitHub Desktop.
Save bueltge/46766131a3674c12fa5c to your computer and use it in GitHub Desktop.
MultilingualPress Add on as example to create a custom language switcher widget.
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: MultilingualPress Add on as example to create a custom language switcher widget.
* Description: This is a simple add-on for the MultilingualPress plugin to create a language switcher widget, that is only visible if there are relationship.
* Author: Inpsyde GmbH
* Author URI: http://inpsyde.com
* Version: 2015-10-19
* Text Domain: multilingualpressaddon
* Domain Path: /languages
* License: GPLv2+
* Network:
*/
defined( 'ABSPATH' ) or die();
if ( ! class_exists( 'Multilingual_Press' ) ) {
return;
}
add_action( 'inpsyde_mlp_init', 'mlp_addon_widget_setup' );
/**
* @param Inpsyde_Property_List_Interface $plugin_data Plugin data.
*
* @return void
*/
function mlp_addon_widget_setup( Inpsyde_Property_List_Interface $plugin_data ) {
Mlp_Addon_Widget::insert_asset_instance( $plugin_data->get( 'assets' ) );
}
add_action( 'widgets_init', array( 'Mlp_Addon_Widget', 'widget_register' ) );
/**
* Language switcher widget.
*/
class Mlp_Addon_Widget extends WP_Widget {
/**
* The handle.
*
* @var string
*/
protected static $handle = 'mlp_addon_widget';
/**
* Assets interface from MLP core.
*
* @var Mlp_Assets_Interface
*/
private static $assets;
/**
* Register the widget and set up the description.
*/
public function __construct() {
add_action( 'template_redirect', array( $this, 'require_style' ) );
$widget_ops = array(
'classname' => self::$handle,
'description' => __( 'Custom MultilingualPress Translations', 'multilingualpressaddon' ),
);
parent::__construct( 'Mlp_Addon_Widget', __( 'Language Switcher*', 'multilingualpressaddon' ), $widget_ops );
}
/**
* Load frontend CSS if the widget is active.
*
* @wp-hook template_redirect
*
* @return bool
*/
public function require_style() {
if ( ! is_active_widget( FALSE, FALSE, self::$handle ) ) {
return FALSE;
}
$theme_support = get_theme_support( 'multilingualpress' );
if ( ! empty( $theme_support[ 0 ][ 'language_switcher_widget_style' ] ) ) {
return FALSE;
}
self::$assets->provide( 'mlp_frontend_css' );
return TRUE;
}
/**
* Display widget admin form.
*
* @param array $instance Widget settings
*
* @return void
*/
public function form( $instance ) {
$instance = $this->adapt_settings( $instance );
$title = isset( $instance[ 'widget_title' ] )
? esc_attr( $instance[ 'widget_title' ] ) : '';
?>
<p>
<?php $title_id = $this->get_field_id( 'mlp_widget_title' ); ?>
<label for="<?php echo $title_id; ?>"><?php esc_html_e(
'Title', 'multilingualpressaddon'
); ?></label><br />
<input class="widefat" type="text" id="<?php echo $title_id; ?>"
name="<?php echo $this->get_field_name( 'mlp_widget_title' ); ?>" value="<?php echo $title; ?>">
</p>
<?php
}
/**
* Callback for widget update.
*
* @param array $new_instance New widget settings.
* @param array $old_instance Widget settings.
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'widget_title' ] = esc_html( $new_instance[ 'mlp_widget_title' ] );
return $instance;
}
public function get_mlp_translation() {
$mlp_language_api = apply_filters( 'mlp_language_api', NULL );
if ( is_a( $mlp_language_api, 'Mlp_Language_Api_Interface' ) ) {
$args = [
'site_id' => get_current_blog_id(),
'content_id' => get_queried_object_id(),
'type' => 'post',
'strict' => TRUE,
'search_term' => '',
'post_type' => 'page',
'include_base' => TRUE
];
/* @var Mlp_Language_Api_Interface $mlp_language_api */
$translations = $mlp_language_api->get_translations( $args );
}
if ( ! empty( $translations ) ) {
return TRUE;
}
return FALSE;
}
/**
* Frontend display.
*
* When a widget is restored from trash, the instance might be incomplete, hence the preparations.
*
* @param array $args Widget arguments.
* @param array $instance Widget settings.
*
* @return void
*/
public function widget( $args, $instance ) {
// Exit, if we are outside from singular sites, like post and page.
if ( ! is_singular() ) {
return;
}
// Exit, if we have no translations.
if ( ! $this->get_mlp_translation() ) {
return;
}
// Default arguments for the result.
// @see multilingual-press/src/inc/common/Mlp_Helpers.php Mlp_Helpers::show_linked_elements()
$output_args = array(
'link_text' => 'native',
'show_current_blog' => 0,
'display_flag' => 1,
);
$output = Mlp_Helpers::show_linked_elements( $output_args );
if ( ! $output ) {
return;
}
$title = '';
if ( isset( $instance[ 'widget_title' ] ) ) {
$title = $instance[ 'widget_title' ];
}
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title );
echo $args[ 'before_widget' ];
if ( ! empty( $instance[ 'widget_title' ] ) ) {
echo $args[ 'before_title' ] . $title . $args[ 'after_title' ];
}
echo $output;
echo $args[ 'after_widget' ];
}
/**
* Register the widget.
*
* @return void
*/
public static function widget_register() {
register_widget( __CLASS__ );
}
/**
* Insert assets.
*
* @param Mlp_Assets_Interface $assets Assets.
*
* @return void
*/
public static function insert_asset_instance( Mlp_Assets_Interface $assets ) {
self::$assets = $assets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment