| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Bogo |
| 4 |
Description: A straight-forward multilingual plugin. No more double-digit custom DB tables or hidden HTML comments that could cause you headaches later on. |
| 5 |
Plugin URI: http://ideasilo.wordpress.com/bogo/ |
| 6 |
Author: Takayuki Miyoshi |
| 7 |
Author URI: http://ideasilo.wordpress.com/ |
| 8 |
Text Domain: bogo |
| 9 |
Domain Path: /languages/ |
| 10 |
Version: 2.2 |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Copyright 2007-2014 Takayuki Miyoshi (email: takayukister at gmail.com) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License as published by |
| 17 |
the Free Software Foundation; either version 2 of the License, or |
| 18 |
(at your option) any later version. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
define( 'BOGO_VERSION', '2.2' ); |
| 31 |
|
| 32 |
if ( ! defined( 'BOGO_PLUGIN_BASENAME' ) ) |
| 33 |
define( 'BOGO_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); |
| 34 |
|
| 35 |
if ( ! defined( 'BOGO_PLUGIN_NAME' ) ) |
| 36 |
define( 'BOGO_PLUGIN_NAME', trim( dirname( BOGO_PLUGIN_BASENAME ), '/' ) ); |
| 37 |
|
| 38 |
if ( ! defined( 'BOGO_PLUGIN_DIR' ) ) |
| 39 |
define( 'BOGO_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) ); |
| 40 |
|
| 41 |
if ( ! defined( 'BOGO_PLUGIN_URL' ) ) |
| 42 |
define( 'BOGO_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) ); |
| 43 |
|
| 44 |
require_once BOGO_PLUGIN_DIR . '/includes/functions.php'; |
| 45 |
require_once BOGO_PLUGIN_DIR . '/includes/rewrite.php'; |
| 46 |
require_once BOGO_PLUGIN_DIR . '/includes/link-template.php'; |
| 47 |
require_once BOGO_PLUGIN_DIR . '/includes/nav-menu.php'; |
| 48 |
require_once BOGO_PLUGIN_DIR . '/includes/widgets.php'; |
| 49 |
require_once BOGO_PLUGIN_DIR . '/includes/post.php'; |
| 50 |
require_once BOGO_PLUGIN_DIR . '/includes/user.php'; |
| 51 |
require_once BOGO_PLUGIN_DIR . '/includes/query.php'; |
| 52 |
|
| 53 |
if ( is_admin() ) { |
| 54 |
require_once BOGO_PLUGIN_DIR . '/admin/admin.php'; |
| 55 |
} |
| 56 |
|
| 57 |
add_action( 'plugins_loaded', 'bogo_plugins_loaded' ); |
| 58 |
|
| 59 |
function bogo_plugins_loaded() { |
| 60 |
load_plugin_textdomain( 'bogo', 'wp-content/plugins/bogo/languages', 'bogo/languages' ); |
| 61 |
} |
| 62 |
|
| 63 |
add_action( 'init', 'bogo_init' ); |
| 64 |
|
| 65 |
function bogo_init() { |
| 66 |
bogo_languages(); |
| 67 |
|
| 68 |
if ( ! ( is_admin() || is_robots() || is_feed() || is_trackback() ) ) { |
| 69 |
$locale = get_locale(); |
| 70 |
|
| 71 |
if ( ! isset( $_COOKIE['lang'] ) || $_COOKIE['lang'] != $locale ) |
| 72 |
setcookie( 'lang', $locale, 0, '/' ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
add_filter( 'locale', 'bogo_locale' ); |
| 77 |
|
| 78 |
function bogo_locale( $locale ) { |
| 79 |
global $wp_rewrite, $wp_query; |
| 80 |
|
| 81 |
if ( ! did_action( 'plugins_loaded' ) ) |
| 82 |
return $locale; |
| 83 |
|
| 84 |
if ( is_admin() ) |
| 85 |
return bogo_get_user_locale(); |
| 86 |
|
| 87 |
$default_locale = bogo_get_default_locale(); |
| 88 |
|
| 89 |
if ( ! empty( $wp_query->query_vars ) ) { |
| 90 |
if ( ( $lang = get_query_var( 'lang' ) ) && $closest = bogo_get_closest_locale( $lang ) ) |
| 91 |
return $closest; |
| 92 |
else |
| 93 |
return $default_locale; |
| 94 |
} |
| 95 |
|
| 96 |
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { |
| 97 |
$url = is_ssl() ? 'https://' : 'http://'; |
| 98 |
$url .= $_SERVER['HTTP_HOST']; |
| 99 |
$url .= $_SERVER['REQUEST_URI']; |
| 100 |
|
| 101 |
$home = set_url_scheme( get_option( 'home' ) ); |
| 102 |
$home = trailingslashit( $home ); |
| 103 |
|
| 104 |
$available_languages = bogo_available_languages(); |
| 105 |
$available_languages = array_map( 'bogo_lang_slug', array_keys( $available_languages ) ); |
| 106 |
$available_languages = implode( '|', $available_languages ); |
| 107 |
$pattern = '#^' . preg_quote( $home ) . '(' . $available_languages . ')(/|$)#'; |
| 108 |
|
| 109 |
if ( preg_match( $pattern, $url, $matches ) |
| 110 |
&& $closest = bogo_get_closest_locale( $matches[1] ) ) |
| 111 |
return $closest; |
| 112 |
} |
| 113 |
|
| 114 |
$lang = bogo_get_lang_from_url(); |
| 115 |
|
| 116 |
if ( $lang && $closest = bogo_get_closest_locale( $lang ) ) |
| 117 |
return $closest; |
| 118 |
|
| 119 |
$locale = $default_locale; |
| 120 |
|
| 121 |
return $locale; |
| 122 |
} |
| 123 |
|
| 124 |
add_filter( 'query_vars', 'bogo_query_vars' ); |
| 125 |
|
| 126 |
function bogo_query_vars( $query_vars ) { |
| 127 |
$query_vars[] = 'lang'; |
| 128 |
|
| 129 |
return $query_vars; |
| 130 |
} |
| 131 |
|
| 132 |
add_shortcode( 'bogo', 'bogo_language_switcher' ); |
| 133 |
|
| 134 |
?> |