1: <?php
2: if ( ! defined( 'ABSPATH' ) ) die( "Access denied" );
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: class CMLLanguage {
32:
33: private static $_default_language;
34:
35: private static $_all_languages;
36:
37: private static $_all_enabled;
38:
39: private static $_all_others;
40:
41: private static $_all_by_slug;
42:
43: private static $_current_id;
44:
45: 46: 47:
48: const FLAG_TINY = "tiny";
49:
50: 51: 52:
53: const FLAG_SMALL = "small";
54:
55: 56: 57: 58: 59:
60: public static function get_default() {
61: if( empty( self::$_all_languages ) ) self::get_all();
62:
63: return self::$_default_language;
64: }
65:
66: 67: 68: 69: 70:
71: public static function get_default_id() {
72: return self::get_default()->id;
73: }
74:
75: 76: 77: 78: 79:
80: public static function get_default_slug() {
81: return self::get_default()->cml_language_slug;
82: }
83:
84: 85: 86: 87: 88:
89: public static function get_all() {
90: global $wpdb;
91:
92: 93: 94:
95: if( empty( self::$_all_languages) ) {
96: self::$_all_languages = $wpdb->get_results( sprintf( "SELECT * FROM %s ORDER BY cml_sort_id ", CECEPPA_ML_TABLE ) );
97:
98: $all_languages_by_keys = array();
99: $sorted = array();
100: $enableds = array();
101: $byslug = array();
102: $others = array();
103: foreach( self::$_all_languages as $l ) {
104: $all_languages_by_keys[ $l->id ] = $l;
105: $byslug[ $l->cml_language_slug ] = $l;
106: $sorted[] = $l;
107:
108: if( $l->cml_default == 1 )
109: self::$_default_language = $l;
110: else
111: $others[ $l->id ] = $l;
112:
113: if( $l->cml_enabled == 1 ) $enableds[$l->id] = $l;
114: }
115:
116: if( empty( self::$_default_language ) ) {
117: update_option( "cml_warning_no_default_language", true );
118:
119: self::$_default_language = end( $enableds );
120: }
121:
122: self::$_all_enabled = $enableds;
123: self::$_all_others = $others;
124: self::$_all_languages = $all_languages_by_keys;
125: self::$_all_by_slug = $byslug;
126:
127: if( ! empty( self::$_default_language ) )
128: self::$_current_id = self::$_default_language->id;
129: }
130:
131: return self::$_all_languages;
132: }
133:
134: 135: 136: 137: 138:
139: public static function get_no_default() {
140: if( empty( self::$_all_languages) ) self::get_all();
141:
142: return self::$_all_others;
143: }
144:
145: 146: 147: 148: 149:
150: public static function get_enableds() {
151: if( empty( self::$_all_languages ) ) self::get_all();
152:
153: return self::$_all_enabled;
154: }
155:
156: 157: 158: 159: 160: 161:
162: public static function get_others( $only_enableds = true ) {
163: if( empty( self::$_all_languages ) ) self::get_all();
164:
165: $langs = ( $only_enableds ) ? self::$_all_enabled : self::$_all_languages;
166: unset( $langs[ self::get_current_id() ] );
167:
168: return $langs;
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public static function get_slugs() {
186: if( empty( self::$_all_languages ) ) self::get_all();
187:
188: return self::$_all_by_slug;
189: }
190:
191: 192: 193: 194: 195:
196: public static function get_current() {
197: if( empty( self::$_all_languages ) ) self::get_all();
198:
199: return self::$_all_languages[ self::$_current_id ];
200: }
201:
202: 203: 204: 205: 206:
207: public static function get_current_id() {
208: return self::get_current()->id;
209: }
210:
211: 212: 213:
214: public static function get_current_slug() {
215: return self::get_current()->cml_language_slug;
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: public static function get_name( $lang ) {
226: if( empty( self::$_all_languages ) ) self::get_all();
227: if( empty( self::$_default_language ) ) self::get_default();
228: if( ! is_numeric( $lang ) ) $lang = self::get_id_by_slug( $lang );
229:
230: return isset( self::$_all_languages[ $lang ] ) ? self::$_all_languages[ $lang ]->cml_language : "";
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public static function get_slug( $lang ) {
241: if( empty( self::$_all_languages ) ) self::get_all();
242: if( empty( self::$_default_language ) ) self::get_default();
243: if( is_object( $lang ) ) $lang = $lang->id;
244:
245: if( $lang == 0 ) return self::$_default_language->cml_language_slug;
246:
247: return @self::$_all_languages[ $lang ]->cml_language_slug;
248: }
249:
250: 251: 252: 253: 254: 255: 256:
257: public static function get_by_id( $id ) {
258: if( empty( self::$_all_languages ) ) self::get_all();
259: if( ! is_numeric( $id ) ) $id = CMLLanguage::get_by_slug( $id );
260:
261: return self::$_all_languages[ $id ];
262: }
263:
264: 265: 266: 267: 268: 269: 270: 271: 272:
273: public static function get_by_slug( $slug, $empty = false ) {
274: if( empty( self::$_all_languages ) ) self::get_all();
275:
276: foreach( self::$_all_languages as $lang ) {
277: if( $lang->cml_language_slug == $slug ) return $lang;
278: }
279:
280: return ( ! $empty ) ? self::get_default() : array();
281: }
282:
283: 284: 285: 286: 287: 288: 289:
290: public static function get_id_by_slug( $slug ) {
291: $lang = self::get_by_slug( $slug );
292:
293: return $lang->id;
294: }
295:
296: 297: 298: 299: 300: 301: 302:
303: public static function get_id_by_locale( $locale ) {
304: $langs = self::get_all();
305:
306: foreach( $langs as $lang ) {
307: if( $lang->cml_locale == $locale ) return $lang;
308: }
309:
310: return null;
311: }
312:
313: 314: 315: 316: 317: 318: 319: 320:
321: public static function get_flag( $lang = null, $size = "small" ) {
322: if( empty( $lang ) ) $lang = self::default_language_id();
323: if( ! is_numeric( $lang ) ) $lang = self::get_id_by_slug( $lang );
324:
325: if( empty( self::$_all_languages ) ) self::get_all();
326:
327: return self::$_all_languages[ $lang ]->cml_flag;
328: }
329:
330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340:
341: public static function get_flag_src( $lang = null, $size = CML_FLAG_TINY ) {
342: if( empty( self::$_all_languages ) ) self::get_all();
343: if( empty( self::$_default_language ) ) self::get_default();
344: if( $lang == null ) $lang = self::get_default_id();
345: if( ! is_numeric( $lang ) ) $lang = self::get_id_by_slug( $lang );
346:
347: $lang = self::$_all_languages[ $lang ];
348: $flag = $lang->cml_flag;
349:
350: if( $lang->cml_custom_flag == 1 && file_exists( CML_UPLOAD_DIR . "/$size/$flag.png" ) )
351: $url = CML_UPLOAD_URL . "/$size/$flag.png";
352: else
353: $url = CML_PLUGIN_URL . "flags/$size/$flag.png";
354:
355: return esc_url( $url );
356: }
357:
358: 359: 360: 361: 362: 363: 364: 365:
366: public static function get_flag_img( $lang, $size = CML_FLAG_TINY ) {
367: $url = self::get_flag_src( $lang, $size );
368: $name = self::get_name( $lang );
369: $slug = self::get_slug( $lang );
370:
371: return "<img src='$url' border='0' alt='$slug' title='$name' />";
372: }
373:
374: 375: 376: 377: 378:
379: public static function reload() {
380: self::$_all_languages = null;
381: }
382:
383: 384: 385: 386: 387: 388: 389:
390: public static function get_by_post_id( $post_id ) {
391: return CMLPost::get_language_by_id( $post_id );
392: }
393:
394: 395: 396: 397: 398: 399: 400:
401: public static function get_id_by_post_id( $post_id ) {
402: return CMLPost::get_language_id_by_id( $post_id );
403: }
404:
405: 406: 407: 408: 409: 410: 411:
412: public static function is_default( $lang = null) {
413: if( null == $lang ) {
414: $lang = CMLLanguage::get_current_id();
415: } else {
416: if( ! is_numeric( $lang ) ) {
417: $lang = CMLLanguage::get_id_by_slug( $lang );
418: }
419: }
420:
421: return $lang == CMLLanguage::get_default_id();
422: }
423:
424: 425: 426: 427: 428: 429: 430:
431: public static function is_current( $lang ) {
432: if( null == $lang ) {
433: $lang = CMLLanguage::get_current_id();
434: } else {
435: if( ! is_numeric( $lang ) ) {
436: $lang = CMLLanguage::get_id_by_slug( $lang );
437: }
438: }
439:
440: return $lang == CMLLanguage::get_current_id();
441: }
442:
443: 444: 445: 446: 447: 448: 449: 450: 451:
452: public static function set_current( $lang ) {
453: $id = is_object( $lang ) ? $lang->id : $lang;
454:
455: self::$_current_id = $id;
456: }
457: }
458:
459: 460: 461:
462: class CMLTranslations {
463:
464:
465: private static $_translations = array();
466:
467: private static $_keys = array();
468:
469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485:
486: public static function add( $key, $default, $group ) {
487: global $wpdb;
488:
489: $default = bin2hex( $default );
490: foreach( CMLLanguage::get_all() as $lang ) {
491: $query = sprintf( "SELECT id FROM %s WHERE cml_text = '%s' AND cml_lang_id = %d AND cml_type = '%s'",
492: CECEPPA_ML_TRANSLATIONS,
493: bin2hex( strtolower( $key ) ),
494: $lang->id,
495: $group );
496:
497: $record = $wpdb->get_var( $query );
498: if( empty( $record ) ) {
499: $wpdb->insert( CECEPPA_ML_TRANSLATIONS,
500: array(
501: 'cml_text' => bin2hex( strtolower( $key ) ),
502: 'cml_lang_id' => $lang->id,
503: 'cml_translation' => $default,
504: 'cml_type' => $group
505: ),
506: array(
507: '%s', '%d', '%s', '%s',
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: public static function set( $lang, $original, $translated, $type ) {
542: global $wpdb;
543:
544: if( ! is_numeric( $lang ) ) $lang = CMLLanguage::get_id_by_slug( $lang );
545: $original = trim( $original );
546:
547: $wpdb->delete( CECEPPA_ML_TRANSLATIONS,
548: array( "cml_text" => bin2hex( $original ),
549: "cml_type" => $type,
550: "cml_lang_id" => $lang ),
551: array( "%s", "%s", "%d" ) );
552:
553: return $wpdb->insert( CECEPPA_ML_TRANSLATIONS,
554: array( 'cml_text' => bin2hex( $original ),
555: 'cml_lang_id' => $lang,
556: 'cml_translation' => bin2hex( $translated ),
557: 'cml_type' => $type ),
558: array( '%s', '%d', '%s', '%s' ) );
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: public static function get( $lang, $string, $type = "", $return_empty = false, $ignore_po = false ) {
589: global $wpdb;
590:
591: if( "_" == $type[ 0 ] && ! $return_empty ) {
592: $return_empty = true;
593: }
594:
595: $string = trim( $string );
596:
597:
598: $s = ( $type == "C" ) ? strtolower( $string ) : $string;
599:
600:
601: if( isset( self::$_keys[ $lang ] ) && in_array( sanitize_title( $s ), self::$_keys[ $lang ] ) ) {
602: $index = array_search( sanitize_title( $s ), self::$_keys[ $lang ] );
603: return self::$_translations[ $lang ][ $index ];
604: }
605:
606: if( CML_GET_TRANSLATIONS_FROM_PO &&
607: ! $ignore_po &&
608: 1 == CMLUtils::_get( '_po_loaded' ) ) {
609:
610: $ret = __( $s, 'cmltrans' );
611: }
612:
613: if( ! is_numeric( $lang ) ) {
614: $lang = CMLLanguage::get_id_by_slug( $lang );
615: }
616:
617: $query = sprintf(" SELECT UNHEX(cml_translation) FROM %s WHERE cml_text = '%s' AND cml_lang_id = %d AND cml_type LIKE '%s'",
618: CECEPPA_ML_TRANSLATIONS, bin2hex( $s ), $lang, "%" . $type . "%" );
619:
620: $return = $wpdb->get_var( $query );
621:
622: if( $return_empty && empty( $return ) ) return "";
623:
624: $return = ( empty( $return ) ) ? $string : html_entity_decode( stripslashes( $return ) );
625:
626: self::$_translations[$lang][] = $return;
627: self::$_keys[ $lang ][] = sanitize_title( $string );
628:
629: return $return;
630: }
631:
632: 633: 634: 635: 636:
637: public static function gettext( $lang, $string, $type, $ignore_po = false ) {
638: $ret = self::get( $lang, $string, $type, true, $ignore_po );
639:
640: if( ! empty( $ret ) ) return $ret;
641:
642:
643: require_once( CML_PLUGIN_PATH . "gettext/gettext.inc" );
644:
645: $locale = CMLLanguage::get_current()->cml_locale;
646:
647:
648: T_setlocale( LC_MESSAGES, $locale );
649:
650:
651: $domain = $locale;
652: T_bindtextdomain( $domain, CML_WP_LOCALE_DIR );
653: T_bind_textdomain_codeset( $domain, 'UTF-8' );
654: T_textdomain( $domain );
655:
656: $ret = T_gettext( $string );
657:
658: return ( empty( $ret ) ) ? $string : html_entity_decode( stripslashes( $ret ) );
659: }
660:
661: 662: 663: 664: 665:
666: public static function delete( $type ) {
667: global $wpdb;
668:
669: $wpdb->delete( CECEPPA_ML_TRANSLATIONS,
670: array( "cml_type" => $type ),
671: array( "%s" ) );
672: }
673: }
674:
675: 676: 677: 678:
679: class CMLPost {
680:
681: private static $_indexes = null;
682:
683: private static $_posts_meta = array();
684:
685: 686: 687: 688: 689: 690: 691: 692: 693:
694: public static function get_language_by_id( $post_id ) {
695: if( empty( self::$_indexes ) ) self::_load_indexes();
696:
697: 698: 699: 700: 701:
702: if( @in_array( $post_id, self::$_indexes[ CMLLanguage::get_current_id() ] ) ) {
703: return CMLLanguage::get_current();
704: }
705:
706: foreach( CMLLanguage::get_others() as $lang ) {
707: if( @in_array( $post_id, self::$_indexes[ $lang->id ] ) )
708: return $lang;
709: }
710:
711: return null;
712: }
713:
714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725:
726: public static function get_language_id_by_id( $post_id, $meta = false ) {
727: if( $meta ) {
728:
729: $meta = get_post_meta( $post_id, "_cml_meta", true );
730:
731: if( ! empty( $meta ) && isset( $meta[ "lang" ] ) ) {
732: return $meta[ "lang"];
733: }
734: }
735:
736: $lang = self::get_language_by_id( $post_id );
737:
738: if( null === $lang ) {
739:
740: $meta = get_post_meta( $post_id, "_cml_meta", true );
741:
742: if( empty( $meta ) ) {
743: $lang = self::get_language_by_id( $post_id );
744:
745: if( is_object( $lang ) ) {
746:
747: self::update_meta( $lang->id, $post_id );
748: }
749:
750: return is_object( $lang ) ? $lang->id : 0;
751: } else {
752: return @$meta[ "lang" ];
753: }
754: }
755:
756: return $lang->id;
757: }
758:
759: 760: 761: 762: 763: 764: 765:
766: public static function get_language_slug_by_id( $post_id ) {
767: $lang = self::get_language_by_id( $post_id );
768:
769: return is_object( $lang ) ? $lang->cml_language_slug : "";
770: }
771:
772: 773: 774: 775: 776: 777: 778: 779: 780: 781:
782: public static function get_translation( $lang, $post_id ) {
783: global $wpdb;
784:
785: if( is_numeric( $lang ) ) $lang = CMLLanguage::get_slug( $lang );
786: if( empty( $post_id ) ) return 0;
787:
788:
789:
790:
791:
792: $linked = self::get_translations( $post_id );
793:
794: return ( ! array_key_exists( $lang, $linked) ) ? 0 : $linked[ $lang ];
795: }
796:
797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852:
853: public static function get_translations( $post_id, $force = false ) {
854: global $wpdb;
855:
856: if( empty( $post_id ) ) return array();
857:
858: if( ! isset( self::$_posts_meta[ $post_id ] ) || $force ) {
859:
860: if( empty( $GLOBALS[ '_cml_language_columns' ] ) ) {
861: require_once ( CML_PLUGIN_ADMIN_PATH . 'admin-settings-gen.php' );
862:
863: cml_generate_lang_columns();
864: }
865:
866: $_cml_language_columns = & $GLOBALS[ '_cml_language_columns' ];
867: $_conv = & $GLOBALS[ '_cml_language_keys' ];
868:
869: $query = "SELECT ";
870: foreach( $_conv as $key => $label ) {
871: $select[] = "$key as $label";
872: }
873: $query .= join( ",", $select ) . " FROM " . CECEPPA_ML_RELATIONS . " WHERE ";
874: foreach( $_cml_language_columns as $l ) {
875: $where[] = "$l = $post_id";
876: }
877: $query .= join( " OR ", $where );
878:
879: $row = $wpdb->get_row( $query, ARRAY_A );
880: unset( $row[ "id" ] );
881:
882: $keys = @array_filter( $row );
883: $keys = @array_replace( $keys, $_conv );
884: $others = @array_filter( is_array( $row ) ? $row : array() );
885: unset( $others[ CMLPost::get_language_slug_by_id( $post_id ) ] );
886:
887: $row = @array_merge( (array) $row, array( "indexes" => array_filter( $row ),
888: "linked" => $others ) );
889:
890: self::$_posts_meta[ $post_id ] = $row;
891: } else {
892: $row = self::$_posts_meta[ $post_id ];
893: }
894:
895: return $row;
896: }
897:
898: 899: 900: 901: 902: 903: 904: 905:
906: public static function set_language( $lang, $post_id ) {
907: if( ! is_numeric( $lang ) ) $lang = CMLLanguage::get_id_by_slug( $lang );
908:
909: cml_migrate_database_add_item( $lang, $post_id, 0, 0 );
910: }
911:
912: 913: 914: 915: 916: 917: 918: 919: 920: 921: 922: 923: 924: 925: 926:
927: public static function set_translation( $post_id, $linked_lang, $linked_post, $post_lang = null ) {
928: self::set_translations( $post_id, array( $linked_lang => $linked_post ), $post_lang );
929: }
930:
931: 932: 933: 934: 935: 936: 937: 938: 939: 940: 941: 942: 943: 944: 945: 946: 947: 948:
949: public static function set_translations( $post_id, $translations, $post_lang = null ) {
950:
951: if( null === $post_lang ) $post_lang = CMLPost::get_language_id_by_id( $post_id );
952:
953: foreach( $translations as $key => $id ) {
954: if( ! is_numeric( $key ) ) $key = CMLLanguage::get_by_slug( $key );
955:
956: cml_migrate_database_add_item( $post_lang, $post_id, $key, $id );
957: }
958:
959:
960: cml_fix_rebuild_posts_info();
961: self::_load_indexes();
962:
963: self::update_meta( $post_lang, $post_id );
964: }
965:
966: 967: 968: 969:
970: public static function update_meta( $lang, $post_id ) {
971: 972: 973:
974:
975: $meta = array( "lang" => $lang,
976: "translations" => CMLPost::get_translations( $post_id, true ) );
977:
978: update_post_meta( $post_id, "_cml_meta", $meta );
979: }
980:
981: 982: 983: 984: 985: 986: 987: 988:
989: public static function get_posts_by_language( $lang = null ) {
990: if( empty( $lang ) ) $lang = CMLLanguage::get_current_id();
991: if( ! is_numeric( $lang ) ) $lang = CMLLanguage::get_id_by_slug( $lang );
992:
993:
994: if( empty( self::$_indexes ) ) self::_load_indexes();
995:
996: $posts = @self::$_indexes[ $lang ];
997:
998: return ! empty( $posts ) ? array_unique( $posts ) : array();
999: }
1000:
1001: 1002: 1003: 1004:
1005: public static function _update_posts_by_language( $lang, $ids ) {
1006: self::$_indexes[ $lang ] = $ids;
1007: }
1008:
1009: 1010: 1011: 1012: 1013: 1014: 1015:
1016: public static function get_posts_by_languages() {
1017:
1018: if( empty( self::$_indexes ) ) self::_load_indexes();
1019:
1020: return self::$_indexes;
1021: }
1022:
1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030:
1031: public static function has_translation( $lang, $post_id ) {
1032: if( ! isset( self::$_posts_meta[ $post_id ] ) ) {
1033: self::$_posts_meta[ $post_id ] = self::get_translations( $post_id );
1034: }
1035:
1036: if( is_numeric( $lang ) ) $lang = CMLLanguage::get_slug( $lang );
1037:
1038: return ( isset( self::$_posts_meta[ $post_id ]['indexes'] ) &&
1039: self::$_posts_meta[ $post_id ]['indexes'][ $lang ] > 0 );
1040: }
1041:
1042: 1043: 1044: 1045: 1046: 1047: 1048:
1049: public static function has_translations( $post_id ) {
1050: if( ! isset( self::$_posts_meta[ $post_id ] ) ) {
1051: self::$_posts_meta[ $post_id ] = self::get_translations( $post_id );
1052: }
1053:
1054: return ! empty( self::$_posts_meta[ $post_id ][ 'linked' ] );
1055: }
1056:
1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064:
1065: public static function is_translation( $post1, $post2 ) {
1066: $translations = CMLPost::get_translations( $post1 );
1067:
1068: return in_array( $post2, $translations[ 'indexes' ] );
1069: }
1070:
1071:
1072: private static function _load_indexes() {
1073: $langs = cml_get_languages( false );
1074:
1075: foreach($langs as $lang) {
1076: self::$_indexes[ $lang->id ] = get_option( "cml_posts_of_lang_" . $lang->id );
1077: }
1078: }
1079:
1080: 1081: 1082: 1083: 1084: 1085:
1086: public static function ( $permalink, $post ) {
1087: global $wpdb;
1088:
1089: $removed = false;
1090:
1091: if( is_object( $post ) && CMLPost::has_translations( $post->ID ) ) {
1092:
1093: $url = untrailingslashit( $permalink );
1094: $url = str_replace( CMLUtils::home_url(), "", $url );
1095:
1096: 1097: 1098:
1099: preg_match_all( "/-\d+/", $url, $out );
1100:
1101: 1102: 1103:
1104: if( count( $out[ 0 ] ) > 0 ) {
1105: 1106: 1107:
1108: $post_title = $post->post_title;
1109:
1110:
1111:
1112: 1113: 1114:
1115: preg_match_all( "/\d+/", $post_title, $pout );
1116:
1117: 1118: 1119: 1120:
1121:
1122: if( count( $pout[0] ) < count( $out[ 0 ] ) ) {
1123: $permalink = trailingslashit( preg_replace( "/-\d*$/", "",
1124: untrailingslashit( $permalink ) ) );
1125:
1126: $removed = true;
1127: }
1128: }
1129:
1130: if( $removed &&
1131: CMLUtils::get_url_mode() == PRE_NONE ) {
1132: $post_id = is_object( $post ) ? $post->ID : $post;
1133:
1134: $lang = CMLLanguage::get_by_post_id( $post_id );
1135: if( empty( $lang ) ) {
1136: $lang = CMLLanguage::get_current();
1137: }
1138:
1139: $permalink = add_query_arg( array(
1140: "lang" => $lang->cml_language_slug,
1141: ), $permalink );
1142: }
1143: }
1144:
1145: return $permalink;
1146: }
1147: }
1148:
1149: 1150: 1151: 1152:
1153: class CMLUtils {
1154:
1155: private static $_permalink_structure;
1156:
1157: private static $_url_mode;
1158:
1159: private static $_date_format;
1160:
1161: private static $_home_url;
1162:
1163: private static $_clean_url;
1164:
1165: private static $_clean_request;
1166:
1167: private static $_request_url;
1168:
1169: private static $_clean_applied = false;
1170:
1171: private static $_url;
1172:
1173: private static $_language_detected = null;
1174:
1175: private static $_vars = array();
1176:
1177: 1178: 1179: 1180: 1181: 1182: 1183:
1184: public static function get_permalink_structure() {
1185: if( ! isset( self::$_permalink_structure ) ) self::$_permalink_structure = get_option( "permalink_structure" );
1186:
1187: return self::$_permalink_structure;
1188: }
1189:
1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198:
1199: public static function get_home_url( $slug = null ) {
1200: $_cml_settings = & $GLOBALS[ '_cml_settings' ];
1201:
1202: if( null === $slug ) $slug = CMLLanguage::get_default()->cml_language_slug;
1203:
1204: switch( CMLUtils::get_url_mode() ) {
1205: case PRE_PATH:
1206: if( $slug == CMLLanguage::get_default()->cml_language_slug &&
1207: $_cml_settings[ 'url_mode_remove_default' ] ) {
1208: $slug = "";
1209: } else
1210: $slug = "/$slug";
1211:
1212: $link = CMLUtils::home_url() . $slug;
1213: break;
1214: case PRE_DOMAIN:
1215: $link = CMLUtils::home_url();
1216: break;
1217: default:
1218: $link = CMLUtils::home_url() . "?lang=$slug";
1219: break;
1220: }
1221:
1222: return $link;
1223: }
1224:
1225: 1226: 1227:
1228: public static function get_url_mode() {
1229: if( empty( self::$_url_mode ) ) {
1230: global $_cml_settings;
1231:
1232: self::$_url_mode = $_cml_settings[ 'url_mode' ];
1233:
1234: $permalink = self::get_permalink_structure();
1235: if( empty( $permalink ) && self::$_url_mode == PRE_PATH )
1236: self::$_url_mode = PRE_LANG;
1237: }
1238:
1239: return self::$_url_mode;
1240: }
1241:
1242: 1243: 1244: 1245:
1246: public static function get_date_format() {
1247: if( empty( self::$_date_format ) ) self::$_date_format = get_option( 'date_format' );
1248:
1249: return self::$_date_format;
1250: }
1251:
1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270:
1271: public static function home_url() {
1272: if( empty( self::$_home_url ) ) {
1273: $GLOBALS[ '_cml_no_translate_home_url' ] = true;
1274: self::$_home_url = home_url();
1275:
1276: unset( $GLOBALS[ '_cml_no_translate_home_url' ] );
1277: }
1278:
1279: return self::$_home_url;
1280: }
1281:
1282: 1283: 1284: 1285: 1286:
1287: public static function clear_url( $url = null ) {
1288: global $wp_rewrite;
1289:
1290: if( self::get_url_mode() != PRE_PATH
1291: || ( self::$_clean_applied == true &&
1292: null === $url ) ) {
1293: return self::$_language_detected;
1294: }
1295:
1296: if( empty( self::$_url ) ) {
1297: self::$_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
1298: self::$_request_url = str_replace( trailingslashit( self::home_url() ),
1299: "", self::$_url );
1300: }
1301:
1302: if( null === $url ) {
1303: $_url = self::$_url;
1304: $request_url = self::$_request_url;
1305:
1306: 1307: 1308:
1309: self::$_clean_url = preg_replace( "/\?.*/", "", $_url );
1310: self::$_clean_request = $request_url;
1311: } else {
1312: $_url = $url;
1313: $request_url = str_replace( trailingslashit( self::home_url() ),
1314: "", $url );
1315: }
1316:
1317: $_url = $request_url;
1318: $base_url = str_replace( "http://" . $_SERVER['HTTP_HOST'], "", get_option( 'home' ) );
1319:
1320: if( preg_match( "#^([a-z]{2})(/.*)?$#i", $_url, $match ) ) {
1321: $lang = CMLLanguage::get_id_by_slug( $match[1] );
1322: if( empty( $lang ) ) {
1323: return $url;
1324: }
1325:
1326: $_url = substr( $_url, 3 );
1327: $_url = preg_replace( "/\?.*/", "", $_url );
1328:
1329: if( null === $url ) {
1330: self::$_language_detected = $lang;
1331:
1332: CMLLanguage::set_current( self::$_language_detected );
1333:
1334: self::$_clean_url = trailingslashit( self::$_home_url ) . $_url;
1335: self::$_clean_request = trailingslashit( $base_url ) . $_url;
1336: } else {
1337: $_url = trailingslashit( CMLUtils::home_url() ) . $_url;
1338: }
1339: }
1340:
1341: if( null === $url ) {
1342: self::$_clean_applied = true;
1343: }
1344:
1345: return ( null === $url ) ? self::$_language_detected : $_url;
1346: }
1347:
1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360:
1361: public static function get_clean_url() {
1362: return self::$_clean_url;
1363: }
1364:
1365: 1366: 1367: 1368: 1369:
1370: public static function get_clean_request() {
1371: return self::$_clean_request;
1372: }
1373:
1374: 1375: 1376:
1377: public static function _set( $key, $value ) {
1378: self::$_vars[ $key ] = $value;
1379: }
1380:
1381: 1382: 1383:
1384: public static function _get( $key ) {
1385: return isset( self::$_vars[ $key ] ) ? self::$_vars[ $key ] : null;
1386: }
1387:
1388: 1389: 1390:
1391: public static function _del( $key ) {
1392: unset( self::$_vars[ $key ] );
1393: }
1394:
1395: 1396: 1397:
1398: public static function _append( $key, $value ) {
1399: if( ! isset( self::$_vars[ $key ] ) ) {
1400: self::$_vars[ $key ] = array();
1401: }
1402:
1403: self::$_vars[ $key ][] = $value;
1404: }
1405: }
1406: ?>