Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 653 lines (593 sloc) 20.414 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
<?php

/**
* Translations and languages API.
*
* @package Babble
* @since Alpha 1
*/

/* Copyright 2013 Code for the People

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*/


/**
* Returns the current content language code.
*
* @FIXME: Currently does not check for language validity, though perhaps we should check that elsewhere and redirect?
*
* @return string A language code
* @access public
**/
function bbl_get_current_content_lang_code() {
global $bbl_locale;
return $bbl_locale->get_content_lang();
}

/**
* Returns the current interface language code.
*
* @FIXME: Currently does not check for language validity, though perhaps we should check that elsewhere and redirect?
*
* @return string A language code
* @access public
**/
function bbl_get_current_interface_lang_code() {
global $bbl_locale;
return $bbl_locale->get_interface_lang();
}

/**
* Returns the current (content) language code.
*
* @return string A language code
* @access public
**/
function bbl_get_current_lang_code() {
return bbl_get_current_content_lang_code();
}

/**
* Given a lang object or lang code, this checks whether the
* language is public or not.
*
* @param string $lang_code A language code
* @return boolean True if public
* @access public
**/
function bbl_is_public_lang( $lang_code ) {
global $bbl_languages;
return $bbl_languages->is_public_lang( $lang_code );
}

/**
* Set the current (content) lang.
*
* @uses Babble_Locale::switch_to_lang to do the actual work
* @see switch_to_blog for similarities
*
* @param string $lang The language code to switch to
* @return void
**/
function bbl_switch_to_lang( $lang ) {
global $bbl_locale;
$bbl_locale->switch_to_lang( $lang );
}

/**
* Restore the previous lang.
*
* @uses Babble_Locale::restore_lang to do the actual work
* @see restore_current_blog for similarities
*
* @return void
**/
function bbl_restore_lang() {
global $bbl_locale;
$bbl_locale->restore_lang();
}

/**
* Get the terms which are the translations for the provided
* term ID. N.B. The returned array of term objects (and false
* values) will include the term for the term ID passed.
*
* @param int|object $term Either a WP Term object, or a term_id
* @return array Either an array keyed by the site languages, each key containing false (if no translation) or a WP Post object
* @access public
**/
function bbl_get_term_translations( $term, $taxonomy ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_term_translations( $term, $taxonomy );
}

/**
* Get the posts which are the translation jobs for the provided
* term ID.
*
* @param int|object $term Either a WP Term object, or a term_id
* @return array An array keyed by the site languages, each key containing a WP Post object
* @access public
**/
function bbl_get_term_jobs( $term, $taxonomy ) {
global $bbl_jobs;
return $bbl_jobs->get_term_jobs( $term, $taxonomy );
}

/**
* Return the admin URL to create a new translation for a term in a
* particular language.
*
* @param int|object $default_term The term in the default language to create a new translation for, either WP Post object or post ID
* @param string $lang The language code
* @param string $taxonomy The taxonomy
* @return string The admin URL to create the new translation
* @access public
**/
function bbl_get_new_term_translation_url( $default_term, $lang, $taxonomy = null ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_new_term_translation_url( $default_term, $lang, $taxonomy );
}

/**
* Returns the language code associated with a particular taxonomy.
*
* @param string $taxonomy The taxonomy to get the language for
* @return string The lang code
**/
function bbl_get_taxonomy_lang_code( $taxonomy ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_taxonomy_lang_code( $taxonomy );
}

/**
* Return the base taxonomy (in the default language) for a
* provided taxonomy.
*
* @param string $taxonomy The name of a taxonomy
* @return string The name of the base taxonomy
**/
function bbl_get_base_taxonomy( $taxonomy ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_base_taxonomy( $taxonomy );
}

/**
* Returns the equivalent taxonomy in the specified language.
*
* @param string $taxonomy A taxonomy to return in a given language
* @param string $lang_code The language code for the required language (optional, defaults to current)
* @return string The taxonomy name
**/
function bbl_get_taxonomy_in_lang( $taxonomy, $lang_code = null ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_taxonomy_in_lang( $taxonomy, $lang_code );
}

/**
* Test whether a particular taxonomy is translated or not.
*
* @param string $taxonomy The name of the taxonomy to check
* @return bool True if this is a translated taxonomy
*/
function bbl_is_translated_taxonomy( $taxonomy ) {
return (bool) apply_filters( 'bbl_translated_taxonomy', true, $taxonomy );
}

/**
* Test whether a particular post type is translated or not.
*
* @param string $post_type The name of the post type to check
* @return bool True if this is a translated post type
*/
function bbl_is_translated_post_type( $post_type ) {
return (bool) apply_filters( 'bbl_translated_post_type', true, $post_type );
}

/**
* Returns a taxonomy slug translated into a particular language.
*
* @param string $slug The slug to translate
* @param string $lang_code The language code for the required language (optional, defaults to current)
* @return string A translated slug
**/
function bbl_get_taxonomy_slug_in_lang( $slug, $lang_code = null ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_slug_in_lang( $slug, $lang_code );
}

/**
* Get the posts which are the translations for the provided
* post ID. N.B. The returned array of post objects (and false
* values) will include the post for the post ID passed.
*
* @param int|object $post Either a WP Post object, or a post ID
* @return array Either an array keyed by the site languages, each key containing false (if no translation) or a WP Post object
* @access public
**/
function bbl_get_post_translations( $post ) {
global $bbl_post_public;
return $bbl_post_public->get_post_translations( $post );
}

/**
* Get the posts which are the translation jobs for the provided
* post ID.
*
* @param int|object $post Either a WP Post object, or a post ID
* @return array Either an array keyed by the site languages, each key containing a WP Post object
* @access public
**/
function bbl_get_post_jobs( $post ) {
global $bbl_jobs;
return $bbl_jobs->get_post_jobs( $post );
}

/**
* Returns the post ID for the post in the default language from which
* this post was translated.
*
* @param int|object $post Either a WP Post object, or a post ID
* @return int The ID of the default language equivalent post
* @access public
**/
function bbl_get_default_lang_post( $post ) {
global $bbl_post_public;
return $bbl_post_public->get_default_lang_post( $post );
}

/**
* Return the language code for the language a given post is written for/in.
*
* @param int|object $post Either a WP Post object, or a post ID
* @return string|object Either a language code, or a WP_Error object
* @access public
**/
function bbl_get_post_lang_code( $post ) {
global $bbl_post_public;
return $bbl_post_public->get_post_lang_code( $post );
}

/**
* Return the admin URL to create a new translation for a post in a
* particular language.
*
* @param int|object $default_post The post in the default language to create a new translation for, either WP Post object or post ID
* @param string $lang The language code
* @return string The admin URL to create the new translation
* @access public
**/
function bbl_get_new_post_translation_url( $default_post, $lang ) {
global $bbl_post_public;
return $bbl_post_public->get_new_post_translation_url( $default_post, $lang );
}

/**
* Return the post type name for the equivalent post type for the
* supplied original post type in the requested language.
*
* @param string $post_type The originating post type
* @param string $lang_code The language code for the required language (optional, defaults to current)
* @return string A post type name, e.g. "page" or "post"
**/
function bbl_get_post_type_in_lang( $original_post_type, $lang_code = null ) {
global $bbl_post_public;
if ( is_null( $lang_code ) )
$lang_code = bbl_get_current_lang_code();
return $bbl_post_public->get_post_type_in_lang( $original_post_type, $lang_code );
}

add_filter( 'bbl_get_content_post_type', 'bbl_get_post_type_in_lang' );

/**
* Is the query for a single page or translation or a single page?
*
* If the $page parameter is specified, this function will additionally
* check if the query is for one of the pages specified.
*
* @see is_page()
*
* @param mixed $page Page ID, title, slug, or array of such.
* @return bool
*/
function bbl_is_page( $page = '' ) {
$base_page = bbl_get_post_in_lang( get_the_ID(), bbl_get_default_lang_code() );
if ( ! $page )
return 'page' == $base_page->post_type;
if ( is_int( $page ) )
return $page == $base_page->ID;
if ( $page == $base_page->post_name )
return true;
if ( $page == $base_page->post_title )
return true;
if ( $page == (string) $base_page->ID )
return true;
return false;
}

/**
* Returns the post in a particular language
*
* @param int|object $post Either a WP Post object, or a post ID
* @param string $lang_code The language code for the required language
* @param boolean $fallback If true: if a post is not available, fallback to the default language content (defaults to true)
* @return object|boolean The WP Post object, or if $fallback was false and no post then returns false
**/
function bbl_get_post_in_lang( $post, $lang_code, $fallback = true ) {
global $bbl_post_public;
return $bbl_post_public->get_post_in_lang( $post, $lang_code, $fallback );
}

/**
* Returns the term in a particular language
*
* @param int|object $term Either a term object, or a term ID
* @param string $taxonomy The term taxonomy
* @param string $lang_code The language code for the required language
* @param boolean $fallback If true: if a term is not available, fallback to the default language content (defaults to true)
* @return object|boolean The term object, or if $fallback was false and no term then returns false
**/
function bbl_get_term_in_lang( $term, $taxonomy, $lang_code, $fallback = true ) {
global $bbl_taxonomies;
return $bbl_taxonomies->get_term_in_lang( $term, $taxonomy, $lang_code, $fallback );
}

/**
* Returns a post_type slug translated into a particular language.
*
* @param string $slug The slug to translate
* @param string $lang_code The language code for the required language (optional, defaults to current)
* @return string A translated slug
**/
function bbl_get_post_type_slug_in_lang( $slug, $lang_code = null ) {
global $bbl_post_public;
$lang = bbl_get_lang( $lang_code );
return $bbl_post_public->get_slug_in_lang( $slug, $lang );
}

/**
* Echoes the title of a post, in the requested language (if available).
*
* @param int|object $post Either a WP Post object, or a post ID
* @param string $lang_code The code for the language the title is requested in
* @param bool $fallback Whether to provide a fallback title in the default language if the requested language is unavailable (defaults to false)
* @return void
**/
function bbl_the_title_in_lang( $post = null, $lang_code = null, $fallback = false ) {
echo bbl_get_the_title_in_lang( $post, $lang_code, $fallback );
}

/**
* Returns the title of a post, in the requested language (if available).
*
* @param int|object $post Either a WP Post object, or a post ID
* @param string $lang_code The code for the language the title is requested in
* @param bool $fallback Whether to provide a fallback title in the default language if the requested language is unavailable (defaults to false)
* @return void
**/
function bbl_get_the_title_in_lang( $post = null, $lang_code = null, $fallback = false ) {
$post = get_post( $post );
if ( is_null( $lang_code ) )
$lang_code = bbl_get_current_lang_code();

// Hopefully we find the post in the right language
if ( $lang_post = bbl_get_post_in_lang( $post, $lang_code, $fallback ) )
return apply_filters( 'bbl_the_title_in_lang', get_the_title( $lang_post->ID ), $lang_code );

// We have failed…
return '';
}

/**
* Echoes the permalink of a post, in the requested language (if available).
*
* @param int|object $post Either a WP Post object, or a post ID
* @param string $lang_code The code for the language the title is requested in
* @param bool $fallback Whether to provide a fallback title in the default language if the requested language is unavailable (defaults to false)
* @return void
**/
function bbl_the_permalink_in_lang( $post = null, $lang_code = null, $fallback = false ) {
echo bbl_get_the_permalink_in_lang( $post, $lang_code, $fallback );
}

/**
* Returns the permalink of a post, in the requested language (if available).
*
* @param int|object $post Either a WP Post object, or a post ID
* @param string $lang_code The code for the language the title is requested in
* @param bool $fallback Whether to provide a fallback title in the default language if the requested language is unavailable (defaults to false)
* @return void
**/
function bbl_get_the_permalink_in_lang( $post = null, $lang_code = null, $fallback = false ) {
$post = get_post( $post );
if ( is_null( $lang_code ) )
$lang_code = bbl_get_current_lang_code();

// Hopefully we find the post in the right language
if ( $lang_post = bbl_get_post_in_lang( $post, $lang_code, $fallback ) )
return apply_filters( 'bbl_permalink_in_lang', get_permalink( $lang_post->ID ), $lang_code );

// We have failed…
return '';
}

/**
* Returns the link to a post type in a particular language.
*
* @param string $post_type A post type for which you want a translated archive link
* @param string $lang_code The code for the language the link is requested in
* @return void
**/
function bbl_get_post_type_archive_link_in_lang( $post_type, $lang_code = null ) {
if ( is_null( $lang_code ) )
$lang_code = bbl_get_current_lang_code();
bbl_switch_to_lang( $lang_code );
$lang_post_type = bbl_get_post_type_in_lang( $post_type, $lang_code );
$link = get_post_type_archive_link( $lang_post_type );
bbl_restore_lang();
return apply_filters( 'bbl_post_type_archive_link_in_lang', $link );
}

/**
* Return the base post type (in the default language) for a
* provided post type.
*
* @param string $post_type The name of a post type
* @return string The name of the base post type
**/
function bbl_get_base_post_type( $post_type ) {
global $bbl_post_public;
return $bbl_post_public->get_base_post_type( $post_type );
}

/**
* Return all the base post types (in the default language).
*
* @return array An array of post_type objects
**/
function bbl_get_base_post_types() {
global $bbl_post_public;
return $bbl_post_public->get_base_post_types();
}

/**
* Returns an array of all the shadow post types associated with
* this post type.
*
* @param string $base_post_type The post type to look up shadow post types for
* @return array The names of all the related shadow post types
**/
function bbl_get_shadow_post_types( $base_post_type ) {
global $bbl_post_public;
return $bbl_post_public->get_shadow_post_types( $base_post_type );
}

/**
* Return the active language objects for the current site. A
* language object looks like:
* 'ar' =>
* object(stdClass)
* public 'name' => string 'Arabic'
* public 'code' => string 'ar'
* public 'url_prefix' => string 'ar'
* public 'text_direction' => string 'rtl'
* public 'display_name' => string 'Arabic'
*
* @uses Babble_Languages::get_active_langs to do the actual work
*
* @return array An array of Babble language objects
**/
function bbl_get_active_langs() {
global $bbl_languages;
return $bbl_languages->get_active_langs();
}

/**
* Returns the requested language object.
*
* @param string $code A language code, e.g. "fr_BE"
* @return object|boolean A Babble language object
**/
function bbl_get_lang( $lang_code ) {
global $bbl_languages;
return $bbl_languages->get_lang( $lang_code );
}

/**
* Returns the current language object, respecting any
* language switches; i.e. if your request was for
* Arabic, but the language is currently switched to
* French, this will return French.
*
* @return object A Babble language object
**/
function bbl_get_current_lang() {
global $bbl_languages;
return $bbl_languages->get_current_lang();
}

/**
* Returns the default language code for this site.
*
* @return string A language code, e.g. "he_IL"
**/
function bbl_get_default_lang_code() {
global $bbl_languages;
return $bbl_languages->get_default_lang_code();
}

/**
* Returns the default language for this site.
*
* @return object A language object
**/
function bbl_get_default_lang() {
global $bbl_languages;
return $bbl_languages->get_default_lang();
}

/**
* Checks whether either the provided language code,
* if provided, or the current language code are
* the default language.
*
* i.e. is this language the default language
*
* n.b. the current language could have been switched
* using bbl_switch_to_lang
*
* @param string $lang_code The language code to check (optional)
* @return bool True if the default language
**/
function bbl_is_default_lang( $lang_code = null ) {
if ( is_null( $lang_code ) )
$lang = bbl_get_current_lang();
else if ( is_string( $lang_code ) ) // In case someone passes a lang object
$lang = bbl_get_lang( $lang_code );
return ( bbl_get_default_lang_code() == $lang->code );
}

/**
* Returns the default language code for this site.
*
* @return string The language URL prefix set by the admin, e.g. "de"
**/
function bbl_get_default_lang_url_prefix() {
global $bbl_languages;
$code = $bbl_languages->get_default_lang_code();
return $bbl_languages->get_url_prefix_from_code( $code );
}

/**
* Returns the language code for the provided URL prefix.
*
* @param string $url_prefix The URL prefix to find the language code for
* @return string The language code, or false
**/
function bbl_get_lang_from_prefix( $url_prefix ) {
global $bbl_languages;
return $bbl_languages->get_code_from_url_prefix( $url_prefix );
}

/**
* Returns the language code for the provided URL prefix.
*
* @param string $lang_code The language code to look up
* @return string The language URL prefix set by the admin, e.g. "de"
**/
function bbl_get_prefix_from_lang_code( $lang_code ) {
global $bbl_languages;
return $bbl_languages->get_url_prefix_from_code( $lang_code );
}

/**
* Returns the switch links for the current content.
*
* @param string $id_prefix A prefix to the ID for each item
* @return array An array of admin menu nodes
**/
function bbl_get_switcher_links( $id_prefix = '' ) {
global $bbl_switcher_menu;
return $bbl_switcher_menu->get_switcher_links( $id_prefix );
}

/**
* Start logging for Babble
*
* @return void
**/
function bbl_start_logging() {
global $bbl_log;
$bbl_log->logging = true;
}

/**
* Stop logging for Babble
*
* @return void
**/
function bbl_stop_logging() {
global $bbl_log;
$bbl_log->logging = false;
}

/**
* Log a message.
*
* @param string $msg Log this message
* @return void
**/
function bbl_log( $msg ) {
global $bbl_log;
if ( $bbl_log )
$bbl_log->log( $msg );
else
error_log( "Full Babble logging unavailable: $msg" );
}

/**
* Whether Babble is logging right now.
*
* @return boolean True for yes, natch
**/
function bbl_is_logging() {
global $bbl_log;
return $bbl_log->logging;
}

?>
Something went wrong with that request. Please try again.