Created
November 28, 2015 13:46
-
-
Save bueltge/0f9daf6a7f19f2890ed3 to your computer and use it in GitHub Desktop.
MultilingualPress Add-on that get a Dropdown Language Menu
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 # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: MultilingualPress Add-on that get a Dropdown Language Menu | |
* Description: This is a simple add-on for the MultilingualPress plugin to get a dropdown language menu. | |
* Author: Inpsyde GmbH | |
* Author URI: http://inpsyde.com | |
* Version: 2015-11-28 | |
* Text Domain: textdomain | |
* License: GPLv2+ | |
* Network: true | |
*/ | |
defined( 'ABSPATH' ) || die(); | |
add_action( 'mlp_and_wp_loaded', array( MlpAddonDropdown::get_instance(), 'init' ) ); | |
/** | |
* Create a dropdown menu as example for custom usage. | |
* | |
* Class MlpAddonDropdown | |
*/ | |
class MlpAddonDropdown { | |
/** | |
* Plugin instance. | |
* | |
* @see get_instance() | |
* @type object | |
* @var $instance | |
*/ | |
protected static $instance = NULL; | |
/** | |
* Get instance. | |
* | |
* @return MlpAddonDropdown | |
*/ | |
public static function get_instance() { | |
if ( ! self::$instance instanceof self ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* Publish_Confirm constructor. | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Run the plugin working instance. | |
*/ | |
public function init() { | |
add_action( 'get_sidebar', array( $this, 'get_dropdown_menu' ) ); | |
add_action( 'the_content', array( $this, 'get_dropdown_menu' ) ); | |
// Include this only for testing. | |
add_action( 'wp_print_styles', array( $this, 'add_bootstrap' ) ); | |
} | |
/** | |
* Include Bootstrap for testing. | |
*/ | |
public function add_bootstrap() { | |
wp_register_script( | |
'mlp_test_bootstrap', | |
'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', | |
array( 'jquery' ) | |
); | |
wp_enqueue_script( 'mlp_test_bootstrap' ); | |
wp_register_style( | |
'mlp_test_bootstrap', | |
'http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' | |
); | |
wp_enqueue_style( 'mlp_test_bootstrap' ); | |
} | |
/** | |
* Get language items. | |
* | |
* @return array|void | |
*/ | |
public function get_language_items() { | |
$api = apply_filters( 'mlp_language_api', NULL ); | |
/** @var Mlp_Language_Api_Interface $api */ | |
if ( ! is_a( $api, 'Mlp_Language_Api_Interface' ) ) { | |
return; | |
} | |
/** | |
* @type int $site_id Base site | |
* @type int $content_id post or term_taxonomy ID, *not* term ID | |
* @type string $type @see Mlp_Language_Api::get_request_type() | |
* @type bool $strict When TRUE (default) only matching exact | |
* translations will be included | |
* @type string $search_term If you want to translate a search | |
* @type string $post_type For post type archives | |
* @type bool $include_base Include the base site in returned list | |
*/ | |
$translations_args = array( | |
'strict' => FALSE, | |
'include_base' => TRUE, | |
); | |
$translations = $api->get_translations( $translations_args ); | |
if ( empty( $translations ) ) { | |
return; | |
} | |
$items = array(); | |
/** @var Mlp_Translation_Interface $translation */ | |
foreach ( $translations as $site_id => $translation ) { | |
$url = $translation->get_remote_url(); | |
if ( empty( $url ) ) { | |
continue; | |
} | |
$language = $translation->get_language(); | |
$active = FALSE; | |
if ( get_current_blog_id() === $site_id ) { | |
$active = TRUE; | |
} | |
$items[ $site_id ] = array( | |
'url' => $url, | |
'http' => $language->get_name( 'http' ), | |
'name' => $language->get_name( 'native' ), | |
'active' => $active, | |
); | |
} | |
return $items; | |
} | |
/** | |
* Format each item to usable link. | |
* | |
* @param $items | |
* | |
* @return array | |
*/ | |
public function get_formated_links( $items ) { | |
$links = array(); | |
foreach ( $items as $item ) { | |
$hreflang = ''; | |
if ( ! empty ( $item[ 'http' ] ) ) { | |
$hreflang = ' hreflang="' . esc_attr( $item[ 'http' ] ) . '"'; | |
} | |
$active = ''; | |
if ( $item[ 'active' ] ) { | |
$active = ' class="active"'; | |
} | |
$links[] = sprintf( | |
'<a href="%1$s" role="menuitem" rel="nofollow"%2$s%3$s>%4$s</a>', | |
esc_url( $item[ 'url' ] ), | |
$hreflang, | |
$active, | |
$item[ 'name' ] | |
); | |
} | |
return $links; | |
} | |
/** | |
* Show dropdown markup. | |
*/ | |
public function get_dropdown_menu() { | |
$items = $this->get_language_items(); | |
$items = $this->get_formated_links( $items ); | |
?> | |
<div class="dropdown"> | |
<button class="btn btn-primary dropdown-toggle" type="button" | |
data-toggle="dropdown"><?php esc_attr_e( 'Dropdown Example', 'textdomain' ); ?> | |
<span class="caret"></span> | |
</button> | |
<ul class="dropdown-menu" role="menu" aria-labelledby="menu1"> | |
<?php foreach ( $items as $item ) { | |
echo '<li>' . $item . '</li>'; | |
} ?> | |
</ul> | |
</div> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment