| 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.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Copyright 2007-2012 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.0' ); |
| 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/widgets.php'; |
| 48 |
require_once BOGO_PLUGIN_DIR . '/includes/post-l10n-functions.php'; |
| 49 |
require_once BOGO_PLUGIN_DIR . '/includes/user-l10n-functions.php'; |
| 50 |
require_once BOGO_PLUGIN_DIR . '/includes/post-l10n.php'; |
| 51 |
require_once BOGO_PLUGIN_DIR . '/includes/user-l10n.php'; |
| 52 |
|
| 53 |
add_action( 'plugins_loaded', 'bogo_plugins_loaded' ); |
| 54 |
|
| 55 |
function bogo_plugins_loaded() { |
| 56 |
load_plugin_textdomain( 'bogo', 'wp-content/plugins/bogo/languages', 'bogo/languages' ); |
| 57 |
} |
| 58 |
|
| 59 |
add_action( 'init', 'bogo_init' ); |
| 60 |
|
| 61 |
function bogo_init() { |
| 62 |
if ( ! ( is_admin() || is_robots() || is_feed() || is_trackback() ) ) { |
| 63 |
$locale = get_locale(); |
| 64 |
|
| 65 |
if ( ! isset( $_COOKIE['lang'] ) || $_COOKIE['lang'] != $locale ) |
| 66 |
setcookie( 'lang', $locale, 0, '/' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
add_filter( 'locale', 'bogo_locale' ); |
| 71 |
|
| 72 |
function bogo_locale( $locale ) { |
| 73 |
global $wp_rewrite, $wp_query; |
| 74 |
|
| 75 |
if ( is_admin() ) |
| 76 |
return bogo_get_user_locale(); |
| 77 |
|
| 78 |
$default_locale = bogo_get_default_locale(); |
| 79 |
|
| 80 |
if ( ! empty( $wp_query->query_vars ) ) { |
| 81 |
if ( ( $lang = get_query_var( 'lang' ) ) && $closest = bogo_get_closest_locale( $lang ) ) |
| 82 |
return $closest; |
| 83 |
else |
| 84 |
return $default_locale; |
| 85 |
} |
| 86 |
|
| 87 |
if ( isset( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { |
| 88 |
$url = is_ssl() ? 'https://' : 'http://'; |
| 89 |
$url .= $_SERVER['HTTP_HOST']; |
| 90 |
$url .= $_SERVER['REQUEST_URI']; |
| 91 |
|
| 92 |
$home = trailingslashit( home_url() ); |
| 93 |
$available_languages = bogo_available_languages(); |
| 94 |
$available_languages = array_map( 'bogo_lang_slug', array_keys( $available_languages ) ); |
| 95 |
$available_languages = implode( '|', $available_languages ); |
| 96 |
$pattern = '#^' . preg_quote( $home ) . '(' . $available_languages . ')(/|$)#'; |
| 97 |
|
| 98 |
if ( preg_match( $pattern, $url, $matches ) |
| 99 |
&& $closest = bogo_get_closest_locale( $matches[1] ) ) |
| 100 |
return $closest; |
| 101 |
} |
| 102 |
|
| 103 |
$lang = bogo_get_lang_from_url(); |
| 104 |
|
| 105 |
if ( $lang && $closest = bogo_get_closest_locale( $lang ) ) |
| 106 |
return $closest; |
| 107 |
|
| 108 |
$locale = $default_locale; |
| 109 |
|
| 110 |
return $locale; |
| 111 |
} |
| 112 |
|
| 113 |
add_filter( 'query_vars', 'bogo_query_vars' ); |
| 114 |
|
| 115 |
function bogo_query_vars( $query_vars ) { |
| 116 |
$query_vars[] = 'lang'; |
| 117 |
|
| 118 |
return $query_vars; |
| 119 |
} |
| 120 |
|
| 121 |
add_action( 'parse_query', 'bogo_parse_query' ); |
| 122 |
|
| 123 |
function bogo_parse_query( $query ) { |
| 124 |
$qv = &$query->query_vars; |
| 125 |
|
| 126 |
if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) |
| 127 |
return; |
| 128 |
|
| 129 |
if ( isset( $qv['post_type'] ) && 'any' != $qv['post_type'] ) { |
| 130 |
$localizable = array_filter( (array) $qv['post_type'], 'bogo_is_localizable_post_type' ); |
| 131 |
|
| 132 |
if ( empty( $localizable ) ) { |
| 133 |
$qv['bogo_suppress_locale_query'] = true; |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$lang = isset( $qv['lang'] ) ? $qv['lang'] : ''; |
| 139 |
|
| 140 |
if ( is_admin() ) { |
| 141 |
$locale = $lang; |
| 142 |
} else { |
| 143 |
if ( $lang ) |
| 144 |
$locale = bogo_get_closest_locale( $lang ); |
| 145 |
else |
| 146 |
$locale = get_locale(); |
| 147 |
|
| 148 |
if ( empty( $locale ) ) |
| 149 |
$locale = bogo_get_default_locale(); |
| 150 |
} |
| 151 |
|
| 152 |
if ( empty( $locale ) || ! bogo_is_available_locale( $locale ) ) { |
| 153 |
$qv['bogo_suppress_locale_query'] = true; |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$qv['lang'] = $locale; |
| 158 |
|
| 159 |
if ( is_admin() ) |
| 160 |
return; |
| 161 |
|
| 162 |
if ( '' != $qv['pagename'] ) { |
| 163 |
$query->queried_object = bogo_get_page_by_path( $qv['pagename'], $locale ); |
| 164 |
|
| 165 |
if ( ! empty( $query->queried_object ) ) |
| 166 |
$query->queried_object_id = (int) $query->queried_object->ID; |
| 167 |
else |
| 168 |
unset( $query->queried_object ); |
| 169 |
|
| 170 |
if ( 'page' == get_option( 'show_on_front' ) |
| 171 |
&& isset( $query->queried_object_id ) |
| 172 |
&& $query->queried_object_id == get_option( 'page_for_posts' ) ) { |
| 173 |
$query->is_page = false; |
| 174 |
$query->is_home = true; |
| 175 |
$query->is_posts_page = true; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
add_filter( 'posts_join', 'bogo_posts_join', 10, 2 ); |
| 181 |
|
| 182 |
function bogo_posts_join( $join, $query ) { |
| 183 |
global $wpdb; |
| 184 |
|
| 185 |
$qv = &$query->query_vars; |
| 186 |
|
| 187 |
if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) |
| 188 |
return $join; |
| 189 |
|
| 190 |
$locale = empty( $qv['lang'] ) ? '' : $qv['lang']; |
| 191 |
|
| 192 |
if ( ! bogo_is_available_locale( $locale ) ) |
| 193 |
return $join; |
| 194 |
|
| 195 |
if ( ! $meta_table = _get_meta_table( 'post' ) ) |
| 196 |
return $join; |
| 197 |
|
| 198 |
$join .= " LEFT JOIN $meta_table AS postmeta_bogo ON ($wpdb->posts.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')"; |
| 199 |
|
| 200 |
return $join; |
| 201 |
} |
| 202 |
|
| 203 |
add_filter( 'posts_where', 'bogo_posts_where', 10, 2 ); |
| 204 |
|
| 205 |
function bogo_posts_where( $where, $query ) { |
| 206 |
global $wpdb; |
| 207 |
|
| 208 |
$qv = &$query->query_vars; |
| 209 |
|
| 210 |
if ( ! empty( $qv['bogo_suppress_locale_query'] ) ) |
| 211 |
return $where; |
| 212 |
|
| 213 |
$locale = empty( $qv['lang'] ) ? '' : $qv['lang']; |
| 214 |
|
| 215 |
if ( ! bogo_is_available_locale( $locale ) ) |
| 216 |
return $where; |
| 217 |
|
| 218 |
if ( ! $meta_table = _get_meta_table( 'post' ) ) |
| 219 |
return $where; |
| 220 |
|
| 221 |
$where .= " AND (1=0"; |
| 222 |
|
| 223 |
$where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale ); |
| 224 |
|
| 225 |
if ( bogo_is_default_locale( $locale ) ) |
| 226 |
$where .= " OR postmeta_bogo.meta_id IS NULL"; |
| 227 |
|
| 228 |
$where .= ")"; |
| 229 |
|
| 230 |
return $where; |
| 231 |
} |
| 232 |
|
| 233 |
?> |