1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: function cml_is_homepage( $url = null ) {
12: global $wpdb;
13:
14: if( ! empty( $wpdb ) && method_exists( $wpdb, 'is_category' ) ) {
15: if( is_category() || is_archive() ) return false;
16: }
17:
18:
19: if( cml_use_static_page() ) {
20: global $wp_query;
21: $static_id = array( get_option( "page_for_posts" ), get_option( "page_on_front" ) );
22:
23: $lang_id = CMLLanguage::get_current_id();
24:
25: 26: 27: 28: 29: 30: 31:
32: 33: 34: 35: 36:
37: if( ! isset( $GLOBALS[ '_cml_get_queried_object_id' ] ) ) {
38: if( ! empty( $wpdb ) && method_exists( $wpdb, 'get_queried_object' ) ) {
39: $GLOBALS[ '_cml_get_queried_object_id' ] = get_queried_object()->ID;
40: $GLOBALS[ '_cml_get_queried_object' ] = get_queried_object();
41:
42: $the_id = & $GLOBALS[ '_cml_get_queried_object_id' ];
43: } else {
44: if( is_object( get_post() ) ) {
45: $the_id = get_the_ID();
46:
47: $GLOBALS[ '_cml_get_queried_object_id' ] = $the_id;
48: }
49: }
50: } else {
51: $the_id = $GLOBALS[ '_cml_get_queried_object_id' ];
52: }
53:
54: if( ! empty( $the_id ) ) {
55: if( in_array( $the_id, $static_id ) ) return true;
56:
57:
58: $linked = CMLPost::get_translation( CMLLanguage::get_current_id(), $the_id );
59: if( ! empty( $linked ) ) {
60: return in_array( $linked, $static_id );
61: }
62: }
63: }
64:
65:
66:
67: if( ! empty( $wpdb ) && method_exists( $wpdb, 'is_home' ) ) {
68: return is_home();
69: }
70:
71:
72: CMLUtils::clear_url();
73:
74: return trailingslashit( CMLUtils::get_clean_url() ) == trailingslashit( CMLUtils::home_url() );
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85: 86:
87: function cml_get_page_id_by_path($url, $types = null) {
88: $url = untrailingslashit( $url );
89: $plinks = explode( "/", $url );
90:
91: if( $types == null ) {
92: $types = array_keys( get_post_types() );
93: }
94:
95: $p = cml_get_page_by_path( $url, OBJECT, $types );
96: $the_id = is_object( $p ) ? $p->ID : 0;
97:
98: return $the_id;
99: }
100:
101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115:
116: function cml_get_page_by_path($page_path, $output = OBJECT, $post_type = array('page')) {
117: global $wpdb;
118:
119: $page_path = rawurlencode(urldecode($page_path));
120: $page_path = str_replace('%2F', '/', $page_path);
121: $page_path = str_replace('%20', ' ', $page_path);
122: $parts = explode( '/', trim( $page_path, '/' ) );
123: $parts = array_map( 'esc_sql', $parts );
124: $parts = array_map( 'sanitize_title_for_query', $parts );
125:
126: $in_string = "'". implode( "','", $parts ) . "'";
127: $post_type_sql = implode( "','", $post_type );
128:
129:
130: if( empty( $in_string ) ) {
131: return;
132: }
133:
134: $query = "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type IN ( '$post_type_sql' ) ) AND post_status = 'publish'";
135: $pages = $wpdb->get_results( $query, OBJECT_K );
136: $revparts = array_reverse( $parts );
137:
138: $foundid = 0;
139: foreach ( (array) $pages as $page ) {
140: if ( $page->post_name == $revparts[0] ) {
141: $count = 0;
142: $p = $page;
143:
144: while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {
145: $count++;
146: $parent = $pages[ $p->post_parent ];
147: if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] )
148: break;
149: $p = $parent;
150: }
151:
152:
153: if ( $p->post_parent == 0 && $p->post_name == $revparts[ $count ] ) {
154: $foundid = $page->ID;
155: if ( $page->post_type == $post_type )
156: break;
157: }
158: }
159: }
160:
161: if ( $foundid )
162: return get_post( $foundid, $output );
163:
164: return null;
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177:
178: function cml_get_the_link( $result, $linked = true, $only_existings = false, $queried = false ) {
179: global $wpCeceppaML, $_cml_settings;
180:
181: if( defined( 'CML_DEBUG' ) && isset( $_GET[ 'cdb' ] ) ) {
182: echo "queried: $queried<br />";
183: echo "is_home: " . cml_is_homepage() . "<br />";
184: }
185: if( $queried && ( cml_is_homepage() || is_search() ) ) {
186:
187: $link = CMLUtils::get_home_url( $result->cml_language_slug );
188:
189: if( is_search() ) {
190: if( isset( $_GET[ 's' ] ) ) {
191: $args[ 's' ] = esc_attr( $_GET[ 's' ] );
192: }
193:
194: if( CMLUtils::get_url_mode() <= PRE_LANG ) {
195: $args[ 'lang' ] = $result->cml_language_slug;
196: }
197:
198: $link = add_query_arg( $args, trailingslashit( $link ) );
199: }
200: } else {
201: $GLOBALS[ '_cml_force_home_slug' ] = $result->cml_language_slug;
202:
203:
204: $wpCeceppaML->force_category_lang( $result->id );
205:
206: if( $queried ) {
207: if( empty( $GLOBALS[ '_cml_get_queried_object' ] ) ) {
208: $GLOBALS[ '_cml_get_queried_object' ] = get_queried_object();
209: }
210: $q = & $GLOBALS[ '_cml_get_queried_object' ];
211:
212: $is_category = isset( $q->taxonomy ) && "category" == $q->taxonomy;
213: $is_single = isset( $q->ID );
214: $is_page = $is_single;
215: $the_id = ( $is_single ) ? $q->ID : 0;
216: $is_tag = isset( $q->taxonomy ) && "post_tag" == $q->taxonomy;
217:
218: if( empty( $q ) ) {
219: $is_404 = is_404();
220: }
221: } else {
222: $is_category = is_category();
223: $is_single = is_single();
224: $is_page = is_page();
225: $the_id = get_the_ID();
226: $is_404 = is_404();
227: $is_tag = is_tag();
228: }
229:
230:
231: $link = "";
232:
233: if( ! in_the_loop() ) {
234: $lang_id = CMLLanguage::get_current_id();
235: } else
236: $lang_id = CMLLanguage::get_id_by_post_id( $the_id );
237:
238: 239: 240: 241: 242:
243: if( ( ( $is_single || $is_page ) || $linked ) && ! $is_category ) {
244: $linked_id = CMLPost::get_translation( $result->id, $the_id );
245:
246: if( ! empty( $linked_id ) ) {
247: $link = get_permalink( $linked_id );
248: $link = CMLPost::remove_extra_number( $link, get_post( $linked_id ) );
249:
250: if( CMLUtils::_get( '_real_language' ) != CMLLanguage::get_current_id()
251: && $linked_id == $the_id ) {
252:
253: if( CMLUtils::get_url_mode() == PRE_PATH ) {
254: $link = $wpCeceppaML->convert_url( $link, $result->cml_language_slug );
255: }
256: }
257: }
258: }
259:
260: if( is_archive() && ! $is_category && ! is_post_type_archive() ) {
261: global $wp;
262:
263: $link = trailingslashit( home_url( $wp->request ) );
264:
265: if( CMLUtils::get_url_mode() == PRE_NONE ||
266: CMLUtils::get_url_mode() == PRE_LANG ) {
267: $link = add_query_arg( array( "lang" => $result->cml_language_slug ), $link );
268: }
269: }
270:
271:
272: if( $is_category ) {
273: if( $queried && isset( $q->term_id ) ) {
274: $cat = array( "term_id" => $q->term_id );
275: } else {
276: $cat = get_the_category();
277: }
278:
279: if( is_array( $cat ) ) {
280: $cat_id = ( isset( $cat[ 'term_id' ] ) ) ? $cat[ 'term_id' ] : ( $cat[ count($cat) - 1 ]->term_id );
281:
282:
283: CMLUtils::_set( '_force_category_lang', $result->id );
284: $link = get_category_link( $cat_id );
285:
286: CMLUtils::_del( '_force_category_lang' );
287: }
288:
289: if( CMLUtils::get_category_url_mode() == PRE_LANG &&
290: CMLUtils::get_url_mode() == PRE_NONE ) {
291: $link = add_query_arg( array( 'lang' => $result->cml_language_slug ), $link );
292: }
293: }
294:
295: if( $queried && $is_tag ) {
296: if( ! empty( $q ) ) {
297: $term_id = $q->term_id;
298: } else {
299: $term_id = CMLUtils::_get( "_reverted" );
300: }
301:
302: if( ! empty( $term_id ) ) {
303: CMLUtils::_set( '_force_category_lang', $result->id );
304:
305: $link = get_tag_link( $term_id );
306:
307: CMLUtils::_del( '_force_category_lang' );
308: }
309: }
310:
311: if( is_paged() ) {
312: $link = add_query_arg( array( "lang" => $result->cml_language_slug ) );
313: }
314:
315: unset( $GLOBALS[ '_cml_force_home_slug' ] );
316: $wpCeceppaML->unset_category_lang();
317:
318: 319: 320: 321: 322: 323:
324: if( empty( $link ) && ! $only_existings ) {
325: if( 1 === CMLUtils::_get( "is_crawler" ) ) {
326: return;
327: }
328:
329:
330: if( $_cml_settings[ 'cml_force_languge' ] == 1 ) {
331: 332: 333:
334: if( is_single() || is_page() ) {
335: $l = cml_get_linked_post( $the_id, CMLLanguage::get_default_id() );
336:
337: }
338:
339: 340: 341: 342:
343: $link = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
344: if( CMLPost::get_language_by_id( $the_id ) != $result->id ) {
345: $link = add_query_arg( array( "lang" => $result->cml_language_slug ), $link );
346: }
347: } else {
348: $link = CMLUtils::get_home_url( $result->cml_language_slug );
349: }
350:
351: if( ( $is_tag || ( isset( $is_404 ) && $is_404 ) ) && CMLUtils::get_url_mode() > PRE_LANG ) {
352: $clean = CMLUtils::get_clean_url();
353: $url = CMLUtils::home_url();
354:
355:
356: $link = str_replace( $url, "", $clean );
357: $link = CMLUtils::get_home_url( $result->cml_language_slug ) . $link;
358: }
359: }
360: }
361:
362: return $link;
363: }
364:
365: 366: 367: 368: 369: 370:
371: function cml_use_static_page() {
372: return (get_option("page_for_posts") > 0) ||
373: (get_option("page_on_front") > 0);
374: }
375:
376: 377: 378: 379: 380:
381: function cml_get_browser_lang() {
382: if( isset( $GLOBALS[ '_browser_lang' ] ) ) return $GLOBALS[ '_browser_lang' ];
383:
384: global $wpdb;
385:
386: $browser_langs = @explode( ";", $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
387: $lang = null;
388:
389:
390: foreach( $browser_langs as $blang ) {
391: @list( $code1, $code2 ) = explode( ",", $blang );
392:
393: $locale[] = str_replace( "-", "_", $code1 );
394: $locale[] = str_replace( "-", "_", $code2 );
395:
396:
397:
398:
399: $i = 0;
400: while( empty( $lang ) && $i < count( $locale ) ) {
401: $l = $locale[$i];
402:
403: if( strlen( $l ) > 2 ) {
404: $lang = CMLLanguage::get_id_by_locale( $l );
405: } else {
406:
407: $lang = CMLLanguage::get_id_by_slug( $l );
408: }
409:
410: $i++;
411: }
412:
413: if( ! empty ($lang ) ) {
414: break;
415: }
416: }
417:
418: $GLOBALS[ '_browser_lang' ] = $lang;
419:
420: return $lang;
421: }
422:
423: 424: 425: 426: 427: 428: 429:
430: function cml_get_notice( $lang ) {
431: global $wpdb, $wpCeceppaML;
432:
433:
434: $type = ( is_single() ) ? "post" : "page";
435:
436: $r = CMLTranslations::get( $lang,
437: "_notice_$type",
438: "N", true, true );
439:
440: if( ! empty( $r ) )
441: return $r;
442: else
443: CMLLanguage::get_current()->cml_language;
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: function cml_show_flags( $args ) {
499: global $wpdb;
500:
501: $args = extract( shortcode_atts( array(
502: "show" => "text",
503: "size" => "tiny",
504: "class" => "",
505: "image_class" => "",
506: "echo" => true,
507: "linked" => true,
508: "only_existings" => false,
509: "sort" => false,
510: "queried" => false,
511: "show_flag" => true,
512: ), $args ) );
513:
514: $_cml_settings = & $GLOBALS[ '_cml_settings' ];
515: $redirect = $_cml_settings[ 'cml_option_redirect' ];
516:
517: $results = cml_get_languages( true, false );
518: $width = ( $size == "tiny" ) ? 16 : 32;
519:
520: $r = "<ul class=\"cml_flags $class\">";
521:
522:
523: $lang_id = ( ! $sort ) ? -1 : CMLPost::get_language_by_id( get_the_ID() );
524: $items = array();
525:
526: foreach($results as $result) {
527: $lang = ( $show == "text" ) ? $result->cml_language : "";
528: $lang = ( $show == "slug" ) ? $result->cml_language_slug : $lang;
529:
530: $link = cml_get_the_link( $result, $linked, $only_existings, $queried );
531: if( empty( $link) ) continue;
532:
533: if( $show_flag ) {
534: $img = "<img class=\"$size $image_class\" src='" . cml_get_flag_by_lang_id( $result->id, $size ) . "' title='$result->cml_language' width=\"$width\"/>";
535: } else {
536: $img = "";
537: }
538:
539: $class = ( $result->id == CMLLanguage::get_current_id() ) ? "current" : "";
540: $li = "<li class=\"$class\"><a href=\"$link\">{$img}{$lang}</a></li>";
541: if( $sort && is_array( $items ) && $result->id == $lang_id ) {
542: array_unshift( $items, $li );
543: } else {
544: $items[] = $li;
545: }
546:
547: }
548:
549:
550: $r .= join( "\n", $items );
551: $r .= "</ul>";
552:
553: if( $echo )
554: echo $r;
555: else
556: return $r;
557: }
558:
559: 560: 561: 562: 563:
564: function cml_is_custom_post_type() {
565: $types = get_post_types( array ( '_builtin' => FALSE ), 'names' );
566:
567: if( empty( $types) ) return FALSE;
568:
569: $name = get_post_type();
570: return in_array( $name, $types );
571: }
572:
573: 574: 575:
576: function removesmartquotes($content) {
577: $content = str_replace('“', '"', $content);
578: $content = str_replace('”', '"', $content);
579: $content = str_replace('‘', ''', $content);
580: $content = str_replace('’', ''', $content);
581:
582: return $content;
583: }
584:
585: 586: 587: 588: 589: 590: 591: 592: 593: 594:
595: function isCrawler()
596: {
597: global $wp_query;
598:
599: if( ! empty( $wp_query ) && $wp_query->is_robots() ) {
600: CMLUtils::_set( "is_crawler", 1 );
601:
602: return true;
603: }
604:
605: $USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
606:
607:
608:
609:
610:
611: $crawlers_agents = 'Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
612:
613: if ( strpos($crawlers_agents , $USER_AGENT) === false )
614: return false;
615:
616:
617: 618: 619: 620: 621:
622: return true;
623: }
624:
625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638:
639: function () {
640:
641: $lang = cml_get_current_language();
642:
643: return "cml_menu_" . $lang->cml_language_slug;
644: }
645: ?>
646: