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